VECTOR.H 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. ** Command & Conquer Red Alert(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: /CounterStrike/VECTOR.H 1 3/03/97 10:26a 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. #ifdef NEVER
  54. #ifndef false
  55. #define false 0
  56. #endif
  57. #ifndef true
  58. #define true 1
  59. #endif
  60. #endif
  61. #include <stdlib.h>
  62. #include <stddef.h>
  63. inline void * operator new(size_t , void * pointer) {return(pointer);}
  64. inline void * operator new[](size_t , void * pointer) {return(pointer);}
  65. /**************************************************************************
  66. ** This is a general purpose vector class. A vector is defined by this
  67. ** class, as an array of arbitrary objects where the array can be dynamically
  68. ** sized. Because is deals with arbitrary object types, it can handle everything.
  69. ** As a result of this, it is not terribly efficient for integral objects (such
  70. ** as char or int). It will function correctly, but the copy constructor and
  71. ** equality operator could be highly optimized if the integral type were known.
  72. ** This efficiency can be implemented by deriving an integral vector template
  73. ** from this one in order to supply more efficient routines.
  74. */
  75. template<class T>
  76. class VectorClass
  77. {
  78. public:
  79. VectorClass(NoInitClass const & ) {};
  80. VectorClass(unsigned size=0, T const * array=0);
  81. VectorClass(VectorClass<T> const &); // Copy constructor.
  82. virtual ~VectorClass(void);
  83. T & operator[](unsigned index) {return(Vector[index]);};
  84. T const & operator[](unsigned index) const {return(Vector[index]);};
  85. virtual VectorClass<T> & operator =(VectorClass<T> const &); // Assignment operator.
  86. virtual int operator == (VectorClass<T> const &) const; // Equality operator.
  87. virtual int Resize(unsigned newsize, T const * array=0);
  88. virtual void Clear(void);
  89. unsigned Length(void) const {return VectorMax;};
  90. virtual int ID(T const * ptr); // Pointer based identification.
  91. virtual int ID(T const & ptr); // Value based identification.
  92. protected:
  93. /*
  94. ** This is a pointer to the allocated vector array of elements.
  95. */
  96. T * Vector;
  97. /*
  98. ** This is the maximum number of elements allowed in this vector.
  99. */
  100. unsigned VectorMax;
  101. /*
  102. ** Does the vector data pointer refer to memory that this class has manually
  103. ** allocated? If so, then this class is responsible for deleting it.
  104. */
  105. unsigned IsAllocated:1;
  106. };
  107. /**************************************************************************
  108. ** This derivative vector class adds the concept of adding and deleting
  109. ** objects. The objects are packed to the beginning of the vector array.
  110. ** If this is instantiated for a class object, then the assignment operator
  111. ** and the equality operator must be supported. If the vector allocates its
  112. ** own memory, then the vector can grow if it runs out of room adding items.
  113. ** The growth rate is controlled by setting the growth step rate. A growth
  114. ** step rate of zero disallows growing.
  115. */
  116. template<class T>
  117. class DynamicVectorClass : public VectorClass<T>
  118. {
  119. public:
  120. DynamicVectorClass(unsigned size=0, T const * array=0);
  121. // Change maximum size of vector.
  122. virtual int Resize(unsigned newsize, T const * array=0);
  123. // Resets and frees the vector array.
  124. virtual void Clear(void) {ActiveCount = 0;VectorClass<T>::Clear();};
  125. // Fetch number of "allocated" vector objects.
  126. int Count(void) const {return(ActiveCount);};
  127. // Add object to vector (growing as necessary).
  128. int Add(T const & object);
  129. int Add_Head(T const & object);
  130. // Delete object just like this from vector.
  131. int Delete(T const & object);
  132. // Delete object at this vector index.
  133. int Delete(int index);
  134. // Deletes all objects in the vector.
  135. void Delete_All(void) {ActiveCount = 0;};
  136. // Set amount that vector grows by.
  137. int Set_Growth_Step(int step) {return(GrowthStep = step);};
  138. // Fetch current growth step rate.
  139. int Growth_Step(void) {return GrowthStep;};
  140. virtual int ID(T const * ptr) {return(VectorClass<T>::ID(ptr));};
  141. virtual int ID(T const & ptr);
  142. protected:
  143. /*
  144. ** This is a count of the number of active objects in this
  145. ** vector. The memory array often times is bigger than this
  146. ** value.
  147. */
  148. int ActiveCount;
  149. /*
  150. ** If there is insufficient room in the vector array for a new
  151. ** object to be added, then the vector will grow by the number
  152. ** of objects specified by this value. This is controlled by
  153. ** the Set_Growth_Step() function.
  154. */
  155. int GrowthStep;
  156. };
  157. /**************************************************************************
  158. ** This is a derivative of a vector class that supports boolean flags. Since
  159. ** a boolean flag can be represented by a single bit, this class packs the
  160. ** array of boolean flags into an array of bytes containing 8 boolean values
  161. ** each. For large boolean arrays, this results in an 87.5% savings. Although
  162. ** the indexing "[]" operator is supported, DO NOT pass pointers to sub elements
  163. ** of this bit vector class. A pointer derived from the indexing operator is
  164. ** only valid until the next call. Because of this, only simple
  165. ** direct use of the "[]" operator is allowed.
  166. */
  167. class BooleanVectorClass
  168. {
  169. public:
  170. BooleanVectorClass(unsigned size=0, unsigned char * array=0);
  171. BooleanVectorClass(BooleanVectorClass const & vector);
  172. // Assignment operator.
  173. BooleanVectorClass & operator =(BooleanVectorClass const & vector);
  174. // Equivalency operator.
  175. int operator == (BooleanVectorClass const & vector);
  176. // Fetch number of boolean objects in vector.
  177. int Length(void) {return BitCount;};
  178. // Set all boolean values to false;
  179. void Reset(void);
  180. // Set all boolean values to true.
  181. void Set(void);
  182. // Resets vector to zero length (frees memory).
  183. void Clear(void);
  184. // Change size of this boolean vector.
  185. int Resize(unsigned size);
  186. // Fetch reference to specified index.
  187. bool const & operator[](int index) const {
  188. if (LastIndex != index) Fixup(index);
  189. return(Copy);
  190. };
  191. bool & operator[](int index) {
  192. if (LastIndex != index) Fixup(index);
  193. return(Copy);
  194. };
  195. // Quick check on boolean state.
  196. bool Is_True(int index) const {
  197. if (index == LastIndex) return(Copy);
  198. return(Get_Bit(&BitArray[0], index));
  199. };
  200. // Find first index that is false.
  201. int First_False(void) const {
  202. if (LastIndex != -1) Fixup(-1);
  203. int retval = First_False_Bit(&BitArray[0]);
  204. if (retval < BitCount) return(retval);
  205. /*
  206. ** Failure to find a false boolean value in the vector. Return this
  207. ** fact in the form of an invalid index number.
  208. */
  209. return(-1);
  210. }
  211. // Find first index that is true.
  212. int First_True(void) const {
  213. if (LastIndex != -1) Fixup(-1);
  214. int retval = First_True_Bit(&BitArray[0]);
  215. if (retval < BitCount) return(retval);
  216. /*
  217. ** Failure to find a true boolean value in the vector. Return this
  218. ** fact in the form of an invalid index number.
  219. */
  220. return(-1);
  221. }
  222. private:
  223. void Fixup(int index=-1) const;
  224. /*
  225. ** This is the number of boolean values in the vector. This value is
  226. ** not necessarily a multiple of 8, even though the underlying character
  227. ** vector contains a multiple of 8 bits.
  228. */
  229. int BitCount;
  230. /*
  231. ** This is a referential copy of an element in the bit vector. The
  232. ** purpose of this copy is to allow normal reference access to this
  233. ** object (for speed reasons). This hides the bit packing scheme from
  234. ** the user of this class.
  235. */
  236. bool Copy;
  237. /*
  238. ** This records the index of the value last fetched into the reference
  239. ** boolean variable. This index is used to properly restore the value
  240. ** when the reference copy needs updating.
  241. */
  242. int LastIndex;
  243. /*
  244. ** This points to the allocated bitfield array.
  245. */
  246. VectorClass<unsigned char> BitArray;
  247. };
  248. #endif