home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************************
- * Copyright (c) 1999 Microsoft Corporation. All Rights Reserved.
- *
- * File:
- * table.hpp
- *
- * Description:
- *
- *
- *
- ***************************************************************************************/
- #ifndef __CONTAINER_H__
- #define __CONTAINER_H__
-
- #include "ProfilerBase.h"
-
-
- /***************************************************************************************
- ******************** ********************
- ******************** SListNode ********************
- ******************** ********************
- ***************************************************************************************/
- template<class T, class K> class SList;
-
-
- template<class T, class K>
- class SListNode
- {
- friend class SList<T, K>;
-
- public:
-
- SListNode( T entry, K Key );
- virtual ~SListNode();
-
-
- private:
-
- K m_key;
- T m_pEntry;
-
- SListNode<T, K> *m_pNext;
-
- }; // SListNode
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- SListNode<T, K>::SListNode( T entry, K key ) :
- m_key( key ),
- m_pNext( NULL ),
- m_pEntry( entry )
- {
-
- } // ctor
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- SListNode<T, K>::~SListNode()
- {
- if ( m_pEntry != NULL )
- delete m_pEntry;
-
- } // dtor
-
-
- /***************************************************************************************
- ******************** ********************
- ******************** SList Declaration ********************
- ******************** ********************
- ***************************************************************************************/
- template<class T, class K>
- class SList
- {
- public:
-
- SList();
- virtual ~SList();
-
- SList<T, K>( const SList<T, K> &source );
- SList<T, K> &operator=( const SList<T, K> &source );
-
-
- public:
-
- void Dump();
-
- BOOL IsEmpty();
- T Lookup( K key );
- void DelEntry( K key );
- void AddEntry( T entry, K key );
-
- //
- // getters
- //
- T Head();
- T Tail();
- T Entry();
- ULONG Count();
-
- //
- // intrinsic iteration
- //
- void Next();
- void Reset();
- BOOL AtEnd();
-
-
- private:
-
- ULONG m_count;
- SListNode<T, K> *m_pHead;
- SListNode<T, K> *m_pTail;
- SListNode<T, K> *m_pCursor;
- CRITICAL_SECTION m_criticalSection;
-
- }; // SList
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- SList<T, K>::SList() :
- m_count( 0 ),
- m_pHead( NULL ),
- m_pTail( NULL ),
- m_pCursor( NULL )
- {
- InitializeCriticalSection( &m_criticalSection );
-
- } // ctor
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- SList<T, K>::~SList()
- {
- if ( m_pHead != NULL )
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
- SListNode<T, K> *cursor = m_pHead;
-
-
- // delete all entries
- m_count = 0;
- for ( ; ((m_pHead = m_pHead->m_pNext) != NULL); cursor = m_pHead )
- delete cursor;
-
-
- delete cursor;
- }
-
- DeleteCriticalSection( &m_criticalSection );
-
- } // dtor
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- SList<T, K> &SList<T, K>::operator=( const SList<T, K> &source )
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
- memcpy( this, &source, sizeof( SList<T, K> ) );
-
-
- return *this;
-
- } // assignment operator
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- SList<T, K>::SList( const SList<T, K> &source )
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
- m_pHead = source.m_pHead;
- m_pTail = source.m_pTail;
-
- } // copy ctor
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- void SList<T, K>::Dump()
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
- SListNode<T, K> *cursor = m_pHead;
-
-
- // dump all entries
- for ( ; (cursor != NULL); cursor = cursor->m_pNext )
- cursor->m_pEntry->Dump();
-
-
- } // SList<T, K>::Dump
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- ULONG SList<T, K>::Count()
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
-
- return m_count;
-
- } // SList<T, K>::Count
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- void SList<T, K>::AddEntry( T entry, K key )
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
- SListNode<T, K> *temp;
-
-
- ++m_count;
- temp = new SListNode<T, K>( entry, key );
-
- // since we are inserting at the head of
- // the list, we only need to set the tail
- // once (when adding the first entry).
- if ( m_pHead == NULL )
- m_pTail = temp;
-
-
- // add new entry and adjust head
- temp->m_pNext = m_pHead;
- m_pHead = temp;
-
-
- } // SList<T, K>::AddEntry
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- void SList<T, K>::DelEntry( K key )
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
- // Case 0: no entries---nothing to delete
- if ( m_pHead != NULL )
- {
- SListNode<T, K> *cursor = m_pHead;
-
-
- // Case 1: delete head entry
- if ( m_pHead->m_pEntry->Compare( key ) == TRUE )
- {
- --m_count;
-
- // consider case where head == tail
- if ( (m_pHead = m_pHead->m_pNext) == NULL )
- m_pCursor = m_pTail = m_pHead;
-
-
- delete cursor;
- }
- // Case 2: delete inner entry
- else
- {
- SListNode<T, K> *precursor = cursor;
-
-
- // scan for match
- for ( ; ((cursor = cursor->m_pNext) != NULL); precursor = cursor )
- {
- if ( cursor->m_pEntry->Compare( key ) == TRUE )
- {
- --m_count;
- m_pCursor = precursor;
- precursor->m_pNext = cursor->m_pNext;
-
- // consider case where deleted entry is the tail
- if ( m_pTail == cursor )
- m_pTail = precursor;
-
-
- delete cursor;
- break;
- }
-
- } // for
- }
- }
-
- } // SList<T, K>::DelEntry
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- T SList<T, K>::Lookup( K key )
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
- SListNode<T, K> *cursor = m_pHead;
-
-
- // scan for match
- for ( ;
- ((cursor != NULL) && (cursor->m_pEntry->Compare( key ) == FALSE));
- cursor = cursor->m_pNext )
- ; // continue
-
-
- return ((cursor != NULL) ? cursor->m_pEntry : NULL);
-
- } // SList<T, K>::Lookup
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- T SList<T, K>::Entry()
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
-
- return ((m_pCursor != NULL) ? m_pCursor->m_pEntry : NULL);
-
- } // SList<T, K>::Entry
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- T SList<T, K>::Head()
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
-
- return ((m_pHead != NULL) ? m_pHead->m_pEntry : NULL);
-
- } // SList<T, K>::Head
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- T SList<T, K>::Tail()
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
-
- return ((m_pTail != NULL) ? m_pTail->m_pEntry : NULL);
-
- } // SList<T, K>::Tail
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- void SList<T, K>::Next()
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
- if ( m_pCursor != NULL )
- m_pCursor = m_pCursor->m_pNext;
-
-
- } // SList<T, K>::Next()
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- void SList<T, K>::Reset()
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
- m_pCursor = m_pHead;
-
- } // SList<T, K>::Reset
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- BOOL SList<T, K>::AtEnd()
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
-
- return (BOOL)(m_pCursor == NULL);
-
- } // SList<T, K>::AtEnd
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class T, class K>
- BOOL SList<T, K>::IsEmpty()
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
-
- return (BOOL)(m_pHead == NULL);
-
- } // SList<T, K>::IsEmpty
-
-
- /***************************************************************************************
- ******************** ********************
- ******************** CNode ********************
- ******************** ********************
- ***************************************************************************************/
- template<class K> class CStack;
-
-
- template<class K>
- class CNode
- {
- friend class CStack<K>;
-
- public:
-
- CNode( K item );
- virtual ~CNode();
-
-
- private:
-
- K m_pEntry;
- CNode<K> *m_pNext;
-
- }; // CNode
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class K>
- CNode<K>::CNode( K item ) :
- m_pNext( NULL ),
- m_pEntry( item )
- {
-
- } // ctor
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class K>
- CNode<K>::~CNode()
- {
-
- if ( m_pEntry != NULL )
- delete m_pEntry;
-
-
- } // dtor
-
-
- /***************************************************************************************
- ******************** ********************
- ******************** CStack ********************
- ******************** ********************
- ***************************************************************************************/
- template< class K>
- class CStack
- {
- public:
-
- CStack();
- virtual ~CStack();
-
- CStack<K>( const CStack<K> &source );
- CStack<K> &operator=( const CStack<K> &source );
-
-
- public:
-
- void Push( K item );
- K Pop();
- BOOL Empty();
- void Dump();
- ULONG Count();
-
-
- private:
-
- CNode<K> *m_pTop;
- int m_Count;
-
- CRITICAL_SECTION m_criticalSection;
-
- }; // CStack
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class K>
- CStack<K>::CStack() :
- m_Count( 0 ),
- m_pTop( NULL )
- {
-
- InitializeCriticalSection( &m_criticalSection );
-
- } // ctor
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class K>
- CStack<K>::~CStack()
- {
- if ( m_Count != 0 )
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
- while ( m_Count != 0 )
- {
- CNode<K> *np = m_pTop;
- CNode<K> *temp = np->m_pNext;
-
-
- m_pTop = temp;
- m_Count--;
-
- delete np;
- }
- }
-
- DeleteCriticalSection( &m_criticalSection );
-
- } // dtor
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class K>
- CStack<K> &CStack<K>::operator=( const CStack<K> &source )
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
- memcpy( this, &source, sizeof( CStack<K> ) );
-
-
- return *this;
-
- } // assignment operator
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class K>
- CStack<K>::CStack( const CStack<K> &source )
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
- m_pTop = source.m_pTop;
- m_Count = source.m_Count;
-
- } // copy ctor
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class K>
- ULONG CStack<K>::Count()
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
-
- return m_Count;
-
- } // CStack<K>::Count
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class K>
- void CStack<K>::Dump()
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
- CNode<K> *cursor = m_pTop;
-
-
- // dump all entries
- for ( ; (cursor != NULL); cursor = cursor->m_pNext )
- cursor->m_pEntry->Dump();
-
-
- } // CStack<K>::Dump
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class K>
- void CStack<K>::Push( K item )
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
- CNode<K> *np = new CNode<K>( item );
-
- if ( np != NULL )
- {
- m_Count++;
-
- np->m_pNext = m_pTop;
- m_pTop = np;
- }
-
- } // CStack<K>::Push
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class K>
- K CStack<K>::Pop()
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
- K item = NULL;
-
-
- if ( m_Count !=0 )
- {
- CNode<K> *np = m_pTop;
-
-
- item = np->m_pEntry->Clone();
- m_pTop = np->m_pNext;
-
- m_Count--;
- delete np;
- }
-
-
- return item;
-
- } // CStack<K>::Pop
-
-
- /***************************************************************************************
- * Method:
- *
- *
- * Purpose:
- *
- *
- * Parameters:
- *
- *
- * Return value:
- *
- *
- * Notes:
- *
- ***************************************************************************************/
- /* public */
- template<class K>
- BOOL CStack<K>::Empty()
- {
- ///////////////////////////////////////////////////////////////////////////
- Synchronize guard( m_criticalSection );
- ///////////////////////////////////////////////////////////////////////////
-
-
- return (BOOL)(m_Count == NULL);
-
- } // CStack<K>::Empty
-
- #endif __CONTAINER_H__
-
- // End of File
-
-