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.

255 lines
6.2 KiB

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