VectorBase.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #include "Atomic/Atomic.h"
  24. #include "../Container/Swap.h"
  25. namespace Atomic
  26. {
  27. /// Random access iterator.
  28. template <class T> struct RandomAccessIterator
  29. {
  30. /// Construct.
  31. RandomAccessIterator() :
  32. ptr_(0)
  33. {
  34. }
  35. /// Construct with an object pointer.
  36. explicit RandomAccessIterator(T* ptr) :
  37. ptr_(ptr)
  38. {
  39. }
  40. /// Point to the object.
  41. T* operator ->() const { return ptr_; }
  42. /// Dereference the object.
  43. T& operator *() const { return *ptr_; }
  44. /// Preincrement the pointer.
  45. RandomAccessIterator<T>& operator ++()
  46. {
  47. ++ptr_;
  48. return *this;
  49. }
  50. /// Postincrement the pointer.
  51. RandomAccessIterator<T> operator ++(int)
  52. {
  53. RandomAccessIterator<T> it = *this;
  54. ++ptr_;
  55. return it;
  56. }
  57. /// Predecrement the pointer.
  58. RandomAccessIterator<T>& operator --()
  59. {
  60. --ptr_;
  61. return *this;
  62. }
  63. /// Postdecrement the pointer.
  64. RandomAccessIterator<T> operator --(int)
  65. {
  66. RandomAccessIterator<T> it = *this;
  67. --ptr_;
  68. return it;
  69. }
  70. /// Add an offset to the pointer.
  71. RandomAccessIterator<T>& operator +=(int value)
  72. {
  73. ptr_ += value;
  74. return *this;
  75. }
  76. /// Subtract an offset from the pointer.
  77. RandomAccessIterator<T>& operator -=(int value)
  78. {
  79. ptr_ -= value;
  80. return *this;
  81. }
  82. /// Add an offset to the pointer.
  83. RandomAccessIterator<T> operator +(int value) const { return RandomAccessIterator<T>(ptr_ + value); }
  84. /// Subtract an offset from the pointer.
  85. RandomAccessIterator<T> operator -(int value) const { return RandomAccessIterator<T>(ptr_ - value); }
  86. /// Calculate offset to another iterator.
  87. int operator -(const RandomAccessIterator& rhs) const { return (int)(ptr_ - rhs.ptr_); }
  88. /// Test for equality with another iterator.
  89. bool operator ==(const RandomAccessIterator& rhs) const { return ptr_ == rhs.ptr_; }
  90. /// Test for inequality with another iterator.
  91. bool operator !=(const RandomAccessIterator& rhs) const { return ptr_ != rhs.ptr_; }
  92. /// Test for less than with another iterator.
  93. bool operator <(const RandomAccessIterator& rhs) const { return ptr_ < rhs.ptr_; }
  94. /// Test for greater than with another iterator.
  95. bool operator >(const RandomAccessIterator& rhs) const { return ptr_ > rhs.ptr_; }
  96. /// Test for less than or equal with another iterator.
  97. bool operator <=(const RandomAccessIterator& rhs) const { return ptr_ <= rhs.ptr_; }
  98. /// Test for greater than or equal with another iterator.
  99. bool operator >=(const RandomAccessIterator& rhs) const { return ptr_ >= rhs.ptr_; }
  100. /// Pointer.
  101. T* ptr_;
  102. };
  103. /// Random access const iterator.
  104. template <class T> struct RandomAccessConstIterator
  105. {
  106. /// Construct.
  107. RandomAccessConstIterator() :
  108. ptr_(0)
  109. {
  110. }
  111. /// Construct with an object pointer.
  112. explicit RandomAccessConstIterator(T* ptr) :
  113. ptr_(ptr)
  114. {
  115. }
  116. /// Construct from a non-const iterator.
  117. RandomAccessConstIterator(const RandomAccessIterator<T>& rhs) :
  118. ptr_(rhs.ptr_)
  119. {
  120. }
  121. /// Assign from a non-const iterator.
  122. RandomAccessConstIterator<T>& operator =(const RandomAccessIterator<T>& rhs)
  123. {
  124. ptr_ = rhs.ptr_;
  125. return *this;
  126. }
  127. /// Point to the object.
  128. const T* operator ->() const { return ptr_; }
  129. /// Dereference the object.
  130. const T& operator *() const { return *ptr_; }
  131. /// Preincrement the pointer.
  132. RandomAccessConstIterator<T>& operator ++()
  133. {
  134. ++ptr_;
  135. return *this;
  136. }
  137. /// Postincrement the pointer.
  138. RandomAccessConstIterator<T> operator ++(int)
  139. {
  140. RandomAccessConstIterator<T> it = *this;
  141. ++ptr_;
  142. return it;
  143. }
  144. /// Predecrement the pointer.
  145. RandomAccessConstIterator<T>& operator --()
  146. {
  147. --ptr_;
  148. return *this;
  149. }
  150. /// Postdecrement the pointer.
  151. RandomAccessConstIterator<T> operator --(int)
  152. {
  153. RandomAccessConstIterator<T> it = *this;
  154. --ptr_;
  155. return it;
  156. }
  157. /// Add an offset to the pointer.
  158. RandomAccessConstIterator<T>& operator +=(int value)
  159. {
  160. ptr_ += value;
  161. return *this;
  162. }
  163. /// Subtract an offset from the pointer.
  164. RandomAccessConstIterator<T>& operator -=(int value)
  165. {
  166. ptr_ -= value;
  167. return *this;
  168. }
  169. /// Add an offset to the pointer.
  170. RandomAccessConstIterator<T> operator +(int value) const { return RandomAccessConstIterator<T>(ptr_ + value); }
  171. /// Subtract an offset from the pointer.
  172. RandomAccessConstIterator<T> operator -(int value) const { return RandomAccessConstIterator<T>(ptr_ - value); }
  173. /// Calculate offset to another iterator.
  174. int operator -(const RandomAccessConstIterator& rhs) const { return (int)(ptr_ - rhs.ptr_); }
  175. /// Test for equality with another iterator.
  176. bool operator ==(const RandomAccessConstIterator& rhs) const { return ptr_ == rhs.ptr_; }
  177. /// Test for inequality with another iterator.
  178. bool operator !=(const RandomAccessConstIterator& rhs) const { return ptr_ != rhs.ptr_; }
  179. /// Test for less than with another iterator.
  180. bool operator <(const RandomAccessConstIterator& rhs) const { return ptr_ < rhs.ptr_; }
  181. /// Test for greater than with another iterator.
  182. bool operator >(const RandomAccessConstIterator& rhs) const { return ptr_ > rhs.ptr_; }
  183. /// Test for less than or equal with another iterator.
  184. bool operator <=(const RandomAccessConstIterator& rhs) const { return ptr_ <= rhs.ptr_; }
  185. /// Test for greater than or equal with another iterator.
  186. bool operator >=(const RandomAccessConstIterator& rhs) const { return ptr_ >= rhs.ptr_; }
  187. /// Pointer.
  188. T* ptr_;
  189. };
  190. /// Returns an iterator pointing to the first element in the range [first, last) that is not less than value.
  191. template <class TRandomAccessIterator, class T>
  192. TRandomAccessIterator LowerBound(TRandomAccessIterator first, TRandomAccessIterator last, const T& value)
  193. {
  194. unsigned count = last - first;
  195. while (count > 0)
  196. {
  197. const unsigned step = count / 2;
  198. const TRandomAccessIterator it = first + step;
  199. if (*it < value)
  200. {
  201. first = it + 1;
  202. count -= step + 1;
  203. }
  204. else
  205. {
  206. count = step;
  207. }
  208. }
  209. return first;
  210. }
  211. /// Returns an iterator pointing to the first element in the range [first, last) that is greater than value.
  212. template <class TRandomAccessIterator, class T>
  213. TRandomAccessIterator UpperBound(TRandomAccessIterator first, TRandomAccessIterator last, const T& value)
  214. {
  215. unsigned count = last - first;
  216. while (count > 0)
  217. {
  218. const unsigned step = count / 2;
  219. const TRandomAccessIterator it = first + step;
  220. if (!(value < *it))
  221. {
  222. first = it + 1;
  223. count -= step + 1;
  224. }
  225. else
  226. {
  227. count = step;
  228. };
  229. }
  230. return first;
  231. }
  232. /// %Vector base class.
  233. /** Note that to prevent extra memory use due to vtable pointer, %VectorBase intentionally does not declare a virtual destructor
  234. and therefore %VectorBase pointers should never be used.
  235. */
  236. class ATOMIC_API VectorBase
  237. {
  238. public:
  239. /// Construct.
  240. VectorBase() :
  241. size_(0),
  242. capacity_(0),
  243. buffer_(0)
  244. {
  245. }
  246. /// Swap with another vector.
  247. void Swap(VectorBase& rhs)
  248. {
  249. Atomic::Swap(size_, rhs.size_);
  250. Atomic::Swap(capacity_, rhs.capacity_);
  251. Atomic::Swap(buffer_, rhs.buffer_);
  252. }
  253. protected:
  254. static unsigned char* AllocateBuffer(unsigned size);
  255. /// Size of vector.
  256. unsigned size_;
  257. /// Buffer capacity.
  258. unsigned capacity_;
  259. /// Buffer.
  260. unsigned char* buffer_;
  261. };
  262. }