#pragma once #include #include #include //----------------------------------------------------------------------------- // __CoreIterator // 迭代器的基本类 //----------------------------------------------------------------------------- namespace Iterator { class __CoreIterator { public: // 构造一个全空的迭代器, 用于容器指针为空的情形 __CoreIterator () { m_Count = -1; m_Index = 0; } __CoreIterator (const __CoreIterator & from) { CopyAssign (from); } __CoreIterator (__CoreIterator && from) { MoveAssign (from); } ~__CoreIterator () {} public: inline __CoreIterator & operator = (const __CoreIterator & from) { CopyAssign (from); return (*this); } inline __CoreIterator & operator = (__CoreIterator && from) { MoveAssign (from); return (*this); } public: inline virtual bool IsEmpty () const { return (m_Count <= 0) || (m_Index >= m_Count); } inline operator bool () const { return ! IsEmpty (); } inline int Index () const { return m_Index; } // 当前下标 inline int Count () const { return m_Count; } // 总数 inline int Remainder () const { return m_Count - m_Index; } // 剩下 inline virtual void Next () { if (m_Index < m_Count) { ++ m_Index; } } inline void operator ++ () // ++ Iter; { Next (); } inline void operator ++ (int) // Iter ++; { Next (); } inline void Prev () { if (m_Index > 0) { -- m_Index; } else { m_Index = -1; } } inline void operator -- () // -- Iter; { Prev (); } inline void operator -- (int) // Iter --; { Prev (); } public: inline void Restart () { m_Index = 0; } inline void GoToEnd () { m_Index = m_Count; } // 只要前面的 N 项 inline void Take (int N) { int last = m_Index + N; if (last < m_Count) m_Count = last; } protected: inline void CopyAssign (const __CoreIterator & from) { m_Count = from.m_Count; m_Index = from.m_Index; } inline void MoveAssign (__CoreIterator & from) { m_Count = from.m_Count; m_Index = from.m_Index; from.m_Count = 0; from.m_Index = 0; } protected: int m_Count; int m_Index; }; } //----------------------------------------------------------------------------- // __BaseIterator //----------------------------------------------------------------------------- namespace Iterator { template class __BaseIterator : public __CoreIterator { typedef __CoreIterator inherited; public: using IterType = typename _IterType; public: // 构造一个全空的迭代器, 用于容器指针为空的情形 __BaseIterator () { } __BaseIterator (const __BaseIterator & from) { CopyAssign (from); } __BaseIterator (__BaseIterator && from) { MoveAssign (from); } ~__BaseIterator () {} public: inline __BaseIterator & operator = (const __BaseIterator & from) { CopyAssign (from); return (*this); } inline __BaseIterator & operator = (__BaseIterator && from) { MoveAssign (from); return (*this); } public: inline bool operator == (const __BaseIterator & from) const { return m_stdIter == from.m_stdIter; } public: inline virtual bool IsEmpty () const override { if (inherited::IsEmpty ()) return true; return (m_stdIter == m_stdEnd); } inline virtual void Next () override { if (m_Index < m_Count) { ++ m_stdIter; ++ m_Index; } } inline void operator ++ () // ++ Iter; { Next (); } inline void operator ++ (int) // Iter ++; { Next (); } inline void Prev () { if (m_Index > 0) { -- m_stdIter; -- m_Index; } else { m_stdIter = m_stdEnd; m_Index = -1; } } inline void operator -- () // -- Iter; { Prev (); } inline void operator -- (int) // Iter --; { Prev (); } public: inline void Restart () { m_stdIter = m_stdBegin; m_Index = 0; } inline void GoToEnd () { m_stdIter = m_stdEnd; m_Index = m_Count; } public: inline IterType stdBegin () { return m_stdBegin; } inline IterType stdCurrent () { return m_stdIter; } inline IterType stdEnd () { return m_stdEnd; } protected: inline void CopyAssign (const __BaseIterator & from) { inherited::CopyAssign (from); m_stdIter = from.m_stdIter; m_stdBegin = from.m_stdBegin; m_stdEnd = from.m_stdEnd; } inline void MoveAssign (__BaseIterator & from) { inherited::MoveAssign (from); m_stdIter = from.m_stdIter; m_stdBegin = from.m_stdBegin; m_stdEnd = from.m_stdEnd; from.m_stdIter = from.m_stdBegin = from.m_stdEnd; } protected: typename IterType m_stdIter; typename IterType m_stdBegin; typename IterType m_stdEnd; }; }