Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
3.3 KiB

  1. //
  2. // MODULE: RSSTACK,H
  3. //
  4. // COMPANY: Saltmine Creative, Inc. (206)-633-4743 [email protected]
  5. //
  6. // AUTHOR: Richard Meadows
  7. //
  8. // ORIGINAL DATE: 8/7/96
  9. //
  10. // NOTES:
  11. // 1. A stack of any structure. Can be used for pointers, but
  12. // will cause a memory leak when the stack is destroyed with
  13. // objects on it.
  14. //
  15. // 2. This file has no .cpp file. Every thing is inline, due to the
  16. // template class.
  17. //
  18. // Version Date By Comments
  19. //--------------------------------------------------------------------
  20. // V0.1 - RM Original
  21. //
  22. #ifndef __RSSTACK_H_
  23. #define __RSSTACK_H_ 1
  24. template<class T>
  25. class RSStack
  26. {
  27. public:
  28. RSStack();
  29. virtual ~RSStack();
  30. // Attributes
  31. public:
  32. private:
  33. typedef struct tagRSStackNode
  34. {
  35. T SItem;
  36. struct tagRSStackNode *pNext;
  37. } RSStackNode;
  38. RSStackNode *m_pTop;
  39. RSStackNode *m_pPeak;
  40. // Operations
  41. public:
  42. /*
  43. Push returns -1 when out of memory.
  44. */
  45. int Push(T);
  46. /*
  47. Pop returns the top T item.
  48. */
  49. T Pop();
  50. /*
  51. 1 is the top most item in the stack. Returns the T item at
  52. tdown index by copying the value to refedItem. False is returned if
  53. tdown is greater than the number of items in the stack.
  54. */
  55. BOOL GetAt(int tdown, T &refedItem);
  56. /*
  57. PeakFirst returns the top most item and initializes variables that are
  58. used by PeakNext. PeakFirst returns false when the stack is empty.
  59. */
  60. BOOL PeakFirst(T &refedItem);
  61. /*
  62. Use PeakNext to quickly peak at all of the items on the stack.
  63. PeakNext returns false when it can not copy a T item to refedItem.
  64. */
  65. BOOL PeakNext(T &refedItem);
  66. /*
  67. Empty returns TRUE (Non-Zero) when the stack is empty.
  68. */
  69. BOOL Empty();
  70. /*
  71. RemoveAll throws away the contents of the stack.
  72. */
  73. void RemoveAll();
  74. };
  75. template<class T>
  76. inline RSStack<T>::RSStack()
  77. {
  78. m_pTop = NULL;
  79. m_pPeak = NULL;
  80. }
  81. template<class T>
  82. inline RSStack<T>::~RSStack()
  83. {
  84. RSStackNode *pOld;
  85. while(m_pTop != NULL)
  86. {
  87. pOld = m_pTop;
  88. m_pTop = m_pTop->pNext;
  89. delete pOld;
  90. }
  91. }
  92. template<class T>
  93. inline int RSStack<T>::Push(T Item)
  94. {
  95. int Ret;
  96. RSStackNode *pNew = new RSStackNode;
  97. if(NULL == pNew)
  98. {
  99. Ret = -1;
  100. }
  101. else
  102. {
  103. Ret = 1;
  104. pNew->pNext = m_pTop;
  105. m_pTop = pNew;
  106. pNew->SItem = Item;
  107. }
  108. return Ret;
  109. }
  110. template<class T>
  111. inline T RSStack<T>::Pop()
  112. {
  113. T Ret;
  114. if(NULL != m_pTop)
  115. {
  116. RSStackNode *pOld = m_pTop;
  117. m_pTop = m_pTop->pNext;
  118. Ret = pOld->SItem;
  119. delete pOld;
  120. }
  121. return Ret;
  122. }
  123. template<class T>
  124. inline BOOL RSStack<T>::Empty()
  125. {
  126. BOOL bRet;
  127. if(NULL == m_pTop)
  128. bRet = TRUE;
  129. else
  130. bRet = FALSE;
  131. return bRet;
  132. }
  133. template<class T>
  134. inline void RSStack<T>::RemoveAll()
  135. {
  136. RSStackNode *pOld;
  137. while(m_pTop != NULL)
  138. {
  139. pOld = m_pTop;
  140. m_pTop = m_pTop->pNext;
  141. delete pOld;
  142. }
  143. }
  144. template<class T>
  145. inline BOOL RSStack<T>::GetAt(int tdown, T &refedItem)
  146. {
  147. BOOL bRet = FALSE;
  148. RSStackNode *pNode = m_pTop;
  149. while(pNode != NULL && tdown > 1)
  150. {
  151. pNode = pNode->pNext;
  152. tdown--;
  153. }
  154. if (pNode && 1 == tdown)
  155. {
  156. refedItem = pNode->SItem;
  157. bRet = TRUE;
  158. }
  159. return bRet;
  160. }
  161. template<class T>
  162. inline BOOL RSStack<T>::PeakFirst(T &refedItem)
  163. {
  164. BOOL bRet = FALSE;
  165. if (NULL != m_pTop)
  166. {
  167. m_pPeak = m_pTop;
  168. refedItem = m_pTop->SItem;
  169. bRet = TRUE;
  170. }
  171. return bRet;
  172. }
  173. template<class T>
  174. inline BOOL RSStack<T>::PeakNext(T &refedItem)
  175. {
  176. ASSERT(NULL != m_pPeak);
  177. BOOL bRet = FALSE;
  178. m_pPeak = m_pPeak->pNext;
  179. if (NULL != m_pPeak)
  180. {
  181. refedItem = m_pPeak->SItem;
  182. bRet = TRUE;
  183. }
  184. return bRet;
  185. }
  186. #endif