123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- #pragma once
- #include <iterator>
- #include <functional>
- #include <algorithm>
- //-----------------------------------------------------------------------------
- // __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 <typename _IterType> 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;
- };
- }
|