vector.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. ** Command & Conquer Renegade(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. /***********************************************************************************************
  19. *** 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 ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/vector.cpp $*
  25. * *
  26. * $Author:: Jani_p $*
  27. * *
  28. * $Modtime:: 1/16/02 11:40a $*
  29. * *
  30. * $Revision:: 20 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * BooleanVectorClass::BooleanVectorClass -- Copy constructor for boolean array. *
  35. * BooleanVectorClass::BooleanVectorClass -- Explicit data buffer constructor. *
  36. * BooleanVectorClass::Clear -- Resets boolean vector to empty state. *
  37. * BooleanVectorClass::Fixup -- Updates the boolean vector to a known state. *
  38. * BooleanVectorClass::Reset -- Clear all boolean values in array. *
  39. * BooleanVectorClass::Resize -- Resizes a boolean vector object. *
  40. * BooleanVectorClass::Set -- Forces all boolean elements to true. *
  41. * BooleanVectorClass::operator = -- Assignment operator. *
  42. * BooleanVectorClass::operator == -- Comparison operator for boolean vector. *
  43. * VectorClass<T>::Clear -- Frees and clears the vector. *
  44. * VectorClass<T>::ID -- Finds object ID based on value. *
  45. * VectorClass<T>::ID -- Pointer based conversion to index number. *
  46. * VectorClass<T>::Resize -- Changes the size of the vector. *
  47. * VectorClass<T>::VectorClass -- Constructor for vector class. *
  48. * VectorClass<T>::VectorClass -- Copy constructor for vector object. *
  49. * VectorClass<T>::operator = -- The assignment operator. *
  50. * VectorClass<T>::operator == -- Equality operator for vector objects. *
  51. * VectorClass<T>::~VectorClass -- Default destructor for vector class. *
  52. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  53. #include "always.h"
  54. #include "vector.h"
  55. #include <string.h>
  56. /*
  57. ** The following template function can be located here ONLY if all the instantiations are
  58. ** declared in a header file this module includes. By placing the template functions here,
  59. ** it speeds up compiler operation and reduces object module size.
  60. */
  61. //----------------------------------------------------------------------------------------------
  62. /***********************************************************************************************
  63. * BooleanVectorClass::BooleanVectorClass -- Explicit data buffer constructor. *
  64. * *
  65. * This is the constructor for a boolean array. This constructor takes the memory pointer *
  66. * provided as assigns that as the array data pointer. *
  67. * *
  68. * INPUT: size -- The size of the array (in bits). *
  69. * *
  70. * array -- Pointer to the memory that the array is to use. *
  71. * *
  72. * OUTPUT: none *
  73. * *
  74. * WARNINGS: You must make sure that the memory specified is large enough to contain the *
  75. * bits specified. *
  76. * *
  77. * HISTORY: *
  78. * 07/18/1995 JLB : Created. *
  79. *=============================================================================================*/
  80. BooleanVectorClass::BooleanVectorClass(unsigned size, unsigned char * array) :
  81. BitCount(size),
  82. Copy(false),
  83. LastIndex(-1),
  84. BitArray(0, 0)
  85. {
  86. BitArray.Resize(((size + (8-1)) / 8), array);
  87. // LastIndex = -1;
  88. // BitCount = size;
  89. }
  90. /***********************************************************************************************
  91. * BooleanVectorClass::BooleanVectorClass -- Copy constructor of boolean array. *
  92. * *
  93. * This is the copy constructor for a boolean array. It is used to make a duplicate of the *
  94. * boolean array. *
  95. * *
  96. * INPUT: vector -- Reference to the vector to be duplicated. *
  97. * *
  98. * OUTPUT: none *
  99. * *
  100. * WARNINGS: none *
  101. * *
  102. * HISTORY: *
  103. * 07/18/1995 JLB : Created. *
  104. *=============================================================================================*/
  105. BooleanVectorClass::BooleanVectorClass(BooleanVectorClass const & vector)
  106. {
  107. LastIndex = -1;
  108. *this = vector;
  109. }
  110. /***********************************************************************************************
  111. * BooleanVectorClass::operator = -- Assignment operator. *
  112. * *
  113. * This routine will make a copy of the specified boolean vector array. The vector is *
  114. * copied into an already constructed existing vector. The values from the existing vector *
  115. * are destroyed by this copy. *
  116. * *
  117. * INPUT: vector -- Reference to the vector to make a copy of. *
  118. * *
  119. * OUTPUT: none *
  120. * *
  121. * WARNINGS: none *
  122. * *
  123. * HISTORY: *
  124. * 07/18/1995 JLB : Created. *
  125. *=============================================================================================*/
  126. BooleanVectorClass & BooleanVectorClass::operator =(BooleanVectorClass const & vector)
  127. {
  128. Fixup();
  129. Copy = vector.Copy;
  130. LastIndex = vector.LastIndex;
  131. BitArray = vector.BitArray;
  132. BitCount = vector.BitCount;
  133. return(*this);
  134. }
  135. /***********************************************************************************************
  136. * BooleanVectorClass::operator == -- Comparison operator for boolean vector. *
  137. * *
  138. * This is the comparison operator for a boolean vector class. Boolean vectors are equal *
  139. * if the bit count and bit values are identical. *
  140. * *
  141. * INPUT: vector -- Reference to the vector to compare to. *
  142. * *
  143. * OUTPUT: Are the boolean vectors identical? *
  144. * *
  145. * WARNINGS: none *
  146. * *
  147. * HISTORY: *
  148. * 07/18/1995 JLB : Created. *
  149. *=============================================================================================*/
  150. bool BooleanVectorClass::operator == (const BooleanVectorClass & vector) const
  151. {
  152. Fixup(LastIndex);
  153. return(BitCount == vector.BitCount && BitArray == vector.BitArray);
  154. }
  155. /***********************************************************************************************
  156. * BooleanVectorClass::Resize -- Resizes a boolean vector object. *
  157. * *
  158. * This routine will resize the boolean vector object. An index value used with a boolean *
  159. * vector must be less than the value specified in as the new size. *
  160. * *
  161. * INPUT: size -- The new maximum size of this boolean vector. *
  162. * *
  163. * OUTPUT: Was the boolean vector sized successfully? *
  164. * *
  165. * WARNINGS: The boolean array might be reallocated or even deleted by this routine. *
  166. * *
  167. * HISTORY: *
  168. * 07/18/1995 JLB : Created. *
  169. *=============================================================================================*/
  170. int BooleanVectorClass::Resize(unsigned size)
  171. {
  172. Fixup();
  173. if (size > 0) {
  174. /*
  175. ** Record the previous bit count of the boolean vector. This is used
  176. ** to determine if the array has grown in size and thus clearing is
  177. ** necessary.
  178. */
  179. unsigned oldsize = BitCount;
  180. /*
  181. ** Actually resize the bit array. Since this is a bit packed array,
  182. ** there are 8 elements per byte (rounded up).
  183. */
  184. int success = BitArray.Resize(((size + (8-1)) / 8));
  185. /*
  186. ** Since there is no default constructor for bit packed integers, a manual
  187. ** clearing of the bits is required.
  188. */
  189. BitCount = size;
  190. if (success && oldsize < size) {
  191. for (unsigned index = oldsize; index < size; index++) {
  192. (*this)[index] = 0;
  193. }
  194. }
  195. return(success);
  196. }
  197. /*
  198. ** Resizing to zero is the same as clearing and deallocating the array.
  199. ** This is always successful.
  200. */
  201. Clear();
  202. return(true);
  203. }
  204. /***********************************************************************************************
  205. * BooleanVectorClass::Clear -- Resets boolean vector to empty state. *
  206. * *
  207. * This routine will clear out the boolean array. This will free any allocated memory and *
  208. * result in the boolean vector being unusable until the Resize function is subsequently *
  209. * called. *
  210. * *
  211. * INPUT: none *
  212. * *
  213. * OUTPUT: none *
  214. * *
  215. * WARNINGS: The boolean vector cannot be used until it is resized to a non null condition. *
  216. * *
  217. * HISTORY: *
  218. * 07/18/1995 JLB : Created. *
  219. *=============================================================================================*/
  220. void BooleanVectorClass::Clear(void)
  221. {
  222. Fixup();
  223. BitCount = 0;
  224. BitArray.Clear();
  225. }
  226. /***********************************************************************************************
  227. * BooleanVectorClass::Reset -- Clear all boolean values in array. *
  228. * *
  229. * This is the preferred (and quick) method to clear the boolean array to a false condition.*
  230. * *
  231. * INPUT: none *
  232. * *
  233. * OUTPUT: none *
  234. * *
  235. * WARNINGS: none *
  236. * *
  237. * HISTORY: *
  238. * 07/18/1995 JLB : Created. *
  239. *=============================================================================================*/
  240. void BooleanVectorClass::Reset(void)
  241. {
  242. LastIndex = -1;
  243. if (BitArray.Length() > 0) {
  244. memset(&BitArray[0], '\0', BitArray.Length());
  245. }
  246. }
  247. /***********************************************************************************************
  248. * BooleanVectorClass::Set -- Forces all boolean elements to true. *
  249. * *
  250. * This is the preferred (and fast) way to set all boolean elements to true. *
  251. * *
  252. * INPUT: none *
  253. * *
  254. * OUTPUT: none *
  255. * *
  256. * WARNINGS: none *
  257. * *
  258. * HISTORY: *
  259. * 07/18/1995 JLB : Created. *
  260. *=============================================================================================*/
  261. void BooleanVectorClass::Set(void)
  262. {
  263. LastIndex = -1;
  264. if (BitArray.Length() > 0) {
  265. memset(&BitArray[0], '\xFF', BitArray.Length());
  266. }
  267. }
  268. /***********************************************************************************************
  269. * BooleanVectorClass::Fixup -- Updates the boolean vector to a known state. *
  270. * *
  271. * Use this routine to set the boolean value copy to match the appropriate bit in the *
  272. * boolean array. The boolean array will be updated with any changes from the last time *
  273. * a value was fetched from the boolean vector. By using this update method, the boolean *
  274. * array can be treated as a normal array even though the elements are composed of *
  275. * otherwise inaccessible bits. *
  276. * *
  277. * INPUT: index -- The index to set the new copy value to. If the index is -1, then the *
  278. * previous value will be updated into the vector array, but no new value *
  279. * will be fetched from it. *
  280. * *
  281. * OUTPUT: none *
  282. * *
  283. * WARNINGS: Always call this routine with "-1" if any direct manipulation of the bit *
  284. * array is to occur. This ensures that the bit array is accurate. *
  285. * *
  286. * HISTORY: *
  287. * 07/18/1995 JLB : Created. *
  288. *=============================================================================================*/
  289. void BooleanVectorClass::Fixup(int index) const
  290. {
  291. /*
  292. ** If the requested index value is illegal, then force the index
  293. ** to be -1. This is the default non-index value.
  294. */
  295. if ((unsigned)index >= (unsigned)BitCount) {
  296. index = -1;
  297. }
  298. /*
  299. ** If the new index is different than the previous index, there might
  300. ** be some fixing up required.
  301. */
  302. if (index != LastIndex) {
  303. /*
  304. ** If the previously fetched boolean value was changed, then update
  305. ** the boolean array accordingly.
  306. */
  307. if (LastIndex != -1) {
  308. assert(unsigned(LastIndex) < unsigned(BitCount));
  309. Set_Bit((void*)&BitArray[0], LastIndex, Copy);
  310. }
  311. /*
  312. ** If this new current index is valid, then fill in the reference boolean
  313. ** value with the appropriate data from the bit array.
  314. */
  315. if (index != -1) {
  316. assert(unsigned(index) < unsigned(BitCount));
  317. ((unsigned char &)Copy) = (unsigned char)Get_Bit((void*)&BitArray[0], index);
  318. // ((unsigned char&)Copy) = Get_Bit((void*)&BitArray[0], index);
  319. }
  320. ((BooleanVectorClass *)this)->LastIndex = index;
  321. }
  322. }
  323. /***********************************************************************************************
  324. * BooleanVectorClass::Init -- Initializes the bit vector from an user array. *
  325. * *
  326. * INPUT: none *
  327. * *
  328. * OUTPUT: none *
  329. * *
  330. * WARNINGS: none *
  331. * *
  332. * HISTORY: *
  333. * 07/18/1995 JLB : Created. *
  334. *=============================================================================================*/
  335. void BooleanVectorClass::Init(unsigned size, unsigned char * array)
  336. {
  337. Copy = false;
  338. LastIndex = -1;
  339. BitCount = size;
  340. BitArray.Resize(((size + (8-1)) / 8), array);
  341. }
  342. /***********************************************************************************************
  343. * BooleanVectorClass::Init -- Initializes the bit vector from an user array. *
  344. * *
  345. * INPUT: none *
  346. * *
  347. * OUTPUT: none *
  348. * *
  349. * WARNINGS: none *
  350. * *
  351. * HISTORY: *
  352. * 07/18/1995 JLB : Created. *
  353. *=============================================================================================*/
  354. void BooleanVectorClass::Init(unsigned size)
  355. {
  356. Copy = false;
  357. LastIndex = -1;
  358. BitCount = size;
  359. BitArray.Resize(((size + (8-1)) / 8));
  360. }