Vector.h 21 KB

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