//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 05/11/2003 // PROJECT: // SUBSYSTEM: // FILE: LgArray.h // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Header file of LgArray.h //---------------------------------------------------------------------------- #ifndef __LG__ARRAY__H__INCLUDED__ #define __LG__ARRAY__H__INCLUDED__ #ifdef __BCPLUSPLUS__ #pragma warn -rch #pragma warn -ccc #pragma warn -winl template #else template #endif class CLgArray { public: CLgArray() { m_pCurrent = m_pTail = m_pHead = NULL; m_nCount = 0; } CLgArray( CLgArray& array ) { m_pCurrent = m_pTail = m_pHead = NULL; m_nCount = 0; // copy array for( INT i = 0; i < array.GetCount(); i++ ) Add( array[i] ); } virtual ~CLgArray() { RemoveAll(); } public: CLgArray& operator =( CLgArray& array ) { RemoveAll(); // copy array for( INT i = 0; i < array.GetCount(); i++ ) Add( array[i] ); return *this; } public: virtual BOOL AddEmpty() { LPNODE pNode = new NODE; if( !pNode ) return FALSE; // place element pNode->pPrev = m_pTail; pNode->pNext = NULL; pNode->nIndex = m_nCount; // update last & head pointer if( m_pTail ) m_pTail->pNext = pNode; else m_pHead = pNode; // set last pointer & store ponter to current element m_pCurrent = m_pTail = pNode; // increase count m_nCount++; return TRUE; } virtual ARGTYPE AddEmptyRef() { if( AddEmpty() ) return GetAt( GetCount()-1 ); static TYPE data; LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgArray::AddEmptyRef"), _T("Can't allocate new item, returned default item") ); return data; } virtual BOOL Add( ARGTYPE data ) { LPNODE pNode = new NODE; if( !pNode ) return FALSE; // place element pNode->pPrev = m_pTail; pNode->pNext = NULL; pNode->tData = data; pNode->nIndex = m_nCount; // update last & head pointer if( m_pTail ) m_pTail->pNext = pNode; else m_pHead = pNode; // set last pointer & store ponter to current element m_pCurrent = m_pTail = pNode; // increase count m_nCount++; return TRUE; } virtual BOOL Remove( INT nIndex ) { if( nIndex < 0 || nIndex >= m_nCount ) return FALSE; // find index LPNODE pNode = FindItem( nIndex ); if( !pNode ) return FALSE; // update prev element if( pNode->pPrev ) pNode->pPrev->pNext = pNode->pNext; else m_pHead = pNode->pNext; // update next element if( pNode->pNext ) { // set current pointer m_pCurrent = pNode->pNext; m_pCurrent->pPrev = pNode->pPrev; // delete node delete pNode; // update indexes pNode = m_pCurrent; while( pNode ) { pNode->nIndex--; pNode = pNode->pNext; } } else { // set tail pointer m_pCurrent = m_pTail = pNode->pPrev; // delete node delete pNode; } // update counter m_nCount--; return TRUE; } virtual INT InsertAt( INT nIndex, ARGTYPE data ) { if( nIndex < 0 || nIndex > m_nCount ) return FALSE; if( nIndex == m_nCount ) return Add( data ); // find index LPNODE pIndexNode = FindItem( nIndex ); if( !pIndexNode ) return FALSE; LPNODE pNode = new NODE; if( !pNode ) return FALSE; // place element pNode->pPrev = pIndexNode->pPrev; pNode->pNext = pIndexNode; pNode->tData = data; pNode->nIndex = nIndex; // check head position if( pIndexNode->pPrev ) pIndexNode->pPrev->pNext = pNode; else m_pHead = pNode; // update node that was on index place pointer pIndexNode->pPrev = pNode; // update current position m_pCurrent = pNode; // update indexes pNode = pIndexNode; while( pNode ) { pNode->nIndex++; pNode = pNode->pNext; } // increase count m_nCount++; return nIndex; } virtual BOOL RemoveAll() { if( !m_nCount || !m_pTail || !m_pHead ) return TRUE; LPNODE pNode = m_pHead; while( pNode ) { LPNODE pRemoveNode = pNode; pNode = pNode->pNext; delete pRemoveNode; } m_pCurrent = m_pTail = m_pHead = NULL; m_nCount = 0; return TRUE; } virtual BOOL RemoveHead() { return Remove( 0 ); } virtual BOOL RemoveTail() { return Remove( GetCount() - 1 ); } public: enum { INVALID_INDEX = -1 }; ARGTYPE operator[] ( int nIndex ) { return GetAt( nIndex ); } ARGTYPE GetAt( int nIndex ) { static TYPE data; if( nIndex < 0 || nIndex >= m_nCount ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgArray::GetAt"), _T("Index %d is out of bounds [0..%d)"), nIndex, m_nCount ); return data; } // find index m_pCurrent = FindItem( nIndex ); if( !m_pCurrent ) return data; return m_pCurrent->tData; } ARGTYPE GetHead() { return GetAt( 0 ); } ARGTYPE GetTail() { return GetAt( GetCount() - 1 ); } INT GetCount() { return m_nCount; } INT Find( ARGTYPE data ) { LPNODE pNode = m_pHead; while( pNode ) { if( pNode->tData == data ) return pNode->nIndex; pNode = pNode->pNext; } return INVALID_INDEX; } public: friend BOOL operator == ( CLgArray& a1, CLgArray& a2 ) { if( a1.GetCount() != a2.GetCount() ) return FALSE; for( INT i = 0; i < a1.GetCount(); i++ ) if( !(a1[i] == a2[i]) ) return FALSE; return TRUE; } private: struct NODE { struct NODE far *pNext; struct NODE far *pPrev; TYPE tData; INT nIndex; }; typedef NODE* PNODE; typedef NODE far* LPNODE; private: LPNODE FindItem( INT nIndex ) { // if index before then current if( m_pCurrent->nIndex > nIndex ) { // if closer to current if( ( nIndex - m_pHead->nIndex ) > ( m_pCurrent->nIndex - nIndex ) ) { LPNODE pNode = m_pCurrent; while( pNode->nIndex != nIndex ) pNode = pNode->pPrev; return pNode; } else { LPNODE pNode = m_pHead; while( pNode->nIndex != nIndex ) pNode = pNode->pNext; return pNode; } } else { // if closer to current if( ( nIndex - m_pCurrent->nIndex ) > ( m_pTail->nIndex - nIndex ) ) { LPNODE pNode = m_pCurrent; while( pNode->nIndex != nIndex ) pNode = pNode->pNext; return pNode; } else { LPNODE pNode = m_pTail; while( pNode->nIndex != nIndex ) pNode = pNode->pPrev; return pNode; } } LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgArray::FindItem"), _T("Can't find item with index %d"), nIndex ); return NULL; } private: LPNODE m_pHead; LPNODE m_pTail; LPNODE m_pCurrent; INT m_nCount; }; template class CLgQueue : public CLgArray { public: CLgQueue() { m_dwOverflowSkiped = 0i64; } virtual ~CLgQueue() { if( m_dwOverflowSkiped ) LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgQueue::~CLgQueue"), _T("Overflow, lost %ld items"), m_dwOverflowSkiped ); } public: virtual BOOL Push( TYPE data ) { if( GetCount() >= MAXSIZE ) { if( !m_dwOverflowSkiped ) LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgQueue::Push"), _T("Overflow (more then %d items)"), MAXSIZE ); m_dwOverflowSkiped++; return FALSE; } if( m_dwOverflowSkiped ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgQueue::Push"), _T("Overflow, lost %ld items"), m_dwOverflowSkiped ); m_dwOverflowSkiped = 0i64; } return Add( data ); } virtual BOOL Pop( TYPE& data ) { if( !GetCount() ) return FALSE; data = GetAt(0); return Remove(0); } virtual BOOL Clear() { return RemoveAll(); } private: __int64 m_dwOverflowSkiped; }; template class CLgStack : public CLgArray { public: virtual ~CLgStack() { RemoveAll(); } public: virtual BOOL Push( ARGTYPE data ) { return Add( data ); } virtual BOOL Peek( ARGTYPE data ) { if( !GetCount() ) return FALSE; // store data data = GetTail(); return TRUE; } virtual BOOL Pop( ARGTYPE data ) { // get data if( !Peek( data ) ) return FALSE; // remove last return RemoveTail(); } }; template class CLgList { private: struct NODE { struct NODE far *pNext; struct NODE far *pPrev; TYPE tData; INT nIndex; }; typedef NODE* PNODE; typedef NODE far* LPNODE; public: CLgList() { m_pTail = m_pHead = NULL; m_nCount = 0; } CLgList( CLgList& list ) { m_pTail = m_pHead = NULL; m_nCount = 0; // copy list POSITION pos = list.GetHeadPosition(); while( pos ) Add( list.GetNext( pPos ) ); } virtual ~CLgList() { RemoveAll(); } public: CLgList& operator =( CLgList& list ) { RemoveAll(); // copy list PVOID pos = list.GetHeadPosition(); while( pos ) Add( list.GetNext( pos ) ); return *this; } public: virtual PVOID AddEmpty() { LPNODE pNode = new NODE; if( !pNode ) return FALSE; // place element pNode->pPrev = m_pTail; pNode->pNext = NULL; pNode->nIndex = m_nCount; // update last & head pointer if( m_pTail ) m_pTail->pNext = pNode; else m_pHead = pNode; // set last pointer m_pTail = pNode; // increase count m_nCount++; return pNode; } virtual ARGTYPE AddEmptyRef() { PNODE pNode = (PNODE)AddEmpty(); if( pNode ) return GetAt( pNode ); static TYPE data; LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgList::AddEmptyRef"), _T("Can't allocate new item, returned default item") ); return data; } virtual PVOID Add( ARGTYPE data ) { LPNODE pNode = new NODE; if( !pNode ) return NULL; // place element pNode->pPrev = m_pTail; pNode->pNext = NULL; pNode->tData = data; // update last & head pointer if( m_pTail ) m_pTail->pNext = pNode; else m_pHead = pNode; // set last pointer m_pTail = pNode; // increase count m_nCount++; return pNode; } virtual BOOL Remove( PVOID pos ) { LPNODE pNode = (PNODE)pos; if( !pNode ) return FALSE; // update prev element if( pNode->pPrev ) pNode->pPrev->pNext = pNode->pNext; else m_pHead = pNode->pNext; // update next element if( pNode->pNext ) pNode->pNext->pPrev = pNode->pPrev; else m_pTail = pNode->pPrev; // delete node delete pNode; // update counter m_nCount--; return TRUE; } virtual PVOID InsertAt( PVOID pos, ARGTYPE data ) { if( pos == m_pTail ) return Add( data ); // find index LPNODE pIndexNode = (PNODE)pos; if( !pIndexNode ) return FALSE; LPNODE pNode = new NODE; if( !pNode ) return FALSE; // place element pNode->pPrev = pIndexNode->pPrev; pNode->pNext = pIndexNode; pNode->tData = data; // check head PVOID if( pIndexNode->pPrev ) pIndexNode->pPrev->pNext = pNode; else m_pHead = pNode; // update node that was on index place pointer pIndexNode->pPrev = pNode; // increase count m_nCount++; return pNode; } virtual BOOL MoveToTail( PVOID pos ) { PNODE pNode = (PNODE)pos; if( !pNode ) return FALSE; if( pNode == m_pTail ) return TRUE; // update prev element if( pNode->pPrev ) pNode->pPrev->pNext = pNode->pNext; else m_pHead = pNode->pNext; // update next element if( pNode->pNext ) pNode->pNext->pPrev = pNode->pPrev; else m_pTail = pNode->pPrev; // move to tail m_pTail->pNext = pNode; pNode->pNext = NULL; pNode->pPrev = m_pTail; m_pTail = pNode; return TRUE; } virtual BOOL RemoveAll() { LPNODE pNode = m_pHead; while( pNode ) { LPNODE pRemoveNode = pNode; pNode = pNode->pNext; delete pRemoveNode; } m_pTail = m_pHead = NULL; m_nCount = 0; return TRUE; } virtual BOOL RemoveHead() { return Remove( m_pHead ); } virtual BOOL RemoveTail() { return Remove( m_pTail ); } public: ARGTYPE GetAt( PVOID pos ) { static TYPE data; // find index PNODE pNode = (PNODE)pos; if( !pNode ) return data; return pNode->tData; } ARGTYPE GetNext( PVOID& pos ) { static TYPE data; // find index PNODE pNode = (PNODE)pos; if( !pNode ) return data; reinterpret_cast(pos) = reinterpret_cast(pos)->pNext; return pNode->tData; } ARGTYPE GetPrev( PVOID& pos ) { static TYPE data; // find index PNODE pNode = (PNODE)pos; if( !pNode ) return data; reinterpret_cast(pos) = reinterpret_cast(pos)->pPrev; return pNode->tData; } PVOID GetHeadPosition() { return m_pHead; } PVOID GetTailPosition() { return m_pTail; } ARGTYPE GetHead() { return GetAt( m_pHead ); } ARGTYPE GetTail() { return GetAt( m_pTail ); } INT GetCount() { return m_nCount; } PVOID Find( ARGTYPE data ) { LPNODE pNode = m_pHead; while( pNode ) { if( pNode->tData == data ) return pNode->nIndex; pNode = pNode->pNext; } return NULL; } public: friend BOOL operator == ( CLgList& a1, CLgList& a2 ) { if( a1.GetCount() != a2.GetCount() ) return FALSE; for( INT i = 0; i < a1.GetCount(); i++ ) if( !(a1[i] == a2[i]) ) return FALSE; return TRUE; } private: LPNODE m_pHead; LPNODE m_pTail; INT m_nCount; }; #endif