#pragma once #include //----------------------------------------------------------------------------- // BaseIterator // 迭代器的基本类 //----------------------------------------------------------------------------- namespace RangeIterator { template class __BaseIterator { public: // 构造一个全空的迭代器, 用于容器指针为空的情形 __BaseIterator () { m_Count = -1; m_Index = 0; // m_MinIndex = 0; // m_MaxIndex = m_Count - 1; } __BaseIterator (const __BaseIterator & from) { CopyAssign (from); } __BaseIterator (__BaseIterator && from) { MoveAssign (from); } ~__BaseIterator () {} public: inline __BaseIterator & operator = (const __BaseIterator & from) { CopyAssign (from); return (*this); } public: // inline operator bool () const { return (m_Index >= 0) && (m_Index < m_Count); } // inline operator bool () const { return (Iter._Ptr != nullptr) && (Iter != _end); } inline operator bool () const { return (m_Count > 0) && (Iter != _end); } inline int Index () const { return m_Index; } inline int Count () const { return m_Count; } inline void Next () { if (m_Index <= m_MaxIndex) { ++ Iter; ++ m_Index; } } inline void operator ++ () // ++ Iter; { Next (); } inline void operator ++ (int) // Iter ++; { Next (); } public: inline void Restart () { Iter = _begin; m_Index = 0; if (m_Count > 0) { Step (m_MinIndex); _end = _begin + m_MaxIndex + 1; } } protected: inline void Step (int delta) { assert (delta >= 0); if (m_Index + delta >= m_Count) { m_Index = m_Count; Iter = _end; } else { Iter += delta; m_Index += delta; } } public: inline void SetRange (int minIndex, int maxIndex) { m_MinIndex = minIndex; m_MaxIndex = maxIndex; if (m_MinIndex < 0) m_MinIndex = 0; if (m_MinIndex >= m_Count) m_MinIndex = m_Count - 1; if (m_MaxIndex < 0) m_MaxIndex = 0; if (m_MaxIndex >= m_Count) m_MaxIndex = m_Count - 1; if (m_MinIndex > m_MaxIndex) m_MinIndex = m_MaxIndex; Restart (); } void GotoIndex (int Index) { Iter = _begin; m_Index = 0; if (Index < m_MinIndex) Index = m_MinIndex; if (Index > m_MaxIndex) Index = m_MaxIndex; Step (Index); } protected: inline void CopyAssign (const __BaseIterator & from) { Iter = from.Iter; _begin = from._begin; _end = from._end; m_Count = from.m_Count; m_Index = from.m_Index; m_MinIndex = from.m_MinIndex; m_MaxIndex = from.m_MaxIndex; } inline void MoveAssign (__BaseIterator & from) { Iter = from.Iter; _begin = from._begin; _end = from._end; m_Count = from.m_Count; m_Index = from.m_Index; m_MinIndex = from.m_MinIndex; m_MaxIndex = from.m_MaxIndex; from.Iter = from._begin = from._end; from.m_Count = 0; from.m_Index = 0; } protected: typename IterType Iter; typename IterType _begin; typename IterType _end; int m_Count; int m_Index; int m_MinIndex; int m_MaxIndex; }; }