Leaked source code of windows server 2003
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.

206 lines
3.7 KiB

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