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.

515 lines
8.8 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. #ifdef __cplusplus
  16. //
  17. //==============================================================================
  18. //
  19. // CDynamicArray
  20. //
  21. template <class T>
  22. class CDynamicArray
  23. {
  24. public:
  25. // Constructors & Destructor
  26. CDynamicArray(void);
  27. virtual ~CDynamicArray();
  28. // Properties
  29. // Methods
  30. void Clear(void);
  31. void Empty(void);
  32. void Set(IN ULONG nItem, IN const T &Item);
  33. void Insert(IN ULONG nItem, IN const T &Item);
  34. void Add(IN const T &Item);
  35. T &Get(IN ULONG nItem) const;
  36. ULONG Count(void) const
  37. { return m_Mac; };
  38. T * const Array(void) const
  39. { return m_List; };
  40. // Operators
  41. T & operator[](ULONG nItem) const
  42. { return Get(nItem); };
  43. protected:
  44. // Properties
  45. ULONG
  46. m_Max, // Number of element slots available.
  47. m_Mac; // Number of element slots used.
  48. T *
  49. m_List; // The elements.
  50. // Methods
  51. };
  52. /*++
  53. Set:
  54. This routine sets an item in the collection array. If the array isn't that
  55. big, it is expanded with zeroed elements to become that big.
  56. Arguments:
  57. nItem - Supplies the index value to be set.
  58. Item - Supplies the value to be set into the given index.
  59. Return Value:
  60. None
  61. Author:
  62. Doug Barlow (dbarlow) 7/13/1995
  63. --*/
  64. template<class T> void
  65. CDynamicArray<T>::Set(
  66. IN ULONG nItem,
  67. IN const T &Item)
  68. {
  69. //
  70. // Make sure the array is big enough.
  71. //
  72. if (nItem >= m_Max)
  73. {
  74. ULONG dwI;
  75. ULONG newSize = (0 == m_Max ? 4 : m_Max);
  76. while (nItem >= newSize)
  77. newSize *= 2;
  78. T *newList = new T[newSize];
  79. if (NULL == newList)
  80. throw (ULONG)ERROR_OUTOFMEMORY;
  81. if (NULL != m_List)
  82. {
  83. for (dwI = 0; dwI < m_Mac; dwI += 1)
  84. newList[dwI] = m_List[dwI];
  85. delete[] m_List;
  86. }
  87. m_List = newList;
  88. m_Max = newSize;
  89. }
  90. //
  91. // Make sure intermediate elements are filled in.
  92. //
  93. if (nItem >= m_Mac)
  94. {
  95. ZeroMemory(&m_List[m_Mac + 1], (nItem - m_Mac) * sizeof(T));
  96. m_Mac = nItem + 1;
  97. }
  98. //
  99. // Fill in the list element.
  100. //
  101. m_List[nItem] = Item;
  102. }
  103. /*++
  104. Insert:
  105. This routine inserts an element in the array by moving all elements above it
  106. up one, then inserting the new element.
  107. Arguments:
  108. nItem - Supplies the index value to be inserted.
  109. Item - Supplies the value to be set into the given index.
  110. Return Value:
  111. None
  112. Author:
  113. Doug Barlow (dbarlow) 10/10/1995
  114. --*/
  115. template<class T> void
  116. CDynamicArray<T>::Insert(
  117. IN ULONG nItem,
  118. IN const T &Item)
  119. {
  120. ULONG index;
  121. for (index = nItem; index < m_Mac; index += 1)
  122. Set(index + 1, Get(index)))
  123. Set(nItem, Item);
  124. }
  125. /*++
  126. Add:
  127. This method adds an element to the end of the dynamic array.
  128. Arguments:
  129. Item - Supplies the value to be added to the list.
  130. Return Value:
  131. None
  132. Author:
  133. Doug Barlow (dbarlow) 10/10/1995
  134. --*/
  135. template<class T> void
  136. CDynamicArray<T>::Add(
  137. IN const T &Item)
  138. {
  139. Set(Count(), Item);
  140. }
  141. /*++
  142. Get:
  143. This method returns the element at the given index. If there is no element
  144. previously stored at that element, it returns NULL. It does not expand the
  145. array.
  146. Arguments:
  147. nItem - Supplies the index into the list.
  148. Return Value:
  149. The value stored at that index in the list, or NULL if nothing has ever been
  150. stored there.
  151. Author:
  152. Doug Barlow (dbarlow) 7/13/1995
  153. --*/
  154. template <class T> T &
  155. CDynamicArray<T>::Get(
  156. ULONG nItem)
  157. const
  158. {
  159. if (m_Mac <= nItem)
  160. return *(T *)NULL;
  161. else
  162. return m_List[nItem];
  163. }
  164. //
  165. // Other members
  166. //
  167. template <class T>
  168. CDynamicArray<T>::CDynamicArray(
  169. void)
  170. {
  171. m_Max = m_Mac = 0;
  172. m_List = NULL;
  173. }
  174. template <class T>
  175. CDynamicArray<T>::~CDynamicArray()
  176. {
  177. Clear();
  178. }
  179. template <class T>
  180. void
  181. CDynamicArray<T>::Clear(
  182. void)
  183. {
  184. if (NULL != m_List)
  185. {
  186. delete[] m_List;
  187. m_List = NULL;
  188. m_Max = 0;
  189. m_Mac = 0;
  190. }
  191. };
  192. //
  193. //==============================================================================
  194. //
  195. // CDynamicPointerArray
  196. //
  197. template <class T>
  198. class CDynamicPointerArray
  199. {
  200. public:
  201. // Constructors & Destructor
  202. CDynamicPointerArray(void);
  203. virtual ~CDynamicPointerArray();
  204. // Properties
  205. // Methods
  206. void Clear(void);
  207. void Empty(void);
  208. void Set(IN ULONG nItem, IN T *pItem);
  209. void Insert(IN ULONG nItem, IN T *pItem);
  210. void Add(IN T *pItem);
  211. T *Get(IN ULONG nItem) const;
  212. ULONG Count(void) const
  213. { return m_Mac; };
  214. T ** const Array(void) const
  215. { return m_List; };
  216. // Operators
  217. T * operator[](ULONG nItem) const
  218. { return Get(nItem); };
  219. protected:
  220. // Properties
  221. ULONG m_Max; // Number of element slots available.
  222. ULONG m_Mac; // Number of element slots used.
  223. T **m_List; // The elements.
  224. // Methods
  225. };
  226. /*++
  227. Set:
  228. This routine sets an item in the collection array. If the array isn't that
  229. big, it is expanded with zeroed elements to become that big.
  230. Arguments:
  231. nItem - Supplies the index value to be set.
  232. pItem - Supplies the value to be set into the given index.
  233. Return Value:
  234. None
  235. Author:
  236. Doug Barlow (dbarlow) 7/13/1995
  237. --*/
  238. template<class T> void
  239. CDynamicPointerArray<T>::Set(
  240. IN ULONG nItem,
  241. IN T *pItem)
  242. {
  243. //
  244. // Make sure the array is big enough.
  245. //
  246. if (nItem >= m_Max)
  247. {
  248. ULONG newSize = (0 == m_Max ? 4 : m_Max);
  249. while (nItem >= newSize)
  250. newSize *= 2;
  251. T **newList = (T **)LocalAlloc(LPTR, sizeof(T *) * newSize);
  252. if (NULL == newList)
  253. throw (ULONG)ERROR_OUTOFMEMORY;
  254. if (NULL != m_List)
  255. {
  256. CopyMemory(newList, m_List, m_Mac * sizeof(T *));
  257. LocalFree(m_List);
  258. }
  259. m_List = newList;
  260. m_Max = newSize;
  261. }
  262. //
  263. // Make sure intermediate elements are filled in.
  264. //
  265. if (nItem >= m_Mac)
  266. {
  267. ZeroMemory(&m_List[m_Mac + 1], (nItem - m_Mac) * sizeof(T *));
  268. m_Mac = nItem + 1;
  269. }
  270. //
  271. // Fill in the list element.
  272. //
  273. m_List[nItem] = pItem;
  274. }
  275. /*++
  276. Insert:
  277. This routine inserts an element in the array by moving all elements above it
  278. up one, then inserting the new element.
  279. Arguments:
  280. nItem - Supplies the index value to be inserted.
  281. pItem - Supplies the value to be set into the given index.
  282. Return Value:
  283. None
  284. Author:
  285. Doug Barlow (dbarlow) 10/10/1995
  286. --*/
  287. template<class T> void
  288. CDynamicPointerArray<T>::Insert(
  289. IN ULONG nItem,
  290. IN T *pItem)
  291. {
  292. ULONG index;
  293. for (index = nItem; index < m_Mac; index += 1)
  294. Set(index + 1, Get(index)))
  295. Set(nItem, pItem);
  296. }
  297. /*++
  298. Add:
  299. This method adds an element to the end of the dynamic array.
  300. Arguments:
  301. pItem - Supplies the value to be added to the list.
  302. Return Value:
  303. None
  304. Author:
  305. Doug Barlow (dbarlow) 10/10/1995
  306. --*/
  307. template<class T> void
  308. CDynamicPointerArray<T>::Add(
  309. IN T *pItem)
  310. {
  311. Set(Count(), pItem);
  312. }
  313. /*++
  314. Get:
  315. This method returns the element at the given index. If there is no element
  316. previously stored at that element, it returns NULL. It does not expand the
  317. array.
  318. Arguments:
  319. nItem - Supplies the index into the list.
  320. Return Value:
  321. The value stored at that index in the list, or NULL if nothing has ever been
  322. stored there.
  323. Author:
  324. Doug Barlow (dbarlow) 7/13/1995
  325. --*/
  326. template <class T> T *
  327. CDynamicPointerArray<T>::Get(
  328. ULONG nItem)
  329. const
  330. {
  331. if (m_Mac <= nItem)
  332. return NULL;
  333. else
  334. return m_List[nItem];
  335. }
  336. //
  337. // Other members
  338. //
  339. template <class T>
  340. CDynamicPointerArray<T>::CDynamicPointerArray(
  341. void)
  342. {
  343. m_Max = m_Mac = 0;
  344. m_List = NULL;
  345. }
  346. template <class T>
  347. CDynamicPointerArray<T>::~CDynamicPointerArray()
  348. {
  349. Clear();
  350. }
  351. template <class T>
  352. void
  353. CDynamicPointerArray<T>::Clear(
  354. void)
  355. {
  356. if (NULL != m_List)
  357. {
  358. LocalFree(m_List);
  359. m_List = NULL;
  360. m_Max = 0;
  361. m_Mac = 0;
  362. }
  363. };
  364. #endif
  365. #endif // _DYNARRAY_H_