/*------------------------------------------------------------------------*/ /* */ /* LISTIMP.H */ /* */ /* Copyright (c) 1991, 1994 Borland International */ /* All Rights Reserved */ /* */ /*------------------------------------------------------------------------*/ #if !defined( CLASSLIB_LISTIMP_H ) #define CLASSLIB_LISTIMP_H #if !defined( __LIMITS_H ) #include #endif #if !defined( __CHECKS_H ) #include #endif #if !defined( CLASSLIB_DEFS_H ) #include #endif #if !defined( CLASSLIB_MEMMGR_H ) #include #endif #if !defined( CLASSLIB_ALLOCTR_H ) #include #endif #if !defined( CLASSLIB_VOIDP_H ) #include #endif #pragma option -Vo- #if defined( BI_CLASSLIB_NO_po ) #pragma option -po- #endif /*------------------------------------------------------------------------*/ /* */ /* template class TMListElement */ /* */ /* Node for templates TMListImp and TMIListImp */ /* */ /*------------------------------------------------------------------------*/ template class TMListImp; template class TMListElement; template class TMListBlockInitializer : public Alloc { protected: TMListBlockInitializer(); ~TMListBlockInitializer(); static unsigned Count; }; #if defined( BI_OLDNAMES ) #define BI_MListBlockInitializer TMListBlockInitializer #endif template TMListBlockInitializer::TMListBlockInitializer() { PRECONDITION( Count != UINT_MAX ); if( Count++ == 0 ) TMListElement::Mgr = new(*this)TMMemBlocks( sizeof(TMListElement), 20 ); } template TMListBlockInitializer::~TMListBlockInitializer() { PRECONDITION( Count != 0 ); if( --Count == 0 ) { delete TMListElement::Mgr; TMListElement::Mgr = 0; } } template unsigned TMListBlockInitializer::Count = 0; template class TMListElement { public: TMListElement( const T& t, TMListElement *p ) : Data(t) { Next = p->Next; p->Next = this; } TMListElement(); TMListElement *Next; T Data; void *operator new( size_t sz ); void operator delete( void * ); private: friend TMListBlockInitializer; static TMMemBlocks *Mgr; }; #if defined( BI_OLDNAMES ) #define BI_MListElement TMListElement #endif template TMMemBlocks *TMListElement::Mgr = 0; template inline TMListElement::TMListElement() { Next = 0; } template void *TMListElement::operator new( size_t sz ) { PRECONDITION( Mgr != 0 ); return Mgr->Allocate( sz ); } template void TMListElement::operator delete( void *b ) { PRECONDITION( Mgr != 0 ); Mgr->Free( b ); } /*------------------------------------------------------------------------*/ /* */ /* template class TMListImp */ /* */ /* Implements a managed list of objects of type T. Assumes that */ /* T has meaningful copy semantics and a default constructor. */ /* */ /*------------------------------------------------------------------------*/ template class TMListIteratorImp; template class TMListImp : private TMListBlockInitializer { typedef TMListBlockInitializer Parent; public: typedef void (*IterFunc)(T &, void *); typedef int (*CondFunc)(const T &, void *); friend TMListIteratorImp; TMListImp() { InitList(); } ~TMListImp() { Flush(); } const T& PeekHead() const { return Head.Next->Data; } int Add( const T& t ); int Detach( const T& t ) { return DoDetach( t, 0 ); } int DetachAtHead() { return DoDetachAtHead(0); } T *Find( const const T& t ); void Flush() { DoFlush(); } int IsEmpty() const { return ItemsInContainer == 0; } int GetItemsInContainer() const { return ItemsInContainer; } void ForEach( IterFunc iter, void *args ); T *FirstThat( CondFunc cond, void *args ) const; T *LastThat( CondFunc cond, void *args ) const; Parent::operator delete; Parent::operator delete []; #if defined( BI_OLDNAMES ) const T& peekHead() const { return PeekHead(); } void add( const T& t ) { Add(t); } void detach( const T& t, int del = 0 ) { DoDetach( t, del ); } void flush( int = 0 ) { Flush(); } int isEmpty() const { return IsEmpty(); } void forEach( IterFunc iter, void *args ) { ForEach( iter, args ); } T *firstThat( CondFunc cond, void *args ) const { return FirstThat( cond, args ); } T *lastThat( CondFunc cond, void *args ) const { return LastThat( cond, args ); } #endif // BI_OLDNAMES protected: int DoDetach( const T& t, int del = 0 ) { return DetachElement(FindDetach(t),del); } int DoDetachAtHead( int del = 0 ) { return DetachElement(&Head,del); } void DoFlush( int del = 0 ); TMListElement Head, Tail; virtual TMListElement *FindDetach( const T& t ) { return FindPred(t); } virtual TMListElement *FindPred( const T& ); int ItemsInContainer; private: virtual void RemoveData( TMListElement * ) { } void InitList(); int DetachElement( TMListElement *pred, int del = 0 ); }; #if defined( BI_OLDNAMES ) #define BI_MListImp TMListImp #endif template void TMListImp::InitList() { Head.Next = &Tail; Tail.Next = &Tail; ItemsInContainer = 0; } template int TMListImp::Add( const T& toAdd ) { new TMListElement( toAdd, &Head ); ItemsInContainer++; return 1; } template TMListElement *TMListImp::FindPred( const T& t ) { Tail.Data = t; TMListElement *cursor = &Head; while( !(t == cursor->Next->Data) ) cursor = cursor->Next; Tail.Data = T(); return cursor; } template int TMListImp::DetachElement( TMListElement *pred, int del ) { TMListElement *item = pred->Next; if( item == &Tail ) return 0; else { pred->Next = pred->Next->Next; if( del != 0 ) RemoveData( item ); delete item; ItemsInContainer--; return 1; } } template T *TMListImp::Find( const T& t ) { TMListElement *cursor = FindPred(t)->Next; if( cursor == &Tail ) return 0; else return &cursor->Data; } template void TMListImp::DoFlush( int del ) { TMListElement *current = Head.Next; while( current != &Tail ) { TMListElement *temp = current; current = current->Next; if( del != 0 ) RemoveData( temp ); delete temp; } InitList(); } template void TMListImp::ForEach( IterFunc iter, void *args ) { TMListElement *cur = Head.Next; while( cur->Next != cur ) { iter( cur->Data, args ); cur = cur->Next; } } template T *TMListImp::FirstThat( CondFunc cond, void *args ) const { TMListElement *cur = Head.Next; while( cur->Next != cur ) if( cond( cur->Data, args ) != 0 ) return &(cur->Data); else cur = cur->Next; return 0; } template T *TMListImp::LastThat( CondFunc cond, void *args ) const { T *res = 0; TMListElement *cur = Head.Next; while( cur->Next != cur ) { if( cond( cur->Data, args ) != 0 ) res = &(cur->Data); cur = cur->Next; } return res; } /*------------------------------------------------------------------------*/ /* */ /* template class TMListIteratorImp */ /* */ /* Implements a list iterator. This iterator works with any direct */ /* managed list. For indirect lists, see TMIListIteratorImp. */ /* */ /*------------------------------------------------------------------------*/ template class TMListIteratorImp { public: TMListIteratorImp( const TMListImp& l ) { List = &l; Cur = List->Head.Next; } TMListIteratorImp( const TMSListImp& l ); operator int() { return Cur != &(List->Tail); } const T& Current() { PRECONDITION( int(*this) != 0 ); return Cur->Data; } const T& operator ++ ( int ) { PRECONDITION( Cur != &(List->Tail) ); TMListElement *temp = Cur; Cur = Cur->Next; return temp->Data; } const T& operator ++ () { PRECONDITION( Cur->Next != &(List->Tail) ); Cur = Cur->Next; return Cur->Data; } void Restart() { Cur = List->Head.Next; } #if defined( BI_OLDNAMES ) const T& current() { return Current(); } void restart() { Restart(); } #endif // BI_OLDNAMES private: const TMListImp *List; TMListElement *Cur; }; #if defined( BI_OLDNAMES ) #define BI_MListIteratorImp TMListIteratorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TListImp */ /* template class TListIteratorImp */ /* */ /* Implements a list of objects of type T using TStandardAllocator as */ /* its memory manager. Assumes that T has meaningful copy semantics */ /* and a default constructor. */ /* */ /*------------------------------------------------------------------------*/ template class TListImp : public TMListImp { }; template class TListIteratorImp : public TMListIteratorImp { public: TListIteratorImp( const TMListImp& l ) : TMListIteratorImp( l ) {} TListIteratorImp( const TMSListImp& l ) : TMListIteratorImp( l ) {} }; #if defined( BI_OLDNAMES ) #define BI_ListImp TListImp #define BI_ListIteratorImp TListIteratorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TMSListImp */ /* */ /* Implements a managed, sorted list of objects of type T. Assumes that */ /* T has meaningful copy semantics, a meaningful < operator, and a */ /* default constructor. */ /* */ /*------------------------------------------------------------------------*/ template class TMSListImp : private TMListImp { typedef TMListImp Parent; public: friend TMListIteratorImp; int Add( const T& t ); Parent::IterFunc; Parent::CondFunc; Parent::PeekHead; Parent::Detach; Parent::DetachAtHead; Parent::Find; Parent::Flush; Parent::IsEmpty; Parent::GetItemsInContainer; Parent::ForEach; Parent::FirstThat; Parent::LastThat; Parent::operator delete; Parent::operator delete []; #if defined( BI_OLDNAMES ) void add( const T& t ) { Add(t); } #endif // BI_OLDNAMES protected: Parent::Head; Parent::Tail; Parent::ItemsInContainer; Parent::DoDetach; Parent::DoDetachAtHead; Parent::DoFlush; virtual TMListElement *FindDetach( const T& t ); virtual TMListElement *FindPred( const T& ); }; template class TMSListIteratorImp : public TMListIteratorImp { public: TMSListIteratorImp( const TMSListImp& l ) : TMListIteratorImp( l ) {} }; #if defined( BI_OLDNAMES ) #define BI_MSListImp TMSListImp #define BI_MSListIteratorImp TMSListIteratorImp #endif template int TMSListImp::Add( const T& t ) { new TMListElement( t, FindPred(t) ); ItemsInContainer++; return 1; } template TMListElement *TMSListImp::FindDetach( const T& t ) { TMListElement *res = FindPred(t); if( res != 0 && res->Next->Data == t ) return res; else return &Tail; } template TMListElement *TMSListImp::FindPred( const T& t ) { Tail.Data = t; TMListElement *cursor = &Head; while( cursor->Next->Data < t ) cursor = cursor->Next; return cursor; } /*------------------------------------------------------------------------*/ /* */ /* template class TSListImp */ /* template class TSListIteratorImp */ /* */ /* Implements a sorted list of objects of type T using */ /* TStandardAllocator as its memory manager. Assumes that */ /* T has meaningful copy semantics, a meaningful < operator, and a */ /* default constructor. */ /* */ /*------------------------------------------------------------------------*/ template class TSListImp : public TMSListImp { }; template class TSListIteratorImp : public TMSListIteratorImp { public: TSListIteratorImp( const TSListImp& l ) : TMSListIteratorImp( l ) {} }; // constructor for TMListIteratorImp template TMListIteratorImp::TMListIteratorImp( const TMSListImp& l ) { List = &l; Cur = List->Head.Next; } #if defined( BI_OLDNAMES ) #define BI_SListImp TSListImp #define BI_SListIteratorImp TSListIteratorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TMInternalIListImp */ /* */ /* Implements a managed list of pointers to objects of type T. */ /* This is implemented through the form of TMListImp specified by List. */ /* Since pointers always have meaningful copy semantics, this class */ /* can handle any type of object. */ /* */ /*------------------------------------------------------------------------*/ template class TMInternalIListImp : public List { typedef List Parent; public: typedef void (*IterFunc)(T&, void *); typedef int (*CondFunc)(const T&, void *); T *PeekHead() const { return STATIC_CAST(T *,STATIC_CAST(void *,Parent::PeekHead())); } int Add( T *t ) { return Parent::Add( t ); } int Detach( T *t, int del = 0 ) { return Parent::DoDetach( t, del ); } int DetachAtHead( int del = 0 ) { return Parent::DoDetachAtHead( del ); } void Flush( int del = 0 ) { Parent::DoFlush(del); } T *Find( const T *t ); void ForEach( IterFunc iter, void * ); T *FirstThat( CondFunc cond, void * ) const; T *LastThat( CondFunc cond, void * ) const; #if defined( BI_OLDNAMES ) T *peekHead() const { return PeekHead(); } void add( T *t ) { Add(t); } void detach( T *t, int del = 0 ) { Detach( t, del ); } void forEach( IterFunc iter, void *args ) { ForEach( iter, args ); } T *firstThat( CondFunc cond, void *args ) const { return FirstThat( cond, args ); } T *lastThat( CondFunc cond, void *args ) const { return LastThat( cond, args ); } #endif // BI_OLDNAMES protected: virtual TMListElement * FindPred( const TVoidPointer& ) = 0; private: virtual void RemoveData( TMListElement *block ) { delete STATIC_CAST(T *,STATIC_CAST(void *,block->Data)); } }; #if defined( BI_OLDNAMES ) #define BI_MInternalIListImp TMInternalIListImp #endif template T *TMInternalIListImp::Find( const T *t ) { TMListElement *cur = Head.Next; Tail.Data = t; while( !(*STATIC_CAST(T *,STATIC_CAST(void *,cur->Data)) == *t) ) cur = cur->Next; Tail.Data = TVoidPointer(); if( cur == &Tail ) return 0; else return STATIC_CAST(T *,STATIC_CAST(void *,cur->Data)); } template void TMInternalIListImp::ForEach( IterFunc iter, void *args ) { TMListElement *cur = Head.Next; while( cur->Next != cur ) { iter( *STATIC_CAST(T *,STATIC_CAST(void *,cur->Data)), args ); cur = cur->Next; } } template T *TMInternalIListImp::FirstThat( CondFunc cond, void *args ) const { TMListElement *cur = Head.Next; while( cur->Next != cur ) if( cond( *STATIC_CAST(T *,STATIC_CAST(void *,cur->Data)), args ) != 0 ) return STATIC_CAST(T *,STATIC_CAST(void *,cur->Data)); else cur = cur->Next; return 0; } template T *TMInternalIListImp::LastThat( CondFunc cond, void *args ) const { T *res = 0; TMListElement *cur = Head.Next; while( cur->Next != cur ) { if( cond( *STATIC_CAST(T *,STATIC_CAST(void *,cur->Data)), args ) != 0 ) res = STATIC_CAST(T *,STATIC_CAST(void *,cur->Data)); cur = cur->Next; } return res; } /*------------------------------------------------------------------------*/ /* */ /* template class TMIListImp */ /* template class TMIListIteratorImp */ /* */ /* Implements a managed list of pointers to objects of type T. */ /* This is implemented through the template TMInternalIListImp. Since */ /* pointers always have meaningful copy semantics, this class */ /* can handle any type of object. */ /* */ /*------------------------------------------------------------------------*/ template class TMIListImp : public TMInternalIListImp, Alloc > { typedef TMInternalIListImp, Alloc > Parent; public: friend TMIListIteratorImp; void Flush( int del = 0 ) { Parent::DoFlush(del); } Parent::IterFunc; Parent::CondFunc; Parent::PeekHead; Parent::Add; Parent::Detach; Parent::DetachAtHead; Parent::Find; Parent::IsEmpty; Parent::GetItemsInContainer; Parent::ForEach; Parent::FirstThat; Parent::LastThat; Parent::operator delete; Parent::operator delete []; protected: Parent::Head; Parent::Tail; Parent::ItemsInContainer; virtual TMListElement *FindPred( const TVoidPointer& ); }; template class TMIListIteratorImp : private TMListIteratorImp { typedef TMListIteratorImp Parent; public: TMIListIteratorImp( const TMIListImp& l ) : TMListIteratorImp(l) {} T *Current() { return STATIC_CAST(T *,STATIC_CAST(void *,Parent::Current())); } T *operator ++ (int) { return STATIC_CAST(T *,STATIC_CAST(void *,Parent::operator++(1))); } T *operator ++ () { return STATIC_CAST(T *,STATIC_CAST(void *,Parent::operator++())); } Parent::Restart; Parent::operator int; #if defined( BI_OLDNAMES ) T *current() { return Current(); } void restart() { Restart(); } #endif // BI_OLDNAMES }; template TMListElement * TMIListImp::FindPred( const TVoidPointer& t ) { Tail.Data = t; TMListElement *cursor = &Head; while( !(*STATIC_CAST(T *,STATIC_CAST(void *,t)) == *STATIC_CAST(T *,STATIC_CAST(void *,cursor->Next->Data)) )) cursor = cursor->Next; Tail.Data = TVoidPointer(); return cursor; } #if defined( BI_OLDNAMES ) #define BI_MIListImp TMIListImp #define BI_MIListIteratorImp TMIListIteratorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TIListImp */ /* template class TIListIteratorImp */ /* */ /* Implements a list of pointers to objects of type T using */ /* TStandardAllocator as its memory manager. This is implemented */ /* through the template TMInternalIListImp. Since */ /* pointers always have meaningful copy semantics, this class */ /* can handle any type of object. */ /* */ /*------------------------------------------------------------------------*/ template class TIListImp : public TMIListImp { }; template class TIListIteratorImp : public TMIListIteratorImp { public: TIListIteratorImp( const TIListImp& l ) : TMIListIteratorImp( l ) {} }; #if defined( BI_OLDNAMES ) #define BI_IListImp TIListImp #define BI_IListIteratorImp TIListIteratorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TMISListImp */ /* template class TMISListIteratorImp */ /* */ /* Implements a managed sorted list of pointers to objects of type T. */ /* This is implemented through the template TInternalIListImp. Since */ /* pointers always have meaningful copy semantics, this class */ /* can handle any type of object. */ /* */ /*------------------------------------------------------------------------*/ template class TMISListImp : private TMInternalIListImp, Alloc > { typedef TMInternalIListImp, Alloc > Parent; public: friend TMISListIteratorImp; Parent::IterFunc; Parent::CondFunc; Parent::PeekHead; Parent::Add; Parent::Detach; Parent::DetachAtHead; Parent::Find; Parent::Flush; Parent::IsEmpty; Parent::GetItemsInContainer; Parent::ForEach; Parent::FirstThat; Parent::LastThat; Parent::operator delete; Parent::operator delete []; protected: Parent::Head; Parent::Tail; Parent::ItemsInContainer; virtual TMListElement *FindDetach( const TVoidPointer& ); virtual TMListElement *FindPred( const TVoidPointer& ); }; template class TMISListIteratorImp : private TMListIteratorImp { typedef TMListIteratorImp Parent; public: TMISListIteratorImp( const TMISListImp& l ) : TMListIteratorImp(l) {} T *Current() { return STATIC_CAST(T *,STATIC_CAST(void *,Parent::Current())); } T *operator ++ (int) { return STATIC_CAST(T *,STATIC_CAST(void *,Parent::operator++(1))); } T *operator ++ () { return STATIC_CAST(T *,STATIC_CAST(void *,Parent::operator++())); } Parent::Restart; Parent::operator int; #if defined( BI_OLDNAMES ) T *current() { return Current(); } #endif // BI_OLDNAMES }; #if defined( BI_OLDNAMES ) #define BI_MISListImp TMISListImp #define BI_MISListIteratorImp TMISListIteratorImp #endif template TMListElement * TMISListImp::FindDetach( const TVoidPointer& t ) { TMListElement *res = FindPred(t); if( res == 0 || res->Next == &Tail ) return &Tail; else if( *STATIC_CAST(T *,STATIC_CAST(void *,res->Next->Data)) == *STATIC_CAST(T *,STATIC_CAST(void *,t)) ) return res; else return &Tail; } template TMListElement * TMISListImp::FindPred( const TVoidPointer& t ) { Tail.Data = t; TMListElement *cursor = &Head; while( *STATIC_CAST(T *,STATIC_CAST(void *,cursor->Next->Data)) < *STATIC_CAST(T *,STATIC_CAST(void *,t)) ) cursor = cursor->Next; Tail.Data = TVoidPointer(); return cursor; } /*------------------------------------------------------------------------*/ /* */ /* template class TISListImp */ /* template class TISListIteratorImp */ /* */ /* Implements a sorted list of pointers to objects of type T using */ /* TStandardAllocator as its memory manager. */ /* This is implemented through the template TInternalIListImp. Since */ /* pointers always have meaningful copy semantics, this class */ /* can handle any type of object. */ /* */ /*------------------------------------------------------------------------*/ template class TISListImp : public TMISListImp { }; template class TISListIteratorImp : public TMISListIteratorImp { public: TISListIteratorImp( const TISListImp& l ) : TMISListIteratorImp( l ) {} }; #if defined( BI_OLDNAMES ) #define BI_ISListImp TISListImp #endif #if defined( BI_CLASSLIB_NO_po ) #pragma option -po. #endif #pragma option -Vo. #endif // CLASSLIB_LISTIMP_H