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.

292 lines
4.6 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1995 - 1999
  3. Module Name:
  4. dynarray
  5. Abstract:
  6. This header file implements a Dynamic Array.
  7. Author:
  8. Doug Barlow (dbarlow) 10/5/1995
  9. Environment:
  10. Win32, C++ /w Exception Handling
  11. Notes:
  12. --*/
  13. #ifndef _DYNARRAY_H_
  14. #define _DYNARRAY_H_
  15. //
  16. //==============================================================================
  17. //
  18. // CDynamicArray
  19. //
  20. template <class T>
  21. class CDynamicArray
  22. {
  23. public:
  24. // Constructors & Destructor
  25. CDynamicArray(void)
  26. { m_Max = m_Mac = 0; m_pvList = NULL; };
  27. virtual ~CDynamicArray()
  28. { Clear(); };
  29. // Properties
  30. // Methods
  31. void
  32. Clear(void)
  33. {
  34. if (NULL != m_pvList)
  35. {
  36. delete[] m_pvList;
  37. m_pvList = NULL;
  38. m_Max = 0;
  39. m_Mac = 0;
  40. }
  41. };
  42. void
  43. Empty(void)
  44. { m_Mac = 0; };
  45. T *
  46. Set(
  47. IN int nItem,
  48. IN T *pvItem);
  49. T *
  50. Insert(
  51. IN int nItem,
  52. IN T *pvItem);
  53. T *
  54. Add(
  55. IN T *pvItem);
  56. T * const
  57. Get(
  58. IN int nItem)
  59. const;
  60. DWORD
  61. Count(void) const
  62. { return m_Mac; };
  63. T ** const
  64. Array(void) const
  65. { return m_pvList; };
  66. // Operators
  67. T * const
  68. operator[](int nItem) const
  69. { return Get(nItem); };
  70. protected:
  71. // Properties
  72. DWORD
  73. m_Max, // Number of element slots available.
  74. m_Mac; // Number of element slots used.
  75. T **
  76. m_pvList; // The elements.
  77. // Methods
  78. };
  79. /*++
  80. Set:
  81. This routine sets an item in the collection array. If the array isn't that
  82. big, it is expanded with NULL elements to become that big.
  83. Arguments:
  84. nItem - Supplies the index value to be set.
  85. pvItem - Supplies the value to be set into the given index.
  86. Return Value:
  87. The value of the inserted value, or NULL on errors.
  88. Author:
  89. Doug Barlow (dbarlow) 7/13/1995
  90. --*/
  91. template<class T>
  92. inline T *
  93. CDynamicArray<T>::Set(
  94. IN int nItem,
  95. IN T * pvItem)
  96. {
  97. DWORD index;
  98. //
  99. // Make sure the array is big enough.
  100. //
  101. if ((DWORD)nItem >= m_Max)
  102. {
  103. int newSize = (0 == m_Max ? 4 : m_Max);
  104. while (nItem >= newSize)
  105. newSize *= 2;
  106. T **newList = new T*[newSize];
  107. if (NULL == newList)
  108. throw (DWORD)ERROR_OUTOFMEMORY;
  109. for (index = 0; index < m_Mac; index += 1)
  110. newList[index] = m_pvList[index];
  111. if (NULL != m_pvList)
  112. delete[] m_pvList;
  113. m_pvList = newList;
  114. m_Max = newSize;
  115. }
  116. //
  117. // Make sure intermediate elements are filled in.
  118. //
  119. if ((DWORD)nItem >= m_Mac)
  120. {
  121. for (index = m_Mac; index < (DWORD)nItem; index += 1)
  122. m_pvList[index] = NULL;
  123. m_Mac = (DWORD)nItem + 1;
  124. }
  125. //
  126. // Fill in the list element.
  127. //
  128. m_pvList[(DWORD)nItem] = pvItem;
  129. return pvItem;
  130. }
  131. /*++
  132. Insert:
  133. This routine inserts an element in the array by moving all elements above it
  134. up one, then inserting the new element.
  135. Arguments:
  136. nItem - Supplies the index value to be inserted.
  137. pvItem - Supplies the value to be set into the given index.
  138. Return Value:
  139. The value of the inserted value, or NULL on errors.
  140. Author:
  141. Doug Barlow (dbarlow) 10/10/1995
  142. --*/
  143. template<class T>
  144. inline T *
  145. CDynamicArray<T>::Insert(
  146. IN int nItem,
  147. IN T * pvItem)
  148. {
  149. DWORD index;
  150. for (index = nItem; index < m_Mac; index += 1)
  151. if (NULL == Set(index + 1, Get(index)))
  152. return NULL; // Only the first one can fail, so no change
  153. // happens on errors.
  154. return Set(nItem, pvItem);
  155. }
  156. /*++
  157. Add:
  158. This method adds an element to the end of the dynamic array.
  159. Arguments:
  160. pvItem - Supplies the value to be added to the list.
  161. Return Value:
  162. The value of the added value, or NULL on errors.
  163. Author:
  164. Doug Barlow (dbarlow) 10/10/1995
  165. --*/
  166. template<class T>
  167. inline T *
  168. CDynamicArray<T>::Add(
  169. IN T *pvItem)
  170. {
  171. return Set(Count(), pvItem);
  172. }
  173. /*++
  174. Get:
  175. This method returns the element at the given index. If there is no element
  176. previously stored at that element, it returns NULL. It does not expand the
  177. array.
  178. Arguments:
  179. nItem - Supplies the index into the list.
  180. Return Value:
  181. The value stored at that index in the list, or NULL if nothing has ever been
  182. stored there.
  183. Author:
  184. Doug Barlow (dbarlow) 7/13/1995
  185. --*/
  186. template <class T>
  187. inline T * const
  188. CDynamicArray<T>::Get(
  189. int nItem)
  190. const
  191. {
  192. if (m_Mac <= (DWORD)nItem)
  193. return NULL;
  194. else
  195. return m_pvList[nItem];
  196. }
  197. #endif // _DYNARRAY_H_