Vector.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. //
  2. // Copyright (c) 2008-2013 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 using an 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 using an 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.
  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.
  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. for (unsigned i = 0; i < count; ++i)
  380. dest[i] = src[i];
  381. }
  382. // Call the elements' destructors.
  383. static void DestructElements(T* dest, unsigned count)
  384. {
  385. for (unsigned i = 0; i < count; ++i)
  386. (dest + i)->~T();
  387. }
  388. };
  389. /// %Vector template class for POD types. Does not call constructors or destructors and uses block move.
  390. template <class T> class PODVector : public VectorBase
  391. {
  392. public:
  393. typedef RandomAccessIterator<T> Iterator;
  394. typedef RandomAccessConstIterator<T> ConstIterator;
  395. /// Construct empty.
  396. PODVector()
  397. {
  398. }
  399. /// Construct with initial size.
  400. explicit PODVector(unsigned size)
  401. {
  402. Resize(size);
  403. }
  404. /// Construct with initial data.
  405. PODVector(const T* data, unsigned size)
  406. {
  407. Resize(size);
  408. CopyElements(Buffer(), data, size);
  409. }
  410. /// Construct from another vector.
  411. PODVector(const PODVector<T>& vector)
  412. {
  413. *this = vector;
  414. }
  415. /// Destruct.
  416. ~PODVector()
  417. {
  418. delete[] buffer_;
  419. }
  420. /// Assign from another vector.
  421. PODVector<T>& operator = (const PODVector<T>& rhs)
  422. {
  423. Resize(rhs.size_);
  424. CopyElements(Buffer(), rhs.Buffer(), rhs.size_);
  425. return *this;
  426. }
  427. /// Add-assign an element.
  428. PODVector<T>& operator += (const T& rhs)
  429. {
  430. Push(rhs);
  431. return *this;
  432. }
  433. /// Add-assign another vector.
  434. PODVector<T>& operator += (const PODVector<T>& rhs)
  435. {
  436. Push(rhs);
  437. return *this;
  438. }
  439. /// Add an element.
  440. PODVector<T> operator + (const T& rhs) const
  441. {
  442. PODVector<T> ret(*this);
  443. ret.Push(rhs);
  444. return ret;
  445. }
  446. /// Add another vector.
  447. PODVector<T> operator + (const PODVector<T>& rhs) const
  448. {
  449. PODVector<T> ret(*this);
  450. ret.Push(rhs);
  451. return ret;
  452. }
  453. /// Test for equality with another vector.
  454. bool operator == (const PODVector<T>& rhs) const
  455. {
  456. if (rhs.size_ != size_)
  457. return false;
  458. T* buffer = Buffer();
  459. T* rhsBuffer = rhs.Buffer();
  460. for (unsigned i = 0; i < size_; ++i)
  461. {
  462. if (buffer[i] != rhsBuffer[i])
  463. return false;
  464. }
  465. return true;
  466. }
  467. /// Test for inequality with another vector.
  468. bool operator != (const PODVector<T>& rhs) const
  469. {
  470. if (rhs.size_ != size_)
  471. return true;
  472. T* buffer = Buffer();
  473. T* rhsBuffer = rhs.Buffer();
  474. for (unsigned i = 0; i < size_; ++i)
  475. {
  476. if (buffer[i] != rhsBuffer[i])
  477. return true;
  478. }
  479. return false;
  480. }
  481. /// Return element at index.
  482. T& operator [] (unsigned index) { assert(index < size_); return Buffer()[index]; }
  483. /// Return const element at index.
  484. const T& operator [] (unsigned index) const { assert(index < size_); return Buffer()[index]; }
  485. /// Return element at index.
  486. T& At(unsigned index) { assert(index < size_); return Buffer()[index]; }
  487. /// Return const element at index.
  488. const T& At(unsigned index) const { assert(index < size_); return Buffer()[index]; }
  489. /// Add an element at the end.
  490. void Push(const T& value)
  491. {
  492. if (size_ < capacity_)
  493. ++size_;
  494. else
  495. Resize(size_ + 1);
  496. Back() = value;
  497. }
  498. /// Add another vector at the end.
  499. void Push(const PODVector<T>& vector)
  500. {
  501. unsigned oldSize = size_;
  502. Resize(size_ + vector.size_);
  503. CopyElements(Buffer() + oldSize, vector.Buffer(), vector.size_);
  504. }
  505. /// Remove the last element.
  506. void Pop()
  507. {
  508. if (size_)
  509. Resize(size_ - 1);
  510. }
  511. /// Insert an element at position.
  512. void Insert(unsigned pos, const T& value)
  513. {
  514. if (pos > size_)
  515. pos = size_;
  516. unsigned oldSize = size_;
  517. Resize(size_ + 1);
  518. MoveRange(pos + 1, pos, oldSize - pos);
  519. Buffer()[pos] = value;
  520. }
  521. /// Insert another vector at position.
  522. void Insert(unsigned pos, const PODVector<T>& vector)
  523. {
  524. if (pos > size_)
  525. pos = size_;
  526. unsigned oldSize = size_;
  527. Resize(size_ + vector.size_);
  528. MoveRange(pos + vector.size_, pos, oldSize - pos);
  529. CopyElements(Buffer() + pos, vector.Buffer(), vector.size_);
  530. }
  531. /// Insert an element using an iterator.
  532. Iterator Insert(const Iterator& dest, const T& value)
  533. {
  534. unsigned pos = dest - Begin();
  535. if (pos > size_)
  536. pos = size_;
  537. Insert(pos, value);
  538. return Begin() + pos;
  539. }
  540. /// Insert a vector using an iterator.
  541. Iterator Insert(const Iterator& dest, const PODVector<T>& vector)
  542. {
  543. unsigned pos = dest - Begin();
  544. if (pos > size_)
  545. pos = size_;
  546. Insert(pos, vector);
  547. return Begin() + pos;
  548. }
  549. /// Insert a vector partially by iterators.
  550. Iterator Insert(const Iterator& dest, const ConstIterator& start, const ConstIterator& end)
  551. {
  552. unsigned pos = dest - Begin();
  553. if (pos > size_)
  554. pos = size_;
  555. unsigned length = end - start;
  556. Resize(size_ + length);
  557. MoveRange(pos + length, pos, size_ - pos - length);
  558. CopyElements(Buffer() + pos, &(*start), length);
  559. return Begin() + pos;
  560. }
  561. /// Insert elements.
  562. Iterator Insert(const Iterator& dest, const T* start, const T* end)
  563. {
  564. unsigned pos = dest - Begin();
  565. if (pos > size_)
  566. pos = size_;
  567. unsigned length = end - start;
  568. Resize(size_ + length);
  569. MoveRange(pos + length, pos, size_ - pos - length);
  570. T* destPtr = Buffer() + pos;
  571. for (const T* i = start; i != end; ++i)
  572. *destPtr++ = *i;
  573. return Begin() + pos;
  574. }
  575. /// Erase a range of elements.
  576. void Erase(unsigned pos, unsigned length = 1)
  577. {
  578. // Return if the range is illegal
  579. if (!length || pos + length > size_)
  580. return;
  581. MoveRange(pos, pos + length, size_ - pos - length);
  582. Resize(size_ - length);
  583. }
  584. /// Erase an element using an iterator.
  585. Iterator Erase(const Iterator& it)
  586. {
  587. unsigned pos = it - Begin();
  588. if (pos >= size_)
  589. return End();
  590. Erase(pos);
  591. return Begin() + pos;
  592. }
  593. /// Erase a range by iterators.
  594. Iterator Erase(const Iterator& start, const Iterator& end)
  595. {
  596. unsigned pos = start - Begin();
  597. if (pos >= size_)
  598. return End();
  599. unsigned length = end - start;
  600. Erase(pos, length);
  601. return Begin() + pos;
  602. }
  603. /// Erase an element if found.
  604. bool Remove(const T& value)
  605. {
  606. Iterator i = Find(value);
  607. if (i != End())
  608. {
  609. Erase(i);
  610. return true;
  611. }
  612. else
  613. return false;
  614. }
  615. /// Clear the vector.
  616. void Clear() { Resize(0); }
  617. /// Resize the vector.
  618. void Resize(unsigned newSize)
  619. {
  620. if (newSize > capacity_)
  621. {
  622. if (!capacity_)
  623. capacity_ = newSize;
  624. else
  625. {
  626. while (capacity_ < newSize)
  627. capacity_ += (capacity_ + 1) >> 1;
  628. }
  629. unsigned char* newBuffer = AllocateBuffer(capacity_ * sizeof(T));
  630. // Move the data into the new buffer and delete the old
  631. if (buffer_)
  632. {
  633. CopyElements(reinterpret_cast<T*>(newBuffer), Buffer(), size_);
  634. delete[] buffer_;
  635. }
  636. buffer_ = newBuffer;
  637. }
  638. size_ = newSize;
  639. }
  640. /// Set new capacity.
  641. void Reserve(unsigned newCapacity)
  642. {
  643. if (newCapacity < size_)
  644. newCapacity = size_;
  645. if (newCapacity != capacity_)
  646. {
  647. unsigned char* newBuffer = 0;
  648. capacity_ = newCapacity;
  649. if (capacity_)
  650. {
  651. newBuffer = AllocateBuffer(capacity_ * sizeof(T));
  652. // Move the data into the new buffer
  653. CopyElements(reinterpret_cast<T*>(newBuffer), Buffer(), size_);
  654. }
  655. // Delete the old buffer
  656. delete[] buffer_;
  657. buffer_ = newBuffer;
  658. }
  659. }
  660. /// Reallocate so that no extra memory is used.
  661. void Compact() { Reserve(size_); }
  662. /// Return iterator to value, or to the end if not found.
  663. Iterator Find(const T& value)
  664. {
  665. Iterator it = Begin();
  666. while (it != End() && *it != value)
  667. ++it;
  668. return it;
  669. }
  670. /// Return const iterator to value, or to the end if not found.
  671. ConstIterator Find(const T& value) const
  672. {
  673. ConstIterator it = Begin();
  674. while (it != End() && *it != value)
  675. ++it;
  676. return it;
  677. }
  678. /// Return whether contains a specific value.
  679. bool Contains(const T& value) const { return Find(value) != End(); }
  680. /// Return iterator to the beginning.
  681. Iterator Begin() { return Iterator(Buffer()); }
  682. /// Return const iterator to the beginning.
  683. ConstIterator Begin() const { return ConstIterator(Buffer()); }
  684. /// Return iterator to the end.
  685. Iterator End() { return Iterator(Buffer() + size_); }
  686. /// Return const iterator to the end.
  687. ConstIterator End() const { return ConstIterator(Buffer() + size_); }
  688. /// Return first element.
  689. T& Front() { return Buffer()[0]; }
  690. /// Return const first element.
  691. const T& Front() const { return Buffer()[0]; }
  692. /// Return last element.
  693. T& Back() { assert(size_); return Buffer()[size_ - 1]; }
  694. /// Return const last element.
  695. const T& Back() const { assert(size_); return Buffer()[size_ - 1]; }
  696. /// Return number of elements.
  697. unsigned Size() const { return size_; }
  698. /// Return capacity of vector.
  699. unsigned Capacity() const { return capacity_; }
  700. /// Return whether vector is empty.
  701. bool Empty() const { return size_ == 0; }
  702. private:
  703. /// Return the buffer with right type.
  704. T* Buffer() const { return reinterpret_cast<T*>(buffer_); }
  705. /// Move a range of elements within the vector.
  706. void MoveRange(unsigned dest, unsigned src, unsigned count)
  707. {
  708. if (count)
  709. memmove(Buffer() + dest, Buffer() + src, count * sizeof(T));
  710. }
  711. /// Copy elements from one buffer to another.
  712. static void CopyElements(T* dest, const T* src, unsigned count)
  713. {
  714. if (count)
  715. memcpy(dest, src, count * sizeof(T));
  716. }
  717. };
  718. }