Vector.h 23 KB

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