Vector.H 50 KB

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