Vector.H 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/vector.h $*
  25. * *
  26. * $Author:: Jani_p $*
  27. * *
  28. * $Modtime:: 1/16/02 11:40a $*
  29. * *
  30. * $Revision:: 49 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * VectorClass<T>::VectorClass -- Constructor for vector class. *
  35. * VectorClass<T>::~VectorClass -- Default destructor for vector class. *
  36. * VectorClass<T>::VectorClass -- Copy constructor for vector object. *
  37. * VectorClass<T>::operator = -- The assignment operator. *
  38. * VectorClass<T>::operator == -- Equality operator for vector objects. *
  39. * VectorClass<T>::Clear -- Frees and clears the vector. *
  40. * VectorClass<T>::Resize -- Changes the size of the vector. *
  41. * DynamicVectorClass<T>::DynamicVectorClass -- Constructor for dynamic vector. *
  42. * DynamicVectorClass<T>::Resize -- Changes the size of a dynamic vector. *
  43. * DynamicVectorClass<T>::Add -- Add an element to the vector. *
  44. * DynamicVectorClass<T>::Delete -- Remove the specified object from the vector. *
  45. * DynamicVectorClass<T>::Delete -- Deletes the specified index from the vector. *
  46. * VectorClass<T>::ID -- Pointer based conversion to index number. *
  47. * VectorClass<T>::ID -- Finds object ID based on value. *
  48. * DynamicVectorClass<T>::ID -- Find matching value in the dynamic vector. *
  49. * DynamicVectorClass<T>::Uninitialized_Add -- Add an empty place to the vector. *
  50. * DynamicVectorClass<T>::Insert -- insert an object at the desired index *
  51. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  52. #if _MSC_VER >= 1000
  53. #pragma once
  54. #endif // _MSC_VER >= 1000
  55. #ifndef VECTOR_H
  56. #define VECTOR_H
  57. #include "always.h"
  58. #include <assert.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include <new.h>
  62. #ifdef _MSC_VER
  63. #pragma warning (disable : 4702) // unreachable code, happens with some uses of these templates
  64. #endif
  65. class NoInitClass;
  66. /**************************************************************************
  67. ** This is a general purpose vector class. A vector is defined by this
  68. ** class, as an array of arbitrary objects where the array can be dynamically
  69. ** sized. Because is deals with arbitrary object types, it can handle everything.
  70. ** As a result of this, it is not terribly efficient for integral objects (such
  71. ** as char or int). It will function correctly, but the copy constructor and
  72. ** equality operator could be highly optimized if the integral type were known.
  73. ** This efficiency can be implemented by deriving an integral vector template
  74. ** from this one in order to supply more efficient routines.
  75. */
  76. // Why, oh why does Visual C need this!!! It's bugged. <sigh>
  77. #pragma warning(disable : 4505)
  78. template<class T>
  79. class VectorClass
  80. {
  81. public:
  82. WWINLINE VectorClass(NoInitClass const &) {};
  83. VectorClass(int size=0, T const * array=0);
  84. VectorClass(VectorClass<T> const &); // Copy constructor.
  85. virtual ~VectorClass(void);
  86. WWINLINE T & operator[](int index) { assert(unsigned(index) < unsigned(VectorMax));return(Vector[index]); }
  87. WWINLINE T const & operator[](int index) const { assert(unsigned(index) < unsigned(VectorMax));return(Vector[index]); }
  88. VectorClass<T> & operator = (VectorClass<T> const &); // Assignment operator.
  89. virtual bool operator == (VectorClass<T> const &) const; // Equality operator.
  90. virtual bool Resize(int newsize, T const * array=0);
  91. virtual void Clear(void);
  92. WWINLINE int Length(void) const {return VectorMax;};
  93. virtual int ID(T const * ptr); // Pointer based identification.
  94. virtual int ID(T const & ptr); // Value based identification.
  95. protected:
  96. /*
  97. ** This is a pointer to the allocated vector array of elements.
  98. */
  99. T * Vector;
  100. /*
  101. ** This is the maximum number of elements allowed in this vector.
  102. */
  103. int VectorMax;
  104. /*
  105. ** This indicates if the vector is in a valid (even if empty) state.
  106. */
  107. bool IsValid;
  108. /*
  109. ** Does the vector data pointer refer to memory that this class has manually
  110. ** allocated? If so, then this class is responsible for deleting it.
  111. */
  112. bool IsAllocated;
  113. /*
  114. ** Padding to align the class.
  115. */
  116. bool VectorClassPad[2];
  117. };
  118. /***********************************************************************************************
  119. * VectorClass<T>::VectorClass -- Constructor for vector class. *
  120. * *
  121. * This constructor for the vector class is passed the initial size of the vector and an *
  122. * optional pointer to a preallocated block of memory that the vector will be placed in. *
  123. * If this optional pointer is NULL (or not provided), then the vector is allocated out *
  124. * of free store (with the "new" operator). *
  125. * *
  126. * INPUT: size -- The number of elements to initialize this vector to. *
  127. * *
  128. * array -- Optional pointer to a previously allocated memory block to hold the *
  129. * vector. *
  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(int size, T const * array) :
  140. Vector(0),
  141. VectorMax(size),
  142. IsValid(true),
  143. IsAllocated(false)
  144. {
  145. /*
  146. ** Allocate the vector. The default constructor will be called for every
  147. ** object in this vector.
  148. */
  149. if (size) {
  150. if (array) {
  151. Vector = new((void*)array) T[size];
  152. } else {
  153. Vector = W3DNEWARRAY T[size];
  154. IsAllocated = true;
  155. }
  156. }
  157. }
  158. /***********************************************************************************************
  159. * VectorClass<T>::~VectorClass -- Default destructor for vector class. *
  160. * *
  161. * This is the default destructor for the vector class. It will deallocate any memory *
  162. * that it may have allocated. *
  163. * *
  164. * INPUT: none *
  165. * *
  166. * OUTPUT: none *
  167. * *
  168. * WARNINGS: none *
  169. * *
  170. * HISTORY: *
  171. * 03/10/1995 JLB : Created. *
  172. *=============================================================================================*/
  173. template<class T>
  174. VectorClass<T>::~VectorClass(void)
  175. {
  176. VectorClass<T>::Clear();
  177. }
  178. /***********************************************************************************************
  179. * VectorClass<T>::VectorClass -- Copy constructor for vector object. *
  180. * *
  181. * This is the copy constructor for the vector class. It will duplicate the provided *
  182. * vector into the new vector being created. *
  183. * *
  184. * INPUT: vector -- Reference to the vector to use as a copy. *
  185. * *
  186. * OUTPUT: none *
  187. * *
  188. * WARNINGS: none *
  189. * *
  190. * HISTORY: *
  191. * 03/10/1995 JLB : Created. *
  192. *=============================================================================================*/
  193. template<class T>
  194. VectorClass<T>::VectorClass(VectorClass<T> const & vec) :
  195. Vector(0),
  196. VectorMax(0),
  197. IsValid(true),
  198. IsAllocated(false)
  199. {
  200. *this = vec;
  201. }
  202. /***********************************************************************************************
  203. * VectorClass<T>::operator = -- The assignment operator. *
  204. * *
  205. * This the the assignment operator for vector objects. It will alter the existing lvalue *
  206. * vector to duplicate the rvalue one. *
  207. * *
  208. * INPUT: vec -- The rvalue vector to copy into the lvalue one. *
  209. * *
  210. * OUTPUT: Returns with reference to the newly copied vector. *
  211. * *
  212. * WARNINGS: none *
  213. * *
  214. * HISTORY: *
  215. * 03/10/1995 JLB : Created. *
  216. *=============================================================================================*/
  217. template<class T>
  218. VectorClass<T> & VectorClass<T>::operator =(VectorClass<T> const & vec)
  219. {
  220. if (this != &vec) {
  221. Clear();
  222. VectorMax = vec.Length();
  223. if (VectorMax) {
  224. Vector = W3DNEWARRAY T[VectorMax];
  225. if (Vector) {
  226. IsAllocated = true;
  227. for (int index = 0; index < VectorMax; index++) {
  228. Vector[index] = vec[index];
  229. }
  230. }
  231. } else {
  232. Vector = 0;
  233. IsAllocated = false;
  234. }
  235. }
  236. return(*this);
  237. }
  238. /***********************************************************************************************
  239. * VectorClass<T>::operator == -- Equality operator for vector objects. *
  240. * *
  241. * This operator compares two vectors for equality. It does this by performing an object *
  242. * by object comparison between the two vectors. *
  243. * *
  244. * INPUT: vector -- The right vector expression. *
  245. * *
  246. * OUTPUT: bool; Are the two vectors essentially equal? (do they contain comparable elements *
  247. * in the same order?) *
  248. * *
  249. * WARNINGS: The equality operator must exist for the objects that this vector contains. *
  250. * *
  251. * HISTORY: *
  252. * 03/10/1995 JLB : Created. *
  253. *=============================================================================================*/
  254. template<class T>
  255. bool VectorClass<T>::operator == (VectorClass<T> const & vec) const
  256. {
  257. if (VectorMax == vec.Length()) {
  258. for (int index = 0; index < VectorMax; index++) {
  259. if (Vector[index] != vec[index]) {
  260. return(false);
  261. }
  262. }
  263. return(true);
  264. }
  265. return(false);
  266. }
  267. /***********************************************************************************************
  268. * VectorClass<T>::ID -- Pointer based conversion to index number. *
  269. * *
  270. * Use this routine to convert a pointer to an element in the vector back into the index *
  271. * number of that object. This routine ONLY works with actual pointers to object within *
  272. * the vector. For "equivalent" object index number (such as with similar integral values) *
  273. * then use the "by value" index number ID function. *
  274. * *
  275. * INPUT: pointer -- Pointer to an actual object in the vector. *
  276. * *
  277. * OUTPUT: Returns with the index number for the object pointed to by the parameter. *
  278. * *
  279. * WARNINGS: This routine is only valid for actual pointers to object that exist within *
  280. * the vector. All other object pointers will yield undefined results. *
  281. * *
  282. * HISTORY: *
  283. * 03/13/1995 JLB : Created. *
  284. *=============================================================================================*/
  285. template<class T>
  286. inline int VectorClass<T>::ID(T const * ptr)
  287. {
  288. if (!IsValid) return(0);
  289. return(((unsigned long)ptr - (unsigned long)&(*this)[0]) / sizeof(T));
  290. }
  291. /***********************************************************************************************
  292. * VectorClass<T>::ID -- Finds object ID based on value. *
  293. * *
  294. * Use this routine to find the index value of an object with equivalent value in the *
  295. * vector. Typical use of this would be for integral types. *
  296. * *
  297. * INPUT: object -- Reference to the object that is to be looked up in the vector. *
  298. * *
  299. * OUTPUT: Returns with the index number of the object that is equivalent to the one *
  300. * specified. If no matching value could be found then -1 is returned. *
  301. * *
  302. * WARNINGS: none *
  303. * *
  304. * HISTORY: *
  305. * 03/13/1995 JLB : Created. *
  306. *=============================================================================================*/
  307. template<class T>
  308. int VectorClass<T>::ID(T const & object)
  309. {
  310. if (!IsValid) return(0);
  311. for (int index = 0; index < VectorMax; index++) {
  312. if ((*this)[index] == object) {
  313. return(index);
  314. }
  315. }
  316. return(-1);
  317. }
  318. /***********************************************************************************************
  319. * VectorClass<T>::Clear -- Frees and clears the vector. *
  320. * *
  321. * Use this routine to reset the vector to an empty (non-allocated) state. A vector will *
  322. * free all allocated memory when this routine is called. In order for the vector to be *
  323. * useful after this point, the Resize function must be called to give it element space. *
  324. * *
  325. * INPUT: none *
  326. * *
  327. * OUTPUT: none *
  328. * *
  329. * WARNINGS: none *
  330. * *
  331. * HISTORY: *
  332. * 03/10/1995 JLB : Created. *
  333. *=============================================================================================*/
  334. template<class T>
  335. void VectorClass<T>::Clear(void)
  336. {
  337. if (Vector && IsAllocated) {
  338. delete[] Vector;
  339. Vector = 0;
  340. }
  341. IsAllocated = false;
  342. VectorMax = 0;
  343. }
  344. /***********************************************************************************************
  345. * VectorClass<T>::Resize -- Changes the size of the vector. *
  346. * *
  347. * This routine is used to change the size (usually to increase) the size of a vector. This *
  348. * is the only way to increase the vector's working room (number of elements). *
  349. * *
  350. * INPUT: newsize -- The desired size of the vector. *
  351. * *
  352. * array -- Optional pointer to a previously allocated memory block that the *
  353. * array will be located in. If this parameter is not supplied, then *
  354. * the array will be allocated from free store. *
  355. * *
  356. * OUTPUT: bool; Was the array resized successfully? *
  357. * *
  358. * WARNINGS: Failure to succeed could be the result of running out of memory. *
  359. * *
  360. * HISTORY: *
  361. * 03/10/1995 JLB : Created. *
  362. *=============================================================================================*/
  363. template<class T>
  364. bool VectorClass<T>::Resize(int newsize, T const * array)
  365. {
  366. if (newsize) {
  367. /*
  368. ** Allocate a new vector of the size specified. The default constructor
  369. ** will be called for every object in this vector.
  370. */
  371. T * newptr;
  372. /*
  373. ** Either create a new memory block for the object array or initialize
  374. ** an existing block as indicated by the array parameter. When creating a new
  375. ** memory block, flag that the vector object is currently in an invalid
  376. ** state. This is necessary because the default constructor for the object
  377. ** elements may look to the vector to fetch their ID number.
  378. */
  379. IsValid = false;
  380. if (!array) {
  381. newptr = W3DNEWARRAY T[newsize];
  382. } else {
  383. newptr = new((void*)array) T[newsize];
  384. }
  385. IsValid = true;
  386. if (!newptr) {
  387. return(false);
  388. }
  389. /*
  390. ** If there is an old vector, then it must be copied (as much as is feasible)
  391. ** to the new vector.
  392. */
  393. if (Vector != NULL) {
  394. /*
  395. ** Copy as much of the old vector into the new vector as possible. This
  396. ** presumes that there is a functional assignment operator for each
  397. ** of the objects in the vector.
  398. */
  399. int copycount = (newsize < VectorMax) ? newsize : VectorMax;
  400. for (int index = 0; index < copycount; index++) {
  401. newptr[index] = Vector[index];
  402. }
  403. /*
  404. ** Delete the old vector. This might cause the destructors to be called
  405. ** for all of the old elements. This makes the implementation of suitable
  406. ** assignment operator very important. The default assignment operator will
  407. ** only work for the simplest of objects.
  408. */
  409. if (IsAllocated) {
  410. delete[] Vector;
  411. Vector = 0;
  412. }
  413. }
  414. /*
  415. ** Assign the new vector data to this class.
  416. */
  417. Vector = newptr;
  418. VectorMax = newsize;
  419. IsAllocated = (Vector && !array);
  420. } else {
  421. /*
  422. ** Resizing to zero is the same as clearing the vector.
  423. */
  424. Clear();
  425. }
  426. return(true);
  427. }
  428. /**************************************************************************
  429. ** This derivative vector class adds the concept of adding and deleting
  430. ** objects. The objects are packed to the beginning of the vector array.
  431. ** If this is instantiated for a class object, then the assignment operator
  432. ** and the equality operator must be supported. If the vector allocates its
  433. ** own memory, then the vector can grow if it runs out of room adding items.
  434. ** The growth rate is controlled by setting the growth step rate. A growth
  435. ** step rate of zero disallows growing.
  436. */
  437. template<class T>
  438. class DynamicVectorClass : public VectorClass<T>
  439. {
  440. public:
  441. DynamicVectorClass(unsigned size=0, T const * array=0);
  442. // Stubbed equality operators so you can have dynamic vectors of dynamic vectors
  443. bool operator== (const DynamicVectorClass &src) { return false; }
  444. bool operator!= (const DynamicVectorClass &src) { return true; }
  445. // Change maximum size of vector.
  446. virtual bool Resize(int newsize, T const * array=0);
  447. // Resets and frees the vector array.
  448. virtual void Clear(void) {ActiveCount = 0;VectorClass<T>::Clear();};
  449. // retains the memory but zeros the active count
  450. void Reset_Active(void) { ActiveCount = 0; }
  451. void Set_Active(int count) { ActiveCount = count; }
  452. // Fetch number of "allocated" vector objects.
  453. int Count(void) const {return(ActiveCount);};
  454. // Add object to vector (growing as necessary).
  455. bool Add(T const & object);
  456. bool Add_Head(T const & object);
  457. bool Insert(int index,T const & object);
  458. // Delete object just like this from vector.
  459. bool Delete(T const & object);
  460. // Delete object at this vector index.
  461. bool Delete(int index);
  462. bool Delete_Index(int index); // Added to explictly call delete by index
  463. // Deletes all objects in the vector.
  464. void Delete_All(void);
  465. // Set amount that vector grows by.
  466. int Set_Growth_Step(int step) {return(GrowthStep = step);};
  467. // Fetch current growth step rate.
  468. int Growth_Step(void) {return GrowthStep;};
  469. virtual int ID(T const * ptr) {return(VectorClass<T>::ID(ptr));};
  470. virtual int ID(T const & ptr);
  471. DynamicVectorClass<T> & operator =(DynamicVectorClass<T> const & rvalue) {
  472. VectorClass<T>::operator = (rvalue);
  473. ActiveCount = rvalue.ActiveCount;
  474. GrowthStep = rvalue.GrowthStep;
  475. return(*this);
  476. }
  477. // Uninitialized Add - does everything an Add does, except copying an
  478. // object into the 'new' spot in the array. It returns a pointer to
  479. // the 'new' spot. (NULL if the Add failed). NOTE - you must then fill
  480. // this memory area with a valid object (e.g. by using placement new),
  481. // or chaos will result!
  482. T * Uninitialized_Add(void);
  483. protected:
  484. /*
  485. ** This is a count of the number of active objects in this
  486. ** vector. The memory array often times is bigger than this
  487. ** value.
  488. */
  489. int ActiveCount;
  490. /*
  491. ** If there is insufficient room in the vector array for a new
  492. ** object to be added, then the vector will grow by the number
  493. ** of objects specified by this value. This is controlled by
  494. ** the Set_Growth_Step() function.
  495. */
  496. int GrowthStep;
  497. };
  498. /***********************************************************************************************
  499. * DynamicVectorClass<T>::DynamicVectorClass -- Constructor for dynamic vector. *
  500. * *
  501. * This is the normal constructor for the dynamic vector class. It is similar to the normal *
  502. * vector class constructor. The vector is initialized to contain the number of elements *
  503. * specified in the "size" parameter. The memory is allocated from free store unless the *
  504. * optional array parameter is provided. In this case it will place the vector at the *
  505. * memory location specified. *
  506. * *
  507. * INPUT: size -- The maximum number of objects allowed in this vector. *
  508. * *
  509. * array -- Optional pointer to the memory area to place the vector at. *
  510. * *
  511. * OUTPUT: none *
  512. * *
  513. * WARNINGS: none *
  514. * *
  515. * HISTORY: *
  516. * 03/10/1995 JLB : Created. *
  517. *=============================================================================================*/
  518. template<class T>
  519. DynamicVectorClass<T>::DynamicVectorClass(unsigned size, T const * array)
  520. : VectorClass<T>(size, array)
  521. {
  522. GrowthStep = 10;
  523. ActiveCount = 0;
  524. }
  525. /***********************************************************************************************
  526. * DynamicVectorClass<T>::Resize -- Changes the size of a dynamic vector. *
  527. * *
  528. * Use this routine to change the size of the vector. The size changed is the maximum *
  529. * number of allocated objects within this vector. If a memory buffer is provided, then *
  530. * the vector will be located there. Otherwise, the memory will be allocated out of free *
  531. * store. *
  532. * *
  533. * INPUT: newsize -- The desired maximum size of this vector. *
  534. * *
  535. * array -- Optional pointer to a previously allocated memory array. *
  536. * *
  537. * OUTPUT: bool; Was vector successfully resized according to specifications? *
  538. * *
  539. * WARNINGS: Failure to resize the vector could be the result of lack of free store. *
  540. * *
  541. * HISTORY: *
  542. * 03/10/1995 JLB : Created. *
  543. *=============================================================================================*/
  544. template<class T>
  545. bool DynamicVectorClass<T>::Resize(int newsize, T const * array)
  546. {
  547. if (VectorClass<T>::Resize(newsize, array)) {
  548. if (Length() < ActiveCount) ActiveCount = Length();
  549. return(true);
  550. }
  551. return(false);
  552. }
  553. /***********************************************************************************************
  554. * DynamicVectorClass<T>::ID -- Find matching value in the dynamic vector. *
  555. * *
  556. * Use this routine to find a matching object (by value) in the vector. Unlike the base *
  557. * class ID function of similar name, this one restricts the scan to the current number *
  558. * of valid objects. *
  559. * *
  560. * INPUT: object -- A reference to the object that a match is to be found in the *
  561. * vector. *
  562. * *
  563. * OUTPUT: Returns with the index number of the object that is equivalent to the one *
  564. * specified. If no equivalent object could be found then -1 is returned. *
  565. * *
  566. * WARNINGS: none *
  567. * *
  568. * HISTORY: *
  569. * 03/13/1995 JLB : Created. *
  570. *=============================================================================================*/
  571. template<class T>
  572. int DynamicVectorClass<T>::ID(T const & object)
  573. {
  574. for (int index = 0; index < Count(); index++) {
  575. if ((*this)[index] == object) return(index);
  576. }
  577. return(-1);
  578. }
  579. /***********************************************************************************************
  580. * DynamicVectorClass<T>::Add -- Add an element to the vector. *
  581. * *
  582. * Use this routine to add an element to the vector. The vector will automatically be *
  583. * resized to accomodate the new element IF the vector was allocated previously and the *
  584. * growth rate is not zero. *
  585. * *
  586. * INPUT: object -- Reference to the object that will be added to the vector. *
  587. * *
  588. * OUTPUT: bool; Was the object added successfully? If so, the object is added to the end *
  589. * of the vector. *
  590. * *
  591. * WARNINGS: none *
  592. * *
  593. * HISTORY: *
  594. * 03/10/1995 JLB : Created. *
  595. *=============================================================================================*/
  596. template<class T>
  597. bool DynamicVectorClass<T>::Add(T const & object)
  598. {
  599. if (ActiveCount >= Length()) {
  600. if ((IsAllocated || !VectorMax) && GrowthStep > 0) {
  601. if (!Resize(Length() + GrowthStep)) {
  602. /*
  603. ** Failure to increase the size of the vector is an error condition.
  604. ** Return with the error flag.
  605. */
  606. return(false);
  607. }
  608. } else {
  609. /*
  610. ** Increasing the size of this vector is not allowed! Bail this
  611. ** routine with the error code.
  612. */
  613. return(false);
  614. }
  615. }
  616. /*
  617. ** There is room for the new object now. Add it to the end of the object vector.
  618. */
  619. (*this)[ActiveCount++] = object;
  620. return(true);
  621. }
  622. /***********************************************************************************************
  623. * DynamicVectorClass<T>::Add_Head -- Adds element to head of the list. *
  624. * *
  625. * This routine will add the specified element to the head of the vector. If necessary, *
  626. * the vector will be expanded accordingly. *
  627. * *
  628. * INPUT: object -- Reference to the object to add to the head of this vector. *
  629. * *
  630. * OUTPUT: bool; Was the object added without error? *
  631. * *
  632. * WARNINGS: none *
  633. * *
  634. * HISTORY: *
  635. * 09/21/1995 JLB : Created. *
  636. *=============================================================================================*/
  637. template<class T>
  638. bool DynamicVectorClass<T>::Add_Head(T const & object)
  639. {
  640. if (ActiveCount >= Length()) {
  641. if ((IsAllocated || !VectorMax) && GrowthStep > 0) {
  642. if (!Resize(Length() + GrowthStep)) {
  643. /*
  644. ** Failure to increase the size of the vector is an error condition.
  645. ** Return with the error flag.
  646. */
  647. return(false);
  648. }
  649. } else {
  650. /*
  651. ** Increasing the size of this vector is not allowed! Bail this
  652. ** routine with the error code.
  653. */
  654. return(false);
  655. }
  656. }
  657. /*
  658. ** There is room for the new object now. Add it to the end of the object vector.
  659. */
  660. if (ActiveCount) {
  661. memmove(&(*this)[1], &(*this)[0], ActiveCount * sizeof(T));
  662. }
  663. (*this)[0] = object;
  664. ActiveCount++;
  665. // (*this)[ActiveCount++] = object;
  666. return(true);
  667. }
  668. /***********************************************************************************************
  669. * DynamicVectorClass<T>::Insert -- insert an object at the desired index *
  670. * *
  671. * INPUT: *
  672. * *
  673. * OUTPUT: *
  674. * *
  675. * WARNINGS: *
  676. * *
  677. * HISTORY: *
  678. * 4/27/99 GTH : Created. *
  679. *=============================================================================================*/
  680. template<class T>
  681. bool DynamicVectorClass<T>::Insert(int index,T const & object)
  682. {
  683. if (index < 0) return false;
  684. if (index > ActiveCount) return false;
  685. if (ActiveCount >= Length()) {
  686. if ((IsAllocated || !VectorMax) && GrowthStep > 0) {
  687. if (!Resize(Length() + GrowthStep)) {
  688. /*
  689. ** Failure to increase the size of the vector is an error condition.
  690. ** Return with the error flag.
  691. */
  692. return(false);
  693. }
  694. } else {
  695. /*
  696. ** Increasing the size of this vector is not allowed! Bail this
  697. ** routine with the error code.
  698. */
  699. return(false);
  700. }
  701. }
  702. /*
  703. ** There is room for the new object now. Add it at the desired position.
  704. */
  705. if (index < ActiveCount) {
  706. memmove(&(*this)[index+1], &(*this)[index], (ActiveCount-index) * sizeof(T));
  707. }
  708. (*this)[index] = object;
  709. ActiveCount++;
  710. return(true);
  711. }
  712. /***********************************************************************************************
  713. * DynamicVectorClass<T>::Delete -- Remove the specified object from the vector. *
  714. * *
  715. * This routine will delete the object referenced from the vector. All objects in the *
  716. * vector that follow the one deleted will be moved "down" to fill the hole. *
  717. * *
  718. * INPUT: object -- Reference to the object in this vector that is to be deleted. *
  719. * *
  720. * OUTPUT: bool; Was the object deleted successfully? This should always be true. *
  721. * *
  722. * WARNINGS: Do no pass a reference to an object that is NOT part of this vector. The *
  723. * results of this are undefined and probably catastrophic. *
  724. * *
  725. * HISTORY: *
  726. * 03/10/1995 JLB : Created. *
  727. *=============================================================================================*/
  728. template<class T>
  729. bool DynamicVectorClass<T>::Delete(T const & object)
  730. {
  731. int id = ID(object);
  732. if (id != -1) {
  733. return(Delete(id));
  734. }
  735. return(false);
  736. }
  737. /***********************************************************************************************
  738. * DynamicVectorClass<T>::Delete -- Deletes the specified index from the vector. *
  739. * *
  740. * Use this routine to delete the object at the specified index from the objects in the *
  741. * vector. This routine will move all the remaining objects "down" in order to fill the *
  742. * hole. *
  743. * *
  744. * INPUT: index -- The index number of the object in the vector that is to be deleted. *
  745. * *
  746. * OUTPUT: bool; Was the object index deleted successfully? Failure might mean that the index *
  747. * specified was out of bounds. *
  748. * *
  749. * WARNINGS: none *
  750. * *
  751. * HISTORY: *
  752. * 03/10/1995 JLB : Created. *
  753. *=============================================================================================*/
  754. template<class T>
  755. bool DynamicVectorClass<T>::Delete(int index)
  756. {
  757. if (index < ActiveCount) {
  758. ActiveCount--;
  759. /*
  760. ** If there are any objects past the index that was deleted, copy those
  761. ** objects down in order to fill the hole. A simple memory copy is
  762. ** not sufficient since the vector could contain class objects that
  763. ** need to use the assignment operator for movement.
  764. */
  765. // (&(*this)[index])->~ T ();
  766. for (int i = index; i < ActiveCount; i++) {
  767. (*this)[i] = (*this)[i+1];
  768. }
  769. return(true);
  770. }
  771. return(false);
  772. }
  773. template<class T>
  774. bool DynamicVectorClass<T>::Delete_Index(int index)
  775. {
  776. if (index < ActiveCount) {
  777. ActiveCount--;
  778. /*
  779. ** If there are any objects past the index that was deleted, copy those
  780. ** objects down in order to fill the hole. A simple memory copy is
  781. ** not sufficient since the vector could contain class objects that
  782. ** need to use the assignment operator for movement.
  783. */
  784. // (&(*this)[index])->~ T ();
  785. for (int i = index; i < ActiveCount; i++) {
  786. (*this)[i] = (*this)[i+1];
  787. }
  788. return(true);
  789. }
  790. return(false);
  791. }
  792. template<class T>
  793. void DynamicVectorClass<T>::Delete_All(void)
  794. {
  795. int len = VectorMax;
  796. Clear(); // Forces destructor call on each object.
  797. Resize(len);
  798. }
  799. /***********************************************************************************************
  800. * DynamicVectorClass<T>::Uninitialized_Add -- Add an empty place to the vector. *
  801. * *
  802. * To avoid copying when creating an object and adding it to the vector, use this and *
  803. * immediately fill the area that the return value points to with a valid object (by hand *
  804. * for a struct or by using placement new for a class object). *
  805. * This function does everything Add does except copying an object into the new space, *
  806. * thus leaving an uninitialized area of memory. *
  807. * *
  808. * INPUT: none. *
  809. * *
  810. * OUTPUT: T *; Points to the empty space where the new object is to be created. (If the *
  811. * space was not added successfully, returns NULL). *
  812. * *
  813. * WARNINGS: If memory area is left uninitialized, Very Bad Things will happen. *
  814. * *
  815. * HISTORY: *
  816. * 03/04/1998 NH : Created. *
  817. *=============================================================================================*/
  818. template<class T>
  819. T * DynamicVectorClass<T>::Uninitialized_Add(void)
  820. {
  821. if (ActiveCount >= Length()) {
  822. // if ((IsAllocated || !VectorMax) && GrowthStep > 0) {
  823. if (GrowthStep > 0) {
  824. if (!Resize(Length() + GrowthStep)) {
  825. /*
  826. ** Failure to increase the size of the vector is an error condition.
  827. ** Return with the error value.
  828. */
  829. return(NULL);
  830. }
  831. } else {
  832. /*
  833. ** Increasing the size of this vector is not allowed! Bail this
  834. ** routine with the error value.
  835. */
  836. return(NULL);
  837. }
  838. }
  839. /*
  840. ** There is room for the new space now. Add it to the end of the object
  841. ** vector. and return a pointer to it.
  842. */
  843. return &((*this)[ActiveCount++]);
  844. }
  845. void Set_Bit(void * array, int bit, int value);
  846. int Get_Bit(void const * array, int bit);
  847. int First_True_Bit(void const * array);
  848. int First_False_Bit(void const * array);
  849. /**************************************************************************
  850. ** This is a derivative of a vector class that supports boolean flags. Since
  851. ** a boolean flag can be represented by a single bit, this class packs the
  852. ** array of boolean flags into an array of bytes containing 8 boolean values
  853. ** each. For large boolean arrays, this results in an 87.5% savings. Although
  854. ** the indexing "[]" operator is supported, DO NOT pass pointers to sub elements
  855. ** of this bit vector class. A pointer derived from the indexing operator is
  856. ** only valid until the next call. Because of this, only simple
  857. ** direct use of the "[]" operator is allowed.
  858. */
  859. class BooleanVectorClass
  860. {
  861. public:
  862. BooleanVectorClass(unsigned size=0, unsigned char * array=0);
  863. BooleanVectorClass(BooleanVectorClass const & vector);
  864. // Assignment operator.
  865. BooleanVectorClass & operator =(BooleanVectorClass const & vector);
  866. // Equivalency operator.
  867. bool operator == (BooleanVectorClass const & vector) const;
  868. // Initialization
  869. void Init(unsigned size, unsigned char * array);
  870. void Init(unsigned size);
  871. // Fetch number of boolean objects in vector.
  872. int Length(void) {return BitCount;};
  873. // Set all boolean values to false;
  874. void Reset(void);
  875. // Set all boolean values to true.
  876. void Set(void);
  877. // Resets vector to zero length (frees memory).
  878. void Clear(void);
  879. // Change size of this boolean vector.
  880. int Resize(unsigned size);
  881. // Fetch reference to specified index.
  882. bool const & operator[](int index) const {
  883. if (LastIndex != index) Fixup(index);
  884. return(Copy);
  885. };
  886. bool & operator[](int index) {
  887. if (LastIndex != index) Fixup(index);
  888. return(Copy);
  889. };
  890. // Quick check on boolean state.
  891. bool Is_True(int index) const {
  892. if (index == LastIndex) return(Copy);
  893. return(Get_Bit(&BitArray[0], index) != 0);
  894. };
  895. // Find first index that is false.
  896. int First_False(void) const {
  897. if (LastIndex != -1) Fixup(-1);
  898. int retval = First_False_Bit(&BitArray[0]);
  899. if (retval < BitCount) return(retval);
  900. /*
  901. ** Failure to find a false boolean value in the vector. Return this
  902. ** fact in the form of an invalid index number.
  903. */
  904. return(-1);
  905. }
  906. // Find first index that is true.
  907. int First_True(void) const {
  908. if (LastIndex != -1) Fixup(-1);
  909. int retval = First_True_Bit(&BitArray[0]);
  910. if (retval < BitCount) return(retval);
  911. /*
  912. ** Failure to find a true boolean value in the vector. Return this
  913. ** fact in the form of an invalid index number.
  914. */
  915. return(-1);
  916. }
  917. // Accessors (usefull for saving the bit vector)
  918. const VectorClass<unsigned char> & Get_Bit_Array(void) { return BitArray; }
  919. protected:
  920. void Fixup(int index=-1) const;
  921. /*
  922. ** This is the number of boolean values in the vector. This value is
  923. ** not necessarily a multiple of 8, even though the underlying character
  924. ** vector contains a multiple of 8 bits.
  925. */
  926. int BitCount;
  927. /*
  928. ** This is a referential copy of an element in the bit vector. The
  929. ** purpose of this copy is to allow normal reference access to this
  930. ** object (for speed reasons). This hides the bit packing scheme from
  931. ** the user of this class.
  932. */
  933. bool Copy;
  934. /*
  935. ** This records the index of the value last fetched into the reference
  936. ** boolean variable. This index is used to properly restore the value
  937. ** when the reference copy needs updating.
  938. */
  939. int LastIndex;
  940. /*
  941. ** This points to the allocated bitfield array.
  942. */
  943. VectorClass<unsigned char> BitArray;
  944. };
  945. template<class T>
  946. int Pointer_Vector_Add(T * ptr, VectorClass<T *> & vec)
  947. {
  948. int id = 0;
  949. bool foundspot = false;
  950. for (int index = 0; index < vec.Length(); index++) {
  951. if (vec[index] == NULL) {
  952. id = index;
  953. foundspot = true;
  954. break;
  955. }
  956. }
  957. if (!foundspot) {
  958. id = vec.Length();
  959. vec.Resize((vec.Length()+1) * 2);
  960. for (int index = id; index < vec.Length(); index++) {
  961. vec[index] = NULL;
  962. }
  963. }
  964. vec[id] = ptr;
  965. return(id);
  966. }
  967. template<class T>
  968. bool Pointer_Vector_Remove(T const * ptr, VectorClass<T *> & vec)
  969. {
  970. int id = vec.ID((T *)ptr);
  971. if (id != -1) {
  972. vec[id] = NULL;
  973. return(true);
  974. }
  975. return(false);
  976. }
  977. #ifdef _MSC_VER
  978. #pragma warning (default : 4702) // unreachable code, happens with some uses of these templates
  979. #endif
  980. #endif