Vector.h 25 KB

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