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.

293 lines
4.9 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  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
  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. // Operators
  64. T * const
  65. operator[](int nItem) const
  66. { return Get(nItem); };
  67. protected:
  68. // Properties
  69. DWORD
  70. m_Max, // Number of element slots available.
  71. m_Mac; // Number of element slots used.
  72. T **
  73. m_pvList; // The elements.
  74. // Methods
  75. };
  76. /*++
  77. Set:
  78. This routine sets an item in the collection array. If the array isn't that
  79. big, it is expanded with NULL elements to become that big.
  80. Arguments:
  81. nItem - Supplies the index value to be set.
  82. pvItem - Supplies the value to be set into the given index.
  83. Return Value:
  84. The value of the inserted value, or NULL on errors.
  85. Author:
  86. Doug Barlow (dbarlow) 7/13/1995
  87. --*/
  88. template<class T>
  89. inline T *
  90. CDynamicArray<T>::Set(
  91. IN int nItem,
  92. IN T * pvItem)
  93. {
  94. DWORD index;
  95. //
  96. // Make sure the array is big enough.
  97. //
  98. if ((DWORD)nItem >= m_Max)
  99. {
  100. int newSize = (0 == m_Max ? 4 : m_Max);
  101. while (nItem >= newSize)
  102. newSize *= 2;
  103. NEWReason("Dynamic Array")
  104. T **newList = new T*[newSize];
  105. if (NULL == newList)
  106. goto ErrorExit;
  107. for (index = 0; index < m_Mac; index += 1)
  108. newList[index] = m_pvList[index];
  109. if (NULL != m_pvList)
  110. delete[] m_pvList;
  111. m_pvList = newList;
  112. m_Max = newSize;
  113. }
  114. //
  115. // Make sure intermediate elements are filled in.
  116. //
  117. if ((DWORD)nItem >= m_Mac)
  118. {
  119. for (index = m_Mac; index < (DWORD)nItem; index += 1)
  120. m_pvList[index] = NULL;
  121. m_Mac = (DWORD)nItem + 1;
  122. }
  123. //
  124. // Fill in the list element.
  125. //
  126. m_pvList[(DWORD)nItem] = pvItem;
  127. return pvItem;
  128. ErrorExit:
  129. return NULL;
  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_