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.

240 lines
5.9 KiB

  1. #ifndef __8fbf879f_339a_4738_b8ae_2948b62b9863__
  2. #define __8fbf879f_339a_4738_b8ae_2948b62b9863__
  3. template<class T>
  4. class CSimpleDynamicArray
  5. {
  6. private:
  7. int m_nSize;
  8. int m_nMaxSize;
  9. int m_nGrowSize;
  10. T *m_pArray;
  11. enum
  12. {
  13. eGrowSize = 10 // The number of items to add each time the array grows.
  14. };
  15. public:
  16. CSimpleDynamicArray(void)
  17. : m_nSize(0),
  18. m_nMaxSize(0),
  19. m_nGrowSize(eGrowSize),
  20. m_pArray(NULL)
  21. {
  22. }
  23. CSimpleDynamicArray( int nInitialSize, int nGrowSize=0 )
  24. : m_nSize(0),
  25. m_nMaxSize(0),
  26. m_nGrowSize(nGrowSize ? nGrowSize : eGrowSize),
  27. m_pArray(NULL)
  28. {
  29. GrowTo(nInitialSize);
  30. }
  31. CSimpleDynamicArray( const CSimpleDynamicArray<T> &other )
  32. : m_nSize(0),
  33. m_nMaxSize(0),
  34. m_nGrowSize(eGrowSize),
  35. m_pArray(NULL)
  36. {
  37. Append(other);
  38. }
  39. virtual ~CSimpleDynamicArray(void)
  40. {
  41. Destroy();
  42. }
  43. CSimpleDynamicArray &operator=( const CSimpleDynamicArray &other )
  44. {
  45. if (this != &other)
  46. {
  47. Destroy();
  48. Append(other);
  49. }
  50. return *this;
  51. }
  52. void Destroy(void)
  53. {
  54. if (m_pArray)
  55. {
  56. delete[] m_pArray;
  57. m_pArray = NULL;
  58. }
  59. m_nSize = m_nMaxSize = 0;
  60. }
  61. void Append( const CSimpleDynamicArray &other )
  62. {
  63. if (GrowTo( m_nSize + other.Size() ))
  64. {
  65. for (int i=0;i<other.Size();i++)
  66. {
  67. Append(other[i]);
  68. }
  69. }
  70. }
  71. int Append( const T &element )
  72. {
  73. if (GrowTo( m_nSize + 1 ))
  74. {
  75. m_pArray[m_nSize] = element;
  76. int nResult = m_nSize;
  77. m_nSize++;
  78. return nResult;
  79. }
  80. else return -1;
  81. }
  82. int Insert( const T &element, int nIndex )
  83. {
  84. //
  85. // Make sure we can accomodate this new item
  86. //
  87. if (GrowTo( m_nSize + 1 ))
  88. {
  89. //
  90. // Make sure the item is within the range we've allocated
  91. //
  92. if (nIndex >= 0 && nIndex <= m_nSize)
  93. {
  94. //
  95. // Make room for the new item by moving all items above up by one slot
  96. //
  97. for (int i=Size();i>nIndex;i--)
  98. {
  99. m_pArray[i] = m_pArray[i-1];
  100. }
  101. //
  102. // Save the new item
  103. //
  104. m_pArray[nIndex] = element;
  105. //
  106. // We're now one larger
  107. //
  108. m_nSize++;
  109. //
  110. // Return the index of the slot we used
  111. //
  112. return nIndex;
  113. }
  114. }
  115. //
  116. // Return an error
  117. //
  118. return -1;
  119. }
  120. void Delete( int nItem )
  121. {
  122. if (nItem >= 0 && nItem < m_nSize && m_pArray)
  123. {
  124. T *pTmpArray = new T[m_nMaxSize];
  125. if (pTmpArray)
  126. {
  127. T *pSrc, *pTgt;
  128. pSrc = m_pArray;
  129. pTgt = pTmpArray;
  130. for (int i=0;i<m_nSize;i++)
  131. {
  132. if (i != nItem)
  133. {
  134. *pTgt = *pSrc;
  135. pTgt++;
  136. }
  137. pSrc++;
  138. }
  139. delete[] m_pArray;
  140. m_pArray = pTmpArray;
  141. m_nSize--;
  142. }
  143. }
  144. }
  145. bool GrowTo( int nSize )
  146. {
  147. //
  148. // If the array is already large enough, just return true
  149. //
  150. if (nSize < m_nMaxSize)
  151. {
  152. return true;
  153. }
  154. //
  155. // Save old size, in case we can't allocate a new array
  156. //
  157. int nOldMaxSize = m_nMaxSize;
  158. //
  159. // Find the correct size to grow to
  160. //
  161. while (m_nMaxSize < nSize)
  162. {
  163. m_nMaxSize += m_nGrowSize;
  164. }
  165. //
  166. // Allocate the array
  167. //
  168. T *pTmpArray = new T[m_nMaxSize];
  169. if (pTmpArray)
  170. {
  171. //
  172. // Copy the old array over
  173. //
  174. for (int i=0;i<m_nSize;i++)
  175. {
  176. pTmpArray[i] = m_pArray[i];
  177. }
  178. //
  179. // Delete the old array
  180. //
  181. if (m_pArray)
  182. {
  183. delete[] m_pArray;
  184. }
  185. //
  186. // Assign the new array to the old one and return true
  187. //
  188. m_pArray = pTmpArray;
  189. return true;
  190. }
  191. else
  192. {
  193. //
  194. // If we couldn't allocate the new array, restore the maximum size
  195. // and return false
  196. //
  197. m_nMaxSize = nOldMaxSize;
  198. return false;
  199. }
  200. }
  201. int Find( const T& element )
  202. {
  203. for (int i=0;i<m_nSize;i++)
  204. if (m_pArray[i] == element)
  205. return i;
  206. return -1;
  207. }
  208. bool operator==( const CSimpleDynamicArray &other )
  209. {
  210. if (Size() != other.Size())
  211. return false;
  212. for (int i=0;i<Size();i++)
  213. if (!(m_pArray[i] == other[i]))
  214. return false;
  215. return true;
  216. }
  217. bool Contains( const T& element ) { return(Find(element) >= 0);}
  218. void Size( int nSize ) { m_nSize = nSize;}
  219. void MaxSize( int nMaxSize ) { m_nMaxSize = nMaxSize;}
  220. void GrowSize( int nGrowSize ) { m_nGrowSize = nGrowSize;}
  221. int Size(void) const { return m_nSize;}
  222. int MaxSize(void) const { return m_nMaxSize;}
  223. int GrowSize(void) const { return m_nGrowSize;}
  224. const T *Array(void) const { return m_pArray;}
  225. const T &operator[](int nIndex) const { return m_pArray[nIndex];}
  226. T &operator[](int nIndex) { return m_pArray[nIndex];}
  227. };
  228. #endif