vector.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _VECTOR_H_
  23. #define _VECTOR_H_
  24. //Includes
  25. #ifndef _PLATFORM_H_
  26. #include "platform/platform.h"
  27. #endif
  28. //-----------------------------------------------------------------------------
  29. /// Size of memory blocks to allocate at a time for vectors.
  30. const static S32 VectorBlockSize = 16;
  31. #ifdef TORQUE_DEBUG
  32. extern bool VectorResize(U32 *aSize, U32 *aCount, void **arrayPtr, U32 newCount, U32 elemSize,
  33. const char* fileName,
  34. const U32 lineNum);
  35. #else
  36. extern bool VectorResize(U32 *aSize, U32 *aCount, void **arrayPtr, U32 newCount, U32 elemSize);
  37. #endif
  38. /// Use the following macro to bind a vector to a particular line
  39. /// of the owning class for memory tracking purposes
  40. #ifdef TORQUE_DEBUG
  41. #define VECTOR_SET_ASSOCIATION(x) x.setFileAssociation(__FILE__, __LINE__)
  42. #else
  43. #define VECTOR_SET_ASSOCIATION(x)
  44. #endif
  45. //-----------------------------------------------------------------------------
  46. /// A dynamic array class.
  47. ///
  48. /// The vector grows as you insert or append
  49. /// elements. Insertion is fastest at the end of the array. Resizing
  50. /// of the array can be avoided by pre-allocating space using the
  51. /// reserve() method.
  52. ///
  53. /// <b>***WARNING***</b>
  54. ///
  55. /// This template does not initialize, construct or destruct any of
  56. /// it's elements. This means don't use this template for elements
  57. /// (classes) that need these operations. This template is intended
  58. /// to be used for simple structures that have no constructors or
  59. /// destructors.
  60. ///
  61. /// @nosubgrouping
  62. template<class T>
  63. class Vector
  64. {
  65. protected:
  66. U32 mElementCount;
  67. U32 mArraySize;
  68. T* mArray;
  69. #ifdef TORQUE_DEBUG
  70. const char* mFileAssociation;
  71. U32 mLineAssociation;
  72. #endif
  73. bool resize(U32); // resizes, but does no construction/destruction
  74. void destroy(U32 start, U32 end); ///< Destructs elements from <i>start</i> to <i>end-1</i>
  75. void construct(U32 start, U32 end); ///< Constructs elements from <i>start</i> to <i>end-1</i>
  76. void construct(U32 start, U32 end, const T* array);
  77. public:
  78. Vector(const U32 initialSize = 0);
  79. Vector(const U32 initialSize, const char* fileName, const U32 lineNum);
  80. Vector(const char* fileName, const U32 lineNum);
  81. Vector(const Vector&);
  82. ~Vector();
  83. #ifdef TORQUE_DEBUG
  84. void setFileAssociation(const char* file, const U32 line);
  85. #endif
  86. /// @name STL interface
  87. /// @{
  88. typedef T value_type;
  89. typedef T& reference;
  90. typedef const T& const_reference;
  91. typedef T* iterator;
  92. typedef const T* const_iterator;
  93. typedef S32 difference_type;
  94. typedef U32 size_type;
  95. typedef difference_type (QSORT_CALLBACK *compare_func)(const T *a, const T *b);
  96. Vector<T>& operator=(const Vector<T>& p);
  97. iterator begin();
  98. const_iterator begin() const;
  99. iterator end();
  100. const_iterator end() const;
  101. S32 size() const;
  102. bool empty() const;
  103. bool contains(const T&) const;
  104. void insert(iterator, const T&);
  105. void erase(iterator);
  106. T& front();
  107. const T& front() const;
  108. T& back();
  109. const T& back() const;
  110. void push_front(const T&);
  111. void push_back(const T&);
  112. U32 push_front_unique(const T&);
  113. U32 push_back_unique(const T&);
  114. S32 find_next( const T&, U32 start = 0 ) const;
  115. void pop_front();
  116. void pop_back();
  117. T& operator[](U32);
  118. const T& operator[](U32) const;
  119. T& operator[](S32 i) { return operator[](U32(i)); }
  120. const T& operator[](S32 i ) const { return operator[](U32(i)); }
  121. T& at(U32);
  122. const T& at(U32) const;
  123. void reserve(U32);
  124. U32 capacity() const;
  125. /// @}
  126. /// @name Extended interface
  127. /// @{
  128. U32 memSize() const;
  129. T* address() const;
  130. U32 setSize(U32);
  131. void increment( U32 = 1);
  132. void increment(const T* array, U32 = 1);
  133. void decrement(U32 = 1);
  134. void insert(U32);
  135. void erase(U32);
  136. void erase_fast(U32);
  137. void erase_fast(iterator);
  138. void clear();
  139. void compact();
  140. void sort(compare_func f);
  141. T& first();
  142. T& last();
  143. const T& first() const;
  144. const T& last() const;
  145. void set(void * addr, U32 sz);
  146. void merge(const Vector& p);
  147. /// @}
  148. };
  149. template<class T> inline Vector<T>::~Vector()
  150. {
  151. dFree(mArray);
  152. }
  153. template<class T> inline Vector<T>::Vector(const U32 initialSize)
  154. {
  155. #ifdef TORQUE_DEBUG
  156. mFileAssociation = NULL;
  157. mLineAssociation = 0;
  158. #endif
  159. mArray = 0;
  160. mElementCount = 0;
  161. mArraySize = 0;
  162. if(initialSize)
  163. reserve(initialSize);
  164. }
  165. template<class T> inline Vector<T>::Vector(const U32 initialSize,
  166. const char* fileName,
  167. const U32 lineNum)
  168. {
  169. #ifdef TORQUE_DEBUG
  170. mFileAssociation = fileName;
  171. mLineAssociation = lineNum;
  172. #else
  173. TORQUE_UNUSED( fileName );
  174. TORQUE_UNUSED( lineNum );
  175. #endif
  176. mArray = 0;
  177. mElementCount = 0;
  178. mArraySize = 0;
  179. if(initialSize)
  180. reserve(initialSize);
  181. }
  182. template<class T> inline Vector<T>::Vector(const char* fileName,
  183. const U32 lineNum)
  184. {
  185. #ifdef TORQUE_DEBUG
  186. mFileAssociation = fileName;
  187. mLineAssociation = lineNum;
  188. #else
  189. TORQUE_UNUSED( fileName );
  190. TORQUE_UNUSED( lineNum );
  191. #endif
  192. mArray = 0;
  193. mElementCount = 0;
  194. mArraySize = 0;
  195. }
  196. template<class T> inline Vector<T>::Vector(const Vector& p)
  197. {
  198. #ifdef TORQUE_DEBUG
  199. mFileAssociation = p.mFileAssociation;
  200. mLineAssociation = p.mLineAssociation;
  201. #endif
  202. mArray = 0;
  203. resize(p.mElementCount);
  204. if (p.mElementCount)
  205. dMemcpy(mArray,p.mArray,mElementCount * sizeof(value_type));
  206. }
  207. #ifdef TORQUE_DEBUG
  208. template<class T> inline void Vector<T>::setFileAssociation(const char* file,
  209. const U32 line)
  210. {
  211. mFileAssociation = file;
  212. mLineAssociation = line;
  213. }
  214. #endif
  215. template<class T> inline void Vector<T>::destroy(U32 start, U32 end) // destroys from start to end-1
  216. {
  217. // This check is a little generous as we can legitimately get (0,0) as
  218. // our parameters... so it won't detect every invalid case but it does
  219. // remain simple.
  220. AssertFatal(start <= mElementCount && end <= mElementCount, "Vector<T>::destroy - out of bounds start/end.");
  221. // destroys from start to end-1
  222. while(start < end)
  223. destructInPlace(&mArray[start++]);
  224. }
  225. template<class T> inline void Vector<T>::construct(U32 start, U32 end) // destroys from start to end-1
  226. {
  227. AssertFatal(start <= mElementCount && end <= mElementCount, "Vector<T>::construct - out of bounds start/end.");
  228. while(start < end)
  229. constructInPlace(&mArray[start++]);
  230. }
  231. template<class T> inline void Vector<T>::construct(U32 start, U32 end, const T* array) // destroys from start to end-1
  232. {
  233. AssertFatal(start <= mElementCount && end <= mElementCount, "Vector<T>::construct - out of bounds start/end.");
  234. for (int i = 0; start + i < end; i++) {
  235. constructInPlace(&mArray[start+i], &array[i]);
  236. }
  237. }
  238. template<class T> inline U32 Vector<T>::memSize() const
  239. {
  240. return capacity() * sizeof(T);
  241. }
  242. template<class T> inline T* Vector<T>::address() const
  243. {
  244. return mArray;
  245. }
  246. template<class T> inline U32 Vector<T>::setSize(U32 size)
  247. {
  248. if (size > mArraySize)
  249. resize(size);
  250. else
  251. mElementCount = size;
  252. return mElementCount;
  253. }
  254. template<class T> inline void Vector<T>::increment( U32 delta)
  255. {
  256. U32 count = mElementCount;
  257. if ((mElementCount += delta) > mArraySize)
  258. resize(mElementCount);
  259. construct(count, mElementCount);
  260. }
  261. template<class T> inline void Vector<T>::increment(const T* array, U32 delta)
  262. {
  263. U32 count = mElementCount;
  264. if ((mElementCount += delta) > mArraySize)
  265. resize(mElementCount);
  266. construct(count, mElementCount, array);
  267. }
  268. template<class T> inline void Vector<T>::decrement(U32 delta)
  269. {
  270. AssertFatal(mElementCount != 0, "Vector<T>::decrement - cannot decrement zero-length vector.");
  271. const U32 count = mElementCount;
  272. // Determine new count after decrement...
  273. U32 newCount = mElementCount;
  274. if (mElementCount > delta)
  275. newCount -= delta;
  276. else
  277. newCount = 0;
  278. // Destruct removed items...
  279. destroy(newCount, count);
  280. // Note new element count.
  281. mElementCount = newCount;
  282. }
  283. template<class T> inline void Vector<T>::insert(U32 index)
  284. {
  285. // Assert: index >= 0 && index < mElementCount
  286. increment();
  287. dMemmove(&mArray[index + 1],
  288. &mArray[index],
  289. (mElementCount - index - 1) * sizeof(value_type));
  290. }
  291. template<class T> inline void Vector<T>::erase(U32 index)
  292. {
  293. AssertFatal(index < mElementCount, "Vector<T>::erase - out of bounds index!");
  294. if (index < (mElementCount - 1))
  295. {
  296. dMemmove(&mArray[index],
  297. &mArray[index + 1],
  298. (mElementCount - index - 1) * sizeof(value_type));
  299. }
  300. mElementCount--;
  301. }
  302. template<class T> inline void Vector<T>::erase_fast(U32 index)
  303. {
  304. AssertFatal(index < mElementCount, "Vector<T>::erase_fast - out of bounds index.");
  305. // CAUTION: this operator does NOT maintain list order
  306. // Copy the last element into the deleted 'hole' and decrement the
  307. // size of the vector.
  308. // Assert: index >= 0 && index < mElementCount
  309. if (index < (mElementCount - 1))
  310. dMemmove(&mArray[index], &mArray[mElementCount - 1], sizeof(value_type));
  311. decrement();
  312. }
  313. template<class T> inline T& Vector<T>::first()
  314. {
  315. AssertFatal(mElementCount != 0, "Vector<T>::first - Error, no first element of a zero sized array!");
  316. return mArray[0];
  317. }
  318. template<class T> inline const T& Vector<T>::first() const
  319. {
  320. AssertFatal(mElementCount != 0, "Vector<T>::first - Error, no first element of a zero sized array! (const)");
  321. return mArray[0];
  322. }
  323. template<class T> inline T& Vector<T>::last()
  324. {
  325. AssertFatal(mElementCount != 0, "Vector<T>::last - Error, no last element of a zero sized array!");
  326. return mArray[mElementCount - 1];
  327. }
  328. template<class T> inline const T& Vector<T>::last() const
  329. {
  330. AssertFatal(mElementCount != 0, "Vector<T>::last - Error, no last element of a zero sized array! (const)");
  331. return mArray[mElementCount - 1];
  332. }
  333. template<class T> inline void Vector<T>::clear()
  334. {
  335. mElementCount = 0;
  336. }
  337. template<class T> inline void Vector<T>::compact()
  338. {
  339. resize(mElementCount);
  340. }
  341. typedef int (QSORT_CALLBACK *qsort_compare_func)(const void *, const void *);
  342. template<class T> inline void Vector<T>::sort(compare_func f)
  343. {
  344. qsort(address(), size(), sizeof(T), (qsort_compare_func) f);
  345. }
  346. //-----------------------------------------------------------------------------
  347. template<class T> inline Vector<T>& Vector<T>::operator=(const Vector<T>& p)
  348. {
  349. resize(p.mElementCount);
  350. if (p.mElementCount)
  351. dMemcpy(mArray,p.mArray,mElementCount * sizeof(value_type));
  352. return *this;
  353. }
  354. template<class T> inline typename Vector<T>::iterator Vector<T>::begin()
  355. {
  356. return mArray;
  357. }
  358. template<class T> inline typename Vector<T>::const_iterator Vector<T>::begin() const
  359. {
  360. return mArray;
  361. }
  362. template<class T> inline typename Vector<T>::iterator Vector<T>::end()
  363. {
  364. return mArray + mElementCount;
  365. }
  366. template<class T> inline typename Vector<T>::const_iterator Vector<T>::end() const
  367. {
  368. return mArray + mElementCount;
  369. }
  370. template<class T> inline S32 Vector<T>::size() const
  371. {
  372. return (S32)mElementCount;
  373. }
  374. template<class T> inline bool Vector<T>::empty() const
  375. {
  376. return (mElementCount == 0);
  377. }
  378. template<class T> inline bool Vector<T>::contains(const T& t) const
  379. {
  380. return find_next(t) != -1;
  381. }
  382. template<class T> inline void Vector<T>::insert(iterator p,const T& x)
  383. {
  384. U32 index = (U32) (p - mArray);
  385. insert(index);
  386. mArray[index] = x;
  387. }
  388. template<class T> inline void Vector<T>::erase(iterator q)
  389. {
  390. erase(U32(q - mArray));
  391. }
  392. template<class T> inline void Vector<T>::erase_fast(iterator q)
  393. {
  394. erase_fast(U32(q - mArray));
  395. }
  396. template<class T> inline T& Vector<T>::front()
  397. {
  398. return *begin();
  399. }
  400. template<class T> inline const T& Vector<T>::front() const
  401. {
  402. return *begin();
  403. }
  404. template<class T> inline T& Vector<T>::back()
  405. {
  406. AssertFatal(mElementCount != 0, "Vector<T>::end - Error, no end element of a zero sized array!");
  407. return *(end()-1);
  408. }
  409. template<class T> inline const T& Vector<T>::back() const
  410. {
  411. AssertFatal(mElementCount != 0, "Vector<T>::end - Error, no end element of a zero sized array! (const)");
  412. return *(end()-1);
  413. }
  414. template<class T> inline void Vector<T>::push_front(const T& x)
  415. {
  416. insert(0);
  417. mArray[0] = x;
  418. }
  419. template<class T> inline void Vector<T>::push_back(const T& x)
  420. {
  421. increment(&x);
  422. // mArray[mElementCount - 1] = x;
  423. }
  424. template<class T> inline U32 Vector<T>::push_front_unique(const T& x)
  425. {
  426. S32 index = find_next(x);
  427. if (index == -1)
  428. {
  429. index = 0;
  430. insert(index);
  431. mArray[index] = x;
  432. }
  433. return index;
  434. }
  435. template<class T> inline U32 Vector<T>::push_back_unique(const T& x)
  436. {
  437. S32 index = find_next(x);
  438. if (index == -1)
  439. {
  440. increment(&x);
  441. index = mElementCount - 1;
  442. }
  443. return index;
  444. }
  445. template<class T> inline S32 Vector<T>::find_next( const T& x, U32 start ) const
  446. {
  447. S32 index = -1;
  448. if (start < mElementCount)
  449. {
  450. for (U32 i = start; i < mElementCount; i++)
  451. {
  452. if (mArray[i] == x)
  453. {
  454. index = i;
  455. break;
  456. }
  457. }
  458. }
  459. return index;
  460. }
  461. template<class T> inline void Vector<T>::pop_front()
  462. {
  463. AssertFatal(mElementCount != 0, "Vector<T>::pop_front - cannot pop the front of a zero-length vector.");
  464. erase(U32(0));
  465. }
  466. template<class T> inline void Vector<T>::pop_back()
  467. {
  468. AssertFatal(mElementCount != 0, "Vector<T>::pop_back - cannot pop the back of a zero-length vector.");
  469. decrement();
  470. }
  471. template<class T> inline T& Vector<T>::operator[](U32 index)
  472. {
  473. AssertFatal(index < mElementCount, "Vector<T>::operator[] - out of bounds array access!");
  474. return mArray[index];
  475. }
  476. template<class T> inline const T& Vector<T>::operator[](U32 index) const
  477. {
  478. AssertFatal(index < mElementCount, "Vector<T>::operator[] - out of bounds array access!");
  479. return mArray[index];
  480. }
  481. template<class T> inline T& Vector<T>::at(U32 index)
  482. {
  483. AssertFatal(index < mElementCount, "Vector<T>::at - out of bounds array access!");
  484. return mArray[index];
  485. }
  486. template<class T> inline const T& Vector<T>::at(U32 index) const
  487. {
  488. AssertFatal(index < mElementCount, "Vector<T>::at - out of bounds array access!");
  489. return mArray[index];
  490. }
  491. template<class T> inline void Vector<T>::reserve(U32 size)
  492. {
  493. if (size <= mArraySize)
  494. return;
  495. const U32 ec = mElementCount;
  496. if (resize(size))
  497. mElementCount = ec;
  498. }
  499. template<class T> inline U32 Vector<T>::capacity() const
  500. {
  501. return mArraySize;
  502. }
  503. template<class T> inline void Vector<T>::set(void * addr, U32 sz)
  504. {
  505. setSize(sz);
  506. if (addr)
  507. dMemcpy(address(),addr,sz*sizeof(T));
  508. }
  509. //-----------------------------------------------------------------------------
  510. template<class T> inline bool Vector<T>::resize(U32 ecount)
  511. {
  512. #ifdef TORQUE_DEBUG
  513. return VectorResize(&mArraySize, &mElementCount, (void**) &mArray, ecount, sizeof(T),
  514. mFileAssociation, mLineAssociation);
  515. #else
  516. return VectorResize(&mArraySize, &mElementCount, (void**) &mArray, ecount, sizeof(T));
  517. #endif
  518. }
  519. template<class T> inline void Vector<T>::merge(const Vector& p)
  520. {
  521. if (!p.size())
  522. return;
  523. const S32 oldsize = size();
  524. resize(oldsize + p.size());
  525. dMemcpy( &mArray[oldsize], p.address(), p.size() * sizeof(T) );
  526. }
  527. //-----------------------------------------------------------------------------
  528. /// Template for vectors of pointers.
  529. template <class T>
  530. class VectorPtr : public Vector<void *>
  531. {
  532. /// @deprecated Disallowed.
  533. VectorPtr(const VectorPtr&); // Disallowed
  534. public:
  535. VectorPtr();
  536. VectorPtr(const char* fileName, const U32 lineNum);
  537. /// @name STL interface
  538. /// @{
  539. typedef T value_type;
  540. typedef T& reference;
  541. typedef const T& const_reference;
  542. typedef T* iterator;
  543. typedef const T* const_iterator;
  544. typedef U32 difference_type;
  545. typedef U32 size_type;
  546. iterator begin();
  547. const_iterator begin() const;
  548. iterator end();
  549. const_iterator end() const;
  550. void insert(iterator,const T&);
  551. void insert(int idx) { Parent::insert(idx); }
  552. void erase(iterator);
  553. T& front();
  554. const T& front() const;
  555. T& back();
  556. const T& back() const;
  557. void push_front(const T&);
  558. void push_back(const T&);
  559. T& operator[](U32);
  560. const T& operator[](U32) const;
  561. /// @}
  562. /// @name Extended interface
  563. /// @{
  564. typedef Vector<void*> Parent;
  565. T& first();
  566. T& last();
  567. const T& first() const;
  568. const T& last() const;
  569. void erase_fast(U32);
  570. void erase_fast(iterator);
  571. /// @}
  572. };
  573. //-----------------------------------------------------------------------------
  574. template<class T> inline VectorPtr<T>::VectorPtr()
  575. {
  576. //
  577. }
  578. template<class T> inline VectorPtr<T>::VectorPtr(const char* fileName,
  579. const U32 lineNum)
  580. : Vector<void*>(fileName, lineNum)
  581. {
  582. //
  583. }
  584. template<class T> inline T& VectorPtr<T>::first()
  585. {
  586. return (T&)Parent::first();
  587. }
  588. template<class T> inline const T& VectorPtr<T>::first() const
  589. {
  590. return (const T)Parent::first();
  591. }
  592. template<class T> inline T& VectorPtr<T>::last()
  593. {
  594. return (T&)Parent::last();
  595. }
  596. template<class T> inline const T& VectorPtr<T>::last() const
  597. {
  598. return (const T&)Parent::last();
  599. }
  600. template<class T> inline typename VectorPtr<T>::iterator VectorPtr<T>::begin()
  601. {
  602. return (iterator)Parent::begin();
  603. }
  604. template<class T> inline typename VectorPtr<T>::const_iterator VectorPtr<T>::begin() const
  605. {
  606. return (const_iterator)Parent::begin();
  607. }
  608. template<class T> inline typename VectorPtr<T>::iterator VectorPtr<T>::end()
  609. {
  610. return (iterator)Parent::end();
  611. }
  612. template<class T> inline typename VectorPtr<T>::const_iterator VectorPtr<T>::end() const
  613. {
  614. return (const_iterator)Parent::end();
  615. }
  616. template<class T> inline void VectorPtr<T>::insert(iterator i,const T& x)
  617. {
  618. Parent::insert( (Parent::iterator)i, (Parent::reference)x );
  619. }
  620. template<class T> inline void VectorPtr<T>::erase(iterator i)
  621. {
  622. Parent::erase( (Parent::iterator)i );
  623. }
  624. template<class T> inline void VectorPtr<T>::erase_fast(U32 index)
  625. {
  626. AssertFatal(index < mElementCount, "VectorPtr<T>::erase_fast - out of bounds index." );
  627. // CAUTION: this operator does not maintain list order
  628. // Copy the last element into the deleted 'hole' and decrement the
  629. // size of the vector.
  630. // Assert: index >= 0 && index < mElementCount
  631. if (index < (mElementCount - 1))
  632. mArray[index] = mArray[mElementCount - 1];
  633. decrement();
  634. }
  635. template<class T> inline void VectorPtr<T>::erase_fast(iterator i)
  636. {
  637. erase_fast(U32(i - iterator(mArray)));
  638. }
  639. template<class T> inline T& VectorPtr<T>::front()
  640. {
  641. return *begin();
  642. }
  643. template<class T> inline const T& VectorPtr<T>::front() const
  644. {
  645. return *begin();
  646. }
  647. template<class T> inline T& VectorPtr<T>::back()
  648. {
  649. return *end();
  650. }
  651. template<class T> inline const T& VectorPtr<T>::back() const
  652. {
  653. return *end();
  654. }
  655. template<class T> inline void VectorPtr<T>::push_front(const T& x)
  656. {
  657. Parent::push_front((Parent::const_reference)x);
  658. }
  659. template<class T> inline void VectorPtr<T>::push_back(const T& x)
  660. {
  661. Parent::push_back((Parent::const_reference)x);
  662. }
  663. template<class T> inline T& VectorPtr<T>::operator[](U32 index)
  664. {
  665. return (T&)Parent::operator[](index);
  666. }
  667. template<class T> inline const T& VectorPtr<T>::operator[](U32 index) const
  668. {
  669. return (const T&)Parent::operator[](index);
  670. }
  671. #endif //_VECTOR_H_