VECTOR.CPP 39 KB

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