vector.h 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. /*
  2. ** Command & Conquer Renegade(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:: 3/05/02 10:09a $*
  29. * *
  30. * $Revision:: 50 $*
  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 = new 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 & vector) :
  195. Vector(0),
  196. VectorMax(0),
  197. IsValid(true),
  198. IsAllocated(false)
  199. {
  200. *this = vector;
  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: vector -- 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 & vector)
  219. {
  220. if (this != &vector) {
  221. Clear();
  222. VectorMax = vector.Length();
  223. if (VectorMax) {
  224. Vector = new T[VectorMax];
  225. if (Vector) {
  226. IsAllocated = true;
  227. for (int index = 0; index < VectorMax; index++) {
  228. Vector[index] = vector[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 & vector) const
  256. {
  257. if (VectorMax == vector.Length()) {
  258. for (int index = 0; index < VectorMax; index++) {
  259. if (Vector[index] != vector[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 = new 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. // Deletes all objects in the vector.
  463. void Delete_All(void);
  464. // Set amount that vector grows by.
  465. int Set_Growth_Step(int step) {return(GrowthStep = step);};
  466. // Fetch current growth step rate.
  467. int Growth_Step(void) {return GrowthStep;};
  468. virtual int ID(T const * ptr) {return(VectorClass<T>::ID(ptr));};
  469. virtual int ID(T const & ptr);
  470. DynamicVectorClass<T> & operator =(DynamicVectorClass<T> const & rvalue) {
  471. VectorClass<T>::operator = (rvalue);
  472. ActiveCount = rvalue.ActiveCount;
  473. GrowthStep = rvalue.GrowthStep;
  474. return(*this);
  475. }
  476. // Uninitialized Add - does everything an Add does, except copying an
  477. // object into the 'new' spot in the array. It returns a pointer to
  478. // the 'new' spot. (NULL if the Add failed). NOTE - you must then fill
  479. // this memory area with a valid object (e.g. by using placement new),
  480. // or chaos will result!
  481. T * Uninitialized_Add(void);
  482. protected:
  483. /*
  484. ** This is a count of the number of active objects in this
  485. ** vector. The memory array often times is bigger than this
  486. ** value.
  487. */
  488. int ActiveCount;
  489. /*
  490. ** If there is insufficient room in the vector array for a new
  491. ** object to be added, then the vector will grow by the number
  492. ** of objects specified by this value. This is controlled by
  493. ** the Set_Growth_Step() function.
  494. */
  495. int GrowthStep;
  496. };
  497. /***********************************************************************************************
  498. * DynamicVectorClass<T>::DynamicVectorClass -- Constructor for dynamic vector. *
  499. * *
  500. * This is the normal constructor for the dynamic vector class. It is similar to the normal *
  501. * vector class constructor. The vector is initialized to contain the number of elements *
  502. * specified in the "size" parameter. The memory is allocated from free store unless the *
  503. * optional array parameter is provided. In this case it will place the vector at the *
  504. * memory location specified. *
  505. * *
  506. * INPUT: size -- The maximum number of objects allowed in this vector. *
  507. * *
  508. * array -- Optional pointer to the memory area to place the vector at. *
  509. * *
  510. * OUTPUT: none *
  511. * *
  512. * WARNINGS: none *
  513. * *
  514. * HISTORY: *
  515. * 03/10/1995 JLB : Created. *
  516. *=============================================================================================*/
  517. template<class T>
  518. DynamicVectorClass<T>::DynamicVectorClass(unsigned size, T const * array)
  519. : VectorClass<T>(size, array)
  520. {
  521. GrowthStep = 10;
  522. ActiveCount = 0;
  523. }
  524. /***********************************************************************************************
  525. * DynamicVectorClass<T>::Resize -- Changes the size of a dynamic vector. *
  526. * *
  527. * Use this routine to change the size of the vector. The size changed is the maximum *
  528. * number of allocated objects within this vector. If a memory buffer is provided, then *
  529. * the vector will be located there. Otherwise, the memory will be allocated out of free *
  530. * store. *
  531. * *
  532. * INPUT: newsize -- The desired maximum size of this vector. *
  533. * *
  534. * array -- Optional pointer to a previously allocated memory array. *
  535. * *
  536. * OUTPUT: bool; Was vector successfully resized according to specifications? *
  537. * *
  538. * WARNINGS: Failure to resize the vector could be the result of lack of free store. *
  539. * *
  540. * HISTORY: *
  541. * 03/10/1995 JLB : Created. *
  542. *=============================================================================================*/
  543. template<class T>
  544. bool DynamicVectorClass<T>::Resize(int newsize, T const * array)
  545. {
  546. if (VectorClass<T>::Resize(newsize, array)) {
  547. if (Length() < ActiveCount) ActiveCount = Length();
  548. return(true);
  549. }
  550. return(false);
  551. }
  552. /***********************************************************************************************
  553. * DynamicVectorClass<T>::ID -- Find matching value in the dynamic vector. *
  554. * *
  555. * Use this routine to find a matching object (by value) in the vector. Unlike the base *
  556. * class ID function of similar name, this one restricts the scan to the current number *
  557. * of valid objects. *
  558. * *
  559. * INPUT: object -- A reference to the object that a match is to be found in the *
  560. * vector. *
  561. * *
  562. * OUTPUT: Returns with the index number of the object that is equivalent to the one *
  563. * specified. If no equivalent object could be found then -1 is returned. *
  564. * *
  565. * WARNINGS: none *
  566. * *
  567. * HISTORY: *
  568. * 03/13/1995 JLB : Created. *
  569. *=============================================================================================*/
  570. template<class T>
  571. int DynamicVectorClass<T>::ID(T const & object)
  572. {
  573. for (int index = 0; index < Count(); index++) {
  574. if ((*this)[index] == object) return(index);
  575. }
  576. return(-1);
  577. }
  578. /***********************************************************************************************
  579. * DynamicVectorClass<T>::Add -- Add an element to the vector. *
  580. * *
  581. * Use this routine to add an element to the vector. The vector will automatically be *
  582. * resized to accomodate the new element IF the vector was allocated previously and the *
  583. * growth rate is not zero. *
  584. * *
  585. * INPUT: object -- Reference to the object that will be added to the vector. *
  586. * *
  587. * OUTPUT: bool; Was the object added successfully? If so, the object is added to the end *
  588. * of the vector. *
  589. * *
  590. * WARNINGS: none *
  591. * *
  592. * HISTORY: *
  593. * 03/10/1995 JLB : Created. *
  594. *=============================================================================================*/
  595. template<class T>
  596. bool DynamicVectorClass<T>::Add(T const & object)
  597. {
  598. if (ActiveCount >= Length()) {
  599. if ((IsAllocated || !VectorMax) && GrowthStep > 0) {
  600. if (!Resize(Length() + GrowthStep)) {
  601. /*
  602. ** Failure to increase the size of the vector is an error condition.
  603. ** Return with the error flag.
  604. */
  605. return(false);
  606. }
  607. } else {
  608. /*
  609. ** Increasing the size of this vector is not allowed! Bail this
  610. ** routine with the error code.
  611. */
  612. return(false);
  613. }
  614. }
  615. /*
  616. ** There is room for the new object now. Add it to the end of the object vector.
  617. */
  618. (*this)[ActiveCount++] = object;
  619. return(true);
  620. }
  621. /***********************************************************************************************
  622. * DynamicVectorClass<T>::Add_Head -- Adds element to head of the list. *
  623. * *
  624. * This routine will add the specified element to the head of the vector. If necessary, *
  625. * the vector will be expanded accordingly. *
  626. * *
  627. * INPUT: object -- Reference to the object to add to the head of this vector. *
  628. * *
  629. * OUTPUT: bool; Was the object added without error? *
  630. * *
  631. * WARNINGS: none *
  632. * *
  633. * HISTORY: *
  634. * 09/21/1995 JLB : Created. *
  635. *=============================================================================================*/
  636. template<class T>
  637. bool DynamicVectorClass<T>::Add_Head(T const & object)
  638. {
  639. if (ActiveCount >= Length()) {
  640. if ((IsAllocated || !VectorMax) && GrowthStep > 0) {
  641. if (!Resize(Length() + GrowthStep)) {
  642. /*
  643. ** Failure to increase the size of the vector is an error condition.
  644. ** Return with the error flag.
  645. */
  646. return(false);
  647. }
  648. } else {
  649. /*
  650. ** Increasing the size of this vector is not allowed! Bail this
  651. ** routine with the error code.
  652. */
  653. return(false);
  654. }
  655. }
  656. /*
  657. ** There is room for the new object now. Add it to the end of the object vector.
  658. */
  659. if (ActiveCount) {
  660. memmove(&(*this)[1], &(*this)[0], ActiveCount * sizeof(T));
  661. }
  662. (*this)[0] = object;
  663. ActiveCount++;
  664. // (*this)[ActiveCount++] = object;
  665. return(true);
  666. }
  667. /***********************************************************************************************
  668. * DynamicVectorClass<T>::Insert -- insert an object at the desired index *
  669. * *
  670. * INPUT: *
  671. * *
  672. * OUTPUT: *
  673. * *
  674. * WARNINGS: *
  675. * *
  676. * HISTORY: *
  677. * 4/27/99 GTH : Created. *
  678. *=============================================================================================*/
  679. template<class T>
  680. bool DynamicVectorClass<T>::Insert(int index,T const & object)
  681. {
  682. if (index < 0) return false;
  683. if (index > ActiveCount) return false;
  684. if (ActiveCount >= Length()) {
  685. if ((IsAllocated || !VectorMax) && GrowthStep > 0) {
  686. if (!Resize(Length() + GrowthStep)) {
  687. /*
  688. ** Failure to increase the size of the vector is an error condition.
  689. ** Return with the error flag.
  690. */
  691. return(false);
  692. }
  693. } else {
  694. /*
  695. ** Increasing the size of this vector is not allowed! Bail this
  696. ** routine with the error code.
  697. */
  698. return(false);
  699. }
  700. }
  701. /*
  702. ** There is room for the new object now. Add it at the desired position.
  703. */
  704. if (index < ActiveCount) {
  705. memmove(&(*this)[index+1], &(*this)[index], (ActiveCount-index) * sizeof(T));
  706. }
  707. (*this)[index] = object;
  708. ActiveCount++;
  709. return(true);
  710. }
  711. /***********************************************************************************************
  712. * DynamicVectorClass<T>::Delete -- Remove the specified object from the vector. *
  713. * *
  714. * This routine will delete the object referenced from the vector. All objects in the *
  715. * vector that follow the one deleted will be moved "down" to fill the hole. *
  716. * *
  717. * INPUT: object -- Reference to the object in this vector that is to be deleted. *
  718. * *
  719. * OUTPUT: bool; Was the object deleted successfully? This should always be true. *
  720. * *
  721. * WARNINGS: Do no pass a reference to an object that is NOT part of this vector. The *
  722. * results of this are undefined and probably catastrophic. *
  723. * *
  724. * HISTORY: *
  725. * 03/10/1995 JLB : Created. *
  726. *=============================================================================================*/
  727. template<class T>
  728. bool DynamicVectorClass<T>::Delete(T const & object)
  729. {
  730. int id = ID(object);
  731. if (id != -1) {
  732. return(Delete(id));
  733. }
  734. return(false);
  735. }
  736. /***********************************************************************************************
  737. * DynamicVectorClass<T>::Delete -- Deletes the specified index from the vector. *
  738. * *
  739. * Use this routine to delete the object at the specified index from the objects in the *
  740. * vector. This routine will move all the remaining objects "down" in order to fill the *
  741. * hole. *
  742. * *
  743. * INPUT: index -- The index number of the object in the vector that is to be deleted. *
  744. * *
  745. * OUTPUT: bool; Was the object index deleted successfully? Failure might mean that the index *
  746. * specified was out of bounds. *
  747. * *
  748. * WARNINGS: none *
  749. * *
  750. * HISTORY: *
  751. * 03/10/1995 JLB : Created. *
  752. *=============================================================================================*/
  753. template<class T>
  754. bool DynamicVectorClass<T>::Delete(int index)
  755. {
  756. if (index < ActiveCount) {
  757. ActiveCount--;
  758. /*
  759. ** If there are any objects past the index that was deleted, copy those
  760. ** objects down in order to fill the hole. A simple memory copy is
  761. ** not sufficient since the vector could contain class objects that
  762. ** need to use the assignment operator for movement.
  763. */
  764. // (&(*this)[index])->~ T ();
  765. for (int i = index; i < ActiveCount; i++) {
  766. (*this)[i] = (*this)[i+1];
  767. }
  768. return(true);
  769. }
  770. return(false);
  771. }
  772. template<class T>
  773. void DynamicVectorClass<T>::Delete_All(void)
  774. {
  775. int len = VectorMax;
  776. Clear(); // Forces destructor call on each object.
  777. Resize(len);
  778. }
  779. /***********************************************************************************************
  780. * DynamicVectorClass<T>::Uninitialized_Add -- Add an empty place to the vector. *
  781. * *
  782. * To avoid copying when creating an object and adding it to the vector, use this and *
  783. * immediately fill the area that the return value points to with a valid object (by hand *
  784. * for a struct or by using placement new for a class object). *
  785. * This function does everything Add does except copying an object into the new space, *
  786. * thus leaving an uninitialized area of memory. *
  787. * *
  788. * INPUT: none. *
  789. * *
  790. * OUTPUT: T *; Points to the empty space where the new object is to be created. (If the *
  791. * space was not added successfully, returns NULL). *
  792. * *
  793. * WARNINGS: If memory area is left uninitialized, Very Bad Things will happen. *
  794. * *
  795. * HISTORY: *
  796. * 03/04/1998 NH : Created. *
  797. *=============================================================================================*/
  798. template<class T>
  799. T * DynamicVectorClass<T>::Uninitialized_Add(void)
  800. {
  801. if (ActiveCount >= Length()) {
  802. // if ((IsAllocated || !VectorMax) && GrowthStep > 0) {
  803. if (GrowthStep > 0) {
  804. if (!Resize(Length() + GrowthStep)) {
  805. /*
  806. ** Failure to increase the size of the vector is an error condition.
  807. ** Return with the error value.
  808. */
  809. return(NULL);
  810. }
  811. } else {
  812. /*
  813. ** Increasing the size of this vector is not allowed! Bail this
  814. ** routine with the error value.
  815. */
  816. return(NULL);
  817. }
  818. }
  819. /*
  820. ** There is room for the new space now. Add it to the end of the object
  821. ** vector. and return a pointer to it.
  822. */
  823. return &((*this)[ActiveCount++]);
  824. }
  825. void Set_Bit(void * array, int bit, int value);
  826. int Get_Bit(void const * array, int bit);
  827. int First_True_Bit(void const * array);
  828. int First_False_Bit(void const * array);
  829. /**************************************************************************
  830. ** This is a derivative of a vector class that supports boolean flags. Since
  831. ** a boolean flag can be represented by a single bit, this class packs the
  832. ** array of boolean flags into an array of bytes containing 8 boolean values
  833. ** each. For large boolean arrays, this results in an 87.5% savings. Although
  834. ** the indexing "[]" operator is supported, DO NOT pass pointers to sub elements
  835. ** of this bit vector class. A pointer derived from the indexing operator is
  836. ** only valid until the next call. Because of this, only simple
  837. ** direct use of the "[]" operator is allowed.
  838. */
  839. class BooleanVectorClass
  840. {
  841. public:
  842. BooleanVectorClass(unsigned size=0, unsigned char * array=0);
  843. BooleanVectorClass(BooleanVectorClass const & vector);
  844. // Assignment operator.
  845. BooleanVectorClass & operator =(BooleanVectorClass const & vector);
  846. // Equivalency operator.
  847. bool operator == (BooleanVectorClass const & vector) const;
  848. // Initialization
  849. void Init(unsigned size, unsigned char * array);
  850. void Init(unsigned size);
  851. // Fetch number of boolean objects in vector.
  852. int Length(void) {return BitCount;};
  853. // Set all boolean values to false;
  854. void Reset(void);
  855. // Set all boolean values to true.
  856. void Set(void);
  857. // Resets vector to zero length (frees memory).
  858. void Clear(void);
  859. // Change size of this boolean vector.
  860. int Resize(unsigned size);
  861. // Fetch reference to specified index.
  862. bool const & operator[](int index) const {
  863. if (LastIndex != index) Fixup(index);
  864. return(Copy);
  865. };
  866. bool & operator[](int index) {
  867. if (LastIndex != index) Fixup(index);
  868. return(Copy);
  869. };
  870. // Quick check on boolean state.
  871. bool Is_True(int index) const {
  872. if (index == LastIndex) return(Copy);
  873. return(Get_Bit(&BitArray[0], index) != 0);
  874. };
  875. // Find first index that is false.
  876. int First_False(void) const {
  877. if (LastIndex != -1) Fixup(-1);
  878. int retval = First_False_Bit(&BitArray[0]);
  879. if (retval < BitCount) return(retval);
  880. /*
  881. ** Failure to find a false boolean value in the vector. Return this
  882. ** fact in the form of an invalid index number.
  883. */
  884. return(-1);
  885. }
  886. // Find first index that is true.
  887. int First_True(void) const {
  888. if (LastIndex != -1) Fixup(-1);
  889. int retval = First_True_Bit(&BitArray[0]);
  890. if (retval < BitCount) return(retval);
  891. /*
  892. ** Failure to find a true boolean value in the vector. Return this
  893. ** fact in the form of an invalid index number.
  894. */
  895. return(-1);
  896. }
  897. // Accessors (usefull for saving the bit vector)
  898. const VectorClass<unsigned char> & Get_Bit_Array(void) { return BitArray; }
  899. protected:
  900. void Fixup(int index=-1) const;
  901. /*
  902. ** This is the number of boolean values in the vector. This value is
  903. ** not necessarily a multiple of 8, even though the underlying character
  904. ** vector contains a multiple of 8 bits.
  905. */
  906. int BitCount;
  907. /*
  908. ** This is a referential copy of an element in the bit vector. The
  909. ** purpose of this copy is to allow normal reference access to this
  910. ** object (for speed reasons). This hides the bit packing scheme from
  911. ** the user of this class.
  912. */
  913. bool Copy;
  914. /*
  915. ** This records the index of the value last fetched into the reference
  916. ** boolean variable. This index is used to properly restore the value
  917. ** when the reference copy needs updating.
  918. */
  919. int LastIndex;
  920. /*
  921. ** This points to the allocated bitfield array.
  922. */
  923. VectorClass<unsigned char> BitArray;
  924. };
  925. template<class T>
  926. int Pointer_Vector_Add(T * ptr, VectorClass<T *> & vec)
  927. {
  928. int id = 0;
  929. bool foundspot = false;
  930. for (int index = 0; index < vec.Length(); index++) {
  931. if (vec[index] == NULL) {
  932. id = index;
  933. foundspot = true;
  934. break;
  935. }
  936. }
  937. if (!foundspot) {
  938. id = vec.Length();
  939. vec.Resize((vec.Length()+1) * 2);
  940. for (int index = id; index < vec.Length(); index++) {
  941. vec[index] = NULL;
  942. }
  943. }
  944. vec[id] = ptr;
  945. return(id);
  946. }
  947. template<class T>
  948. bool Pointer_Vector_Remove(T const * ptr, VectorClass<T *> & vec)
  949. {
  950. int id = vec.ID((T *)ptr);
  951. if (id != -1) {
  952. vec[id] = NULL;
  953. return(true);
  954. }
  955. return(false);
  956. }
  957. #ifdef _MSC_VER
  958. #pragma warning (default : 4702) // unreachable code, happens with some uses of these templates
  959. #endif
  960. #endif