VECTOR.H 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. ** Command & Conquer(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: F:\projects\c&c\vcs\code\vector.h_v 2.15 16 Oct 1995 16:47:38 JOE_BOSTIC $ */
  19. /***********************************************************************************************
  20. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : VECTOR.H *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 02/19/95 *
  30. * *
  31. * Last Update : March 13, 1995 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * VectorClass<T>::VectorClass -- Constructor for vector class. *
  36. * VectorClass<T>::~VectorClass -- Default destructor for vector class. *
  37. * VectorClass<T>::VectorClass -- Copy constructor for vector object. *
  38. * VectorClass<T>::operator = -- The assignment operator. *
  39. * VectorClass<T>::operator == -- Equality operator for vector objects. *
  40. * VectorClass<T>::Clear -- Frees and clears the vector. *
  41. * VectorClass<T>::Resize -- Changes the size of the vector. *
  42. * DynamicVectorClass<T>::DynamicVectorClass -- Constructor for dynamic vector. *
  43. * DynamicVectorClass<T>::Resize -- Changes the size of a dynamic vector. *
  44. * DynamicVectorClass<T>::Add -- Add an element to the vector. *
  45. * DynamicVectorClass<T>::Delete -- Remove the specified object from the vector. *
  46. * DynamicVectorClass<T>::Delete -- Deletes the specified index from the vector. *
  47. * VectorClass<T>::ID -- Pointer based conversion to index number. *
  48. * VectorClass<T>::ID -- Finds object ID based on value. *
  49. * DynamicVectorClass<T>::ID -- Find matching value in the dynamic vector. *
  50. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  51. #ifndef VECTOR_H
  52. #define VECTOR_H
  53. #ifndef false
  54. #define false 0
  55. #endif
  56. #ifndef true
  57. #define true 1
  58. #endif
  59. #include <stdlib.h>
  60. #include <stddef.h>
  61. inline void * operator new(size_t , void * pointer) {return(pointer);}
  62. inline void * operator new[](size_t , void * pointer) {return(pointer);}
  63. /**************************************************************************
  64. ** This is a general purpose vector class. A vector is defined by this
  65. ** class, as an array of arbitrary objects where the array can be dynamically
  66. ** sized. Because is deals with arbitrary object types, it can handle everything.
  67. ** As a result of this, it is not terribly efficient for integral objects (such
  68. ** as char or int). It will function correctly, but the copy constructor and
  69. ** equality operator could be highly optimized if the integral type were known.
  70. ** This efficiency can be implemented by deriving an integral vector template
  71. ** from this one in order to supply more efficient routines.
  72. */
  73. template<class T>
  74. class VectorClass
  75. {
  76. public:
  77. VectorClass(unsigned size=0, T const * array=0);
  78. VectorClass(VectorClass<T> const &); // Copy constructor.
  79. virtual ~VectorClass(void);
  80. T & operator[](unsigned index) {return(Vector[index]);};
  81. T const & operator[](unsigned index) const {return(Vector[index]);};
  82. virtual VectorClass<T> & operator =(VectorClass<T> const &); // Assignment operator.
  83. virtual int operator == (VectorClass<T> const &) const; // Equality operator.
  84. virtual int Resize(unsigned newsize, T const * array=0);
  85. virtual void Clear(void);
  86. unsigned Length(void) const {return VectorMax;};
  87. virtual int ID(T const * ptr); // Pointer based identification.
  88. virtual int ID(T const & ptr); // Value based identification.
  89. protected:
  90. /*
  91. ** This is a pointer to the allocated vector array of elements.
  92. */
  93. T * Vector;
  94. /*
  95. ** This is the maximum number of elements allowed in this vector.
  96. */
  97. unsigned VectorMax;
  98. /*
  99. ** Does the vector data pointer refer to memory that this class has manually
  100. ** allocated? If so, then this class is responsible for deleting it.
  101. */
  102. unsigned IsAllocated:1;
  103. };
  104. /**************************************************************************
  105. ** This derivative vector class adds the concept of adding and deleting
  106. ** objects. The objects are packed to the beginning of the vector array.
  107. ** If this is instantiated for a class object, then the assignment operator
  108. ** and the equality operator must be supported. If the vector allocates its
  109. ** own memory, then the vector can grow if it runs out of room adding items.
  110. ** The growth rate is controlled by setting the growth step rate. A growth
  111. ** step rate of zero disallows growing.
  112. */
  113. template<class T>
  114. class DynamicVectorClass : public VectorClass<T>
  115. {
  116. public:
  117. DynamicVectorClass(unsigned size=0, T const * array=0);
  118. // Change maximum size of vector.
  119. virtual int Resize(unsigned newsize, T const * array=0);
  120. // Resets and frees the vector array.
  121. virtual void Clear(void) {ActiveCount = 0;VectorClass<T>::Clear();};
  122. // Fetch number of "allocated" vector objects.
  123. int Count(void) const {return(ActiveCount);};
  124. // Add object to vector (growing as necessary).
  125. int Add(T const & object);
  126. int Add_Head(T const & object);
  127. // Delete object just like this from vector.
  128. int Delete(T const & object);
  129. // Delete object at this vector index.
  130. int Delete(int index);
  131. // Deletes all objects in the vector.
  132. void Delete_All(void) {ActiveCount = 0;};
  133. // Set amount that vector grows by.
  134. int Set_Growth_Step(int step) {return(GrowthStep = step);};
  135. // Fetch current growth step rate.
  136. int Growth_Step(void) {return GrowthStep;};
  137. virtual int ID(T const * ptr) {return(VectorClass<T>::ID(ptr));};
  138. virtual int ID(T const & ptr);
  139. protected:
  140. /*
  141. ** This is a count of the number of active objects in this
  142. ** vector. The memory array often times is bigger than this
  143. ** value.
  144. */
  145. int ActiveCount;
  146. /*
  147. ** If there is insufficient room in the vector array for a new
  148. ** object to be added, then the vector will grow by the number
  149. ** of objects specified by this value. This is controlled by
  150. ** the Set_Growth_Step() function.
  151. */
  152. int GrowthStep;
  153. };
  154. /**************************************************************************
  155. ** This is a derivative of a vector class that supports boolean flags. Since
  156. ** a boolean flag can be represented by a single bit, this class packs the
  157. ** array of boolean flags into an array of bytes containing 8 boolean values
  158. ** each. For large boolean arrays, this results in an 87.5% savings. Although
  159. ** the indexing "[]" operator is supported, DO NOT pass pointers to sub elements
  160. ** of this bit vector class. A pointer derived from the indexing operator is
  161. ** only valid until the next call. Because of this, only simple
  162. ** direct use of the "[]" operator is allowed.
  163. */
  164. class BooleanVectorClass
  165. {
  166. public:
  167. BooleanVectorClass(unsigned size=0, unsigned char * array=0);
  168. BooleanVectorClass(BooleanVectorClass const & vector);
  169. // Assignment operator.
  170. BooleanVectorClass & operator =(BooleanVectorClass const & vector);
  171. // Equivalency operator.
  172. int operator == (BooleanVectorClass const & vector);
  173. // Fetch number of boolean objects in vector.
  174. int Length(void) {return BitCount;};
  175. // Set all boolean values to false;
  176. void Reset(void);
  177. // Set all boolean values to true.
  178. void Set(void);
  179. // Resets vector to zero length (frees memory).
  180. void Clear(void);
  181. // Change size of this boolean vector.
  182. int Resize(unsigned size);
  183. // Fetch reference to specified index.
  184. bool const & operator[](int index) const {
  185. if (LastIndex != index) Fixup(index);
  186. return(Copy);
  187. };
  188. bool & operator[](int index) {
  189. if (LastIndex != index) Fixup(index);
  190. return(Copy);
  191. };
  192. // Quick check on boolean state.
  193. bool Is_True(int index) const {
  194. if (index == LastIndex) return(Copy);
  195. return(Get_Bit(&BitArray[0], index));
  196. };
  197. // Find first index that is false.
  198. int First_False(void) const {
  199. if (LastIndex != -1) Fixup(-1);
  200. int retval = First_False_Bit(&BitArray[0]);
  201. if (retval < BitCount) return(retval);
  202. /*
  203. ** Failure to find a false boolean value in the vector. Return this
  204. ** fact in the form of an invalid index number.
  205. */
  206. return(-1);
  207. }
  208. // Find first index that is true.
  209. int First_True(void) const {
  210. if (LastIndex != -1) Fixup(-1);
  211. int retval = First_True_Bit(&BitArray[0]);
  212. if (retval < BitCount) return(retval);
  213. /*
  214. ** Failure to find a true boolean value in the vector. Return this
  215. ** fact in the form of an invalid index number.
  216. */
  217. return(-1);
  218. }
  219. private:
  220. void Fixup(int index=-1) const;
  221. /*
  222. ** This is the number of boolean values in the vector. This value is
  223. ** not necessarily a multiple of 8, even though the underlying character
  224. ** vector contains a multiple of 8 bits.
  225. */
  226. int BitCount;
  227. /*
  228. ** This is a referential copy of an element in the bit vector. The
  229. ** purpose of this copy is to allow normal reference access to this
  230. ** object (for speed reasons). This hides the bit packing scheme from
  231. ** the user of this class.
  232. */
  233. bool Copy;
  234. /*
  235. ** This records the index of the value last fetched into the reference
  236. ** boolean variable. This index is used to properly restore the value
  237. ** when the reference copy needs updating.
  238. */
  239. int LastIndex;
  240. /*
  241. ** This points to the allocated bitfield array.
  242. */
  243. VectorClass<unsigned char> BitArray;
  244. };
  245. #endif