Vector.h 26 KB

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