Vector.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/VectorBase.h"
  24. #include <cassert>
  25. #include <cstring>
  26. #include <new>
  27. #if ATOMIC_CXX11
  28. #include <initializer_list>
  29. #endif
  30. #ifdef _MSC_VER
  31. #pragma warning(push)
  32. #pragma warning(disable:6293)
  33. #endif
  34. namespace Atomic
  35. {
  36. /// %Vector template class.
  37. template <class T> class Vector : public VectorBase
  38. {
  39. public:
  40. typedef T ValueType;
  41. typedef RandomAccessIterator<T> Iterator;
  42. typedef RandomAccessConstIterator<T> ConstIterator;
  43. /// Construct empty.
  44. Vector()
  45. {
  46. }
  47. /// Construct with initial size.
  48. explicit Vector(unsigned size)
  49. {
  50. Resize(size);
  51. }
  52. /// Construct with initial size and default value.
  53. Vector(unsigned size, const T& value)
  54. {
  55. Resize(size);
  56. for (unsigned i = 0; i < size; ++i)
  57. At(i) = value;
  58. }
  59. /// Construct with initial data.
  60. Vector(const T* data, unsigned size)
  61. {
  62. InsertElements(0, data, data + size);
  63. }
  64. /// Construct from another vector.
  65. Vector(const Vector<T>& vector)
  66. {
  67. *this = vector;
  68. }
  69. #if ATOMIC_CXX11
  70. /// Aggregate initialization constructor.
  71. Vector(const std::initializer_list<T>& list) : Vector()
  72. {
  73. for (auto it = list.begin(); it != list.end(); it++)
  74. {
  75. Push(*it);
  76. }
  77. }
  78. #endif
  79. /// Destruct.
  80. ~Vector()
  81. {
  82. DestructElements(Buffer(), size_);
  83. delete[] buffer_;
  84. }
  85. /// Assign from another vector.
  86. Vector<T>& operator =(const Vector<T>& rhs)
  87. {
  88. // In case of self-assignment do nothing
  89. if (&rhs != this)
  90. {
  91. Clear();
  92. InsertElements(0, rhs.Begin(), rhs.End());
  93. }
  94. return *this;
  95. }
  96. /// Add-assign an element.
  97. Vector<T>& operator +=(const T& rhs)
  98. {
  99. Push(rhs);
  100. return *this;
  101. }
  102. /// Add-assign another vector.
  103. Vector<T>& operator +=(const Vector<T>& rhs)
  104. {
  105. Push(rhs);
  106. return *this;
  107. }
  108. /// Add an element.
  109. Vector<T> operator +(const T& rhs) const
  110. {
  111. Vector<T> ret(*this);
  112. ret.Push(rhs);
  113. return ret;
  114. }
  115. /// Add another vector.
  116. Vector<T> operator +(const Vector<T>& rhs) const
  117. {
  118. Vector<T> ret(*this);
  119. ret.Push(rhs);
  120. return ret;
  121. }
  122. /// Test for equality with another vector.
  123. bool operator ==(const Vector<T>& rhs) const
  124. {
  125. if (rhs.size_ != size_)
  126. return false;
  127. T* buffer = Buffer();
  128. T* rhsBuffer = rhs.Buffer();
  129. for (unsigned i = 0; i < size_; ++i)
  130. {
  131. if (buffer[i] != rhsBuffer[i])
  132. return false;
  133. }
  134. return true;
  135. }
  136. /// Test for inequality with another vector.
  137. bool operator !=(const Vector<T>& rhs) const
  138. {
  139. if (rhs.size_ != size_)
  140. return true;
  141. T* buffer = Buffer();
  142. T* rhsBuffer = rhs.Buffer();
  143. for (unsigned i = 0; i < size_; ++i)
  144. {
  145. if (buffer[i] != rhsBuffer[i])
  146. return true;
  147. }
  148. return false;
  149. }
  150. /// Return element at index.
  151. T& operator [](unsigned index)
  152. {
  153. assert(index < size_);
  154. return Buffer()[index];
  155. }
  156. /// Return const element at index.
  157. const T& operator [](unsigned index) const
  158. {
  159. assert(index < size_);
  160. return Buffer()[index];
  161. }
  162. /// Return element at index.
  163. T& At(unsigned index)
  164. {
  165. assert(index < size_);
  166. return Buffer()[index];
  167. }
  168. /// Return const element at index.
  169. const T& At(unsigned index) const
  170. {
  171. assert(index < size_);
  172. return Buffer()[index];
  173. }
  174. /// Add an element at the end.
  175. #ifndef COVERITY_SCAN_MODEL
  176. void Push(const T& value)
  177. {
  178. InsertElements(size_, &value, &value + 1);
  179. }
  180. #else
  181. // FIXME: Attempt had been made to use this model in the Coverity-Scan model file without any success
  182. // Probably because the model had generated a different mangled name than the one used by static analyzer
  183. void Push(const T& value)
  184. {
  185. T array[] = {value};
  186. InsertElements(size_, array, array + 1);
  187. }
  188. #endif
  189. /// Add another vector at the end.
  190. void Push(const Vector<T>& vector) { InsertElements(size_, vector.Begin(), vector.End()); }
  191. /// Remove the last element.
  192. void Pop()
  193. {
  194. if (size_)
  195. Resize(size_ - 1);
  196. }
  197. /// Insert an element at position.
  198. void Insert(unsigned pos, const T& value)
  199. {
  200. InsertElements(pos, &value, &value + 1);
  201. }
  202. /// Insert another vector at position.
  203. void Insert(unsigned pos, const Vector<T>& vector)
  204. {
  205. InsertElements(pos, vector.Begin(), vector.End());
  206. }
  207. /// Insert an element by iterator.
  208. Iterator Insert(const Iterator& dest, const T& value)
  209. {
  210. unsigned pos = (unsigned)(dest - Begin());
  211. return InsertElements(pos, &value, &value + 1);
  212. }
  213. /// Insert a vector by iterator.
  214. Iterator Insert(const Iterator& dest, const Vector<T>& vector)
  215. {
  216. unsigned pos = (unsigned)(dest - Begin());
  217. return InsertElements(pos, vector.Begin(), vector.End());
  218. }
  219. /// Insert a vector partially by iterators.
  220. Iterator Insert(const Iterator& dest, const ConstIterator& start, const ConstIterator& end)
  221. {
  222. unsigned pos = (unsigned)(dest - Begin());
  223. return InsertElements(pos, start, end);
  224. }
  225. /// Insert elements.
  226. Iterator Insert(const Iterator& dest, const T* start, const T* end)
  227. {
  228. unsigned pos = (unsigned)(dest - Begin());
  229. return InsertElements(pos, start, end);
  230. }
  231. /// Erase a range of elements.
  232. void Erase(unsigned pos, unsigned length = 1)
  233. {
  234. // Return if the range is illegal
  235. if (pos + length > size_ || !length)
  236. return;
  237. MoveRange(pos, pos + length, size_ - pos - length);
  238. Resize(size_ - length);
  239. }
  240. /// Erase a range of elements by swapping elements from the end of the array.
  241. void EraseSwap(unsigned pos, unsigned length = 1)
  242. {
  243. unsigned shiftStartIndex = pos + length;
  244. // Return if the range is illegal
  245. if (shiftStartIndex > size_ || !length)
  246. return;
  247. unsigned newSize = size_ - length;
  248. unsigned trailingCount = size_ - shiftStartIndex;
  249. if (trailingCount <= length)
  250. {
  251. // We're removing more elements from the array than exist past the end of the range being removed, so perform a normal shift and destroy
  252. MoveRange(pos, shiftStartIndex, trailingCount);
  253. }
  254. else
  255. {
  256. // Swap elements from the end of the array into the empty space
  257. CopyElements(Buffer() + pos, Buffer() + newSize, length);
  258. }
  259. Resize(newSize);
  260. }
  261. /// Erase an element by iterator. Return iterator to the next element.
  262. Iterator Erase(const Iterator& it)
  263. {
  264. unsigned pos = (unsigned)(it - Begin());
  265. if (pos >= size_)
  266. return End();
  267. Erase(pos);
  268. return Begin() + pos;
  269. }
  270. /// Erase a range by iterators. Return iterator to the next element.
  271. Iterator Erase(const Iterator& start, const Iterator& end)
  272. {
  273. unsigned pos = (unsigned)(start - Begin());
  274. if (pos >= size_)
  275. return End();
  276. unsigned length = (unsigned)(end - start);
  277. Erase(pos, length);
  278. return Begin() + pos;
  279. }
  280. /// Erase an element by value. Return true if was found and erased.
  281. bool Remove(const T& value)
  282. {
  283. Iterator i = Find(value);
  284. if (i != End())
  285. {
  286. Erase(i);
  287. return true;
  288. }
  289. else
  290. return false;
  291. }
  292. /// Erase an element by value by swapping with the last element. Return true if was found and erased.
  293. bool RemoveSwap(const T& value)
  294. {
  295. Iterator i = Find(value);
  296. if (i != End())
  297. {
  298. EraseSwap(i - Begin());
  299. return true;
  300. }
  301. else
  302. return false;
  303. }
  304. /// Clear the vector.
  305. void Clear() { Resize(0); }
  306. /// Resize the vector.
  307. void Resize(unsigned newSize) { Vector<T> tempBuffer; Resize(newSize, 0, tempBuffer); }
  308. /// Resize the vector and fill new elements with default value.
  309. void Resize(unsigned newSize, const T& value)
  310. {
  311. unsigned oldSize = Size();
  312. Vector<T> tempBuffer;
  313. Resize(newSize, 0, tempBuffer);
  314. for (unsigned i = oldSize; i < newSize; ++i)
  315. At(i) = value;
  316. }
  317. /// Set new capacity.
  318. void Reserve(unsigned newCapacity)
  319. {
  320. if (newCapacity < size_)
  321. newCapacity = size_;
  322. if (newCapacity != capacity_)
  323. {
  324. T* newBuffer = 0;
  325. capacity_ = newCapacity;
  326. if (capacity_)
  327. {
  328. newBuffer = reinterpret_cast<T*>(AllocateBuffer((unsigned)(capacity_ * sizeof(T))));
  329. // Move the data into the new buffer
  330. ConstructElements(newBuffer, Buffer(), size_);
  331. }
  332. // Delete the old buffer
  333. DestructElements(Buffer(), size_);
  334. delete[] buffer_;
  335. buffer_ = reinterpret_cast<unsigned char*>(newBuffer);
  336. }
  337. }
  338. /// Reallocate so that no extra memory is used.
  339. void Compact() { Reserve(size_); }
  340. /// Return iterator to value, or to the end if not found.
  341. Iterator Find(const T& value)
  342. {
  343. Iterator it = Begin();
  344. while (it != End() && *it != value)
  345. ++it;
  346. return it;
  347. }
  348. /// Return const iterator to value, or to the end if not found.
  349. ConstIterator Find(const T& value) const
  350. {
  351. ConstIterator it = Begin();
  352. while (it != End() && *it != value)
  353. ++it;
  354. return it;
  355. }
  356. /// Return index of value in vector, or size if not found.
  357. unsigned IndexOf(const T& value) const
  358. {
  359. return Find(value) - Begin();
  360. }
  361. /// Return whether contains a specific value.
  362. bool Contains(const T& value) const { return Find(value) != End(); }
  363. /// Return iterator to the beginning.
  364. Iterator Begin() { return Iterator(Buffer()); }
  365. /// Return const iterator to the beginning.
  366. ConstIterator Begin() const { return ConstIterator(Buffer()); }
  367. /// Return iterator to the end.
  368. Iterator End() { return Iterator(Buffer() + size_); }
  369. /// Return const iterator to the end.
  370. ConstIterator End() const { return ConstIterator(Buffer() + size_); }
  371. /// Return first element.
  372. T& Front()
  373. {
  374. assert(size_);
  375. return Buffer()[0];
  376. }
  377. /// Return const first element.
  378. const T& Front() const
  379. {
  380. assert(size_);
  381. return Buffer()[0];
  382. }
  383. /// Return last element.
  384. T& Back()
  385. {
  386. assert(size_);
  387. return Buffer()[size_ - 1];
  388. }
  389. /// Return const last element.
  390. const T& Back() const
  391. {
  392. assert(size_);
  393. return Buffer()[size_ - 1];
  394. }
  395. /// Return size of vector.
  396. unsigned Size() const { return size_; }
  397. /// Return capacity of vector.
  398. unsigned Capacity() const { return capacity_; }
  399. /// Return whether vector is empty.
  400. bool Empty() const { return size_ == 0; }
  401. /// Return the buffer with right type.
  402. T* Buffer() const { return reinterpret_cast<T*>(buffer_); }
  403. private:
  404. /// Resize the vector and create/remove new elements as necessary. Current buffer will be stored in tempBuffer in case of reallocation.
  405. void Resize(unsigned newSize, const T* src, Vector<T>& tempBuffer)
  406. {
  407. // If size shrinks, destruct the removed elements
  408. if (newSize < size_)
  409. DestructElements(Buffer() + newSize, size_ - newSize);
  410. else
  411. {
  412. // Allocate new buffer if necessary and copy the current elements
  413. if (newSize > capacity_)
  414. {
  415. Swap(tempBuffer);
  416. size_ = tempBuffer.size_;
  417. capacity_ = tempBuffer.capacity_;
  418. if (!capacity_)
  419. capacity_ = newSize;
  420. else
  421. {
  422. while (capacity_ < newSize)
  423. capacity_ += (capacity_ + 1) >> 1;
  424. }
  425. buffer_ = AllocateBuffer((unsigned)(capacity_ * sizeof(T)));
  426. if (tempBuffer.Buffer())
  427. {
  428. ConstructElements(Buffer(), tempBuffer.Buffer(), size_);
  429. }
  430. }
  431. // Initialize the new elements
  432. ConstructElements(Buffer() + size_, src, newSize - size_);
  433. }
  434. size_ = newSize;
  435. }
  436. /// Insert elements.
  437. template <typename RandomIteratorT>
  438. Iterator InsertElements(unsigned pos, RandomIteratorT start, RandomIteratorT end)
  439. {
  440. assert(start <= end);
  441. if (pos > size_)
  442. pos = size_;
  443. unsigned length = (unsigned)(end - start);
  444. Vector<T> tempBuffer;
  445. Resize(size_ + length, 0, tempBuffer);
  446. MoveRange(pos + length, pos, size_ - pos - length);
  447. T* destPtr = Buffer() + pos;
  448. for (RandomIteratorT it = start; it != end; ++it)
  449. *destPtr++ = *it;
  450. return Begin() + pos;
  451. }
  452. /// Move a range of elements within the vector.
  453. void MoveRange(unsigned dest, unsigned src, unsigned count)
  454. {
  455. T* buffer = Buffer();
  456. if (src < dest)
  457. {
  458. for (unsigned i = count - 1; i < count; --i)
  459. buffer[dest + i] = buffer[src + i];
  460. }
  461. if (src > dest)
  462. {
  463. for (unsigned i = 0; i < count; ++i)
  464. buffer[dest + i] = buffer[src + i];
  465. }
  466. }
  467. /// Construct elements, optionally with source data.
  468. static void ConstructElements(T* dest, const T* src, unsigned count)
  469. {
  470. if (!src)
  471. {
  472. for (unsigned i = 0; i < count; ++i)
  473. new(dest + i) T();
  474. }
  475. else
  476. {
  477. for (unsigned i = 0; i < count; ++i)
  478. new(dest + i) T(*(src + i));
  479. }
  480. }
  481. /// Copy elements from one buffer to another.
  482. static void CopyElements(T* dest, const T* src, unsigned count)
  483. {
  484. while (count--)
  485. *dest++ = *src++;
  486. }
  487. /// Call the elements' destructors.
  488. static void DestructElements(T* dest, unsigned count)
  489. {
  490. while (count--)
  491. {
  492. dest->~T();
  493. ++dest;
  494. }
  495. }
  496. };
  497. /// %Vector template class for POD types. Does not call constructors or destructors and uses block move. Is intentionally (for performance reasons) unsafe for self-insertion.
  498. template <class T> class PODVector : public VectorBase
  499. {
  500. public:
  501. typedef T ValueType;
  502. typedef RandomAccessIterator<T> Iterator;
  503. typedef RandomAccessConstIterator<T> ConstIterator;
  504. /// Construct empty.
  505. PODVector()
  506. {
  507. }
  508. /// Construct with initial size.
  509. explicit PODVector(unsigned size)
  510. {
  511. Resize(size);
  512. }
  513. /// Construct with initial size and default value.
  514. PODVector(unsigned size, const T& value)
  515. {
  516. Resize(size);
  517. for (unsigned i = 0; i < size; ++i)
  518. At(i) = value;
  519. }
  520. /// Construct with initial data.
  521. PODVector(const T* data, unsigned size)
  522. {
  523. Resize(size);
  524. CopyElements(Buffer(), data, size);
  525. }
  526. /// Construct from another vector.
  527. PODVector(const PODVector<T>& vector)
  528. {
  529. *this = vector;
  530. }
  531. #if ATOMIC_CXX11
  532. /// Aggregate initialization constructor.
  533. PODVector(const std::initializer_list<T>& list) : PODVector()
  534. {
  535. for (auto it = list.begin(); it != list.end(); it++)
  536. {
  537. Push(*it);
  538. }
  539. }
  540. #endif
  541. /// Destruct.
  542. ~PODVector()
  543. {
  544. delete[] buffer_;
  545. }
  546. /// Assign from another vector.
  547. PODVector<T>& operator =(const PODVector<T>& rhs)
  548. {
  549. // In case of self-assignment do nothing
  550. if (&rhs != this)
  551. {
  552. Resize(rhs.size_);
  553. CopyElements(Buffer(), rhs.Buffer(), rhs.size_);
  554. }
  555. return *this;
  556. }
  557. /// Add-assign an element.
  558. PODVector<T>& operator +=(const T& rhs)
  559. {
  560. Push(rhs);
  561. return *this;
  562. }
  563. /// Add-assign another vector.
  564. PODVector<T>& operator +=(const PODVector<T>& rhs)
  565. {
  566. Push(rhs);
  567. return *this;
  568. }
  569. /// Add an element.
  570. PODVector<T> operator +(const T& rhs) const
  571. {
  572. PODVector<T> ret(*this);
  573. ret.Push(rhs);
  574. return ret;
  575. }
  576. /// Add another vector.
  577. PODVector<T> operator +(const PODVector<T>& rhs) const
  578. {
  579. PODVector<T> ret(*this);
  580. ret.Push(rhs);
  581. return ret;
  582. }
  583. /// Test for equality with another vector.
  584. bool operator ==(const PODVector<T>& rhs) const
  585. {
  586. if (rhs.size_ != size_)
  587. return false;
  588. T* buffer = Buffer();
  589. T* rhsBuffer = rhs.Buffer();
  590. for (unsigned i = 0; i < size_; ++i)
  591. {
  592. if (buffer[i] != rhsBuffer[i])
  593. return false;
  594. }
  595. return true;
  596. }
  597. /// Test for inequality with another vector.
  598. bool operator !=(const PODVector<T>& rhs) const
  599. {
  600. if (rhs.size_ != size_)
  601. return true;
  602. T* buffer = Buffer();
  603. T* rhsBuffer = rhs.Buffer();
  604. for (unsigned i = 0; i < size_; ++i)
  605. {
  606. if (buffer[i] != rhsBuffer[i])
  607. return true;
  608. }
  609. return false;
  610. }
  611. /// Return element at index.
  612. T& operator [](unsigned index)
  613. {
  614. assert(index < size_);
  615. return Buffer()[index];
  616. }
  617. /// Return const element at index.
  618. const T& operator [](unsigned index) const
  619. {
  620. assert(index < size_);
  621. return Buffer()[index];
  622. }
  623. /// Return element at index.
  624. T& At(unsigned index)
  625. {
  626. assert(index < size_);
  627. return Buffer()[index];
  628. }
  629. /// Return const element at index.
  630. const T& At(unsigned index) const
  631. {
  632. assert(index < size_);
  633. return Buffer()[index];
  634. }
  635. /// Add an element at the end.
  636. void Push(const T& value)
  637. {
  638. if (size_ < capacity_)
  639. ++size_;
  640. else
  641. Resize(size_ + 1);
  642. Back() = value;
  643. }
  644. /// Add another vector at the end.
  645. void Push(const PODVector<T>& vector)
  646. {
  647. unsigned oldSize = size_;
  648. Resize(size_ + vector.size_);
  649. CopyElements(Buffer() + oldSize, vector.Buffer(), vector.size_);
  650. }
  651. /// Remove the last element.
  652. void Pop()
  653. {
  654. if (size_)
  655. Resize(size_ - 1);
  656. }
  657. /// Insert an element at position.
  658. void Insert(unsigned pos, const T& value)
  659. {
  660. if (pos > size_)
  661. pos = size_;
  662. unsigned oldSize = size_;
  663. Resize(size_ + 1);
  664. MoveRange(pos + 1, pos, oldSize - pos);
  665. Buffer()[pos] = value;
  666. }
  667. /// Insert another vector at position.
  668. void Insert(unsigned pos, const PODVector<T>& vector)
  669. {
  670. if (pos > size_)
  671. pos = size_;
  672. unsigned oldSize = size_;
  673. Resize(size_ + vector.size_);
  674. MoveRange(pos + vector.size_, pos, oldSize - pos);
  675. CopyElements(Buffer() + pos, vector.Buffer(), vector.size_);
  676. }
  677. /// Insert an element by iterator.
  678. Iterator Insert(const Iterator& dest, const T& value)
  679. {
  680. unsigned pos = (unsigned)(dest - Begin());
  681. if (pos > size_)
  682. pos = size_;
  683. Insert(pos, value);
  684. return Begin() + pos;
  685. }
  686. /// Insert a vector by iterator.
  687. Iterator Insert(const Iterator& dest, const PODVector<T>& vector)
  688. {
  689. unsigned pos = (unsigned)(dest - Begin());
  690. if (pos > size_)
  691. pos = size_;
  692. Insert(pos, vector);
  693. return Begin() + pos;
  694. }
  695. /// Insert a vector partially by iterators.
  696. Iterator Insert(const Iterator& dest, const ConstIterator& start, const ConstIterator& end)
  697. {
  698. unsigned pos = (unsigned)(dest - Begin());
  699. if (pos > size_)
  700. pos = size_;
  701. unsigned length = (unsigned)(end - start);
  702. Resize(size_ + length);
  703. MoveRange(pos + length, pos, size_ - pos - length);
  704. CopyElements(Buffer() + pos, &(*start), length);
  705. return Begin() + pos;
  706. }
  707. /// Insert elements.
  708. Iterator Insert(const Iterator& dest, const T* start, const T* end)
  709. {
  710. unsigned pos = (unsigned)(dest - Begin());
  711. if (pos > size_)
  712. pos = size_;
  713. unsigned length = (unsigned)(end - start);
  714. Resize(size_ + length);
  715. MoveRange(pos + length, pos, size_ - pos - length);
  716. T* destPtr = Buffer() + pos;
  717. for (const T* i = start; i != end; ++i)
  718. *destPtr++ = *i;
  719. return Begin() + pos;
  720. }
  721. /// Erase a range of elements.
  722. void Erase(unsigned pos, unsigned length = 1)
  723. {
  724. // Return if the range is illegal
  725. if (!length || pos + length > size_)
  726. return;
  727. MoveRange(pos, pos + length, size_ - pos - length);
  728. Resize(size_ - length);
  729. }
  730. /// Erase an element by iterator. Return iterator to the next element.
  731. Iterator Erase(const Iterator& it)
  732. {
  733. unsigned pos = (unsigned)(it - Begin());
  734. if (pos >= size_)
  735. return End();
  736. Erase(pos);
  737. return Begin() + pos;
  738. }
  739. /// Erase a range by iterators. Return iterator to the next element.
  740. Iterator Erase(const Iterator& start, const Iterator& end)
  741. {
  742. unsigned pos = (unsigned)(start - Begin());
  743. if (pos >= size_)
  744. return End();
  745. unsigned length = (unsigned)(end - start);
  746. Erase(pos, length);
  747. return Begin() + pos;
  748. }
  749. /// Erase a range of elements by swapping elements from the end of the array.
  750. void EraseSwap(unsigned pos, unsigned length = 1)
  751. {
  752. unsigned shiftStartIndex = pos + length;
  753. // Return if the range is illegal
  754. if (shiftStartIndex > size_ || !length)
  755. return;
  756. unsigned newSize = size_ - length;
  757. unsigned trailingCount = size_ - shiftStartIndex;
  758. if (trailingCount <= length)
  759. {
  760. // We're removing more elements from the array than exist past the end of the range being removed, so perform a normal shift and destroy
  761. MoveRange(pos, shiftStartIndex, trailingCount);
  762. }
  763. else
  764. {
  765. // Swap elements from the end of the array into the empty space
  766. CopyElements(Buffer() + pos, Buffer() + newSize, length);
  767. }
  768. Resize(newSize);
  769. }
  770. /// Erase an element by value. Return true if was found and erased.
  771. bool Remove(const T& value)
  772. {
  773. Iterator i = Find(value);
  774. if (i != End())
  775. {
  776. Erase(i);
  777. return true;
  778. }
  779. else
  780. return false;
  781. }
  782. /// Erase an element by value by swapping with the last element. Return true if was found and erased.
  783. bool RemoveSwap(const T& value)
  784. {
  785. Iterator i = Find(value);
  786. if (i != End())
  787. {
  788. EraseSwap(i - Begin());
  789. return true;
  790. }
  791. else
  792. return false;
  793. }
  794. /// Clear the vector.
  795. void Clear() { Resize(0); }
  796. /// Resize the vector.
  797. void Resize(unsigned newSize)
  798. {
  799. if (newSize > capacity_)
  800. {
  801. if (!capacity_)
  802. capacity_ = newSize;
  803. else
  804. {
  805. while (capacity_ < newSize)
  806. capacity_ += (capacity_ + 1) >> 1;
  807. }
  808. unsigned char* newBuffer = AllocateBuffer((unsigned)(capacity_ * sizeof(T)));
  809. // Move the data into the new buffer and delete the old
  810. if (buffer_)
  811. {
  812. CopyElements(reinterpret_cast<T*>(newBuffer), Buffer(), size_);
  813. delete[] buffer_;
  814. }
  815. buffer_ = newBuffer;
  816. }
  817. size_ = newSize;
  818. }
  819. /// Set new capacity.
  820. void Reserve(unsigned newCapacity)
  821. {
  822. if (newCapacity < size_)
  823. newCapacity = size_;
  824. if (newCapacity != capacity_)
  825. {
  826. unsigned char* newBuffer = 0;
  827. capacity_ = newCapacity;
  828. if (capacity_)
  829. {
  830. newBuffer = AllocateBuffer((unsigned)(capacity_ * sizeof(T)));
  831. // Move the data into the new buffer
  832. CopyElements(reinterpret_cast<T*>(newBuffer), Buffer(), size_);
  833. }
  834. // Delete the old buffer
  835. delete[] buffer_;
  836. buffer_ = newBuffer;
  837. }
  838. }
  839. /// Reallocate so that no extra memory is used.
  840. void Compact() { Reserve(size_); }
  841. /// Return iterator to value, or to the end if not found.
  842. Iterator Find(const T& value)
  843. {
  844. Iterator it = Begin();
  845. while (it != End() && *it != value)
  846. ++it;
  847. return it;
  848. }
  849. /// Return const iterator to value, or to the end if not found.
  850. ConstIterator Find(const T& value) const
  851. {
  852. ConstIterator it = Begin();
  853. while (it != End() && *it != value)
  854. ++it;
  855. return it;
  856. }
  857. /// Return index of value in vector, or size if not found.
  858. unsigned IndexOf(const T& value) const
  859. {
  860. return Find(value) - Begin();
  861. }
  862. /// Return whether contains a specific value.
  863. bool Contains(const T& value) const { return Find(value) != End(); }
  864. /// Return iterator to the beginning.
  865. Iterator Begin() { return Iterator(Buffer()); }
  866. /// Return const iterator to the beginning.
  867. ConstIterator Begin() const { return ConstIterator(Buffer()); }
  868. /// Return iterator to the end.
  869. Iterator End() { return Iterator(Buffer() + size_); }
  870. /// Return const iterator to the end.
  871. ConstIterator End() const { return ConstIterator(Buffer() + size_); }
  872. /// Return first element.
  873. T& Front() { return Buffer()[0]; }
  874. /// Return const first element.
  875. const T& Front() const { return Buffer()[0]; }
  876. /// Return last element.
  877. T& Back()
  878. {
  879. assert(size_);
  880. return Buffer()[size_ - 1];
  881. }
  882. /// Return const last element.
  883. const T& Back() const
  884. {
  885. assert(size_);
  886. return Buffer()[size_ - 1];
  887. }
  888. /// Return number of elements.
  889. unsigned Size() const { return size_; }
  890. /// Return capacity of vector.
  891. unsigned Capacity() const { return capacity_; }
  892. /// Return whether vector is empty.
  893. bool Empty() const { return size_ == 0; }
  894. /// Return the buffer with right type.
  895. T* Buffer() const { return reinterpret_cast<T*>(buffer_); }
  896. private:
  897. /// Move a range of elements within the vector.
  898. void MoveRange(unsigned dest, unsigned src, unsigned count)
  899. {
  900. if (count)
  901. memmove(Buffer() + dest, Buffer() + src, count * sizeof(T));
  902. }
  903. /// Copy elements from one buffer to another.
  904. static void CopyElements(T* dest, const T* src, unsigned count)
  905. {
  906. if (count)
  907. memcpy(dest, src, count * sizeof(T));
  908. }
  909. };
  910. template <class T> typename Atomic::Vector<T>::ConstIterator begin(const Atomic::Vector<T>& v) { return v.Begin(); }
  911. template <class T> typename Atomic::Vector<T>::ConstIterator end(const Atomic::Vector<T>& v) { return v.End(); }
  912. template <class T> typename Atomic::Vector<T>::Iterator begin(Atomic::Vector<T>& v) { return v.Begin(); }
  913. template <class T> typename Atomic::Vector<T>::Iterator end(Atomic::Vector<T>& v) { return v.End(); }
  914. template <class T> typename Atomic::PODVector<T>::ConstIterator begin(const Atomic::PODVector<T>& v) { return v.Begin(); }
  915. template <class T> typename Atomic::PODVector<T>::ConstIterator end(const Atomic::PODVector<T>& v) { return v.End(); }
  916. template <class T> typename Atomic::PODVector<T>::Iterator begin(Atomic::PODVector<T>& v) { return v.Begin(); }
  917. template <class T> typename Atomic::PODVector<T>::Iterator end(Atomic::PODVector<T>& v) { return v.End(); }
  918. }
  919. #ifdef _MSC_VER
  920. #pragma warning(pop)
  921. #endif