Vector.h 25 KB

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