Iter.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Base/PrimitiveTypes.h"
  5. #include <iterator>
  6. namespace Urho3D
  7. {
  8. /// Random access iterator.
  9. template <class T> struct RandomAccessIterator
  10. {
  11. using iterator_category = std::random_access_iterator_tag;
  12. using difference_type = i32;
  13. using value_type = T;
  14. using pointer = T*;
  15. using reference = T&;
  16. /// Construct.
  17. constexpr RandomAccessIterator() :
  18. ptr_(nullptr)
  19. {
  20. }
  21. /// Construct with an object pointer.
  22. explicit constexpr RandomAccessIterator(T* ptr) :
  23. ptr_(ptr)
  24. {
  25. }
  26. /// Point to the object.
  27. constexpr T* operator ->() const { return ptr_; }
  28. /// Dereference the object.
  29. constexpr T& operator *() const { return *ptr_; }
  30. /// Preincrement the pointer.
  31. constexpr RandomAccessIterator<T>& operator ++()
  32. {
  33. ++ptr_;
  34. return *this;
  35. }
  36. /// Postincrement the pointer.
  37. constexpr RandomAccessIterator<T> operator ++(int)
  38. {
  39. RandomAccessIterator<T> it = *this;
  40. ++ptr_;
  41. return it;
  42. }
  43. /// Predecrement the pointer.
  44. constexpr RandomAccessIterator<T>& operator --()
  45. {
  46. --ptr_;
  47. return *this;
  48. }
  49. /// Postdecrement the pointer.
  50. constexpr RandomAccessIterator<T> operator --(int)
  51. {
  52. RandomAccessIterator<T> it = *this;
  53. --ptr_;
  54. return it;
  55. }
  56. /// Add an offset to the pointer.
  57. constexpr RandomAccessIterator<T>& operator +=(int value)
  58. {
  59. ptr_ += value;
  60. return *this;
  61. }
  62. /// Subtract an offset from the pointer.
  63. constexpr RandomAccessIterator<T>& operator -=(int value)
  64. {
  65. ptr_ -= value;
  66. return *this;
  67. }
  68. /// Add an offset to the pointer.
  69. constexpr RandomAccessIterator<T> operator +(int value) const { return RandomAccessIterator<T>(ptr_ + value); }
  70. /// Subtract an offset from the pointer.
  71. constexpr RandomAccessIterator<T> operator -(int value) const { return RandomAccessIterator<T>(ptr_ - value); }
  72. /// Calculate offset to another iterator.
  73. constexpr int operator -(const RandomAccessIterator& rhs) const { return (int)(ptr_ - rhs.ptr_); }
  74. /// Test for equality with another iterator.
  75. constexpr bool operator ==(const RandomAccessIterator& rhs) const { return ptr_ == rhs.ptr_; }
  76. /// Test for inequality with another iterator.
  77. constexpr bool operator !=(const RandomAccessIterator& rhs) const { return ptr_ != rhs.ptr_; }
  78. /// Test for less than with another iterator.
  79. constexpr bool operator <(const RandomAccessIterator& rhs) const { return ptr_ < rhs.ptr_; }
  80. /// Test for greater than with another iterator.
  81. constexpr bool operator >(const RandomAccessIterator& rhs) const { return ptr_ > rhs.ptr_; }
  82. /// Test for less than or equal with another iterator.
  83. constexpr bool operator <=(const RandomAccessIterator& rhs) const { return ptr_ <= rhs.ptr_; }
  84. /// Test for greater than or equal with another iterator.
  85. constexpr bool operator >=(const RandomAccessIterator& rhs) const { return ptr_ >= rhs.ptr_; }
  86. /// Pointer.
  87. T* ptr_;
  88. };
  89. /// Random access const iterator.
  90. template <class T> struct RandomAccessConstIterator
  91. {
  92. using iterator_category = std::random_access_iterator_tag;
  93. using difference_type = i32;
  94. using value_type = T;
  95. using pointer = T*;
  96. using reference = T&;
  97. /// Construct.
  98. constexpr RandomAccessConstIterator() :
  99. ptr_(0)
  100. {
  101. }
  102. /// Construct with an object pointer.
  103. constexpr explicit RandomAccessConstIterator(const T* ptr) :
  104. ptr_(ptr)
  105. {
  106. }
  107. /// Construct from a non-const iterator.
  108. constexpr RandomAccessConstIterator(const RandomAccessIterator<T>& rhs) : // NOLINT(google-explicit-constructor)
  109. ptr_(rhs.ptr_)
  110. {
  111. }
  112. /// Assign from a non-const iterator.
  113. constexpr RandomAccessConstIterator<T>& operator =(const RandomAccessIterator<T>& rhs)
  114. {
  115. ptr_ = rhs.ptr_;
  116. return *this;
  117. }
  118. /// Point to the object.
  119. constexpr const T* operator ->() const { return ptr_; }
  120. /// Dereference the object.
  121. constexpr const T& operator *() const { return *ptr_; }
  122. /// Preincrement the pointer.
  123. constexpr RandomAccessConstIterator<T>& operator ++()
  124. {
  125. ++ptr_;
  126. return *this;
  127. }
  128. /// Postincrement the pointer.
  129. constexpr RandomAccessConstIterator<T> operator ++(int)
  130. {
  131. RandomAccessConstIterator<T> it = *this;
  132. ++ptr_;
  133. return it;
  134. }
  135. /// Predecrement the pointer.
  136. constexpr RandomAccessConstIterator<T>& operator --()
  137. {
  138. --ptr_;
  139. return *this;
  140. }
  141. /// Postdecrement the pointer.
  142. constexpr RandomAccessConstIterator<T> operator --(int)
  143. {
  144. RandomAccessConstIterator<T> it = *this;
  145. --ptr_;
  146. return it;
  147. }
  148. /// Add an offset to the pointer.
  149. constexpr RandomAccessConstIterator<T>& operator +=(int value)
  150. {
  151. ptr_ += value;
  152. return *this;
  153. }
  154. /// Subtract an offset from the pointer.
  155. constexpr RandomAccessConstIterator<T>& operator -=(int value)
  156. {
  157. ptr_ -= value;
  158. return *this;
  159. }
  160. /// Add an offset to the pointer.
  161. constexpr RandomAccessConstIterator<T> operator +(int value) const { return RandomAccessConstIterator<T>(ptr_ + value); }
  162. /// Subtract an offset from the pointer.
  163. constexpr RandomAccessConstIterator<T> operator -(int value) const { return RandomAccessConstIterator<T>(ptr_ - value); }
  164. /// Calculate offset to another iterator.
  165. constexpr int operator -(const RandomAccessConstIterator& rhs) const { return (int)(ptr_ - rhs.ptr_); }
  166. /// Test for equality with another iterator.
  167. constexpr bool operator ==(const RandomAccessConstIterator& rhs) const { return ptr_ == rhs.ptr_; }
  168. /// Test for inequality with another iterator.
  169. constexpr bool operator !=(const RandomAccessConstIterator& rhs) const { return ptr_ != rhs.ptr_; }
  170. /// Test for less than with another iterator.
  171. constexpr bool operator <(const RandomAccessConstIterator& rhs) const { return ptr_ < rhs.ptr_; }
  172. /// Test for greater than with another iterator.
  173. constexpr bool operator >(const RandomAccessConstIterator& rhs) const { return ptr_ > rhs.ptr_; }
  174. /// Test for less than or equal with another iterator.
  175. constexpr bool operator <=(const RandomAccessConstIterator& rhs) const { return ptr_ <= rhs.ptr_; }
  176. /// Test for greater than or equal with another iterator.
  177. constexpr bool operator >=(const RandomAccessConstIterator& rhs) const { return ptr_ >= rhs.ptr_; }
  178. /// Pointer.
  179. const T* ptr_;
  180. };
  181. }