1. /* snstack.h by K.Tsuru */
  2. /**************************************************************
  3. Stack class using Block class
  4. [Notice]
  5. 1.do not lnitialize
  6. 2.If "T" is a class it must have operator=() and operator==().
  7. 3.If there is no data to pop, terminate the program.
  8. **************************************************************/
  9. #ifndef SNSTACK_H
  10. #define SNSTACK_H
  11. #ifndef SNBLOCK_H
  12. #include "snblock.h"
  13. #endif
  14. const uint stackMinSize = 4u;
  15. template <class T> class SNStack {
  16. SNBlock <T> v;
  17. uint stackElements;
  18. //When elements become small, reduce the size(1) or not(0).
  19. int resize;
  20. //Does not provide copy constructor nor operator=(), probably not necessary.
  21. SNStack(const SNStack<T>& a); // has no body, then cannot use.
  22. SNStack<T>& operator=(const SNStack<T>& a); // same above.
  23. public:
  24. // constructor
  25. SNStack(int rsz = 1) : stackElements(0), resize(rsz){
  26. v.size(stackMinSize, -1);
  27. }
  28. ~SNStack(){}
  29. uint Size() const { return v.size(); }
  30. T operator()(uint n) const {
  31. #ifndef NDEBUG
  32. assert( n < stackElements);
  33. #endif
  34. return v(n);
  35. }
  36. void ResizeSwitch(int rsz){ resize = rsz; }
  37. //Return the number of stacked elements.
  38. uint Elements() const { return v.size() ? stackElements : 0; }
  39. void Push(const T& x);
  40. T Pop();
  41. //Search an element "x", if it was found return its number(first element is one).
  42. //If not found return 0.
  43. uint Search(const T& x);
  44. //Search an elements"x", if it is not found push it on stack.
  45. //T must have operator==()
  46. bool Entry(const T& x); // ver. 2,17
  47. //delete an element x
  48. bool Extract(const T& x); // ver. 2,17
  49. int Error() { return v.Error(); }
  50. };
  51. template <class T> void SNStack<T>::Push(const T& x){
  52. v.reserve(stackElements);
  53. v[stackElements++] = x;
  54. #ifndef NDEBUG
  55. assert(v.Error() == v.NORMAL);
  56. #endif
  57. }
  58. template <class T> T SNStack<T>::Pop(){
  59. if(!stackElements) exit(EXIT_FAILURE); //no data to pop
  60. if( resize && (stackElements >= stackMinSize) && (stackElements <= v.size()/2) ){
  61. // reduce the size, example : size = 8, stackElements = 4 ===> size = 4
  62. v.size(stackElements, 1);
  63. }
  64. return v(--stackElements);
  65. }
  66. template <class T> uint SNStack<T>::Search(const T& x){
  67. uint j = 0;
  68. bool found = false; // ver. 2,17
  69. while(j < stackElements) if( v(j++) == x ){ found = true; break; }
  70. return found ? j : 0; // j >= 1
  71. }
  72. template <class T> bool SNStack<T>::Entry(const T& x){ // ver. 2,17
  73. int found = Search(x);
  74. if(!found) Push(x);
  75. return !found;
  76. }
  77. template <class T> bool SNStack<T>::Extract(const T& x){ // ver. 2,17
  78. int found = Search(x);
  79. if(!found) return false;
  80. for(int i = found; i < stackElements; i++) v[i-1] = v[i];
  81. stackElements--;
  82. if( resize && (stackElements >= stackMinSize) && (stackElements <= v.size()/2) ){
  83. v.size(stackElements, 1);
  84. }
  85. return true;
  86. }
  87. #endif // STACK_H

snstack.h : last modifiled at 2016/09/04 14:21:36(2,950 bytes)
created at 2016/04/11 11:18:59
The creation time of this html file is 2017/10/11 16:07:52 (Wed Oct 11 16:07:52 2017).