Vector.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 "Iterator.h"
  25. #include "VectorBase.h"
  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 from another vector
  44. Vector(const Vector<T>& vector)
  45. {
  46. *this = vector;
  47. }
  48. /// Destruct
  49. ~Vector()
  50. {
  51. Clear();
  52. delete[] buffer_;
  53. }
  54. /// Assign from another vector
  55. Vector<T>& operator = (const Vector<T>& rhs)
  56. {
  57. Clear();
  58. Resize(rhs.size_, rhs.GetBuffer());
  59. return *this;
  60. }
  61. /// Add-assign an element
  62. Vector<T>& operator += (const T& rhs)
  63. {
  64. Push(rhs);
  65. return *this;
  66. }
  67. /// Add-assign another vector
  68. Vector<T>& operator += (const Vector<T>& rhs)
  69. {
  70. Push(rhs);
  71. return *this;
  72. }
  73. /// Add an element
  74. Vector<T> operator + (const T& rhs) const
  75. {
  76. Vector<T> ret(*this);
  77. ret.Push(rhs);
  78. return ret;
  79. }
  80. /// Add another vector
  81. Vector<T> operator + (const Vector<T>& rhs) const
  82. {
  83. Vector<T> ret(*this);
  84. ret.Push(rhs);
  85. return ret;
  86. }
  87. /// Test for equality with another vector
  88. bool operator == (const Vector<T>& rhs) const
  89. {
  90. if (rhs.size_ != size_)
  91. return false;
  92. T* buffer = GetBuffer();
  93. T* rhsBuffer = rhs.GetBuffer();
  94. for (unsigned i = 0; i < size_; ++i)
  95. {
  96. if (buffer[i] != rhsBuffer[i])
  97. return false;
  98. }
  99. return true;
  100. }
  101. /// Test for inequality with another vector
  102. bool operator != (const Vector<T>& rhs) const
  103. {
  104. if (rhs.size_ != size_)
  105. return true;
  106. T* buffer = GetBuffer();
  107. T* rhsBuffer = rhs.GetBuffer();
  108. for (unsigned i = 0; i < size_; ++i)
  109. {
  110. if (buffer[i] != rhsBuffer[i])
  111. return true;
  112. }
  113. return false;
  114. }
  115. /// Return element at index
  116. T& operator [] (unsigned index) { return GetBuffer()[index]; }
  117. /// Return const element at index
  118. const T& operator [] (unsigned index) const { return GetBuffer()[index]; }
  119. /// Return element at index
  120. T& At(unsigned index) { return GetBuffer()[index]; }
  121. /// Return const element at index
  122. const T& At(unsigned index) const { return GetBuffer()[index]; }
  123. /// Add an element at the end
  124. void Push(const T& value)
  125. {
  126. unsigned oldSize = size_;
  127. Resize(size_ + 1, &value);
  128. }
  129. /// Add another vector at the end
  130. void Push(const Vector<T>& vector)
  131. {
  132. if (!vector.size_)
  133. return;
  134. unsigned oldSize = size_;
  135. Resize(size_ + vector.size_, vector.GetBuffer());
  136. }
  137. /// Remove the last element
  138. void Pop()
  139. {
  140. if (size_)
  141. Resize(size_ - 1, 0);
  142. }
  143. /// Insert an element at position
  144. void Insert(unsigned pos, const T& value)
  145. {
  146. if (!size_)
  147. {
  148. Push(value);
  149. return;
  150. }
  151. if (pos > size_)
  152. pos = size_;
  153. unsigned oldSize = size_;
  154. Resize(size_ + 1, 0);
  155. MoveRange(pos + 1, pos, oldSize - pos);
  156. GetBuffer()[pos] = value;
  157. }
  158. /// Insert another vector at position
  159. void Insert(unsigned pos, const Vector<T>& vector)
  160. {
  161. if (!vector.size_)
  162. return;
  163. if (!size_)
  164. {
  165. *this = vector;
  166. return;
  167. }
  168. if (pos > size_)
  169. pos = size_;
  170. unsigned oldSize = size_;
  171. Resize(size_ + vector.size_, 0);
  172. MoveRange(pos + vector.size_, pos, oldSize - pos);
  173. CopyElements(GetBuffer() + pos, vector.GetBuffer(), vector.size_);
  174. }
  175. /// Insert an element using an iterator
  176. Iterator Insert(const Iterator& dest, const T& value)
  177. {
  178. unsigned pos = dest - Begin();
  179. if (pos > size_)
  180. pos = size_;
  181. Insert(pos, value);
  182. return Begin() + pos;
  183. }
  184. /// Insert a vector using an iterator
  185. Iterator Insert(const Iterator& dest, const Vector<T>& vector)
  186. {
  187. unsigned pos = dest - Begin();
  188. if (pos > size_)
  189. pos = size_;
  190. Insert(pos, vector);
  191. return Begin() + pos;
  192. }
  193. /// Insert a vector partially using iterators
  194. Iterator Insert(const Iterator& dest, const Iterator& start, const Iterator& end)
  195. {
  196. unsigned pos = dest - Begin();
  197. if (pos > size_)
  198. pos = size_;
  199. unsigned length = end - start;
  200. Resize(size_ + length, 0);
  201. MoveRange(pos + length, pos, size_ - pos - length);
  202. T* destPtr = GetBuffer() + pos;
  203. for (Iterator i = start; i != end; ++i)
  204. *destPtr++ = *i;
  205. return Begin() + pos;
  206. }
  207. /// Erase a range of elements
  208. void Erase(unsigned pos, unsigned length = 1)
  209. {
  210. // Return if the range is illegal
  211. if ((!length) || (pos + length > size_))
  212. return;
  213. MoveRange(pos, pos + length, size_ - pos - length);
  214. Resize(size_ - length, 0);
  215. }
  216. /// Erase an element using an iterator
  217. Iterator Erase(const Iterator& it)
  218. {
  219. unsigned pos = it - Begin();
  220. if (pos >= size_)
  221. return End();
  222. Erase(pos);
  223. return Begin() + pos;
  224. }
  225. /// Erase a range of values using iterators
  226. Iterator Erase(const Iterator& start, const Iterator& end)
  227. {
  228. unsigned pos = start - Begin();
  229. if (pos >= size_)
  230. return End();
  231. unsigned length = end - start;
  232. Erase(pos, length);
  233. return Begin() + pos;
  234. }
  235. /// Clear the vector
  236. void Clear()
  237. {
  238. Resize(0);
  239. }
  240. /// Resize the vector
  241. void Resize(unsigned newSize)
  242. {
  243. Resize(newSize, 0);
  244. }
  245. /// Set new capacity
  246. void Reserve(unsigned newCapacity)
  247. {
  248. if (newCapacity < size_)
  249. newCapacity = size_;
  250. if (newCapacity == capacity_)
  251. return;
  252. T* newBuffer = 0;
  253. capacity_ = newCapacity;
  254. if (capacity_)
  255. {
  256. newBuffer = reinterpret_cast<T*>(new unsigned char[capacity_ * sizeof(T)]);
  257. // Move the data into the new buffer
  258. ConstructElements(newBuffer, GetBuffer(), size_);
  259. }
  260. // Delete the old buffer
  261. DestructElements(GetBuffer(), size_);
  262. delete[] buffer_;
  263. buffer_ = reinterpret_cast<unsigned char*>(newBuffer);
  264. }
  265. /// Reallocate so that no extra memory is used
  266. void Compact()
  267. {
  268. Reserve(size_);
  269. }
  270. /// Return iterator to the beginning
  271. Iterator Begin() { return Iterator(GetBuffer()); }
  272. /// Return const iterator to the beginning
  273. ConstIterator Begin() const { return ConstIterator(GetBuffer()); }
  274. /// Return iterator to the end
  275. Iterator End() { return Iterator(GetBuffer() + size_); }
  276. /// Return const iterator to the end
  277. ConstIterator End() const { return ConstIterator(GetBuffer() + size_); }
  278. /// Return first element
  279. T& Front() { return GetBuffer()[0]; }
  280. /// Return const first element
  281. const T& Front() const { return GetBuffer()[0]; }
  282. /// Return last element
  283. T& Back() { return GetBuffer()[size_ - 1]; }
  284. /// Return const last element
  285. const T& Back() const { return GetBuffer()[size_ - 1]; }
  286. /// Return size of vector
  287. unsigned Size() const { return size_; }
  288. /// Return capacity of vector
  289. unsigned Capacity() const { return capacity_; }
  290. /// Return whether vector is empty
  291. bool Empty() const { return size_ == 0; }
  292. /// Minimum dynamic allocation size
  293. static const unsigned MIN_CAPACITY = 1;
  294. private:
  295. /// Return the buffer with right type
  296. T* GetBuffer() const { return reinterpret_cast<T*>(buffer_); }
  297. /// Resize the vector and create/remove new elements as necessary
  298. void Resize(unsigned newSize, const T* src)
  299. {
  300. if (newSize == size_)
  301. return;
  302. // If size shrinks, destruct the removed elements
  303. if (newSize < size_)
  304. DestructElements(GetBuffer() + newSize, size_ - newSize);
  305. else
  306. {
  307. // Allocate new buffer if necessary and copy the current elements
  308. if (newSize > capacity_)
  309. {
  310. if (!capacity_)
  311. capacity_ = newSize;
  312. else
  313. {
  314. while (capacity_ < newSize)
  315. {
  316. unsigned increment = capacity_ >> 1;
  317. if (!increment)
  318. increment = 1;
  319. capacity_ += increment;
  320. }
  321. }
  322. unsigned char* newBuffer = new unsigned char[capacity_ * sizeof(T)];
  323. if (buffer_)
  324. {
  325. ConstructElements(reinterpret_cast<T*>(newBuffer), GetBuffer(), size_);
  326. DestructElements(GetBuffer(), size_);
  327. delete[] buffer_;
  328. }
  329. buffer_ = newBuffer;
  330. }
  331. // Initialize the new elements
  332. ConstructElements(GetBuffer() + 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 = GetBuffer();
  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. };