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.

234 lines
4.6 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. m_nMaxSize += iSize;
  51. pT = ( T * )new T[ m_nMaxSize ];
  52. if( pT == NULL )
  53. {
  54. return 0;
  55. }
  56. ZeroMemory( ( PVOID )pT , sizeof( T ) * m_nMaxSize );
  57. CopyMemory( pT , m_pT , sizeof( T ) * ( m_idx ) );
  58. if( m_pT != NULL )
  59. {
  60. delete[] m_pT;
  61. }
  62. m_pT = pT;
  63. }
  64. return m_nMaxSize;
  65. }
  66. //------------------------------------------------------------------------
  67. // Simply put, increase the array size if empty, and place item at the
  68. // end of the list
  69. int Insert( T tItem )
  70. {
  71. if( m_pT == NULL || ( m_idx ) >= m_nMaxSize )
  72. {
  73. if( GrowBy( 0 ) == 0 )
  74. {
  75. return 0;
  76. }
  77. }
  78. m_pT[ m_idx ] = tItem;
  79. m_idx++;
  80. return m_idx;
  81. }
  82. //------------------------------------------------------------------------
  83. // exposes the array for direct reference
  84. T* ExposeArray( )
  85. {
  86. if( m_pT != NULL )
  87. {
  88. return &m_pT[0];
  89. }
  90. return NULL;
  91. }
  92. //------------------------------------------------------------------------
  93. // Returns the number of valid entries in the array
  94. int GetSize( ) const
  95. {
  96. return ( m_idx );
  97. }
  98. //------------------------------------------------------------------------
  99. // Returns an item in the array, or null if not with in range
  100. T* GetAt( int idx )
  101. {
  102. if( idx < 0 || idx >= m_idx )
  103. {
  104. return NULL;
  105. }
  106. return &m_pT[ idx ];
  107. }
  108. //------------------------------------------------------------------------
  109. // Assigns a value in the array
  110. int SetAt( int idx , T tItem )
  111. {
  112. if( idx < 0 || idx >= m_idx )
  113. {
  114. return -1;
  115. }
  116. m_pT[ idx ] = tItem;
  117. return idx;
  118. }
  119. //------------------------------------------------------------------------
  120. // Finds an item in the array ( incase one forgot the index )
  121. int FindItem( T tItem , BOOL& bFound )
  122. {
  123. bFound = FALSE;
  124. int idx = 0;
  125. while( idx < m_idx )
  126. {
  127. if( m_pT[ idx ] == tItem )
  128. {
  129. bFound = TRUE;
  130. break;
  131. }
  132. idx++;
  133. }
  134. return idx;
  135. }
  136. //------------------------------------------------------------------------
  137. // Deletes an item from the array
  138. int DeleteItemAt( int idx )
  139. {
  140. if( 0 > idx || idx >= m_idx )
  141. {
  142. return 0;
  143. }
  144. if( idx == m_idx - 1 ) //delete last item
  145. {
  146. m_idx--;
  147. return -1;
  148. }
  149. void *pvDest = &m_pT[ idx ];
  150. void *pvSrc = &m_pT[ idx + 1 ];
  151. ULONG ulDistance = (ULONG)( ( BYTE *)&m_pT[ m_nMaxSize - 1 ] - ( BYTE * )pvSrc ) + sizeof( T );
  152. if( ulDistance != 0 )
  153. {
  154. MoveMemory( pvDest , pvSrc , ulDistance );
  155. // Adjust the array status
  156. m_idx--;
  157. m_nMaxSize--;
  158. }
  159. return ulDistance;
  160. }
  161. //------------------------------------------------------------------------
  162. // Deletes the array of items
  163. int DeleteArray( )
  164. {
  165. if( m_pT != NULL )
  166. {
  167. delete[] m_pT;
  168. }
  169. m_pT = NULL;
  170. m_nMaxSize = 0;
  171. m_idx = 0;
  172. return 0;
  173. }
  174. };
  175. #endif //_TARRAY_H