Vector.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. //
  2. // Copyright (c) 2008-2016 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);
  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. /// Set new capacity.
  309. void Reserve(unsigned newCapacity)
  310. {
  311. if (newCapacity < size_)
  312. newCapacity = size_;
  313. if (newCapacity != capacity_)
  314. {
  315. T* newBuffer = 0;
  316. capacity_ = newCapacity;
  317. if (capacity_)
  318. {
  319. newBuffer = reinterpret_cast<T*>(AllocateBuffer((unsigned)(capacity_ * sizeof(T))));
  320. // Move the data into the new buffer
  321. ConstructElements(newBuffer, Buffer(), size_);
  322. }
  323. // Delete the old buffer
  324. DestructElements(Buffer(), size_);
  325. delete[] buffer_;
  326. buffer_ = reinterpret_cast<unsigned char*>(newBuffer);
  327. }
  328. }
  329. /// Reallocate so that no extra memory is used.
  330. void Compact() { Reserve(size_); }
  331. /// Return iterator to value, or to the end if not found.
  332. Iterator Find(const T& value)
  333. {
  334. Iterator it = Begin();
  335. while (it != End() && *it != value)
  336. ++it;
  337. return it;
  338. }
  339. /// Return const iterator to value, or to the end if not found.
  340. ConstIterator Find(const T& value) const
  341. {
  342. ConstIterator it = Begin();
  343. while (it != End() && *it != value)
  344. ++it;
  345. return it;
  346. }
  347. /// Return whether contains a specific value.
  348. bool Contains(const T& value) const { return Find(value) != End(); }
  349. /// Return iterator to the beginning.
  350. Iterator Begin() { return Iterator(Buffer()); }
  351. /// Return const iterator to the beginning.
  352. ConstIterator Begin() const { return ConstIterator(Buffer()); }
  353. /// Return iterator to the end.
  354. Iterator End() { return Iterator(Buffer() + size_); }
  355. /// Return const iterator to the end.
  356. ConstIterator End() const { return ConstIterator(Buffer() + size_); }
  357. /// Return first element.
  358. T& Front()
  359. {
  360. assert(size_);
  361. return Buffer()[0];
  362. }
  363. /// Return const first element.
  364. const T& Front() const
  365. {
  366. assert(size_);
  367. return Buffer()[0];
  368. }
  369. /// Return last element.
  370. T& Back()
  371. {
  372. assert(size_);
  373. return Buffer()[size_ - 1];
  374. }
  375. /// Return const last element.
  376. const T& Back() const
  377. {
  378. assert(size_);
  379. return Buffer()[size_ - 1];
  380. }
  381. /// Return size of vector.
  382. unsigned Size() const { return size_; }
  383. /// Return capacity of vector.
  384. unsigned Capacity() const { return capacity_; }
  385. /// Return whether vector is empty.
  386. bool Empty() const { return size_ == 0; }
  387. /// Return the buffer with right type.
  388. T* Buffer() const { return reinterpret_cast<T*>(buffer_); }
  389. private:
  390. /// Resize the vector and create/remove new elements as necessary. Current buffer will be stored in tempBuffer in case of reallocation.
  391. void Resize(unsigned newSize, const T* src, Vector<T>& tempBuffer)
  392. {
  393. // If size shrinks, destruct the removed elements
  394. if (newSize < size_)
  395. DestructElements(Buffer() + newSize, size_ - newSize);
  396. else
  397. {
  398. // Allocate new buffer if necessary and copy the current elements
  399. if (newSize > capacity_)
  400. {
  401. Swap(tempBuffer);
  402. size_ = tempBuffer.size_;
  403. capacity_ = tempBuffer.capacity_;
  404. if (!capacity_)
  405. capacity_ = newSize;
  406. else
  407. {
  408. while (capacity_ < newSize)
  409. capacity_ += (capacity_ + 1) >> 1;
  410. }
  411. buffer_ = AllocateBuffer((unsigned)(capacity_ * sizeof(T)));
  412. if (tempBuffer.Buffer())
  413. {
  414. ConstructElements(Buffer(), tempBuffer.Buffer(), size_);
  415. }
  416. }
  417. // Initialize the new elements
  418. ConstructElements(Buffer() + size_, src, newSize - size_);
  419. }
  420. size_ = newSize;
  421. }
  422. /// Insert elements.
  423. template <typename RandomIteratorT>
  424. Iterator InsertElements(unsigned pos, RandomIteratorT start, RandomIteratorT end)
  425. {
  426. assert(start <= end);
  427. if (pos > size_)
  428. pos = size_;
  429. unsigned length = (unsigned)(end - start);
  430. Vector<T> tempBuffer;
  431. Resize(size_ + length, 0, tempBuffer);
  432. MoveRange(pos + length, pos, size_ - pos - length);
  433. T* destPtr = Buffer() + pos;
  434. for (RandomIteratorT it = start; it != end; ++it)
  435. *destPtr++ = *it;
  436. return Begin() + pos;
  437. }
  438. /// Move a range of elements within the vector.
  439. void MoveRange(unsigned dest, unsigned src, unsigned count)
  440. {
  441. T* buffer = Buffer();
  442. if (src < dest)
  443. {
  444. for (unsigned i = count - 1; i < count; --i)
  445. buffer[dest + i] = buffer[src + i];
  446. }
  447. if (src > dest)
  448. {
  449. for (unsigned i = 0; i < count; ++i)
  450. buffer[dest + i] = buffer[src + i];
  451. }
  452. }
  453. /// Construct elements, optionally with source data.
  454. static void ConstructElements(T* dest, const T* src, unsigned count)
  455. {
  456. if (!src)
  457. {
  458. for (unsigned i = 0; i < count; ++i)
  459. new(dest + i) T();
  460. }
  461. else
  462. {
  463. for (unsigned i = 0; i < count; ++i)
  464. new(dest + i) T(*(src + i));
  465. }
  466. }
  467. /// Copy elements from one buffer to another.
  468. static void CopyElements(T* dest, const T* src, unsigned count)
  469. {
  470. while (count--)
  471. *dest++ = *src++;
  472. }
  473. /// Call the elements' destructors.
  474. static void DestructElements(T* dest, unsigned count)
  475. {
  476. while (count--)
  477. {
  478. dest->~T();
  479. ++dest;
  480. }
  481. }
  482. };
  483. /// %Vector template class for POD types. Does not call constructors or destructors and uses block move.
  484. template <class T> class PODVector : public VectorBase
  485. {
  486. public:
  487. typedef T ValueType;
  488. typedef RandomAccessIterator<T> Iterator;
  489. typedef RandomAccessConstIterator<T> ConstIterator;
  490. /// Construct empty.
  491. PODVector()
  492. {
  493. }
  494. /// Construct with initial size.
  495. explicit PODVector(unsigned size)
  496. {
  497. Resize(size);
  498. }
  499. /// Construct with initial size and default value.
  500. PODVector(unsigned size, const T& value)
  501. {
  502. Resize(size);
  503. for (unsigned i = 0; i < size; ++i)
  504. At(i) = value;
  505. }
  506. /// Construct with initial data.
  507. PODVector(const T* data, unsigned size)
  508. {
  509. InsertElements(0, data, data + size);
  510. }
  511. /// Construct from another vector.
  512. PODVector(const PODVector<T>& vector)
  513. {
  514. *this = vector;
  515. }
  516. #if ATOMIC_CXX11
  517. /// Aggregate initialization constructor.
  518. PODVector(const std::initializer_list<T>& list) : PODVector()
  519. {
  520. for (auto it = list.begin(); it != list.end(); it++)
  521. {
  522. Push(*it);
  523. }
  524. }
  525. #endif
  526. /// Destruct.
  527. ~PODVector()
  528. {
  529. delete[] buffer_;
  530. }
  531. /// Assign from another vector.
  532. PODVector<T>& operator =(const PODVector<T>& rhs)
  533. {
  534. // In case of self-assignment do nothing
  535. if (&rhs != this)
  536. {
  537. Clear();
  538. InsertElements(0, rhs.Begin(), rhs.End());
  539. }
  540. return *this;
  541. }
  542. /// Add-assign an element.
  543. PODVector<T>& operator +=(const T& rhs)
  544. {
  545. Push(rhs);
  546. return *this;
  547. }
  548. /// Add-assign another vector.
  549. PODVector<T>& operator +=(const PODVector<T>& rhs)
  550. {
  551. Push(rhs);
  552. return *this;
  553. }
  554. /// Add an element.
  555. PODVector<T> operator +(const T& rhs) const
  556. {
  557. PODVector<T> ret(*this);
  558. ret.Push(rhs);
  559. return ret;
  560. }
  561. /// Add another vector.
  562. PODVector<T> operator +(const PODVector<T>& rhs) const
  563. {
  564. PODVector<T> ret(*this);
  565. ret.Push(rhs);
  566. return ret;
  567. }
  568. /// Test for equality with another vector.
  569. bool operator ==(const PODVector<T>& rhs) const
  570. {
  571. if (rhs.size_ != size_)
  572. return false;
  573. T* buffer = Buffer();
  574. T* rhsBuffer = rhs.Buffer();
  575. for (unsigned i = 0; i < size_; ++i)
  576. {
  577. if (buffer[i] != rhsBuffer[i])
  578. return false;
  579. }
  580. return true;
  581. }
  582. /// Test for inequality with another vector.
  583. bool operator !=(const PODVector<T>& rhs) const
  584. {
  585. if (rhs.size_ != size_)
  586. return true;
  587. T* buffer = Buffer();
  588. T* rhsBuffer = rhs.Buffer();
  589. for (unsigned i = 0; i < size_; ++i)
  590. {
  591. if (buffer[i] != rhsBuffer[i])
  592. return true;
  593. }
  594. return false;
  595. }
  596. /// Return element at index.
  597. T& operator [](unsigned index)
  598. {
  599. assert(index < size_);
  600. return Buffer()[index];
  601. }
  602. /// Return const element at index.
  603. const T& operator [](unsigned index) const
  604. {
  605. assert(index < size_);
  606. return Buffer()[index];
  607. }
  608. /// Return element at index.
  609. T& At(unsigned index)
  610. {
  611. assert(index < size_);
  612. return Buffer()[index];
  613. }
  614. /// Return const element at index.
  615. const T& At(unsigned index) const
  616. {
  617. assert(index < size_);
  618. return Buffer()[index];
  619. }
  620. /// Add an element at the end.
  621. void Push(const T& value)
  622. {
  623. InsertElements(size_, &value, &value + 1);
  624. }
  625. /// Add another vector at the end.
  626. void Push(const PODVector<T>& vector)
  627. {
  628. InsertElements(size_, vector.Begin(), vector.End());
  629. }
  630. /// Remove the last element.
  631. void Pop()
  632. {
  633. if (size_)
  634. Resize(size_ - 1);
  635. }
  636. /// Insert an element at position.
  637. void Insert(unsigned pos, const T& value)
  638. {
  639. InsertElements(pos, &value, &value + 1);
  640. }
  641. /// Insert another vector at position.
  642. void Insert(unsigned pos, const PODVector<T>& vector)
  643. {
  644. InsertElements(pos, vector.Begin(), vector.End());
  645. }
  646. /// Insert an element by iterator.
  647. Iterator Insert(const Iterator& dest, const T& value)
  648. {
  649. unsigned pos = (unsigned)(dest - Begin());
  650. return InsertElements(pos, &value, &value + 1);
  651. }
  652. /// Insert a vector by iterator.
  653. Iterator Insert(const Iterator& dest, const PODVector<T>& vector)
  654. {
  655. unsigned pos = (unsigned)(dest - Begin());
  656. return InsertElements(pos, vector.Begin(), vector.End());
  657. }
  658. /// Insert a vector partially by iterators.
  659. Iterator Insert(const Iterator& dest, const ConstIterator& start, const ConstIterator& end)
  660. {
  661. unsigned pos = (unsigned)(dest - Begin());
  662. return InsertElements(pos, start, end);
  663. }
  664. /// Insert elements.
  665. Iterator Insert(const Iterator& dest, const T* start, const T* end)
  666. {
  667. unsigned pos = (unsigned)(dest - Begin());
  668. return InsertElements(pos, start, end);
  669. }
  670. /// Erase a range of elements.
  671. void Erase(unsigned pos, unsigned length = 1)
  672. {
  673. // Return if the range is illegal
  674. if (!length || pos + length > size_)
  675. return;
  676. MoveRange(pos, pos + length, size_ - pos - length);
  677. Resize(size_ - length);
  678. }
  679. /// Erase an element by iterator. Return iterator to the next element.
  680. Iterator Erase(const Iterator& it)
  681. {
  682. unsigned pos = (unsigned)(it - Begin());
  683. if (pos >= size_)
  684. return End();
  685. Erase(pos);
  686. return Begin() + pos;
  687. }
  688. /// Erase a range by iterators. Return iterator to the next element.
  689. Iterator Erase(const Iterator& start, const Iterator& end)
  690. {
  691. unsigned pos = (unsigned)(start - Begin());
  692. if (pos >= size_)
  693. return End();
  694. unsigned length = (unsigned)(end - start);
  695. Erase(pos, length);
  696. return Begin() + pos;
  697. }
  698. /// Erase a range of elements by swapping elements from the end of the array.
  699. void EraseSwap(unsigned pos, unsigned length = 1)
  700. {
  701. unsigned shiftStartIndex = pos + length;
  702. // Return if the range is illegal
  703. if (shiftStartIndex > size_ || !length)
  704. return;
  705. unsigned newSize = size_ - length;
  706. unsigned trailingCount = size_ - shiftStartIndex;
  707. if (trailingCount <= length)
  708. {
  709. // 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
  710. MoveRange(pos, shiftStartIndex, trailingCount);
  711. }
  712. else
  713. {
  714. // Swap elements from the end of the array into the empty space
  715. CopyElements(Buffer() + pos, Buffer() + newSize, length);
  716. }
  717. Resize(newSize);
  718. }
  719. /// Erase an element by value. Return true if was found and erased.
  720. bool Remove(const T& value)
  721. {
  722. Iterator i = Find(value);
  723. if (i != End())
  724. {
  725. Erase(i);
  726. return true;
  727. }
  728. else
  729. return false;
  730. }
  731. /// Erase an element by value by swapping with the last element. Return true if was found and erased.
  732. bool RemoveSwap(const T& value)
  733. {
  734. Iterator i = Find(value);
  735. if (i != End())
  736. {
  737. EraseSwap(i);
  738. return true;
  739. }
  740. else
  741. return false;
  742. }
  743. /// Clear the vector.
  744. void Clear() { Resize(0); }
  745. /// Resize the vector.
  746. void Resize(unsigned newSize)
  747. {
  748. PODVector<T> tempBuffer;
  749. Resize(newSize, tempBuffer);
  750. }
  751. /// Set new capacity.
  752. void Reserve(unsigned newCapacity)
  753. {
  754. if (newCapacity < size_)
  755. newCapacity = size_;
  756. if (newCapacity != capacity_)
  757. {
  758. unsigned char* newBuffer = 0;
  759. capacity_ = newCapacity;
  760. if (capacity_)
  761. {
  762. newBuffer = AllocateBuffer((unsigned)(capacity_ * sizeof(T)));
  763. // Move the data into the new buffer
  764. CopyElements(reinterpret_cast<T*>(newBuffer), Buffer(), size_);
  765. }
  766. // Delete the old buffer
  767. delete[] buffer_;
  768. buffer_ = newBuffer;
  769. }
  770. }
  771. /// Reallocate so that no extra memory is used.
  772. void Compact() { Reserve(size_); }
  773. /// Return iterator to value, or to the end if not found.
  774. Iterator Find(const T& value)
  775. {
  776. Iterator it = Begin();
  777. while (it != End() && *it != value)
  778. ++it;
  779. return it;
  780. }
  781. /// Return const iterator to value, or to the end if not found.
  782. ConstIterator Find(const T& value) const
  783. {
  784. ConstIterator it = Begin();
  785. while (it != End() && *it != value)
  786. ++it;
  787. return it;
  788. }
  789. /// Return whether contains a specific value.
  790. bool Contains(const T& value) const { return Find(value) != End(); }
  791. /// Return iterator to the beginning.
  792. Iterator Begin() { return Iterator(Buffer()); }
  793. /// Return const iterator to the beginning.
  794. ConstIterator Begin() const { return ConstIterator(Buffer()); }
  795. /// Return iterator to the end.
  796. Iterator End() { return Iterator(Buffer() + size_); }
  797. /// Return const iterator to the end.
  798. ConstIterator End() const { return ConstIterator(Buffer() + size_); }
  799. /// Return first element.
  800. T& Front() { return Buffer()[0]; }
  801. /// Return const first element.
  802. const T& Front() const { return Buffer()[0]; }
  803. /// Return last element.
  804. T& Back()
  805. {
  806. assert(size_);
  807. return Buffer()[size_ - 1];
  808. }
  809. /// Return const last element.
  810. const T& Back() const
  811. {
  812. assert(size_);
  813. return Buffer()[size_ - 1];
  814. }
  815. /// Return number of elements.
  816. unsigned Size() const { return size_; }
  817. /// Return capacity of vector.
  818. unsigned Capacity() const { return capacity_; }
  819. /// Return whether vector is empty.
  820. bool Empty() const { return size_ == 0; }
  821. /// Return the buffer with right type.
  822. T* Buffer() const { return reinterpret_cast<T*>(buffer_); }
  823. private:
  824. /// Resize the vector. Current buffer will be stored in tempBuffer in case of reallocation.
  825. void Resize(unsigned newSize, PODVector<T>& tempBuffer)
  826. {
  827. if (newSize > capacity_)
  828. {
  829. Swap(tempBuffer);
  830. size_ = tempBuffer.size_;
  831. capacity_ = tempBuffer.capacity_;
  832. if (!capacity_)
  833. capacity_ = newSize;
  834. else
  835. {
  836. while (capacity_ < newSize)
  837. capacity_ += (capacity_ + 1) >> 1;
  838. }
  839. buffer_ = AllocateBuffer((unsigned)(capacity_ * sizeof(T)));
  840. // Move the data into the new buffer
  841. if (tempBuffer.Buffer())
  842. {
  843. CopyElements(Buffer(), tempBuffer.Buffer(), size_);
  844. }
  845. }
  846. size_ = newSize;
  847. }
  848. /// Insert elements.
  849. template <typename RandomIteratorT>
  850. Iterator InsertElements(unsigned pos, RandomIteratorT start, RandomIteratorT end)
  851. {
  852. assert(start <= end);
  853. if (pos > size_)
  854. pos = size_;
  855. unsigned length = (unsigned)(end - start);
  856. PODVector<T> tempBuffer;
  857. Resize(size_ + length, tempBuffer);
  858. MoveRange(pos + length, pos, size_ - pos - length);
  859. T* destPtr = Buffer() + pos;
  860. for (RandomIteratorT i = start; i != end; ++i)
  861. *destPtr++ = *i;
  862. return Begin() + pos;
  863. }
  864. /// Move a range of elements within the vector.
  865. void MoveRange(unsigned dest, unsigned src, unsigned count)
  866. {
  867. if (count)
  868. memmove(Buffer() + dest, Buffer() + src, count * sizeof(T));
  869. }
  870. /// Copy elements from one buffer to another.
  871. static void CopyElements(T* dest, const T* src, unsigned count)
  872. {
  873. if (count)
  874. memcpy(dest, src, count * sizeof(T));
  875. }
  876. };
  877. template <class T> typename Atomic::Vector<T>::ConstIterator begin(const Atomic::Vector<T>& v) { return v.Begin(); }
  878. template <class T> typename Atomic::Vector<T>::ConstIterator end(const Atomic::Vector<T>& v) { return v.End(); }
  879. template <class T> typename Atomic::Vector<T>::Iterator begin(Atomic::Vector<T>& v) { return v.Begin(); }
  880. template <class T> typename Atomic::Vector<T>::Iterator end(Atomic::Vector<T>& v) { return v.End(); }
  881. template <class T> typename Atomic::PODVector<T>::ConstIterator begin(const Atomic::PODVector<T>& v) { return v.Begin(); }
  882. template <class T> typename Atomic::PODVector<T>::ConstIterator end(const Atomic::PODVector<T>& v) { return v.End(); }
  883. template <class T> typename Atomic::PODVector<T>::Iterator begin(Atomic::PODVector<T>& v) { return v.Begin(); }
  884. template <class T> typename Atomic::PODVector<T>::Iterator end(Atomic::PODVector<T>& v) { return v.End(); }
  885. }
  886. #ifdef _MSC_VER
  887. #pragma warning(pop)
  888. #endif