VECTOR.CPP 50 KB

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