123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- #ifndef _AKLISTBARE_H
- #define _AKLISTBARE_H
- template <class T> struct AkListBareNextItem
- {
-
- static AkForceInline T *& Get( T * in_pItem )
- {
- return in_pItem->pNextItem;
- }
- };
- template <class T>
- class AkCountPolicyNoCount
- {
- protected:
- AkForceInline void ResetCount( T* ) {}
- AkForceInline void IncrementCount( T* ) {}
- AkForceInline void DecrementCount( T* ) {}
- };
- template <class T>
- class AkCountPolicyWithCount
- {
- public:
-
- AkForceInline unsigned int Length() const
- {
- return m_ulNumListItems;
- }
- protected:
- AkCountPolicyWithCount() :m_ulNumListItems( 0 ) {}
- AkForceInline void ResetCount( T* ) { m_ulNumListItems = 0; }
- AkForceInline void IncrementCount( T* ) { ++m_ulNumListItems; }
- AkForceInline void DecrementCount( T* ) { --m_ulNumListItems; }
- private:
- unsigned int m_ulNumListItems;
- };
- template <class T>
- class AkLastPolicyWithLast
- {
- public:
-
- AkForceInline T * Last() { return m_pLast; }
- AkForceInline const T * Last() const { return m_pLast; }
- protected:
- AkForceInline AkLastPolicyWithLast() : m_pLast( NULL ) {}
-
-
-
-
- AkForceInline void UpdateLast( T * in_pLast ) { m_pLast = in_pLast; }
- AkForceInline void SetLast( T * in_pLast ) { m_pLast = in_pLast; }
- AkForceInline void RemoveItem( T * in_pItem, T * in_pPrevItem )
- {
-
- if( in_pItem == m_pLast )
- {
-
- m_pLast = in_pPrevItem;
- }
- }
- AkForceInline void AddItem( T * in_pItem, T * in_pNextItem )
- {
-
-
- if ( in_pNextItem == NULL )
- m_pLast = in_pItem;
- }
- protected:
- T * m_pLast;
- };
- template <class T>
- class AkLastPolicyNoLast
- {
- protected:
-
-
-
-
- AkForceInline void UpdateLast( T * ) {}
-
-
- AkForceInline void RemoveItem( T *, T * ) {}
- AkForceInline void AddItem( T *, T * ) {}
- };
- template <class T, template <class> class U_NEXTITEM = AkListBareNextItem, template <class> class COUNT_POLICY = AkCountPolicyNoCount, template <class> class LAST_POLICY = AkLastPolicyWithLast > class AkListBare : public COUNT_POLICY< T >, public LAST_POLICY< T >
- {
- public:
-
- struct Iterator
- {
- T* pItem;
-
- inline Iterator& operator++()
- {
- AKASSERT( pItem );
- pItem = U_NEXTITEM<T>::Get( pItem );
- return *this;
- }
-
- inline T * operator*() const
- {
- AKASSERT( pItem );
- return pItem;
- }
-
- inline T * operator->() const
- {
- AKASSERT(pItem);
- return pItem;
- }
-
- bool operator !=( const Iterator& in_rOp ) const
- {
- return ( pItem != in_rOp.pItem );
- }
-
-
- bool operator ==( const Iterator& in_rOp ) const
- {
- return ( pItem == in_rOp.pItem );
- }
- };
-
-
- struct IteratorEx : public Iterator
- {
- T* pPrevItem;
-
- IteratorEx& operator++()
- {
- AKASSERT( this->pItem );
-
- pPrevItem = this->pItem;
- this->pItem = U_NEXTITEM<T>::Get( this->pItem );
-
- return *this;
- }
- };
-
- IteratorEx Erase( const IteratorEx& in_rIter )
- {
- IteratorEx returnedIt;
- returnedIt.pItem = U_NEXTITEM<T>::Get( in_rIter.pItem );
- returnedIt.pPrevItem = in_rIter.pPrevItem;
- RemoveItem( in_rIter.pItem, in_rIter.pPrevItem );
- return returnedIt;
- }
-
- IteratorEx Insert( const IteratorEx& in_rIter,
- T * in_pItem )
- {
- IteratorEx returnedIt;
- AddItem( in_pItem, in_rIter.pItem, in_rIter.pPrevItem );
- returnedIt = in_rIter;
- returnedIt.pPrevItem = in_pItem;
- return returnedIt;
- }
-
- inline Iterator End() const
- {
- Iterator returnedIt;
- returnedIt.pItem = NULL;
- return returnedIt;
- }
-
- inline IteratorEx BeginEx()
- {
- IteratorEx returnedIt;
-
- returnedIt.pItem = m_pFirst;
- returnedIt.pPrevItem = NULL;
-
- return returnedIt;
- }
-
- inline Iterator Begin() const
- {
- Iterator returnedIt;
-
- returnedIt.pItem = m_pFirst;
-
- return returnedIt;
- }
-
- inline IteratorEx FindEx( T * in_pItem )
- {
- IteratorEx it = BeginEx();
- for ( ; it != End(); ++it )
- {
- if ( it.pItem == in_pItem )
- break;
- }
- return it;
- }
-
- AkListBare()
- : m_pFirst( NULL )
- {
- }
-
-
- ~AkListBare()
- {
- }
-
- void Term()
- {
- RemoveAll();
- }
-
- void AddFirst( T * in_pItem )
- {
- if ( m_pFirst == NULL )
- {
- m_pFirst = in_pItem;
- LAST_POLICY<T>::UpdateLast( in_pItem );
- U_NEXTITEM<T>::Get( in_pItem ) = NULL;
- }
- else
- {
- U_NEXTITEM<T>::Get( in_pItem ) = m_pFirst;
- m_pFirst = in_pItem;
- }
- COUNT_POLICY<T>::IncrementCount( in_pItem );
- }
-
- void AddLast( T * in_pItem )
- {
- U_NEXTITEM<T>::Get( in_pItem ) = NULL;
- if ( m_pFirst == NULL )
- {
- m_pFirst = in_pItem;
- }
- else
- {
- U_NEXTITEM<T>::Get( LAST_POLICY<T>::Last() ) = in_pItem;
- }
- LAST_POLICY<T>::SetLast( in_pItem );
- COUNT_POLICY<T>::IncrementCount( in_pItem );
- }
-
- AKRESULT Remove( T * in_pItem )
- {
- IteratorEx it = FindEx( in_pItem );
- if ( it != End() )
- {
- Erase( it );
- return AK_Success;
- }
- return AK_Fail;
- }
-
- AKRESULT RemoveFirst()
- {
- if( m_pFirst == NULL )
- return AK_Fail;
- if ( U_NEXTITEM<T>::Get( m_pFirst ) == NULL )
- {
- m_pFirst = NULL;
- LAST_POLICY<T>::UpdateLast( NULL );
- }
- else
- {
- m_pFirst = U_NEXTITEM<T>::Get( m_pFirst );
- }
- COUNT_POLICY<T>::DecrementCount( m_pFirst );
- return AK_Success;
- }
-
- AkForceInline void RemoveAll()
- {
-
- m_pFirst = NULL;
- LAST_POLICY<T>::UpdateLast( NULL );
- COUNT_POLICY<T>::ResetCount( m_pFirst );
- }
-
- AkForceInline T * First()
- {
- return m_pFirst;
- }
-
- AkForceInline bool IsEmpty() const
- {
- return m_pFirst == NULL;
- }
-
- void RemoveItem( T * in_pItem, T * in_pPrevItem )
- {
-
- if( in_pItem == m_pFirst )
- {
-
- m_pFirst = U_NEXTITEM<T>::Get( in_pItem );
- }
- else
- {
-
- U_NEXTITEM<T>::Get( in_pPrevItem ) = U_NEXTITEM<T>::Get( in_pItem );
- }
- LAST_POLICY<T>::RemoveItem( in_pItem, in_pPrevItem );
- COUNT_POLICY<T>::DecrementCount( m_pFirst );
- }
-
- void AddItem( T * in_pItem, T * in_pNextItem, T * in_pPrevItem )
- {
- U_NEXTITEM<T>::Get( in_pItem ) = in_pNextItem;
- if ( in_pPrevItem == NULL )
- m_pFirst = in_pItem;
- else
- U_NEXTITEM<T>::Get( in_pPrevItem ) = in_pItem;
- LAST_POLICY<T>::AddItem( in_pItem, in_pNextItem );
-
- COUNT_POLICY<T>::IncrementCount( in_pItem );
- }
- void Transfer(AkListBare<T,U_NEXTITEM,COUNT_POLICY,LAST_POLICY>& in_src)
- {
- Term();
- m_pFirst = in_src.m_pFirst;
- in_src.m_pFirst = nullptr;
- }
- protected:
- T * m_pFirst;
- };
- #endif
|