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.

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