PODVector.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 for POD types. Does not call constructors or destructors and uses block move
  28. template <class T> class PODVector : public VectorBase
  29. {
  30. public:
  31. typedef RandomAccessIterator<T> Iterator;
  32. typedef RandomAccessConstIterator<T> ConstIterator;
  33. /// Construct empty
  34. PODVector()
  35. {
  36. }
  37. /// Construct with initial size
  38. explicit PODVector(unsigned size)
  39. {
  40. Resize(size);
  41. }
  42. /// Construct from another vector
  43. PODVector(const PODVector<T>& vector)
  44. {
  45. *this = vector;
  46. }
  47. /// Destruct
  48. ~PODVector()
  49. {
  50. delete[] buffer_;
  51. }
  52. /// Assign from another vector
  53. PODVector<T>& operator = (const PODVector<T>& rhs)
  54. {
  55. Resize(rhs.size_);
  56. CopyElements(GetBuffer(), rhs.GetBuffer(), rhs.size_);
  57. return *this;
  58. }
  59. /// Add-assign an element
  60. PODVector<T>& operator += (const T& rhs)
  61. {
  62. Push(rhs);
  63. return *this;
  64. }
  65. /// Add-assign another vector
  66. PODVector<T>& operator += (const PODVector<T>& rhs)
  67. {
  68. Push(rhs);
  69. return *this;
  70. }
  71. /// Add an element
  72. PODVector<T> operator + (const T& rhs) const
  73. {
  74. PODVector<T> ret(*this);
  75. ret.Push(rhs);
  76. return ret;
  77. }
  78. /// Add another vector
  79. PODVector<T> operator + (const PODVector<T>& rhs) const
  80. {
  81. PODVector<T> ret(*this);
  82. ret.Push(rhs);
  83. return ret;
  84. }
  85. /// Test for equality with another vector
  86. bool operator == (const PODVector<T>& rhs) const
  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 PODVector<T>& rhs) const
  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. /// Return element at index
  118. T& At(unsigned index) { return GetBuffer()[index]; }
  119. /// Return const element at index
  120. const T& At(unsigned index) const { return GetBuffer()[index]; }
  121. /// Add an element at the end
  122. void Push(const T& value)
  123. {
  124. unsigned oldSize = size_;
  125. Resize(size_ + 1);
  126. GetBuffer()[oldSize] = value;
  127. }
  128. /// Add another vector at the end
  129. void Push(const PODVector<T>& vector)
  130. {
  131. if (!vector.size_)
  132. return;
  133. unsigned oldSize = size_;
  134. Resize(size_ + vector.size_);
  135. CopyElements(GetBuffer() + oldSize, vector.GetBuffer(), 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. Push(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. GetBuffer()[pos] = value;
  157. }
  158. /// Insert another vector at position
  159. void Insert(unsigned pos, const PODVector<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(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 PODVector<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 = 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);
  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. unsigned char* newBuffer = new unsigned char[capacity_ * sizeof(T)];
  260. // Move the data into the new buffer and delete the old
  261. if (buffer_)
  262. {
  263. CopyElements(reinterpret_cast<T*>(newBuffer), GetBuffer(), 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. unsigned char* newBuffer = 0;
  278. capacity_ = newCapacity;
  279. if (capacity_)
  280. {
  281. newBuffer = new unsigned char[capacity_ * sizeof(T)];
  282. // Move the data into the new buffer
  283. CopyElements(reinterpret_cast<T*>(newBuffer), GetBuffer(), 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(GetBuffer()); }
  296. /// Return const iterator to the beginning
  297. ConstIterator Begin() const { return ConstIterator(GetBuffer()); }
  298. /// Return iterator to the end
  299. Iterator End() { return Iterator(GetBuffer() + size_); }
  300. /// Return const iterator to the end
  301. ConstIterator End() const { return ConstIterator(GetBuffer() + size_); }
  302. /// Return first element
  303. T& Front() { return GetBuffer()[0]; }
  304. /// Return const first element
  305. const T& Front() const { return GetBuffer()[0]; }
  306. /// Return last element
  307. T& Back() { return GetBuffer()[size_ - 1]; }
  308. /// Return const last element
  309. const T& Back() const { return GetBuffer()[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. /// Return the buffer with right type
  320. T* GetBuffer() const { return reinterpret_cast<T*>(buffer_); }
  321. /// Move a range of elements within the vector
  322. void MoveRange(unsigned dest, unsigned src, unsigned count)
  323. {
  324. if (count)
  325. memmove(GetBuffer() + dest, GetBuffer() + src, count * sizeof(T));
  326. }
  327. /// Copy elements from one buffer to another
  328. static void CopyElements(T* dest, const T* src, unsigned count)
  329. {
  330. if (count)
  331. memcpy(dest, src, count * sizeof(T));
  332. }
  333. };