Vector.h 23 KB

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