Vector.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 <cstring>
  26. /// Vector template class
  27. template <class T> class Vector
  28. {
  29. public:
  30. typedef RandomAccessIterator<T> Iterator;
  31. typedef RandomAccessConstIterator<T> ConstIterator;
  32. /// Construct empty
  33. Vector() :
  34. size_(0),
  35. capacity_(0),
  36. buffer_(0)
  37. {
  38. }
  39. /// Construct with initial size
  40. explicit Vector(unsigned size) :
  41. size_(0),
  42. capacity_(0),
  43. buffer_(0)
  44. {
  45. Resize(size);
  46. }
  47. /// Construct from another vector
  48. Vector(const Vector<T>& vector) :
  49. size_(0),
  50. capacity_(0),
  51. buffer_(0)
  52. {
  53. *this = vector;
  54. }
  55. /// Destruct
  56. ~Vector()
  57. {
  58. delete[] buffer_;
  59. }
  60. /// Assign from another vector
  61. Vector<T>& operator = (const Vector<T>& rhs)
  62. {
  63. Resize(rhs.size_);
  64. CopyElements(buffer_, rhs.buffer_, rhs.size_);
  65. return *this;
  66. }
  67. /// Add-assign an element
  68. Vector<T>& operator += (const T& rhs)
  69. {
  70. Push(rhs);
  71. return *this;
  72. }
  73. /// Add-assign another vector
  74. Vector<T>& operator += (const Vector<T>& rhs)
  75. {
  76. Push(rhs);
  77. return *this;
  78. }
  79. /// Add an element
  80. Vector<T> operator + (const T& rhs) const
  81. {
  82. Vector<T> ret(*this);
  83. ret.Push(rhs);
  84. return ret;
  85. }
  86. /// Add another vector
  87. Vector<T> operator + (const Vector<T>& rhs) const
  88. {
  89. Vector<T> ret(*this);
  90. ret.Push(rhs);
  91. return ret;
  92. }
  93. /// Test for equality with another vector
  94. bool operator == (const Vector<T>& rhs)
  95. {
  96. if (rhs.size_ != size_)
  97. return false;
  98. for (unsigned i = 0; i < size_; ++i)
  99. {
  100. if (buffer_[i] != rhs.buffer_[i])
  101. return false;
  102. }
  103. return true;
  104. }
  105. /// Test for inequality with another vector
  106. bool operator != (const Vector<T>& rhs)
  107. {
  108. if (rhs.size_ != size_)
  109. return true;
  110. for (unsigned i = 0; i < size_; ++i)
  111. {
  112. if (buffer_[i] != rhs.buffer_[i])
  113. return true;
  114. }
  115. return false;
  116. }
  117. /// Return element at index
  118. T& operator [] (unsigned index) { return buffer_[index]; }
  119. /// Return const element at index
  120. const T& operator [] (unsigned index) const { return buffer_[index]; }
  121. /// Add an element at the end
  122. void Push(const T& value)
  123. {
  124. unsigned oldSize = size_;
  125. Resize(size_ + 1);
  126. buffer_[oldSize] = value;
  127. }
  128. /// Add another vector at the end
  129. void Push(const Vector<T>& vector)
  130. {
  131. if (!vector.size_)
  132. return;
  133. unsigned oldSize = size_;
  134. Resize(size_ + vector.size_);
  135. CopyElements(buffer_ + oldSize, vector.buffer_, vector.size_);
  136. }
  137. /// Remove the last element
  138. void Pop()
  139. {
  140. if (size_)
  141. Resize(size_ - 1);
  142. }
  143. /// Insert an element at position
  144. void Insert(unsigned pos, const T& value)
  145. {
  146. if (!size_)
  147. {
  148. Add(value);
  149. return;
  150. }
  151. if (pos > size_)
  152. pos = size_;
  153. unsigned oldSize = size_;
  154. Resize(size_ + 1);
  155. MoveRange(pos + 1, pos, oldSize - pos);
  156. buffer_[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_);
  172. MoveRange(pos + vector.size_, pos, oldSize - pos);
  173. CopyElements(buffer_ + pos, vector.buffer_, 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);
  201. MoveRange(pos + length, pos, size_ - pos - length);
  202. T* destPtr = buffer_ + 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);
  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. if (newSize == size_)
  244. return;
  245. if (newSize > capacity_)
  246. {
  247. if (!capacity_)
  248. capacity_ = newSize;
  249. else
  250. {
  251. while (capacity_ < newSize)
  252. {
  253. unsigned increment = capacity_ >> 1;
  254. if (!increment)
  255. increment = 1;
  256. capacity_ += increment;
  257. }
  258. }
  259. T* newBuffer = new T[capacity_];
  260. // Move the data into the new buffer and delete the old
  261. if (buffer_)
  262. {
  263. CopyElements(newBuffer, buffer_, size_);
  264. delete[] buffer_;
  265. }
  266. buffer_ = newBuffer;
  267. }
  268. size_ = newSize;
  269. }
  270. /// Set new capacity
  271. void Reserve(unsigned newCapacity)
  272. {
  273. if (newCapacity < size_)
  274. newCapacity = size_;
  275. if (newCapacity == capacity_)
  276. return;
  277. T* newBuffer = 0;
  278. capacity_ = newCapacity;
  279. if (capacity_)
  280. {
  281. newBuffer = new T[capacity_];
  282. // Move the data into the new buffer
  283. CopyElements(newBuffer, buffer_, size_);
  284. }
  285. // Delete the old buffer
  286. delete[] buffer_;
  287. buffer_ = newBuffer;
  288. }
  289. /// Reallocate so that no extra memory is used
  290. void Compact()
  291. {
  292. Reserve(size_);
  293. }
  294. /// Return iterator to the beginning
  295. Iterator Begin() { return Iterator(buffer_); }
  296. /// Return const iterator to the beginning
  297. ConstIterator Begin() const { return ConstIterator(buffer_); }
  298. /// Return iterator to the end
  299. Iterator End() { return Iterator(buffer_ + size_); }
  300. /// Return const iterator to the end
  301. ConstIterator End() const { return ConstIterator(buffer_ + size_); }
  302. /// Return first element
  303. T& Front() { return buffer_[0]; }
  304. /// Return const first element
  305. const T& Front() const { return buffer_[0]; }
  306. /// Return last element
  307. T& Back() { return buffer_[size_ - 1]; }
  308. /// Return const last element
  309. const T& Back() const { return buffer_[size_ - 1]; }
  310. /// Return size of vector
  311. unsigned Size() const { return size_; }
  312. /// Return capacity of vector
  313. unsigned Capacity() const { return capacity_; }
  314. /// Return whether vector is empty
  315. bool Empty() const { return size_ == 0; }
  316. /// Minimum dynamic allocation size
  317. static const unsigned MIN_CAPACITY = 1;
  318. private:
  319. /// Move a range of elements within the vector
  320. void MoveRange(unsigned dest, unsigned src, unsigned count)
  321. {
  322. if (src < dest)
  323. {
  324. for (unsigned i = count - 1; i < count; --i)
  325. buffer_[dest + i] = buffer_[src + i];
  326. }
  327. if (src > dest)
  328. {
  329. for (unsigned i = 0; i < count; ++i)
  330. buffer_[dest + i] = buffer_[src + i];
  331. }
  332. }
  333. /// Copy elements from one buffer to another
  334. static void CopyElements(T* dest, const T* src, unsigned count)
  335. {
  336. for (unsigned i = 0; i < count; ++i)
  337. dest[i] = src[i];
  338. }
  339. /// Size of vector
  340. unsigned size_;
  341. /// Buffer capacity
  342. unsigned capacity_;
  343. /// Buffer
  344. T* buffer_;
  345. };