Vector.h 11 KB

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