Iter.h 7.4 KB

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