Vector.h 24 KB

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