VECTOR.CPP 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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.CPP 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.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 02/19/95 *
  30. * *
  31. * Last Update : September 21, 1995 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * BooleanVectorClass::BooleanVectorClass -- Copy constructor for boolean array. *
  36. * BooleanVectorClass::BooleanVectorClass -- Explicit data buffer constructor. *
  37. * BooleanVectorClass::Clear -- Resets boolean vector to empty state. *
  38. * BooleanVectorClass::Fixup -- Updates the boolean vector to a known state. *
  39. * BooleanVectorClass::Reset -- Clear all boolean values in array. *
  40. * BooleanVectorClass::Resize -- Resizes a boolean vector object. *
  41. * BooleanVectorClass::Set -- Forces all boolean elements to true. *
  42. * BooleanVectorClass::operator = -- Assignment operator. *
  43. * BooleanVectorClass::operator == -- Comparison operator for boolean vector. *
  44. * VectorClass<T>::Clear -- Frees and clears the vector. *
  45. * VectorClass<T>::ID -- Finds object ID based on value. *
  46. * VectorClass<T>::ID -- Pointer based conversion to index number. *
  47. * VectorClass<T>::Resize -- Changes the size of the vector. *
  48. * VectorClass<T>::VectorClass -- Constructor for vector class. *
  49. * VectorClass<T>::VectorClass -- Copy constructor for vector object. *
  50. * VectorClass<T>::operator = -- The assignment operator. *
  51. * VectorClass<T>::operator == -- Equality operator for vector objects. *
  52. * VectorClass<T>::~VectorClass -- Default destructor for vector class. *
  53. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  54. #include "function.h"
  55. #ifdef WINSOCK_IPX
  56. #include "WSProto.h"
  57. #include "WSPUDP.h"
  58. #endif //WINSOCK_IPX
  59. #include "vector.h"
  60. #include <mem.h>
  61. #include <stdio.h>
  62. /*
  63. ** The following template function can be located here ONLY if all the instantiations are
  64. ** declared in a header file this module includes. By placing the template functions here,
  65. ** it speeds up compiler operation and reduces object module size.
  66. */
  67. /***********************************************************************************************
  68. * VectorClass<T>::VectorClass -- Constructor for vector class. *
  69. * *
  70. * This constructor for the vector class is passed the initial size of the vector and an *
  71. * optional pointer to a preallocated block of memory that the vector will be placed in. *
  72. * If this optional pointer is NULL (or not provided), then the vector is allocated out *
  73. * of free store (with the "new" operator). *
  74. * *
  75. * INPUT: size -- The number of elements to initialize this vector to. *
  76. * *
  77. * array -- Optional pointer to a previously allocated memory block to hold the *
  78. * vector. *
  79. * *
  80. * OUTPUT: none *
  81. * *
  82. * WARNINGS: none *
  83. * *
  84. * HISTORY: *
  85. * 03/10/1995 JLB : Created. *
  86. *=============================================================================================*/
  87. template<class T>
  88. VectorClass<T>::VectorClass(unsigned size, T const * array) :
  89. Vector(0),
  90. VectorMax(size),
  91. IsAllocated(false)
  92. {
  93. /*
  94. ** Allocate the vector. The default constructor will be called for every
  95. ** object in this vector.
  96. */
  97. if (size) {
  98. if (array) {
  99. Vector = new((void*)array) T[size];
  100. } else {
  101. Vector = new T[size];
  102. IsAllocated = true;
  103. }
  104. }
  105. }
  106. /***********************************************************************************************
  107. * VectorClass<T>::~VectorClass -- Default destructor for vector class. *
  108. * *
  109. * This is the default destructor for the vector class. It will deallocate any memory *
  110. * that it may have allocated. *
  111. * *
  112. * INPUT: none *
  113. * *
  114. * OUTPUT: none *
  115. * *
  116. * WARNINGS: none *
  117. * *
  118. * HISTORY: *
  119. * 03/10/1995 JLB : Created. *
  120. *=============================================================================================*/
  121. template<class T>
  122. VectorClass<T>::~VectorClass(void)
  123. {
  124. VectorClass<T>::Clear();
  125. }
  126. /***********************************************************************************************
  127. * VectorClass<T>::VectorClass -- Copy constructor for vector object. *
  128. * *
  129. * This is the copy constructor for the vector class. It will duplicate the provided *
  130. * vector into the new vector being created. *
  131. * *
  132. * INPUT: vector -- Reference to the vector to use as a copy. *
  133. * *
  134. * OUTPUT: none *
  135. * *
  136. * WARNINGS: none *
  137. * *
  138. * HISTORY: *
  139. * 03/10/1995 JLB : Created. *
  140. *=============================================================================================*/
  141. template<class T>
  142. VectorClass<T>::VectorClass(VectorClass<T> const & vector) :
  143. Vector(0),
  144. VectorMax(0),
  145. IsAllocated(false)
  146. {
  147. *this = vector;
  148. }
  149. /***********************************************************************************************
  150. * VectorClass<T>::operator = -- The assignment operator. *
  151. * *
  152. * This the the assignment operator for vector objects. It will alter the existing lvalue *
  153. * vector to duplicate the rvalue one. *
  154. * *
  155. * INPUT: vector -- The rvalue vector to copy into the lvalue one. *
  156. * *
  157. * OUTPUT: Returns with reference to the newly copied vector. *
  158. * *
  159. * WARNINGS: none *
  160. * *
  161. * HISTORY: *
  162. * 03/10/1995 JLB : Created. *
  163. *=============================================================================================*/
  164. template<class T>
  165. VectorClass<T> & VectorClass<T>::operator =(VectorClass<T> const & vector)
  166. {
  167. if (this != &vector) {
  168. Clear();
  169. VectorMax = vector.Length();
  170. if (VectorMax) {
  171. Vector = new T[VectorMax];
  172. if (Vector) {
  173. IsAllocated = true;
  174. for (int index = 0; index < VectorMax; index++) {
  175. Vector[index] = vector[index];
  176. }
  177. }
  178. } else {
  179. Vector = 0;
  180. IsAllocated = false;
  181. }
  182. }
  183. return(*this);
  184. }
  185. /***********************************************************************************************
  186. * VectorClass<T>::operator == -- Equality operator for vector objects. *
  187. * *
  188. * This operator compares two vectors for equality. It does this by performing an object *
  189. * by object comparison between the two vectors. *
  190. * *
  191. * INPUT: vector -- The right vector expression. *
  192. * *
  193. * OUTPUT: bool; Are the two vectors essentially equal? (do they contain comparable elements *
  194. * in the same order?) *
  195. * *
  196. * WARNINGS: The equality operator must exist for the objects that this vector contains. *
  197. * *
  198. * HISTORY: *
  199. * 03/10/1995 JLB : Created. *
  200. *=============================================================================================*/
  201. template<class T>
  202. int VectorClass<T>::operator == (VectorClass<T> const & vector) const
  203. {
  204. if (VectorMax == vector.Length()) {
  205. for (int index = 0; index < VectorMax; index++) {
  206. if (Vector[index] != vector[index]) {
  207. return(false);
  208. }
  209. }
  210. return(true);
  211. }
  212. return(false);
  213. }
  214. /***********************************************************************************************
  215. * VectorClass<T>::ID -- Pointer based conversion to index number. *
  216. * *
  217. * Use this routine to convert a pointer to an element in the vector back into the index *
  218. * number of that object. This routine ONLY works with actual pointers to object within *
  219. * the vector. For "equivalent" object index number (such as with similar integral values) *
  220. * then use the "by value" index number ID function. *
  221. * *
  222. * INPUT: pointer -- Pointer to an actual object in the vector. *
  223. * *
  224. * OUTPUT: Returns with the index number for the object pointed to by the parameter. *
  225. * *
  226. * WARNINGS: This routine is only valid for actual pointers to object that exist within *
  227. * the vector. All other object pointers will yield undefined results. *
  228. * *
  229. * HISTORY: *
  230. * 03/13/1995 JLB : Created. *
  231. *=============================================================================================*/
  232. template<class T>
  233. inline int VectorClass<T>::ID(T const * ptr)
  234. {
  235. return(((unsigned long)ptr - (unsigned long)&(*this)[0]) / sizeof(T));
  236. }
  237. /***********************************************************************************************
  238. * VectorClass<T>::ID -- Finds object ID based on value. *
  239. * *
  240. * Use this routine to find the index value of an object with equivalent value in the *
  241. * vector. Typical use of this would be for integral types. *
  242. * *
  243. * INPUT: object -- Reference to the object that is to be looked up in the vector. *
  244. * *
  245. * OUTPUT: Returns with the index number of the object that is equivalent to the one *
  246. * specified. If no matching value could be found then -1 is returned. *
  247. * *
  248. * WARNINGS: none *
  249. * *
  250. * HISTORY: *
  251. * 03/13/1995 JLB : Created. *
  252. *=============================================================================================*/
  253. template<class T>
  254. int VectorClass<T>::ID(T const & object)
  255. {
  256. for (int index = 0; index < VectorMax; index++) {
  257. if ((*this)[index] == object) {
  258. return(index);
  259. }
  260. }
  261. return(-1);
  262. }
  263. /***********************************************************************************************
  264. * VectorClass<T>::Clear -- Frees and clears the vector. *
  265. * *
  266. * Use this routine to reset the vector to an empty (non-allocated) state. A vector will *
  267. * free all allocated memory when this routine is called. In order for the vector to be *
  268. * useful after this point, the Resize function must be called to give it element space. *
  269. * *
  270. * INPUT: none *
  271. * *
  272. * OUTPUT: none *
  273. * *
  274. * WARNINGS: none *
  275. * *
  276. * HISTORY: *
  277. * 03/10/1995 JLB : Created. *
  278. *=============================================================================================*/
  279. template<class T>
  280. void VectorClass<T>::Clear(void)
  281. {
  282. if (Vector && IsAllocated) {
  283. delete[] Vector;
  284. Vector = 0;
  285. }
  286. IsAllocated = false;
  287. VectorMax = 0;
  288. }
  289. /***********************************************************************************************
  290. * VectorClass<T>::Resize -- Changes the size of the vector. *
  291. * *
  292. * This routine is used to change the size (usually to increase) the size of a vector. This *
  293. * is the only way to increase the vector's working room (number of elements). *
  294. * *
  295. * INPUT: newsize -- The desired size of the vector. *
  296. * *
  297. * array -- Optional pointer to a previously allocated memory block that the *
  298. * array will be located in. If this parameter is not supplied, then *
  299. * the array will be allocated from free store. *
  300. * *
  301. * OUTPUT: bool; Was the array resized successfully? *
  302. * *
  303. * WARNINGS: Failure to succeed could be the result of running out of memory. *
  304. * *
  305. * HISTORY: *
  306. * 03/10/1995 JLB : Created. *
  307. *=============================================================================================*/
  308. template<class T>
  309. int VectorClass<T>::Resize(unsigned newsize, T const * array)
  310. {
  311. if (newsize) {
  312. /*
  313. ** Allocate a new vector of the size specified. The default constructor
  314. ** will be called for every object in this vector.
  315. */
  316. T * newptr;
  317. if (!array) {
  318. newptr = new T[newsize];
  319. } else {
  320. newptr = new((void*)array) T[newsize];
  321. }
  322. if (!newptr) {
  323. return(false);
  324. }
  325. /*
  326. ** If there is an old vector, then it must be copied (as much as is feasible)
  327. ** to the new vector.
  328. */
  329. if (Vector) {
  330. /*
  331. ** Copy as much of the old vector into the new vector as possible. This
  332. ** presumes that there is a functional assignment operator for each
  333. ** of the objects in the vector.
  334. */
  335. int copycount = (newsize < VectorMax) ? newsize : VectorMax;
  336. for (int index = 0; index < copycount; index++) {
  337. newptr[index] = Vector[index];
  338. }
  339. /*
  340. ** Delete the old vector. This might cause the destructors to be called
  341. ** for all of the old elements. This makes the implementation of suitable
  342. ** assignment operator very important. The default assignment operator will
  343. ** only work for the simplest of objects.
  344. */
  345. if (IsAllocated) {
  346. delete[] Vector;
  347. Vector = 0;
  348. }
  349. }
  350. /*
  351. ** Assign the new vector data to this class.
  352. */
  353. Vector = newptr;
  354. VectorMax = newsize;
  355. IsAllocated = (Vector && !array);
  356. } else {
  357. /*
  358. ** Resizing to zero is the same as clearing the vector.
  359. */
  360. Clear();
  361. }
  362. return(true);
  363. }
  364. //----------------------------------------------------------------------------------------------
  365. /***********************************************************************************************
  366. * BooleanVectorClass::BooleanVectorClass -- Explicit data buffer constructor. *
  367. * *
  368. * This is the constructor for a boolean array. This constructor takes the memory pointer *
  369. * provided as assigns that as the array data pointer. *
  370. * *
  371. * INPUT: size -- The size of the array (in bits). *
  372. * *
  373. * array -- Pointer to the memory that the array is to use. *
  374. * *
  375. * OUTPUT: none *
  376. * *
  377. * WARNINGS: You must make sure that the memory specified is large enough to contain the *
  378. * bits specified. *
  379. * *
  380. * HISTORY: *
  381. * 07/18/1995 JLB : Created. *
  382. *=============================================================================================*/
  383. BooleanVectorClass::BooleanVectorClass(unsigned size, unsigned char * array)
  384. {
  385. BitArray.Resize(((size + (8-1)) / 8), array);
  386. LastIndex = -1;
  387. BitCount = size;
  388. }
  389. /***********************************************************************************************
  390. * BooleanVectorClass::BooleanVectorClass -- Copy constructor of boolean array. *
  391. * *
  392. * This is the copy constructor for a boolean array. It is used to make a duplicate of the *
  393. * boolean array. *
  394. * *
  395. * INPUT: vector -- Reference to the vector to be duplicated. *
  396. * *
  397. * OUTPUT: none *
  398. * *
  399. * WARNINGS: none *
  400. * *
  401. * HISTORY: *
  402. * 07/18/1995 JLB : Created. *
  403. *=============================================================================================*/
  404. BooleanVectorClass::BooleanVectorClass(BooleanVectorClass const & vector)
  405. {
  406. LastIndex = -1;
  407. *this = vector;
  408. }
  409. /***********************************************************************************************
  410. * BooleanVectorClass::operator = -- Assignment operator. *
  411. * *
  412. * This routine will make a copy of the specified boolean vector array. The vector is *
  413. * copied into an already constructed existing vector. The values from the existing vector *
  414. * are destroyed by this copy. *
  415. * *
  416. * INPUT: vector -- Reference to the vector to make a copy of. *
  417. * *
  418. * OUTPUT: none *
  419. * *
  420. * WARNINGS: none *
  421. * *
  422. * HISTORY: *
  423. * 07/18/1995 JLB : Created. *
  424. *=============================================================================================*/
  425. BooleanVectorClass & BooleanVectorClass::operator =(BooleanVectorClass const & vector)
  426. {
  427. Fixup();
  428. Copy = vector.Copy;
  429. LastIndex = vector.LastIndex;
  430. BitArray = vector.BitArray;
  431. BitCount = vector.BitCount;
  432. return(*this);
  433. }
  434. /***********************************************************************************************
  435. * BooleanVectorClass::operator == -- Comparison operator for boolean vector. *
  436. * *
  437. * This is the comparison operator for a boolean vector class. Boolean vectors are equal *
  438. * if the bit count and bit values are identical. *
  439. * *
  440. * INPUT: vector -- Reference to the vector to compare to. *
  441. * *
  442. * OUTPUT: Are the boolean vectors identical? *
  443. * *
  444. * WARNINGS: none *
  445. * *
  446. * HISTORY: *
  447. * 07/18/1995 JLB : Created. *
  448. *=============================================================================================*/
  449. int BooleanVectorClass::operator == (const BooleanVectorClass & vector)
  450. {
  451. Fixup(LastIndex);
  452. return(BitCount == vector.BitCount && BitArray == vector.BitArray);
  453. }
  454. /***********************************************************************************************
  455. * BooleanVectorClass::Resize -- Resizes a boolean vector object. *
  456. * *
  457. * This routine will resize the boolean vector object. An index value used with a boolean *
  458. * vector must be less than the value specified in as the new size. *
  459. * *
  460. * INPUT: size -- The new maximum size of this boolean vector. *
  461. * *
  462. * OUTPUT: Was the boolean vector sized successfully? *
  463. * *
  464. * WARNINGS: The boolean array might be reallocated or even deleted by this routine. *
  465. * *
  466. * HISTORY: *
  467. * 07/18/1995 JLB : Created. *
  468. *=============================================================================================*/
  469. int BooleanVectorClass::Resize(unsigned size)
  470. {
  471. Fixup();
  472. if (size) {
  473. /*
  474. ** Record the previous bit count of the boolean vector. This is used
  475. ** to determine if the array has grown in size and thus clearing is
  476. ** necessary.
  477. */
  478. int oldsize = BitCount;
  479. /*
  480. ** Actually resize the bit array. Since this is a bit packed array,
  481. ** there are 8 elements per byte (rounded up).
  482. */
  483. int success = BitArray.Resize(((size + (8-1)) / 8));
  484. /*
  485. ** Since there is no default constructor for bit packed integers, a manual
  486. ** clearing of the bits is required.
  487. */
  488. BitCount = size;
  489. if (success && oldsize < size) {
  490. for (int index = oldsize; index < size; index++) {
  491. (*this)[index] = 0;
  492. }
  493. }
  494. return(success);
  495. }
  496. /*
  497. ** Resizing to zero is the same as clearing and deallocating the array.
  498. ** This is always successful.
  499. */
  500. Clear();
  501. return(true);
  502. }
  503. /***********************************************************************************************
  504. * BooleanVectorClass::Clear -- Resets boolean vector to empty state. *
  505. * *
  506. * This routine will clear out the boolean array. This will free any allocated memory and *
  507. * result in the boolean vector being unusable until the Resize function is subsequently *
  508. * called. *
  509. * *
  510. * INPUT: none *
  511. * *
  512. * OUTPUT: none *
  513. * *
  514. * WARNINGS: The boolean vector cannot be used until it is resized to a non null condition. *
  515. * *
  516. * HISTORY: *
  517. * 07/18/1995 JLB : Created. *
  518. *=============================================================================================*/
  519. void BooleanVectorClass::Clear(void)
  520. {
  521. Fixup();
  522. BitCount = 0;
  523. BitArray.Clear();
  524. }
  525. /***********************************************************************************************
  526. * BooleanVectorClass::Reset -- Clear all boolean values in array. *
  527. * *
  528. * This is the preferred (and quick) method to clear the boolean array to a false condition.*
  529. * *
  530. * INPUT: none *
  531. * *
  532. * OUTPUT: none *
  533. * *
  534. * WARNINGS: none *
  535. * *
  536. * HISTORY: *
  537. * 07/18/1995 JLB : Created. *
  538. *=============================================================================================*/
  539. void BooleanVectorClass::Reset(void)
  540. {
  541. LastIndex = -1;
  542. if (BitArray.Length()) {
  543. memset(&BitArray[0], '\0', BitArray.Length());
  544. }
  545. }
  546. /***********************************************************************************************
  547. * BooleanVectorClass::Set -- Forces all boolean elements to true. *
  548. * *
  549. * This is the preferred (and fast) way to set all boolean elements to true. *
  550. * *
  551. * INPUT: none *
  552. * *
  553. * OUTPUT: none *
  554. * *
  555. * WARNINGS: none *
  556. * *
  557. * HISTORY: *
  558. * 07/18/1995 JLB : Created. *
  559. *=============================================================================================*/
  560. void BooleanVectorClass::Set(void)
  561. {
  562. LastIndex = -1;
  563. if (BitArray.Length()) {
  564. memset(&BitArray[0], '\xFF', BitArray.Length());
  565. }
  566. }
  567. /***********************************************************************************************
  568. * BooleanVectorClass::Fixup -- Updates the boolean vector to a known state. *
  569. * *
  570. * Use this routine to set the boolean value copy to match the appropriate bit in the *
  571. * boolean array. The boolean array will be updated with any changes from the last time *
  572. * a value was fetched from the boolean vector. By using this update method, the boolean *
  573. * array can be treated as a normal array even though the elements are composed of *
  574. * otherwise inaccessible bits. *
  575. * *
  576. * INPUT: index -- The index to set the new copy value to. If the index is -1, then the *
  577. * previous value will be updated into the vector array, but no new value *
  578. * will be fetched from it. *
  579. * *
  580. * OUTPUT: none *
  581. * *
  582. * WARNINGS: Always call this routine with "-1" if any direct manipulation of the bit *
  583. * array is to occur. This ensures that the bit array is accurate. *
  584. * *
  585. * HISTORY: *
  586. * 07/18/1995 JLB : Created. *
  587. *=============================================================================================*/
  588. void BooleanVectorClass::Fixup(int index) const
  589. {
  590. /*
  591. ** If the requested index value is illegal, then force the index
  592. ** to be -1. This is the default non-index value.
  593. */
  594. if ((unsigned)index >= BitCount) {
  595. index = -1;
  596. }
  597. /*
  598. ** If the new index is different than the previous index, there might
  599. ** be some fixing up required.
  600. */
  601. if (index != LastIndex) {
  602. /*
  603. ** If the previously fetched boolean value was changed, then update
  604. ** the boolean array accordingly.
  605. */
  606. if (LastIndex != -1) {
  607. Set_Bit((void*)&BitArray[0], LastIndex, Copy);
  608. }
  609. /*
  610. ** If this new current index is valid, then fill in the reference boolean
  611. ** value with the appropriate data from the bit array.
  612. */
  613. if (index != -1) {
  614. ((unsigned char&)Copy) = Get_Bit(&BitArray[0], index);
  615. }
  616. ((int &)LastIndex) = index;
  617. }
  618. }