TSVector.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. #pragma once
  2. template<class T> class Vector;
  3. void tsf_AddVar(const char *, Vector<signed int> *);
  4. void tsf_AddVar(const char *, Vector<float> *);
  5. //-----------------------------------------------------------------------------
  6. // Torque Game Engine
  7. // Copyright (C) GarageGames.com, Inc.
  8. //-----------------------------------------------------------------------------
  9. // This code has been modified to be independent of other TGE code.
  10. #ifndef _TVECTOR_H_
  11. #define _TVECTOR_H_
  12. #include <stdlib.h>
  13. //-----------------------------------------------------------------------------
  14. // Helper definitions for the vector class.
  15. /// Size of memory blocks to allocate at a time for vectors.
  16. #define VectorBlockSize 16
  17. extern bool VectorResize(unsigned int *aSize, unsigned int *aCount, void **arrayPtr, unsigned int newCount, unsigned int elemSize);
  18. // =============================================================================
  19. /// A dynamic array class.
  20. ///
  21. /// The vector grows as you insert or append
  22. /// elements. Insertion is fastest at the end of the array. Resizing
  23. /// of the array can be avoided by pre-allocating space using the
  24. /// reserve() method.
  25. ///
  26. /// <b>***WARNING***</b>
  27. ///
  28. /// This template does not initialize, construct or destruct any of
  29. /// it's elements. This means don't use this template for elements
  30. /// (classes) that need these operations. This template is intended
  31. /// to be used for simple structures that have no constructors or
  32. /// destructors.
  33. ///
  34. /// @nosubgrouping
  35. template<class T>
  36. class Vector
  37. {
  38. protected:
  39. unsigned int mElementCount;
  40. unsigned int mArraySize;
  41. T* mArray;
  42. bool resize(unsigned int);
  43. public:
  44. Vector(const unsigned int initialSize = 0);
  45. Vector(const unsigned int initialSize, const char* fileName, const unsigned int lineNum);
  46. Vector(const char* fileName, const unsigned int lineNum);
  47. Vector(const Vector&);
  48. ~Vector();
  49. /// @name STL interface
  50. /// @{
  51. typedef T value_type;
  52. typedef T& reference;
  53. typedef const T& const_reference;
  54. typedef T* iterator;
  55. typedef const T* const_iterator;
  56. typedef signed int difference_type;
  57. typedef unsigned int size_type;
  58. Vector<T>& operator=(const Vector<T>& p);
  59. iterator begin();
  60. const_iterator begin() const;
  61. iterator end();
  62. const_iterator end() const;
  63. signed int size() const;
  64. bool empty() const;
  65. void insert(iterator, const T&);
  66. void erase(iterator);
  67. T& front();
  68. const T& front() const;
  69. T& back();
  70. const T& back() const;
  71. void push_front(const T&);
  72. void push_back(const T&);
  73. void pop_front();
  74. void pop_back();
  75. T& operator[](unsigned int);
  76. const T& operator[](unsigned int) const;
  77. T& operator[](signed int i) { return operator[](i); }
  78. const T& operator[](signed int i ) const { return operator[](i); }
  79. void reserve(unsigned int);
  80. unsigned int capacity() const;
  81. /// @}
  82. /// @name Extended interface
  83. /// @{
  84. unsigned int memSize() const;
  85. T* address() const;
  86. unsigned int setSize(unsigned int);
  87. void increment(unsigned int = 1);
  88. void decrement(unsigned int = 1);
  89. void insert(unsigned int);
  90. void erase(unsigned int);
  91. void erase_fast(unsigned int);
  92. void erase_fast(iterator);
  93. void clear();
  94. void compact();
  95. T& first();
  96. T& last();
  97. const T& first() const;
  98. const T& last() const;
  99. void set(void * addr, unsigned int sz);
  100. /// Merge another vector into this one.
  101. ///
  102. /// @author BJW 8/20/97
  103. void merge(const Vector& p);
  104. /// @}
  105. };
  106. template<class T> inline Vector<T>::~Vector()
  107. {
  108. free(mArray);
  109. }
  110. template<class T> inline Vector<T>::Vector(const unsigned int initialSize)
  111. {
  112. mArray = 0;
  113. mElementCount = 0;
  114. mArraySize = 0;
  115. if(initialSize)
  116. reserve(initialSize);
  117. }
  118. template<class T> inline Vector<T>::Vector(const unsigned int initialSize,
  119. const char* fileName,
  120. const unsigned int lineNum)
  121. {
  122. fileName;
  123. lineNum;
  124. mArray = 0;
  125. mElementCount = 0;
  126. mArraySize = 0;
  127. if(initialSize)
  128. reserve(initialSize);
  129. }
  130. template<class T> inline Vector<T>::Vector(const char* fileName,
  131. const unsigned int lineNum)
  132. {
  133. fileName;
  134. lineNum;
  135. mArray = 0;
  136. mElementCount = 0;
  137. mArraySize = 0;
  138. }
  139. template<class T> inline Vector<T>::Vector(const Vector& p)
  140. {
  141. mArray = 0;
  142. resize(p.mElementCount);
  143. if (p.mElementCount)
  144. memcpy(mArray,p.mArray,mElementCount * sizeof(value_type));
  145. }
  146. template<class T> inline unsigned int Vector<T>::memSize() const
  147. {
  148. return capacity() * sizeof(T);
  149. }
  150. template<class T> inline T* Vector<T>::address() const
  151. {
  152. return mArray;
  153. }
  154. template<class T> inline unsigned int Vector<T>::setSize(unsigned int size)
  155. {
  156. if (size > mArraySize)
  157. resize(size);
  158. else
  159. mElementCount = size;
  160. return mElementCount;
  161. }
  162. template<class T> inline void Vector<T>::increment(unsigned int delta)
  163. {
  164. if ((mElementCount += delta) > mArraySize)
  165. resize(mElementCount);
  166. }
  167. template<class T> inline void Vector<T>::decrement(unsigned int delta)
  168. {
  169. if (mElementCount > delta)
  170. mElementCount -= delta;
  171. else
  172. mElementCount = 0;
  173. }
  174. template<class T> inline void Vector<T>::insert(unsigned int index)
  175. {
  176. // Assert: index >= 0 && index < mElementCount
  177. increment();
  178. memmove(&mArray[index + 1],
  179. &mArray[index],
  180. (mElementCount - index - 1) * sizeof(value_type));
  181. }
  182. template<class T> inline void Vector<T>::erase(unsigned int index)
  183. {
  184. // Assert: index >= 0 && index < mElementCount
  185. memmove(&mArray[index],
  186. &mArray[index + 1],
  187. (mElementCount - index - 1) * sizeof(value_type));
  188. decrement();
  189. }
  190. template<class T> inline void Vector<T>::erase_fast(unsigned int index)
  191. {
  192. // CAUTION: this operator does NOT maintain list order
  193. // Copy the last element into the deleted 'hole' and decrement the
  194. // size of the vector.
  195. // Assert: index >= 0 && index < mElementCount
  196. if (index < (mElementCount - 1))
  197. memmove(&mArray[index], &mArray[mElementCount - 1], sizeof(value_type));
  198. decrement();
  199. }
  200. template<class T> inline T& Vector<T>::first()
  201. {
  202. return mArray[0];
  203. }
  204. template<class T> inline const T& Vector<T>::first() const
  205. {
  206. return mArray[0];
  207. }
  208. template<class T> inline T& Vector<T>::last()
  209. {
  210. return mArray[mElementCount - 1];
  211. }
  212. template<class T> inline const T& Vector<T>::last() const
  213. {
  214. return mArray[mElementCount - 1];
  215. }
  216. template<class T> inline void Vector<T>::clear()
  217. {
  218. mElementCount = 0;
  219. }
  220. template<class T> inline void Vector<T>::compact()
  221. {
  222. resize(mElementCount);
  223. }
  224. //-----------------------------------------------------------------------------
  225. template<class T> inline Vector<T>& Vector<T>::operator=(const Vector<T>& p)
  226. {
  227. resize(p.mElementCount);
  228. if (p.mElementCount)
  229. memcpy(mArray,p.mArray,mElementCount * sizeof(value_type));
  230. return *this;
  231. }
  232. template<class T> inline typename Vector<T>::iterator Vector<T>::begin()
  233. {
  234. return mArray;
  235. }
  236. template<class T> inline typename Vector<T>::const_iterator Vector<T>::begin() const
  237. {
  238. return mArray;
  239. }
  240. template<class T> inline typename Vector<T>::iterator Vector<T>::end()
  241. {
  242. return mArray + mElementCount;
  243. }
  244. template<class T> inline typename Vector<T>::const_iterator Vector<T>::end() const
  245. {
  246. return mArray + mElementCount;
  247. }
  248. template<class T> inline signed int Vector<T>::size() const
  249. {
  250. return (signed int)mElementCount;
  251. }
  252. template<class T> inline bool Vector<T>::empty() const
  253. {
  254. return (mElementCount == 0);
  255. }
  256. template<class T> inline void Vector<T>::insert(iterator p,const T& x)
  257. {
  258. unsigned int index = (unsigned int) (p - mArray);
  259. insert(index);
  260. mArray[index] = x;
  261. }
  262. template<class T> inline void Vector<T>::erase(iterator q)
  263. {
  264. erase((unsigned int)(q - mArray));
  265. }
  266. template<class T> inline void Vector<T>::erase_fast(iterator q)
  267. {
  268. erase_fast((unsigned int)(q - mArray));
  269. }
  270. template<class T> inline T& Vector<T>::front()
  271. {
  272. return *begin();
  273. }
  274. template<class T> inline const T& Vector<T>::front() const
  275. {
  276. return *begin();
  277. }
  278. template<class T> inline T& Vector<T>::back()
  279. {
  280. return *end();
  281. }
  282. template<class T> inline const T& Vector<T>::back() const
  283. {
  284. return *end();
  285. }
  286. template<class T> inline void Vector<T>::push_front(const T& x)
  287. {
  288. insert(0);
  289. mArray[0] = x;
  290. }
  291. template<class T> inline void Vector<T>::push_back(const T& x)
  292. {
  293. increment();
  294. mArray[mElementCount - 1] = x;
  295. }
  296. template<class T> inline void Vector<T>::pop_front()
  297. {
  298. erase((unsigned int)(0));
  299. }
  300. template<class T> inline void Vector<T>::pop_back()
  301. {
  302. decrement();
  303. }
  304. template<class T> inline T& Vector<T>::operator[](unsigned int index)
  305. {
  306. //AssertFatal((index < mElementCount), "Index too large.");
  307. return mArray[index];
  308. }
  309. template<class T> inline const T& Vector<T>::operator[](unsigned int index) const
  310. {
  311. //AssertFatal((index < mElementCount), "Index too large.");
  312. return mArray[index];
  313. }
  314. template<class T> inline void Vector<T>::reserve(unsigned int size)
  315. {
  316. if (size > mArraySize) {
  317. unsigned int ec = mElementCount;
  318. if (resize(size))
  319. mElementCount = ec;
  320. }
  321. }
  322. template<class T> inline unsigned int Vector<T>::capacity() const
  323. {
  324. return mArraySize;
  325. }
  326. template<class T> inline void Vector<T>::set(void * addr, unsigned int sz)
  327. {
  328. setSize(sz);
  329. if (addr)
  330. memcpy(address(),addr,sz*sizeof(T));
  331. }
  332. //-----------------------------------------------------------------------------
  333. template<class T> inline bool Vector<T>::resize(unsigned int ecount)
  334. {
  335. return VectorResize(&mArraySize, &mElementCount, (void**) &mArray, ecount, sizeof(T));
  336. }
  337. // BJW 8/20/97
  338. // code to merge a vector into this one
  339. template<class T> inline void Vector<T>::merge(const Vector& p)
  340. {
  341. if (p.size()) {
  342. signed int oldsize = size();
  343. resize(oldsize + p.size());
  344. memcpy( &mArray[oldsize], p.address(), p.size() * sizeof(T) );
  345. }
  346. }
  347. //-----------------------------------------------------------------------------
  348. /// Template for vectors of pointers.
  349. template <class T>
  350. class VectorPtr : public Vector<void*>
  351. {
  352. /// @deprecated Disallowed.
  353. VectorPtr(const VectorPtr&); // Disallowed
  354. public:
  355. VectorPtr();
  356. VectorPtr(const char* fileName, const unsigned int lineNum);
  357. /// @name STL interface
  358. /// @{
  359. typedef T value_type;
  360. typedef T& reference;
  361. typedef const T& const_reference;
  362. typedef T* iterator;
  363. typedef const T* const_iterator;
  364. typedef unsigned int difference_type;
  365. typedef unsigned int size_type;
  366. iterator begin();
  367. const_iterator begin() const;
  368. iterator end();
  369. const_iterator end() const;
  370. void insert(iterator,const T&);
  371. void erase(iterator);
  372. T& front();
  373. const T& front() const;
  374. T& back();
  375. const T& back() const;
  376. void push_front(const T&);
  377. void push_back(const T&);
  378. T& operator[](unsigned int);
  379. const T& operator[](unsigned int) const;
  380. /// @}
  381. /// @name Extended interface
  382. /// @{
  383. typedef Vector<void*> Parent;
  384. T& first();
  385. T& last();
  386. const T& first() const;
  387. const T& last() const;
  388. void erase_fast(unsigned int);
  389. void erase_fast(iterator);
  390. /// @}
  391. };
  392. //-----------------------------------------------------------------------------
  393. template<class T> inline VectorPtr<T>::VectorPtr()
  394. {
  395. //
  396. }
  397. template<class T> inline VectorPtr<T>::VectorPtr(const char* fileName,
  398. const unsigned int lineNum)
  399. : Vector<void*>(fileName, lineNum)
  400. {
  401. //
  402. }
  403. template<class T> inline T& VectorPtr<T>::first()
  404. {
  405. return (T&)Parent::first();
  406. }
  407. template<class T> inline const T& VectorPtr<T>::first() const
  408. {
  409. return (const T)Parent::first();
  410. }
  411. template<class T> inline T& VectorPtr<T>::last()
  412. {
  413. return (T&)Parent::last();
  414. }
  415. template<class T> inline const T& VectorPtr<T>::last() const
  416. {
  417. return (const T&)Parent::last();
  418. }
  419. template<class T> inline typename VectorPtr<T>::iterator VectorPtr<T>::begin()
  420. {
  421. return (iterator)Parent::begin();
  422. }
  423. template<class T> inline typename VectorPtr<T>::const_iterator VectorPtr<T>::begin() const
  424. {
  425. return (const_iterator)Parent::begin();
  426. }
  427. template<class T> inline typename VectorPtr<T>::iterator VectorPtr<T>::end()
  428. {
  429. return (iterator)Parent::end();
  430. }
  431. template<class T> inline typename VectorPtr<T>::const_iterator VectorPtr<T>::end() const
  432. {
  433. return (const_iterator)Parent::end();
  434. }
  435. template<class T> inline void VectorPtr<T>::insert(iterator i,const T& x)
  436. {
  437. Parent::insert( (Parent::iterator)i, (Parent::reference)x );
  438. }
  439. template<class T> inline void VectorPtr<T>::erase(iterator i)
  440. {
  441. Parent::erase( (Parent::iterator)i );
  442. }
  443. template<class T> inline void VectorPtr<T>::erase_fast(unsigned int index)
  444. {
  445. // CAUTION: this operator does maintain list order
  446. // Copy the last element into the deleted 'hole' and decrement the
  447. // size of the vector.
  448. // Assert: index >= 0 && index < mElementCount
  449. if (index < (mElementCount - 1))
  450. mArray[index] = mArray[mElementCount - 1];
  451. decrement();
  452. }
  453. template<class T> inline void VectorPtr<T>::erase_fast(iterator i)
  454. {
  455. erase_fast((unsigned int)(i - iterator(mArray)));
  456. }
  457. template<class T> inline T& VectorPtr<T>::front()
  458. {
  459. return *begin();
  460. }
  461. template<class T> inline const T& VectorPtr<T>::front() const
  462. {
  463. return *begin();
  464. }
  465. template<class T> inline T& VectorPtr<T>::back()
  466. {
  467. return *end();
  468. }
  469. template<class T> inline const T& VectorPtr<T>::back() const
  470. {
  471. return *end();
  472. }
  473. template<class T> inline void VectorPtr<T>::push_front(const T& x)
  474. {
  475. Parent::push_front((Parent::const_reference)x);
  476. }
  477. template<class T> inline void VectorPtr<T>::push_back(const T& x)
  478. {
  479. Parent::push_back((Parent::const_reference)x);
  480. }
  481. template<class T> inline T& VectorPtr<T>::operator[](unsigned int index)
  482. {
  483. return (T&)Parent::operator[](index);
  484. }
  485. template<class T> inline const T& VectorPtr<T>::operator[](unsigned int index) const
  486. {
  487. return (const T&)Parent::operator[](index);
  488. }
  489. #endif //_TVECTOR_H_