Iterator.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /// Random access iterator
  25. template <class T> class RandomAccessIterator
  26. {
  27. public:
  28. /// Construct
  29. explicit RandomAccessIterator(T* ptr) :
  30. ptr_(ptr)
  31. {
  32. }
  33. /// Point to the object
  34. T* operator -> () const { return ptr_; }
  35. /// Dereference the object
  36. T& operator * () const { return *ptr_; }
  37. /// Convert to a raw pointer
  38. operator T* () const { return ptr_; }
  39. /// Increment the pointer
  40. void operator ++ () { ++ptr_; }
  41. /// Decrement the pointer
  42. void operator -- () { --ptr_; }
  43. /// Add an offset to the pointer
  44. void operator += (unsigned value) { ptr_ += value; }
  45. /// Subtract an offset from the pointer
  46. void operator -= (unsigned value) { ptr_ -= value; }
  47. /// Add an offset to the pointer
  48. RandomAccessIterator operator + (unsigned value) { return RandomAccessIterator(ptr_ + value); }
  49. /// Subtract an offset from the pointer
  50. RandomAccessIterator operator - (unsigned value) { return RandomAccessIterator(ptr_ - value); }
  51. /// Calculate offset to another iterator
  52. int operator - (const RandomAccessIterator& rhs) const { return ptr_ - rhs.ptr_; }
  53. /// Test for equality with another iterator
  54. bool operator == (const RandomAccessIterator& rhs) const { return ptr_ == rhs.ptr_; }
  55. /// Test for inequality with another iterator
  56. bool operator != (const RandomAccessIterator& rhs) const { return ptr_ != rhs.ptr_; }
  57. private:
  58. /// Pointer
  59. T* ptr_;
  60. };
  61. /// Random access const iterator
  62. template <class T> class RandomAccessConstIterator
  63. {
  64. public:
  65. /// Construct
  66. explicit RandomAccessConstIterator(const T* ptr) :
  67. ptr_(ptr)
  68. {
  69. }
  70. /// Point to the object
  71. const T* operator -> () const { return ptr_; }
  72. /// Dereference the object
  73. const T& operator * () const { return *ptr_; }
  74. /// Convert to a raw pointer
  75. operator const T* () const { return ptr_; }
  76. /// Increment the pointer
  77. void operator ++ () { ++ptr_; }
  78. /// Decrement the pointer
  79. void operator -- () { --ptr_; }
  80. /// Add an offset to the pointer
  81. void operator += (unsigned value) { ptr_ += value; }
  82. /// Subtract an offset from the pointer
  83. void operator -= (unsigned value) { ptr_ -= value; }
  84. /// Add an offset to the pointer
  85. RandomAccessConstIterator operator + (unsigned value) { return RandomAccessConstIterator(ptr_ + value); }
  86. /// Subtract an offset from the pointer
  87. RandomAccessConstIterator operator - (unsigned value) { return RandomAccessConstIterator(ptr_ - value); }
  88. /// Calculate offset to another iterator
  89. int operator - (const RandomAccessConstIterator& rhs) const { return ptr_ - rhs.ptr_; }
  90. /// Test for equality with another iterator
  91. bool operator == (const RandomAccessConstIterator& rhs) const { return ptr_ == rhs.ptr_; }
  92. /// Test for inequality with another iterator
  93. bool operator != (const RandomAccessConstIterator& rhs) const { return ptr_ != rhs.ptr_; }
  94. private:
  95. /// Pointer
  96. const T* ptr_;
  97. };
  98. /// List node base
  99. struct ListNodeBase
  100. {
  101. ListNodeBase() :
  102. prev_(0),
  103. next_(0)
  104. {
  105. }
  106. /// Previous node
  107. ListNodeBase* prev_;
  108. /// Next node
  109. ListNodeBase* next_;
  110. };
  111. /// Skip list node base
  112. struct SkipListNodeBase : public ListNodeBase
  113. {
  114. /// Node height
  115. unsigned height_;
  116. /// Skip list pointers for heights > 1
  117. SkipListNodeBase** levels_;
  118. /// Return next node on a specific height
  119. SkipListNodeBase* GetNext(unsigned height) const
  120. {
  121. if (!height)
  122. return static_cast<SkipListNodeBase*>(next_);
  123. else
  124. return levels_[height - 1];
  125. }
  126. /// Return previous node
  127. SkipListNodeBase* GetPrev() const { return static_cast<SkipListNodeBase*>(prev_); }
  128. // Set next node on a specific height
  129. void SetNext(unsigned height, SkipListNodeBase* node)
  130. {
  131. if (!height)
  132. next_ = node;
  133. else
  134. levels_[height - 1] = node;
  135. }
  136. /// Set previous node
  137. void SetPrev(SkipListNodeBase* node) { prev_ = node; }
  138. };
  139. /// List iterator base class
  140. class ListIteratorBase
  141. {
  142. public:
  143. /// Test for equality with another iterator
  144. bool operator == (const ListIteratorBase& rhs) const { return ptr_ == rhs.ptr_; }
  145. /// Test for inequality with another iterator
  146. bool operator != (const ListIteratorBase& rhs) const { return ptr_ != rhs.ptr_; }
  147. /// Go to the next node
  148. void operator ++ ()
  149. {
  150. if (ptr_->next_)
  151. ptr_ = ptr_->next_;
  152. }
  153. /// Go to the previous node
  154. void operator -- ()
  155. {
  156. if (ptr_->prev_)
  157. ptr_ = ptr_->prev_;
  158. }
  159. protected:
  160. /// Node pointer
  161. ListNodeBase* ptr_;
  162. };