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.

210 lines
3.0 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. stack.cxx
  6. Abstract:
  7. Stack template class.
  8. Author:
  9. Steve Kiraly (SteveKi) 11/06/96
  10. Revision History:
  11. --*/
  12. /********************************************************************
  13. Stack template class.
  14. ********************************************************************/
  15. //
  16. // Note the _stackPtr points to the next available location.
  17. //
  18. template<class T>
  19. _INLINE
  20. TStack<T>::
  21. TStack(
  22. UINT uSize
  23. ) : _uSize( uSize ),
  24. _pStack( NULL ),
  25. _pStackPtr( NULL )
  26. {
  27. _pStack = new T[_uSize+1];
  28. if( _pStack )
  29. {
  30. _pStackPtr = _pStack;
  31. }
  32. }
  33. template<class T>
  34. _INLINE
  35. TStack<T>::
  36. ~TStack(
  37. VOID
  38. )
  39. {
  40. delete [] _pStack;
  41. }
  42. template<class T>
  43. _INLINE
  44. BOOL
  45. TStack<T>::
  46. bValid(
  47. VOID
  48. ) const
  49. {
  50. return _pStack != NULL;
  51. }
  52. template<class T>
  53. _INLINE
  54. BOOL
  55. TStack<T>::
  56. bPush(
  57. IN T Object
  58. )
  59. {
  60. SPLASSERT( _pStack );
  61. BOOL bReturn = TRUE;
  62. if( _pStackPtr >= _pStack + _uSize )
  63. {
  64. bReturn = bGrow( _uSize );
  65. if( !bReturn )
  66. {
  67. DBGMSG( DBG_ERROR, ( "TStack::bPush - failed to grow\n" ) );
  68. }
  69. }
  70. if( bReturn )
  71. {
  72. *_pStackPtr++ = Object;
  73. bReturn = TRUE;
  74. }
  75. return bReturn;
  76. }
  77. template<class T>
  78. _INLINE
  79. BOOL
  80. TStack<T>::
  81. bPop(
  82. OUT T *pObject
  83. )
  84. {
  85. SPLASSERT( _pStack );
  86. BOOL bReturn;
  87. if( _pStackPtr <= _pStack )
  88. {
  89. bReturn = FALSE;
  90. }
  91. else
  92. {
  93. *pObject = *--_pStackPtr;
  94. bReturn = TRUE;
  95. }
  96. return bReturn;
  97. }
  98. template<class T>
  99. _INLINE
  100. UINT
  101. TStack<T>::
  102. uSize(
  103. VOID
  104. ) const
  105. {
  106. if( _pStackPtr < _pStack || _pStackPtr > _pStack + _uSize )
  107. {
  108. SPLASSERT( FALSE );
  109. }
  110. return _pStackPtr - _pStack;
  111. }
  112. template<class T>
  113. _INLINE
  114. BOOL
  115. TStack<T>::
  116. bEmpty(
  117. VOID
  118. ) const
  119. {
  120. SPLASSERT( _pStack );
  121. return _pStackPtr <= _pStack;
  122. }
  123. template<class T>
  124. _INLINE
  125. BOOL
  126. TStack<T>::
  127. bGrow(
  128. IN UINT uSize
  129. )
  130. {
  131. BOOL bReturn = FALSE;
  132. //
  133. // Calculate the new stack size.
  134. //
  135. UINT uNewSize = _uSize + uSize;
  136. //
  137. // Allocate a new stack.
  138. //
  139. T* pNewStack = new T[uNewSize];
  140. if( pNewStack )
  141. {
  142. //
  143. // Copy the old stack contents to the new stack;
  144. //
  145. for( UINT i = 0; i < _uSize; i++ )
  146. {
  147. pNewStack[i] = _pStack[i];
  148. }
  149. //
  150. // Set the stack pointer in the new stack
  151. //
  152. T *pNewStackPtr = _pStackPtr - _pStack + pNewStack;
  153. //
  154. // Release the old stack;
  155. //
  156. delete [] _pStack;
  157. //
  158. // Set the stack pointer.
  159. //
  160. _pStack = pNewStack;
  161. _uSize = uNewSize;
  162. _pStackPtr = pNewStackPtr;
  163. //
  164. // Indicate the stack has grown.
  165. //
  166. bReturn = TRUE;
  167. }
  168. else
  169. {
  170. DBGMSG( DBG_TRACE, ( "TStack::bGrow failed.\n" ) );
  171. }
  172. return bReturn;
  173. }