home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / B-C / C++Source Code Fmtr Folder / Src / Stack.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-27  |  2.5 KB  |  163 lines  |  [TEXT/MPS ]

  1. #ifndef __STACK__
  2. #define __STACK__    1
  3.  
  4. #pragma once
  5.  
  6. #ifndef __VECTOR__
  7. #include "Vector.h"
  8. #endif
  9.  
  10.  
  11.  
  12.  
  13. /*µ class Stack
  14. **    An inspectable entity.  The stack pointer is 0 based, the vector class
  15. ** is 0 based.  Items from 0 .. SP() can be referenced.
  16. */
  17. class Stack : public Vector {
  18. public:
  19.     Stack();
  20.  
  21.     OSErr IStack();
  22.     OSErr IStack(size_t size, size_t incr);
  23.     OSErr IStack(const Stack *aStack);
  24.     // Initialize the stack.
  25.  
  26.     void SetEmptyItem(void *anItem);
  27.     // Set the item to return when empty
  28.  
  29.     short Depth() const;
  30.     // Return the depth of the stack, the number of items on it
  31.  
  32.     short SP() const;
  33.     // Return the index of the top of stack
  34.  
  35.     Boolean IsEmpty() const;
  36.     // Return true when the stack is empty
  37.  
  38.     void Push(const void *anItem);
  39.     // Push the item onto the stack
  40.  
  41.     void *Pop();
  42.     // Pop the stack and return it
  43.  
  44.     void Top(void *anItem);
  45.     void *Top() const;
  46.     // Return the top of stack.  Can be fEmptyItem if stack is empty.
  47.  
  48.     void *Pick(short n) const;
  49.     // Return the n'th item on the stack.  The top is item 0
  50.  
  51.     void Drop(short n);
  52.     // Drop "n" items off the stack
  53.  
  54. private:
  55.     short fSP;                                    // The number of items: one-based
  56.     void *fEmptyItem;                            // What to return when empty
  57. };
  58.  
  59.  
  60. //µ –- inlines -–
  61.  
  62. //µ   Stack::Stack
  63. #pragma segment Stack
  64. inline Stack::Stack()
  65.     : Vector(),
  66.       fSP(0),
  67.       fEmptyItem(0)
  68.     {
  69.     }
  70.  
  71.  
  72. //µ   Stack::IStack
  73. #pragma segment Stack
  74. inline OSErr Stack::IStack()
  75. {
  76.     return (IVector());
  77. }
  78.  
  79.  
  80. //µ   Stack::IStack
  81. #pragma segment Stack
  82. inline OSErr Stack::IStack(size_t size, size_t incr)
  83. {
  84.     return (IVector(size, incr));
  85. }
  86.  
  87.  
  88. //µ   Stack::SetEmptyItem
  89. #pragma segment Stack
  90. inline void Stack::SetEmptyItem(void *anItem)
  91. {
  92.     fEmptyItem = anItem;
  93. }
  94.  
  95.  
  96. //µ   Stack::Depth
  97. #pragma segment Stack
  98. inline short Stack::Depth() const
  99. {
  100.     return (fSP);
  101. }
  102.  
  103.  
  104. //µ   Stack::SP
  105. #pragma segment Stack
  106. inline short Stack::SP() const
  107. {
  108.     return (fSP - 1);
  109. }
  110.  
  111.  
  112. //µ   Stack::IsEmpty
  113. #pragma segment Stack
  114. inline Boolean Stack::IsEmpty() const
  115. {
  116.     return (fSP <= 0);
  117. }
  118.  
  119.  
  120. //µ   Stack::Pop
  121. #pragma segment Stack
  122. inline void *Stack::Pop()
  123. {
  124.     return (IsEmpty() ? fEmptyItem : _At(--fSP));
  125. }
  126.  
  127.  
  128. //µ   Stack::Top
  129. #pragma segment Stack
  130. inline void Stack::Top(void *anItem)
  131. {
  132.     AtPut(SP(), anItem);
  133. }
  134.  
  135.  
  136. //µ   Stack::Top
  137. #pragma segment Stack
  138. inline void *Stack::Top() const
  139. {
  140.     return (IsEmpty() ? fEmptyItem : _At(SP()));
  141. }
  142.  
  143.  
  144. //µ   Stack::Pick
  145. #pragma segment Stack
  146. inline void *Stack::Pick(short n) const
  147. {
  148.     return ((fSP > n) ? At(SP() - n) : fEmptyItem);
  149. }
  150.  
  151.  
  152. //µ   Stack::Drop
  153. #pragma segment Stack
  154. inline void Stack::Drop(short n)
  155. {
  156.     fSP = (fSP >= n) ? (fSP - n) : 0;
  157. }
  158.  
  159.  
  160. #endif
  161.  
  162.  
  163.