ReverseRangeIterator.Base.tlh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #pragma once
  2. #include <iterator>
  3. //-----------------------------------------------------------------------------
  4. // BaseIterator
  5. // 迭代器的基本类
  6. //-----------------------------------------------------------------------------
  7. namespace ReverseRangeIterator
  8. {
  9. template <typename IterType> class __BaseIterator
  10. {
  11. public:
  12. // 构造一个全空的迭代器, 用于容器指针为空的情形
  13. __BaseIterator ()
  14. {
  15. m_Count = -1;
  16. m_Index = 0;
  17. // m_MinIndex = 0;
  18. // m_MaxIndex = m_Count - 1;
  19. }
  20. __BaseIterator (const __BaseIterator & from)
  21. {
  22. CopyAssign (from);
  23. }
  24. __BaseIterator (__BaseIterator && from)
  25. {
  26. MoveAssign (from);
  27. }
  28. ~__BaseIterator () {}
  29. public:
  30. inline __BaseIterator & operator = (const __BaseIterator & from)
  31. {
  32. CopyAssign (from);
  33. return (*this);
  34. }
  35. public:
  36. // inline operator bool () const { return (m_Index >= 0) && (m_Index < m_Count); }
  37. // inline operator bool () const { return (Iter._Ptr != nullptr) && (Iter != _end); }
  38. inline operator bool () const { return (m_Count > 0) && (Iter != _end); }
  39. inline int Index () const { return m_Index; }
  40. inline int Count () const { return m_Count; }
  41. inline void Next ()
  42. {
  43. if (m_Index >= m_MinIndex)
  44. {
  45. ++ Iter;
  46. -- m_Index;
  47. }
  48. }
  49. inline void operator ++ () // ++ Iter;
  50. {
  51. Next ();
  52. }
  53. inline void operator ++ (int) // Iter ++;
  54. {
  55. Next ();
  56. }
  57. public:
  58. inline void Restart ()
  59. {
  60. Iter = _begin;
  61. m_Index = m_Count - 1;
  62. if (m_Count > 0)
  63. {
  64. Step (m_MinIndex);
  65. _end = _begin + m_MaxIndex + 1;
  66. }
  67. }
  68. protected:
  69. inline void Step (int delta)
  70. {
  71. assert (delta >= 0);
  72. if (m_Index - delta < 0)
  73. {
  74. m_Index = -1;
  75. Iter = _end;
  76. }
  77. else
  78. {
  79. Iter += delta;
  80. m_Index -= delta;
  81. }
  82. }
  83. public:
  84. inline void SetRange (int minIndex, int maxIndex)
  85. {
  86. m_MinIndex = minIndex;
  87. m_MaxIndex = maxIndex;
  88. if (m_MinIndex < 0) m_MinIndex = 0;
  89. if (m_MinIndex >= m_Count) m_MinIndex = m_Count - 1;
  90. if (m_MaxIndex < 0) m_MaxIndex = 0;
  91. if (m_MaxIndex >= m_Count) m_MaxIndex = m_Count - 1;
  92. if (m_MinIndex > m_MaxIndex) m_MinIndex = m_MaxIndex;
  93. Restart ();
  94. }
  95. void GotoIndex (int Index)
  96. {
  97. Iter = _begin;
  98. m_Index = m_Count - 1;
  99. if (Index < m_MinIndex) Index = m_MinIndex;
  100. if (Index > m_MaxIndex) Index = m_MaxIndex;
  101. Step (Index);
  102. }
  103. protected:
  104. inline void CopyAssign (const __BaseIterator & from)
  105. {
  106. Iter = from.Iter;
  107. _begin = from._begin;
  108. _end = from._end;
  109. m_Count = from.m_Count;
  110. m_Index = from.m_Index;
  111. m_MinIndex = from.m_MinIndex;
  112. m_MaxIndex = from.m_MaxIndex;
  113. }
  114. inline void MoveAssign (__BaseIterator & from)
  115. {
  116. Iter = from.Iter;
  117. _begin = from._begin;
  118. _end = from._end;
  119. m_Count = from.m_Count;
  120. m_Index = from.m_Index;
  121. m_MinIndex = from.m_MinIndex;
  122. m_MaxIndex = from.m_MaxIndex;
  123. from.Iter = from._begin = from._end;
  124. from.m_Count = 0;
  125. from.m_Index = 0;
  126. }
  127. protected:
  128. typename IterType Iter;
  129. typename IterType _begin;
  130. typename IterType _end;
  131. int m_Count;
  132. int m_Index;
  133. int m_MinIndex;
  134. int m_MaxIndex;
  135. };
  136. }