VectorBase.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #ifdef URHO3D_IS_BUILDING
  24. #include "Urho3D.h"
  25. #else
  26. #include <Urho3D/Urho3D.h>
  27. #endif
  28. #include "../Container/Swap.h"
  29. namespace Urho3D
  30. {
  31. /// Random access iterator.
  32. template <class T> struct RandomAccessIterator
  33. {
  34. /// Construct.
  35. RandomAccessIterator() :
  36. ptr_(0)
  37. {
  38. }
  39. /// Construct with an object pointer.
  40. explicit RandomAccessIterator(T* ptr) :
  41. ptr_(ptr)
  42. {
  43. }
  44. /// Point to the object.
  45. T* operator ->() const { return ptr_; }
  46. /// Dereference the object.
  47. T& operator *() const { return *ptr_; }
  48. /// Preincrement the pointer.
  49. RandomAccessIterator<T>& operator ++()
  50. {
  51. ++ptr_;
  52. return *this;
  53. }
  54. /// Postincrement the pointer.
  55. RandomAccessIterator<T> operator ++(int)
  56. {
  57. RandomAccessIterator<T> it = *this;
  58. ++ptr_;
  59. return it;
  60. }
  61. /// Predecrement the pointer.
  62. RandomAccessIterator<T>& operator --()
  63. {
  64. --ptr_;
  65. return *this;
  66. }
  67. /// Postdecrement the pointer.
  68. RandomAccessIterator<T> operator --(int)
  69. {
  70. RandomAccessIterator<T> it = *this;
  71. --ptr_;
  72. return it;
  73. }
  74. /// Add an offset to the pointer.
  75. RandomAccessIterator<T>& operator +=(int value)
  76. {
  77. ptr_ += value;
  78. return *this;
  79. }
  80. /// Subtract an offset from the pointer.
  81. RandomAccessIterator<T>& operator -=(int value)
  82. {
  83. ptr_ -= value;
  84. return *this;
  85. }
  86. /// Add an offset to the pointer.
  87. RandomAccessIterator<T> operator +(int value) const { return RandomAccessIterator<T>(ptr_ + value); }
  88. /// Subtract an offset from the pointer.
  89. RandomAccessIterator<T> operator -(int value) const { return RandomAccessIterator<T>(ptr_ - value); }
  90. /// Calculate offset to another iterator.
  91. int operator -(const RandomAccessIterator& rhs) const { return (int)(ptr_ - rhs.ptr_); }
  92. /// Test for equality with another iterator.
  93. bool operator ==(const RandomAccessIterator& rhs) const { return ptr_ == rhs.ptr_; }
  94. /// Test for inequality with another iterator.
  95. bool operator !=(const RandomAccessIterator& rhs) const { return ptr_ != rhs.ptr_; }
  96. /// Test for less than with another iterator.
  97. bool operator <(const RandomAccessIterator& rhs) const { return ptr_ < rhs.ptr_; }
  98. /// Test for greater than with another iterator.
  99. bool operator >(const RandomAccessIterator& rhs) const { return ptr_ > rhs.ptr_; }
  100. /// Test for less than or equal with another iterator.
  101. bool operator <=(const RandomAccessIterator& rhs) const { return ptr_ <= rhs.ptr_; }
  102. /// Test for greater than or equal with another iterator.
  103. bool operator >=(const RandomAccessIterator& rhs) const { return ptr_ >= rhs.ptr_; }
  104. /// Pointer.
  105. T* ptr_;
  106. };
  107. /// Random access const iterator.
  108. template <class T> struct RandomAccessConstIterator
  109. {
  110. /// Construct.
  111. RandomAccessConstIterator() :
  112. ptr_(0)
  113. {
  114. }
  115. /// Construct with an object pointer.
  116. explicit RandomAccessConstIterator(T* ptr) :
  117. ptr_(ptr)
  118. {
  119. }
  120. /// Construct from a non-const iterator.
  121. RandomAccessConstIterator(const RandomAccessIterator<T>& rhs) :
  122. ptr_(rhs.ptr_)
  123. {
  124. }
  125. /// Assign from a non-const iterator.
  126. RandomAccessConstIterator<T>& operator =(const RandomAccessIterator<T>& rhs)
  127. {
  128. ptr_ = rhs.ptr_;
  129. return *this;
  130. }
  131. /// Point to the object.
  132. const T* operator ->() const { return ptr_; }
  133. /// Dereference the object.
  134. const T& operator *() const { return *ptr_; }
  135. /// Preincrement the pointer.
  136. RandomAccessConstIterator<T>& operator ++()
  137. {
  138. ++ptr_;
  139. return *this;
  140. }
  141. /// Postincrement the pointer.
  142. RandomAccessConstIterator<T> operator ++(int)
  143. {
  144. RandomAccessConstIterator<T> it = *this;
  145. ++ptr_;
  146. return it;
  147. }
  148. /// Predecrement the pointer.
  149. RandomAccessConstIterator<T>& operator --()
  150. {
  151. --ptr_;
  152. return *this;
  153. }
  154. /// Postdecrement the pointer.
  155. RandomAccessConstIterator<T> operator --(int)
  156. {
  157. RandomAccessConstIterator<T> it = *this;
  158. --ptr_;
  159. return it;
  160. }
  161. /// Add an offset to the pointer.
  162. RandomAccessConstIterator<T>& operator +=(int value)
  163. {
  164. ptr_ += value;
  165. return *this;
  166. }
  167. /// Subtract an offset from the pointer.
  168. RandomAccessConstIterator<T>& operator -=(int value)
  169. {
  170. ptr_ -= value;
  171. return *this;
  172. }
  173. /// Add an offset to the pointer.
  174. RandomAccessConstIterator<T> operator +(int value) const { return RandomAccessConstIterator<T>(ptr_ + value); }
  175. /// Subtract an offset from the pointer.
  176. RandomAccessConstIterator<T> operator -(int value) const { return RandomAccessConstIterator<T>(ptr_ - value); }
  177. /// Calculate offset to another iterator.
  178. int operator -(const RandomAccessConstIterator& rhs) const { return (int)(ptr_ - rhs.ptr_); }
  179. /// Test for equality with another iterator.
  180. bool operator ==(const RandomAccessConstIterator& rhs) const { return ptr_ == rhs.ptr_; }
  181. /// Test for inequality with another iterator.
  182. bool operator !=(const RandomAccessConstIterator& rhs) const { return ptr_ != rhs.ptr_; }
  183. /// Test for less than with another iterator.
  184. bool operator <(const RandomAccessConstIterator& rhs) const { return ptr_ < rhs.ptr_; }
  185. /// Test for greater than with another iterator.
  186. bool operator >(const RandomAccessConstIterator& rhs) const { return ptr_ > rhs.ptr_; }
  187. /// Test for less than or equal with another iterator.
  188. bool operator <=(const RandomAccessConstIterator& rhs) const { return ptr_ <= rhs.ptr_; }
  189. /// Test for greater than or equal with another iterator.
  190. bool operator >=(const RandomAccessConstIterator& rhs) const { return ptr_ >= rhs.ptr_; }
  191. /// Pointer.
  192. T* ptr_;
  193. };
  194. /// Returns an iterator pointing to the first element in the range [first, last) that is not less than value.
  195. template <class TRandomAccessIterator, class T>
  196. TRandomAccessIterator LowerBound(TRandomAccessIterator first, TRandomAccessIterator last, const T& value)
  197. {
  198. unsigned count = last - first;
  199. while (count > 0)
  200. {
  201. const unsigned step = count / 2;
  202. const TRandomAccessIterator it = first + step;
  203. if (*it < value)
  204. {
  205. first = it + 1;
  206. count -= step + 1;
  207. }
  208. else
  209. {
  210. count = step;
  211. }
  212. }
  213. return first;
  214. }
  215. /// Returns an iterator pointing to the first element in the range [first, last) that is greater than value.
  216. template <class TRandomAccessIterator, class T>
  217. TRandomAccessIterator UpperBound(TRandomAccessIterator first, TRandomAccessIterator last, const T& value)
  218. {
  219. unsigned count = last - first;
  220. while (count > 0)
  221. {
  222. const unsigned step = count / 2;
  223. const TRandomAccessIterator it = first + step;
  224. if (!(value < *it))
  225. {
  226. first = it + 1;
  227. count -= step + 1;
  228. }
  229. else
  230. {
  231. count = step;
  232. };
  233. }
  234. return first;
  235. }
  236. /// %Vector base class.
  237. /** Note that to prevent extra memory use due to vtable pointer, %VectorBase intentionally does not declare a virtual destructor
  238. and therefore %VectorBase pointers should never be used.
  239. */
  240. class URHO3D_API VectorBase
  241. {
  242. public:
  243. /// Construct.
  244. VectorBase() :
  245. size_(0),
  246. capacity_(0),
  247. buffer_(nullptr)
  248. {
  249. }
  250. /// Swap with another vector.
  251. void Swap(VectorBase& rhs)
  252. {
  253. Urho3D::Swap(size_, rhs.size_);
  254. Urho3D::Swap(capacity_, rhs.capacity_);
  255. Urho3D::Swap(buffer_, rhs.buffer_);
  256. }
  257. protected:
  258. static unsigned char* AllocateBuffer(unsigned size);
  259. /// Size of vector.
  260. unsigned size_;
  261. /// Buffer capacity.
  262. unsigned capacity_;
  263. /// Buffer.
  264. unsigned char* buffer_;
  265. };
  266. }