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.

389 lines
6.9 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. array.hxx
  5. Abstract:
  6. This module contains the declaration for the ARRAY class.
  7. ARRAY is a concrete implementation of a dynamic (i.e. growable) array
  8. derived from the abstract class SORTABLE_CONTAINER.
  9. ARRAY's support one 'writer' (i.e. one client poutting OBJECTs into
  10. the ARRAY) and multiple readers via an ITERATOR. It is the client's
  11. responsibility to synchronize multiple writers.
  12. The ARRAY does not contain holes, i.e. all elements of the array (up
  13. to QueryMemberCount() ) have objects.
  14. Author:
  15. David J. Gilman (davegi) 31-Oct-1990
  16. Environment:
  17. ULIB, User Mode
  18. Notes:
  19. --*/
  20. #if ! defined( _ARRAY_ )
  21. #define _ARRAY_
  22. #include "sortcnt.hxx"
  23. //
  24. // Forward references
  25. //
  26. DECLARE_CLASS( ARRAY );
  27. DECLARE_CLASS( ARRAY_ITERATOR );
  28. DECLARE_CLASS( SORTED_LIST );
  29. //
  30. // Pointer to array of POBJECTs
  31. //
  32. typedef POBJECT* PPOBJECT;
  33. //
  34. // Default values for an ARRAY object.
  35. //
  36. // - Capacity is the total number of elements that can be stored in an ARRAY
  37. // - CapacityIncrement is the number of elemnts that the ARRAY's Capacity
  38. // will be increased by when it's Capacity is exceeded
  39. //
  40. CONST ULONG DefaultCapacity = 50;
  41. CONST ULONG DefaultCapacityIncrement = 25;
  42. //
  43. // Invalid index within the array
  44. //
  45. CONST ULONG INVALID_INDEX = (ULONG)(-1);
  46. class ARRAY : public SORTABLE_CONTAINER {
  47. friend ARRAY_ITERATOR;
  48. friend SORTED_LIST;
  49. public:
  50. ULIB_EXPORT
  51. DECLARE_CONSTRUCTOR( ARRAY );
  52. DECLARE_CAST_MEMBER_FUNCTION( ARRAY );
  53. VIRTUAL
  54. ULIB_EXPORT
  55. ~ARRAY(
  56. );
  57. NONVIRTUAL
  58. ULIB_EXPORT
  59. BOOLEAN
  60. Initialize (
  61. IN ULONG Capacity DEFAULT DefaultCapacity,
  62. IN ULONG CapacityIncrement DEFAULT DefaultCapacityIncrement
  63. );
  64. VIRTUAL
  65. ULIB_EXPORT
  66. BOOLEAN
  67. DeleteAllMembers(
  68. );
  69. VIRTUAL
  70. POBJECT
  71. GetAt(
  72. IN ULONG Index
  73. ) CONST;
  74. VIRTUAL
  75. ULONG
  76. GetMemberIndex(
  77. IN POBJECT Object
  78. ) CONST;
  79. VIRTUAL
  80. BOOLEAN
  81. ULIB_EXPORT
  82. Put(
  83. IN OUT POBJECT Member
  84. );
  85. VIRTUAL
  86. BOOLEAN
  87. PutAt(
  88. IN OUT POBJECT Member,
  89. IN ULONG Index
  90. );
  91. NONVIRTUAL
  92. ULONG
  93. QueryCapacity (
  94. ) CONST;
  95. NONVIRTUAL
  96. ULONG
  97. QueryCapacityIncrement (
  98. ) CONST;
  99. VIRTUAL
  100. ULIB_EXPORT
  101. PITERATOR
  102. QueryIterator(
  103. ) CONST;
  104. VIRTUAL
  105. ULONG
  106. QueryMemberCount(
  107. ) CONST;
  108. VIRTUAL
  109. ULIB_EXPORT
  110. POBJECT
  111. Remove(
  112. IN OUT PITERATOR Position
  113. );
  114. VIRTUAL
  115. POBJECT
  116. RemoveAt(
  117. IN ULONG Index
  118. );
  119. NONVIRTUAL
  120. ULONG
  121. SetCapacity (
  122. IN ULONG Capacity
  123. );
  124. NONVIRTUAL
  125. VOID
  126. SetCapacityIncrement (
  127. IN ULONG CapacityIncrement
  128. );
  129. VIRTUAL
  130. BOOLEAN
  131. Sort(
  132. IN BOOLEAN Ascending DEFAULT TRUE
  133. );
  134. NONVIRTUAL
  135. BOOLEAN
  136. Insert(
  137. IN OUT POBJECT Member,
  138. IN ULONG Index
  139. );
  140. protected:
  141. STATIC
  142. LONG
  143. CompareAscDesc(
  144. IN POBJECT Object1,
  145. IN POBJECT Object2,
  146. IN BOOLEAN Ascending DEFAULT TRUE
  147. );
  148. NONVIRTUAL
  149. VOID
  150. Construct (
  151. );
  152. NONVIRTUAL
  153. PPOBJECT
  154. GetObjectArray (
  155. );
  156. private:
  157. NONVIRTUAL
  158. ULONG
  159. SetArrayCapacity(
  160. IN ULONG NumberOfElements
  161. );
  162. STATIC
  163. int __cdecl
  164. CompareAscending (
  165. IN const void * Object1,
  166. IN const void * Object2
  167. );
  168. STATIC
  169. int __cdecl
  170. CompareDescending (
  171. IN const void * Object1,
  172. IN const void * Object2
  173. );
  174. PPOBJECT _ObjectArray; // Array of pointers to OBJECTs
  175. ULONG _PutIndex; // Put Index
  176. ULONG _Capacity; // Capacity of the array
  177. ULONG _CapacityIncrement; // Increment
  178. #if DBG==1
  179. ULONG _IteratorCount; // Count of iterators
  180. #endif
  181. };
  182. INLINE
  183. ULONG
  184. ARRAY::QueryCapacity (
  185. ) CONST
  186. /*++
  187. Routine Description:
  188. Return the current capacity (maximum number of members) of the ARRAY.
  189. Arguments:
  190. None.
  191. Return Value:
  192. ULONG - Current capacity.
  193. --*/
  194. {
  195. return( _Capacity );
  196. }
  197. INLINE
  198. ULONG
  199. ARRAY::QueryCapacityIncrement (
  200. ) CONST
  201. /*++
  202. Routine Description:
  203. Return the current capacity increment (realloc amount) of the ARRAY.
  204. Arguments:
  205. None.
  206. Return Value:
  207. ULONG - Current capacity increment.
  208. --*/
  209. {
  210. return( _CapacityIncrement );
  211. }
  212. INLINE
  213. VOID
  214. ARRAY::SetCapacityIncrement (
  215. IN ULONG CapacityIncrement
  216. )
  217. /*++
  218. Routine Description:
  219. Set the capacity incement value.
  220. Arguments:
  221. CapacityIncrement - Supplies the new value for the capacity increment.
  222. Return Value:
  223. None.
  224. --*/
  225. {
  226. _CapacityIncrement = CapacityIncrement;
  227. }
  228. INLINE
  229. LONG
  230. ARRAY::CompareAscDesc(
  231. IN POBJECT Object1,
  232. IN POBJECT Object2,
  233. IN BOOLEAN Ascending
  234. )
  235. /*++
  236. Routine Description:
  237. Compares two object accordint to an Ascending flag
  238. Arguments:
  239. Object1 - Supplies first object
  240. Object2 - Supplies second object
  241. Ascending - Supplies ascending flag
  242. Return Value:
  243. LONG - If Ascending:
  244. <0 if Object1 is less that Object2
  245. 0 if Object1 is equal to Object2
  246. >0 if Object1 is greater than Object2
  247. If !Ascending:
  248. <0 if Object2 is less that Object1
  249. 0 if Object2 is equal to Object1
  250. >0 if Object2 is greater than Object1
  251. --*/
  252. {
  253. return ( Ascending ? CompareAscending( &Object1, &Object2 ) :
  254. CompareDescending( &Object1, &Object2) );
  255. }
  256. INLINE
  257. PPOBJECT
  258. ARRAY::GetObjectArray (
  259. )
  260. /*++
  261. Routine Description:
  262. Obtains pointer to the array of objects
  263. Arguments:
  264. None
  265. Return Value:
  266. PPOBJECT - Pointer to array of objects
  267. --*/
  268. {
  269. return _ObjectArray;
  270. }
  271. #endif // _ARRAY_