VECTOR.CPP 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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.cpv 2.17 16 Oct 1995 16:49:26 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 : July 18, 1995 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * BooleanVectorClass::BooleanVectorClass -- Copy constructor fo 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. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  45. #include "function.h"
  46. #include "vector.h"
  47. #include <mem.h>
  48. #include <stdio.h>
  49. /*
  50. ** The following template function can be located here ONLY if all the instatiations are
  51. ** declared in a header file this module includes. By placing the template functions here,
  52. ** it speeds up compiler operation and reduces object module size.
  53. */
  54. /***********************************************************************************************
  55. * VectorClass<T>::VectorClass -- Constructor for vector class. *
  56. * *
  57. * This constructor for the vector class is passed the initial size of the vector and an *
  58. * optional pointer to a preallocated block of memory that the vector will be placed in. *
  59. * If this optional pointer is NULL (or not provided), then the vector is allocated out *
  60. * of free store (with the "new" operator). *
  61. * *
  62. * INPUT: size -- The number of elements to initialize this vector to. *
  63. * *
  64. * array -- Optional pointer to a previously allocated memory block to hold the *
  65. * vector. *
  66. * *
  67. * OUTPUT: none *
  68. * *
  69. * WARNINGS: none *
  70. * *
  71. * HISTORY: *
  72. * 03/10/1995 JLB : Created. *
  73. *=============================================================================================*/
  74. template<class T>
  75. VectorClass<T>::VectorClass(unsigned size, T const * array)
  76. {
  77. Vector = 0;
  78. VectorMax = size;
  79. IsAllocated = false;
  80. /*
  81. ** Allocate the vector. The default constructor will be called for every
  82. ** object in this vector.
  83. */
  84. if (size) {
  85. if (array) {
  86. Vector = new((void*)array) T[size];
  87. } else {
  88. Vector = new T[size];
  89. IsAllocated = true;
  90. }
  91. }
  92. }
  93. /***********************************************************************************************
  94. * VectorClass<T>::~VectorClass -- Default destructor for vector class. *
  95. * *
  96. * This is the default destructor for the vector class. It will deallocate any memory *
  97. * that it may have allocated. *
  98. * *
  99. * INPUT: none *
  100. * *
  101. * OUTPUT: none *
  102. * *
  103. * WARNINGS: none *
  104. * *
  105. * HISTORY: *
  106. * 03/10/1995 JLB : Created. *
  107. *=============================================================================================*/
  108. template<class T>
  109. VectorClass<T>::~VectorClass(void)
  110. {
  111. VectorClass<T>::Clear();
  112. }
  113. /***********************************************************************************************
  114. * VectorClass<T>::VectorClass -- Copy constructor for vector object. *
  115. * *
  116. * This is the copy constructor for the vector class. It will duplicate the provided *
  117. * vector into the new vector being created. *
  118. * *
  119. * INPUT: vector -- Reference to the vector to use as a copy. *
  120. * *
  121. * OUTPUT: none *
  122. * *
  123. * WARNINGS: none *
  124. * *
  125. * HISTORY: *
  126. * 03/10/1995 JLB : Created. *
  127. *=============================================================================================*/
  128. template<class T>
  129. VectorClass<T>::VectorClass(VectorClass<T> const & vector)
  130. {
  131. VectorMax = 0;
  132. IsAllocated = false;
  133. Vector = 0;
  134. *this = vector;
  135. }
  136. /***********************************************************************************************
  137. * VectorClass<T>::operator = -- The assignment operator. *
  138. * *
  139. * This the the assignment operator for vector objects. It will alter the existing lvalue *
  140. * vector to duplicate the rvalue one. *
  141. * *
  142. * INPUT: vector -- The rvalue vector to copy into the lvalue one. *
  143. * *
  144. * OUTPUT: Returns with reference to the newly copied vector. *
  145. * *
  146. * WARNINGS: none *
  147. * *
  148. * HISTORY: *
  149. * 03/10/1995 JLB : Created. *
  150. *=============================================================================================*/
  151. template<class T>
  152. VectorClass<T> & VectorClass<T>::operator =(VectorClass<T> const & vector)
  153. {
  154. Clear();
  155. VectorMax = vector.Length();
  156. if (VectorMax) {
  157. Vector = new T[VectorMax];
  158. if (Vector) {
  159. IsAllocated = true;
  160. for (int index = 0; index < VectorMax; index++) {
  161. Vector[index] = vector[index];
  162. }
  163. }
  164. } else {
  165. Vector = 0;
  166. IsAllocated = false;
  167. }
  168. return(*this);
  169. }
  170. /***********************************************************************************************
  171. * VectorClass<T>::operator == -- Equality operator for vector objects. *
  172. * *
  173. * This operator compares two vectors for equality. It does this by performing an object *
  174. * by object comparison between the two vectors. *
  175. * *
  176. * INPUT: vector -- The right vector expression. *
  177. * *
  178. * OUTPUT: bool; Are the two vectors essentially equal? (do they contain comparable elements *
  179. * in the same order?) *
  180. * *
  181. * WARNINGS: The equality operator must exist for the objects that this vector contains. *
  182. * *
  183. * HISTORY: *
  184. * 03/10/1995 JLB : Created. *
  185. *=============================================================================================*/
  186. template<class T>
  187. int VectorClass<T>::operator == (VectorClass<T> const & vector) const
  188. {
  189. if (VectorMax == vector.Length()) {
  190. for (int index = 0; index < VectorMax; index++) {
  191. if (Vector[index] != vector[index]) {
  192. return(false);
  193. }
  194. }
  195. return(true);
  196. }
  197. return(false);
  198. }
  199. /***********************************************************************************************
  200. * VectorClass<T>::ID -- Pointer based conversion to index number. *
  201. * *
  202. * Use this routine to convert a pointer to an element in the vector back into the index *
  203. * number of that object. This routine ONLY works with actual pointers to object within *
  204. * the vector. For "equivalent" object index number (such as with similar integral values) *
  205. * then use the "by value" index number ID function. *
  206. * *
  207. * INPUT: pointer -- Pointer to an actual object in the vector. *
  208. * *
  209. * OUTPUT: Returns with the index number for the object pointed to by the parameter. *
  210. * *
  211. * WARNINGS: This routine is only valid for actual pointers to object that exist within *
  212. * the vector. All other object pointers will yield undefined results. *
  213. * *
  214. * HISTORY: *
  215. * 03/13/1995 JLB : Created. *
  216. *=============================================================================================*/
  217. template<class T>
  218. inline int VectorClass<T>::ID(T const * ptr)
  219. {
  220. return(((unsigned long)ptr - (unsigned long)&(*this)[0]) / sizeof(T));
  221. }
  222. /***********************************************************************************************
  223. * VectorClass<T>::ID -- Finds object ID based on value. *
  224. * *
  225. * Use this routine to find the index value of an object with equivalent value in the *
  226. * vector. Typical use of this would be for integral types. *
  227. * *
  228. * INPUT: object -- Reference to the object that is to be looked up in the vector. *
  229. * *
  230. * OUTPUT: Returns with the index number of the object that is equivalent to the one *
  231. * specified. If no matching value could be found then -1 is returned. *
  232. * *
  233. * WARNINGS: none *
  234. * *
  235. * HISTORY: *
  236. * 03/13/1995 JLB : Created. *
  237. *=============================================================================================*/
  238. template<class T>
  239. int VectorClass<T>::ID(T const & object)
  240. {
  241. for (int index = 0; index < VectorMax; index++) {
  242. if ((*this)[index] == object) {
  243. return(index);
  244. }
  245. }
  246. return(-1);
  247. }
  248. /***********************************************************************************************
  249. * VectorClass<T>::Clear -- Frees and clears the vector. *
  250. * *
  251. * Use this routine to reset the vector to an empty (non-allocated) state. A vector will *
  252. * free all allocated memory when this routine is called. In order for the vector to be *
  253. * useful after this point, the Resize function must be called to give it element space. *
  254. * *
  255. * INPUT: none *
  256. * *
  257. * OUTPUT: none *
  258. * *
  259. * WARNINGS: none *
  260. * *
  261. * HISTORY: *
  262. * 03/10/1995 JLB : Created. *
  263. *=============================================================================================*/
  264. template<class T>
  265. void VectorClass<T>::Clear(void)
  266. {
  267. if (Vector && IsAllocated) {
  268. delete[] Vector;
  269. Vector = 0;
  270. }
  271. IsAllocated = false;
  272. VectorMax = 0;
  273. }
  274. /***********************************************************************************************
  275. * VectorClass<T>::Resize -- Changes the size of the vector. *
  276. * *
  277. * This routine is used to change the size (usually to increase) the size of a vector. This *
  278. * is the only way to increase the vector's working room (number of elements). *
  279. * *
  280. * INPUT: newsize -- The desired size of the vector. *
  281. * *
  282. * array -- Optional pointer to a previously allocated memory block that the *
  283. * array will be located in. If this parameter is not supplied, then *
  284. * the array will be allocated from free store. *
  285. * *
  286. * OUTPUT: bool; Was the array resized successfully? *
  287. * *
  288. * WARNINGS: Failure to succeed could be the result of running out of memory. *
  289. * *
  290. * HISTORY: *
  291. * 03/10/1995 JLB : Created. *
  292. *=============================================================================================*/
  293. template<class T>
  294. int VectorClass<T>::Resize(unsigned newsize, T const * array)
  295. {
  296. if (newsize) {
  297. /*
  298. ** Allocate a new vector of the size specified. The default constructor
  299. ** will be called for every object in this vector.
  300. */
  301. T * newptr;
  302. if (!array) {
  303. newptr = new T[newsize];
  304. } else {
  305. newptr = new((void*)array) T[newsize];
  306. }
  307. if (!newptr) {
  308. return(false);
  309. }
  310. /*
  311. ** If there is an old vector, then it must be copied (as much as is feasable)
  312. ** to the new vector.
  313. */
  314. if (Vector) {
  315. /*
  316. ** Copy as much of the old vector into the new vector as possible. This
  317. ** presumes that there is a functional assignment operator for each
  318. ** of the objects in the vector.
  319. */
  320. int copycount = (newsize < VectorMax) ? newsize : VectorMax;
  321. for (int index = 0; index < copycount; index++) {
  322. newptr[index] = Vector[index];
  323. }
  324. /*
  325. ** Delete the old vector. This might cause the destructors to be called
  326. ** for all of the old elements. This makes the implementation of suitable
  327. ** assignment operator very important. The default assigment operator will
  328. ** only work for the simplist of objects.
  329. */
  330. if (IsAllocated) {
  331. delete[] Vector;
  332. Vector = 0;
  333. }
  334. }
  335. /*
  336. ** Assign the new vector data to this class.
  337. */
  338. Vector = newptr;
  339. VectorMax = newsize;
  340. IsAllocated = (Vector && !array);
  341. } else {
  342. /*
  343. ** Resizing to zero is the same as clearing the vector.
  344. */
  345. Clear();
  346. }
  347. return(true);
  348. }
  349. /***********************************************************************************************
  350. * DynamicVectorClass<T>::DynamicVectorClass -- Constructor for dynamic vector. *
  351. * *
  352. * This is the normal constructor for the dynamic vector class. It is similar to the normal *
  353. * vector class constructor. The vector is initialized to contain the number of elements *
  354. * specified in the "size" parameter. The memory is allocated from free store unless the *
  355. * optional array parameter is provided. In this case it will place the vector at the *
  356. * memory location specified. *
  357. * *
  358. * INPUT: size -- The maximum number of objects allowed in this vector. *
  359. * *
  360. * array -- Optional pointer to the memory area to place the vector at. *
  361. * *
  362. * OUTPUT: none *
  363. * *
  364. * WARNINGS: none *
  365. * *
  366. * HISTORY: *
  367. * 03/10/1995 JLB : Created. *
  368. *=============================================================================================*/
  369. template<class T>
  370. DynamicVectorClass<T>::DynamicVectorClass(unsigned size, T const * array)
  371. : VectorClass<T>(size, array)
  372. {
  373. GrowthStep = 10;
  374. ActiveCount = 0;
  375. }
  376. /***********************************************************************************************
  377. * DynamicVectorClass<T>::Resize -- Changes the size of a dynamic vector. *
  378. * *
  379. * Use this routine to change the size of the vector. The size changed is the maximum *
  380. * number of allocated objects within this vector. If a memory buffer is provided, then *
  381. * the vector will be located there. Otherwise, the memory will be allocated out of free *
  382. * store. *
  383. * *
  384. * INPUT: newsize -- The desired maximum size of this vector. *
  385. * *
  386. * array -- Optional pointer to a previosly allocated memory array. *
  387. * *
  388. * OUTPUT: bool; Was vector successfully resized according to specifications? *
  389. * *
  390. * WARNINGS: Failure to resize the vector could be the result of lack of free store. *
  391. * *
  392. * HISTORY: *
  393. * 03/10/1995 JLB : Created. *
  394. *=============================================================================================*/
  395. template<class T>
  396. int DynamicVectorClass<T>::Resize(unsigned newsize, T const * array)
  397. {
  398. if (VectorClass<T>::Resize(newsize, array)) {
  399. if (Length() < ActiveCount) ActiveCount = Length();
  400. return(true);
  401. }
  402. return(false);
  403. }
  404. /***********************************************************************************************
  405. * DynamicVectorClass<T>::ID -- Find matching value in the dynamic vector. *
  406. * *
  407. * Use this routine to find a matching object (by value) in the vector. Unlike the base *
  408. * class ID function of similar name, this one restricts the scan to the current number *
  409. * of valid objects. *
  410. * *
  411. * INPUT: object -- A reference to the object that a match is to be found in the *
  412. * vector. *
  413. * *
  414. * OUTPUT: Returns with the index number of the object that is equivalent to the one *
  415. * specified. If no equivalent object could be found then -1 is returned. *
  416. * *
  417. * WARNINGS: none *
  418. * *
  419. * HISTORY: *
  420. * 03/13/1995 JLB : Created. *
  421. *=============================================================================================*/
  422. template<class T>
  423. int DynamicVectorClass<T>::ID(T const & object)
  424. {
  425. for (int index = 0; index < Count(); index++) {
  426. if ((*this)[index] == object) return(index);
  427. }
  428. return(-1);
  429. }
  430. /***********************************************************************************************
  431. * DynamicVectorClass<T>::Add -- Add an element to the vector. *
  432. * *
  433. * Use this routine to add an element to the vector. The vector will automatically be *
  434. * resized to accomodate the new element IF the vector was allocated previosly and the *
  435. * growth rate is not zero. *
  436. * *
  437. * INPUT: object -- Reference to the object that will be added to the vector. *
  438. * *
  439. * OUTPUT: bool; Was the object added successfully? If so, the object is added to the end *
  440. * of the vector. *
  441. * *
  442. * WARNINGS: none *
  443. * *
  444. * HISTORY: *
  445. * 03/10/1995 JLB : Created. *
  446. *=============================================================================================*/
  447. template<class T>
  448. int DynamicVectorClass<T>::Add(T const & object)
  449. {
  450. if (ActiveCount >= Length()) {
  451. if ((IsAllocated || !VectorMax) && GrowthStep > 0) {
  452. if (!Resize(Length() + GrowthStep)) {
  453. /*
  454. ** Failure to increase the size of the vector is an error condition.
  455. ** Return with the error flag.
  456. */
  457. return(false);
  458. }
  459. } else {
  460. /*
  461. ** Increasing the size of this vector is not allowed! Bail this
  462. ** routine with the error code.
  463. */
  464. return(false);
  465. }
  466. }
  467. /*
  468. ** There is room for the new object now. Add it to the end of the object vector.
  469. */
  470. (*this)[ActiveCount++] = object;
  471. return(true);
  472. }
  473. template<class T>
  474. int DynamicVectorClass<T>::Add_Head(T const & object)
  475. {
  476. if (ActiveCount >= Length()) {
  477. if ((IsAllocated || !VectorMax) && GrowthStep > 0) {
  478. if (!Resize(Length() + GrowthStep)) {
  479. /*
  480. ** Failure to increase the size of the vector is an error condition.
  481. ** Return with the error flag.
  482. */
  483. return(false);
  484. }
  485. } else {
  486. /*
  487. ** Increasing the size of this vector is not allowed! Bail this
  488. ** routine with the error code.
  489. */
  490. return(false);
  491. }
  492. }
  493. /*
  494. ** There is room for the new object now. Add it to the end of the object vector.
  495. */
  496. if (ActiveCount) {
  497. memmove(&(*this)[1], &(*this)[0], ActiveCount * sizeof(T));
  498. }
  499. (*this)[0] = object;
  500. ActiveCount++;
  501. // (*this)[ActiveCount++] = object;
  502. return(true);
  503. }
  504. /***********************************************************************************************
  505. * DynamicVectorClass<T>::Delete -- Remove the specified object from the vector. *
  506. * *
  507. * This routine will delete the object referenced from the vector. All objects in the *
  508. * vector that follow the one deleted will be moved "down" to fill the hole. *
  509. * *
  510. * INPUT: object -- Reference to the object in this vector that is to be deleted. *
  511. * *
  512. * OUTPUT: bool; Was the object deleted successfully? This should always be true. *
  513. * *
  514. * WARNINGS: Do no pass a reference to an object that is NOT part of this vector. The *
  515. * results of this are undefined and probably catastrophic. *
  516. * *
  517. * HISTORY: *
  518. * 03/10/1995 JLB : Created. *
  519. *=============================================================================================*/
  520. template<class T>
  521. int DynamicVectorClass<T>::Delete(T const & object)
  522. {
  523. int index = ID(object);
  524. if (index != -1){
  525. return(Delete(index));
  526. }else{
  527. return (false);
  528. }
  529. }
  530. /***********************************************************************************************
  531. * DynamicVectorClass<T>::Delete -- Deletes the specified index from the vector. *
  532. * *
  533. * Use this routine to delete the object at the specified index from the objects in the *
  534. * vector. This routine will move all the remaining objects "down" in order to fill the *
  535. * hole. *
  536. * *
  537. * INPUT: index -- The index number of the object in the vector that is to be deleted. *
  538. * *
  539. * OUTPUT: bool; Was the object index deleted successfully? Failure might mean that the index *
  540. * specified was out of bounds. *
  541. * *
  542. * WARNINGS: none *
  543. * *
  544. * HISTORY: *
  545. * 03/10/1995 JLB : Created. *
  546. *=============================================================================================*/
  547. template<class T>
  548. int DynamicVectorClass<T>::Delete(int index)
  549. {
  550. if ((unsigned)index < ActiveCount) {
  551. ActiveCount--;
  552. /*
  553. ** If there are any objects past the index that was deleted, copy those
  554. ** objects down in order to fill the hole. A simple memory copy is
  555. ** not sufficient since the vector could contain class objects that
  556. ** need to use the assignment operator for movement.
  557. */
  558. for (int i = index; i < ActiveCount; i++) {
  559. (*this)[i] = (*this)[i+1];
  560. }
  561. return(true);
  562. }
  563. return(false);
  564. }
  565. //----------------------------------------------------------------------------------------------
  566. /***********************************************************************************************
  567. * BooleanVectorClass::BooleanVectorClass -- Explicit data buffer constructor. *
  568. * *
  569. * This is the constructor for a boolean array. This constructor takes the memory pointer *
  570. * provided as assigns that as the array data pointer. *
  571. * *
  572. * INPUT: size -- The size of the array (in bits). *
  573. * *
  574. * array -- Pointer to the memory that the array is to use. *
  575. * *
  576. * OUTPUT: none *
  577. * *
  578. * WARNINGS: You must make sure that the memory specified is large enough to contain the *
  579. * bits specified. *
  580. * *
  581. * HISTORY: *
  582. * 07/18/1995 JLB : Created. *
  583. *=============================================================================================*/
  584. BooleanVectorClass::BooleanVectorClass(unsigned size, unsigned char * array)
  585. {
  586. BitArray.Resize(((size + (8-1)) / 8), array);
  587. LastIndex = -1;
  588. BitCount = size;
  589. }
  590. /***********************************************************************************************
  591. * BooleanVectorClass::BooleanVectorClass -- Copy constructor fo boolean array. *
  592. * *
  593. * This is the copy constructor for a boolean array. It is used to make a duplicate of the *
  594. * boolean array. *
  595. * *
  596. * INPUT: vector -- Reference to the vector to be duplicated. *
  597. * *
  598. * OUTPUT: none *
  599. * *
  600. * WARNINGS: none *
  601. * *
  602. * HISTORY: *
  603. * 07/18/1995 JLB : Created. *
  604. *=============================================================================================*/
  605. BooleanVectorClass::BooleanVectorClass(BooleanVectorClass const & vector)
  606. {
  607. LastIndex = -1;
  608. *this = vector;
  609. }
  610. /***********************************************************************************************
  611. * BooleanVectorClass::operator = -- Assignment operator. *
  612. * *
  613. * This routine will make a copy of the specifed boolean vector array. The vector is *
  614. * copied into an already constructed existing vector. The values from the existing vector *
  615. * are destroyed by this copy. *
  616. * *
  617. * INPUT: vector -- Reference to the vector to make a copy of. *
  618. * *
  619. * OUTPUT: none *
  620. * *
  621. * WARNINGS: none *
  622. * *
  623. * HISTORY: *
  624. * 07/18/1995 JLB : Created. *
  625. *=============================================================================================*/
  626. BooleanVectorClass & BooleanVectorClass::operator =(BooleanVectorClass const & vector)
  627. {
  628. Fixup();
  629. Copy = vector.Copy;
  630. LastIndex = vector.LastIndex;
  631. BitArray = vector.BitArray;
  632. BitCount = vector.BitCount;
  633. return(*this);
  634. }
  635. /***********************************************************************************************
  636. * BooleanVectorClass::operator == -- Comparison operator for boolean vector. *
  637. * *
  638. * This is the comparison operator for a boolean vector class. Boolean vectors are equal *
  639. * if the bit count and bit values are identical. *
  640. * *
  641. * INPUT: vector -- Reference to the vector to compare to. *
  642. * *
  643. * OUTPUT: Are the boolean vectors identical? *
  644. * *
  645. * WARNINGS: none *
  646. * *
  647. * HISTORY: *
  648. * 07/18/1995 JLB : Created. *
  649. *=============================================================================================*/
  650. int BooleanVectorClass::operator == (const BooleanVectorClass & vector)
  651. {
  652. Fixup(LastIndex);
  653. return(BitCount == vector.BitCount && BitArray == vector.BitArray);
  654. }
  655. /***********************************************************************************************
  656. * BooleanVectorClass::Resize -- Resizes a boolean vector object. *
  657. * *
  658. * This routine will resize the boolean vector object. An index value used with a boolean *
  659. * vector must be less than the value specified in as the new size. *
  660. * *
  661. * INPUT: size -- The new maximum size of this boolean vector. *
  662. * *
  663. * OUTPUT: Was the boolean vector sized successfully? *
  664. * *
  665. * WARNINGS: The boolean array might be reallocated or even deleted by this routine. *
  666. * *
  667. * HISTORY: *
  668. * 07/18/1995 JLB : Created. *
  669. *=============================================================================================*/
  670. int BooleanVectorClass::Resize(unsigned size)
  671. {
  672. Fixup();
  673. if (size) {
  674. /*
  675. ** Record the previous bit count of the boolean vector. This is used
  676. ** to determine if the array has grown in size and thus clearing is
  677. ** necessary.
  678. */
  679. int oldsize = BitCount;
  680. /*
  681. ** Actually resize the bit array. Since this is a bit packed array,
  682. ** there are 8 elements per byte (rounded up).
  683. */
  684. int success = BitArray.Resize(((size + (8-1)) / 8));
  685. /*
  686. ** Since there is no default constructor for bit packed integers, a manual
  687. ** clearing of the bits is required.
  688. */
  689. BitCount = size;
  690. if (success && oldsize < size) {
  691. for (int index = oldsize; index < size; index++) {
  692. (*this)[index] = 0;
  693. }
  694. }
  695. return(success);
  696. }
  697. /*
  698. ** Resizing to zero is the same as clearing and deallocating the array.
  699. ** This is always successful.
  700. */
  701. Clear();
  702. return(true);
  703. }
  704. /***********************************************************************************************
  705. * BooleanVectorClass::Clear -- Resets boolean vector to empty state. *
  706. * *
  707. * This routine will clear out the boolean array. This will free any allocated memory and *
  708. * result in the boolean vector being unusable until the Resize function is subsiquently *
  709. * called. *
  710. * *
  711. * INPUT: none *
  712. * *
  713. * OUTPUT: none *
  714. * *
  715. * WARNINGS: The boolean vector cannot be used until it is resized to a non null condition. *
  716. * *
  717. * HISTORY: *
  718. * 07/18/1995 JLB : Created. *
  719. *=============================================================================================*/
  720. void BooleanVectorClass::Clear(void)
  721. {
  722. Fixup();
  723. BitCount = 0;
  724. BitArray.Clear();
  725. }
  726. /***********************************************************************************************
  727. * BooleanVectorClass::Reset -- Clear all boolean values in array. *
  728. * *
  729. * This is the preferred (and quick) method to clear the boolean array to a false condition.*
  730. * *
  731. * INPUT: none *
  732. * *
  733. * OUTPUT: none *
  734. * *
  735. * WARNINGS: none *
  736. * *
  737. * HISTORY: *
  738. * 07/18/1995 JLB : Created. *
  739. *=============================================================================================*/
  740. void BooleanVectorClass::Reset(void)
  741. {
  742. LastIndex = -1;
  743. if (BitArray.Length()) {
  744. memset(&BitArray[0], '\0', BitArray.Length());
  745. }
  746. }
  747. /***********************************************************************************************
  748. * BooleanVectorClass::Set -- Forces all boolean elements to true. *
  749. * *
  750. * This is the preferred (and fast) way to set all boolean elements to true. *
  751. * *
  752. * INPUT: none *
  753. * *
  754. * OUTPUT: none *
  755. * *
  756. * WARNINGS: none *
  757. * *
  758. * HISTORY: *
  759. * 07/18/1995 JLB : Created. *
  760. *=============================================================================================*/
  761. void BooleanVectorClass::Set(void)
  762. {
  763. LastIndex = -1;
  764. if (BitArray.Length()) {
  765. memset(&BitArray[0], '\xFF', BitArray.Length());
  766. }
  767. }
  768. /***********************************************************************************************
  769. * BooleanVectorClass::Fixup -- Updates the boolean vector to a known state. *
  770. * *
  771. * Use this routine to set the boolean value copy to match the appropriate bit in the *
  772. * boolean array. The boolean array will be updated with any changes from the last time *
  773. * a value was fetched from the boolean vector. By using this update method, the boolean *
  774. * array can be treated as a normal array even though the elements are composed of *
  775. * otherwise inaccessable bits. *
  776. * *
  777. * INPUT: index -- The index to set the new copy value to. If the index is -1, then the *
  778. * previous value will be updated into the vector array, but no new value *
  779. * will be fetched from it. *
  780. * *
  781. * OUTPUT: none *
  782. * *
  783. * WARNINGS: Always call this routine with "-1" if any direct manipulation of the bit *
  784. * array is to occur. This ensures that the bit array is accurate. *
  785. * *
  786. * HISTORY: *
  787. * 07/18/1995 JLB : Created. *
  788. *=============================================================================================*/
  789. void BooleanVectorClass::Fixup(int index) const
  790. {
  791. /*
  792. ** If the requested index value is illegal, then force the index
  793. ** to be -1. This is the default non-index value.
  794. */
  795. if ((unsigned)index >= BitCount) {
  796. index = -1;
  797. }
  798. /*
  799. ** If the new index is different than the previous index, there might
  800. ** be some fixing up required.
  801. */
  802. if (index != LastIndex) {
  803. /*
  804. ** If the previously fetched boolean value was changed, then update
  805. ** the boolean array accordingly.
  806. */
  807. if (LastIndex != -1) {
  808. Set_Bit((void*)&BitArray[0], LastIndex, Copy);
  809. }
  810. /*
  811. ** If this new current index is valid, then fill in the reference boolean
  812. ** value with the approriate data from the bit array.
  813. */
  814. if (index != -1) {
  815. ((unsigned char&)Copy) = Get_Bit(&BitArray[0], index);
  816. }
  817. ((int &)LastIndex) = index;
  818. }
  819. }