#pragma once #include //----------------------------------------------------------------------------- // BaseIterator // 迭代器的基本类 //----------------------------------------------------------------------------- namespace ReverseIterator { template class __BaseIterator { public: // 构造一个全空的迭代器, 用于容器指针为空的情形 __BaseIterator () { m_Count = -1; m_Index = 0; } __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 >= 0) { ++ Iter; -- m_Index; } } inline void operator ++ () // ++ Iter; { Next (); } inline void operator ++ (int) // Iter ++; { Next (); } inline void Prev () { if (m_Index > 0) { -- Iter; ++ m_Index; } else { Iter = _end; m_Index = -1; } } inline void operator -- () // -- Iter; { Prev (); } inline void operator -- (int) // Iter --; { Prev (); } public: inline void Restart () { Iter = _begin; m_Index = m_Count - 1; } 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; } inline void MoveAssign (__BaseIterator & from) { Iter = from.Iter; _begin = from._begin; _end = from._end; m_Count = from.m_Count; m_Index = from.m_Index; 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; }; }