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.

236 lines
4.9 KiB

  1. //Copyright (c) 1998 - 1999 Microsoft Corporation
  2. #ifndef _TARRAY_H
  3. #define _TARRAY_H
  4. template< class T > class CArrayT
  5. {
  6. T *m_pT;
  7. int m_nMaxSize;
  8. int m_idx; //current array pos
  9. public:
  10. //------------------------------------------------------------------------
  11. CArrayT( )
  12. {
  13. m_pT = NULL;
  14. m_nMaxSize = 0;
  15. m_idx = 0;
  16. }
  17. //------------------------------------------------------------------------
  18. // destroy the list
  19. ~CArrayT( )
  20. {
  21. if( m_pT != NULL )
  22. {
  23. delete[] m_pT;
  24. }
  25. }
  26. //------------------------------------------------------------------------
  27. // increases array size, returns zero if the operation failed
  28. int GrowBy( int iSize )
  29. {
  30. if( iSize == 0 )
  31. {
  32. //
  33. //Grow by # number of items
  34. //
  35. iSize = 4;
  36. }
  37. if( m_pT == NULL )
  38. {
  39. m_pT = ( T * )new T[ iSize ];
  40. if( m_pT == NULL )
  41. {
  42. return 0;
  43. }
  44. m_nMaxSize = iSize;
  45. m_idx = 0;
  46. }
  47. else
  48. {
  49. T *pT;
  50. int nNewMaxSize = m_nMaxSize + iSize;
  51. pT = ( T * )new T[ nNewMaxSize ];
  52. if( pT == NULL )
  53. {
  54. return 0;
  55. }
  56. m_nMaxSize = nNewMaxSize;
  57. ZeroMemory( ( PVOID )pT , sizeof( T ) * m_nMaxSize );
  58. CopyMemory( pT , m_pT , sizeof( T ) * ( m_idx ) );
  59. if( m_pT != NULL )
  60. {
  61. delete[] m_pT;
  62. }
  63. m_pT = pT;
  64. }
  65. return m_nMaxSize;
  66. }
  67. //------------------------------------------------------------------------
  68. // Simply put, increase the array size if empty, and place item at the
  69. // end of the list
  70. int Insert( T tItem )
  71. {
  72. if( m_pT == NULL || ( m_idx ) >= m_nMaxSize )
  73. {
  74. if( GrowBy( 0 ) == 0 )
  75. {
  76. return 0;
  77. }
  78. }
  79. m_pT[ m_idx ] = tItem;
  80. m_idx++;
  81. return m_idx;
  82. }
  83. //------------------------------------------------------------------------
  84. // exposes the array for direct reference
  85. T* ExposeArray( )
  86. {
  87. if( m_pT != NULL )
  88. {
  89. return &m_pT[0];
  90. }
  91. return NULL;
  92. }
  93. //------------------------------------------------------------------------
  94. // Returns the number of valid entries in the array
  95. int GetSize( ) const
  96. {
  97. return ( m_idx );
  98. }
  99. //------------------------------------------------------------------------
  100. // Returns an item in the array, or null if not with in range
  101. T* GetAt( int idx )
  102. {
  103. if( idx < 0 || idx >= m_idx )
  104. {
  105. return NULL;
  106. }
  107. return &m_pT[ idx ];
  108. }
  109. //------------------------------------------------------------------------
  110. // Assigns a value in the array
  111. int SetAt( int idx , T tItem )
  112. {
  113. if( idx < 0 || idx >= m_idx )
  114. {
  115. return -1;
  116. }
  117. m_pT[ idx ] = tItem;
  118. return idx;
  119. }
  120. //------------------------------------------------------------------------
  121. // Finds an item in the array ( incase one forgot the index )
  122. int FindItem( T tItem , BOOL& bFound )
  123. {
  124. bFound = FALSE;
  125. int idx = 0;
  126. while( idx < m_idx )
  127. {
  128. if( m_pT[ idx ] == tItem )
  129. {
  130. bFound = TRUE;
  131. break;
  132. }
  133. idx++;
  134. }
  135. return idx;
  136. }
  137. //------------------------------------------------------------------------
  138. // Deletes an item from the array
  139. int DeleteItemAt( int idx )
  140. {
  141. if( 0 > idx || idx >= m_idx )
  142. {
  143. return 0;
  144. }
  145. if( idx == m_idx - 1 ) //delete last item
  146. {
  147. m_idx--;
  148. return -1;
  149. }
  150. void *pvDest = &m_pT[ idx ];
  151. void *pvSrc = &m_pT[ idx + 1 ];
  152. ULONG ulDistance = (ULONG)( ( BYTE *)&m_pT[ m_nMaxSize - 1 ] - ( BYTE * )pvSrc ) + sizeof( T );
  153. if( ulDistance != 0 )
  154. {
  155. MoveMemory( pvDest , pvSrc , ulDistance );
  156. // Adjust the array status
  157. m_idx--;
  158. m_nMaxSize--;
  159. }
  160. return ulDistance;
  161. }
  162. //------------------------------------------------------------------------
  163. // Deletes the array of items
  164. int DeleteArray( )
  165. {
  166. if( m_pT != NULL )
  167. {
  168. delete[] m_pT;
  169. }
  170. m_pT = NULL;
  171. m_nMaxSize = 0;
  172. m_idx = 0;
  173. return 0;
  174. }
  175. };
  176. #endif //_TARRAY_H