/*------------------------------------------------------------------------*/ /* */ /* STACKS.H */ /* */ /* Copyright (c) 1991, 1994 Borland International */ /* All Rights Reserved */ /* */ /*------------------------------------------------------------------------*/ #if !defined( CLASSLIB_STACKS_H ) #define CLASSLIB_STACKS_H #if !defined( __CHECKS_H ) #include #endif #if !defined( CLASSLIB_DEFS_H ) #include #endif #if !defined( CLASSLIB_SHDDEL_H ) #include #endif #if !defined( CLASSLIB_VECTIMP_H ) #include #endif #if !defined( CLASSLIB_LISTIMP_H ) #include #endif #pragma option -Vo- #if defined( BI_CLASSLIB_NO_po ) #pragma option -po- #endif /*------------------------------------------------------------------------*/ /* */ /* template class TStackAsVectorImp */ /* */ /* Implements the fundamental stack operations, using a vector */ /* as the underlying implementation. The type Vect specifies the */ /* form of the vector, either a TVectorImp or a */ /* TIVectorImp. The type T specifies the type of the */ /* objects to be put on the stack. When using TVectorImp, */ /* T should be the same as T0. When using TIVectorImp, T */ /* should be of type pointer to T0. See TStackAsVector and */ /* TIStackAsVector for examples. */ /* */ /*------------------------------------------------------------------------*/ template class TStackAsVectorImp { public: TStackAsVectorImp( unsigned max = DEFAULT_STACK_SIZE ) : Data(max), Current(0) { } int IsEmpty() const { return Current == 0; } int IsFull() const { return Current == Data.Limit(); } int GetItemsInContainer() const { return Current; } #if defined( BI_OLDNAMES ) int isEmpty() const { return IsEmpty(); } int isFull() const { return IsFull(); } int getItemsInContainer() const { return GetItemsInContainer(); } #endif // BI_OLDNAMES protected: Vect Data; unsigned Current; }; #if defined( BI_OLDNAMES ) #define BI_StackAsVectorImp TStackAsVectorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TMStackAsVector */ /* template class TMStackAsVectorIterator */ /* */ /* Implements a managed stack of objects of type T, using a vector as */ /* the underlying implementation. */ /* */ /*------------------------------------------------------------------------*/ template class TMStackAsVectorIterator; template class TMStackAsVector : public TStackAsVectorImp,T> { typedef TStackAsVectorImp,T> Parent; public: friend class TMStackAsVectorIterator; typedef void (*IterFunc)(T&, void *); typedef int (*CondFunc)(const T&, void *); TMStackAsVector( unsigned max = DEFAULT_STACK_SIZE ) : TStackAsVectorImp,T>( max ) { } void Push( const T& t ) { PRECONDITION( Current < Data.Limit() ); Data[Current++] = t; } T Pop() { PRECONDITION( Current > 0 ); return Data[--Current]; } const T& Top() const { PRECONDITION( Current > 0 ); return Data[Current-1]; } void ForEach( IterFunc iter, void *args ) { if( !IsEmpty() ) Data.ForEach( iter, args, 0, Current ); } T *FirstThat( CondFunc cond, void *args ) const { if( IsEmpty() ) return 0; return Data.FirstThat( cond, args, 0, Current ); } T *LastThat( CondFunc cond, void *args ) const { if( IsEmpty() ) return 0; return Data.LastThat( cond, args, 0, Current ); } void Flush() { Current = 0; } #if defined( BI_OLDNAMES ) void push( const T& t ) { Push(t); } T pop() { return Pop(); } const T& top() const { return Top(); } 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 ); } void flush( TShouldDelete::DeleteType = TShouldDelete::DefDelete ) { Flush(); } #endif // BI_OLDNAMES }; template class TMStackAsVectorIterator : public TMVectorIteratorImp { public: TMStackAsVectorIterator( const TMStackAsVector& s ) : TMVectorIteratorImp(s.Data,0,s.Current) { } }; #if defined( BI_OLDNAMES ) #define BI_MStackAsVector TMStackAsVector #define BI_MStackAsVectorIterator TMStackAsVectorIterator #endif /*------------------------------------------------------------------------*/ /* */ /* template class TStackAsVector */ /* template class TStackAsVectorIterator */ /* */ /* Implements a stack of objects of type T, using a vector as */ /* the underlying implementation and TStandardAllocator as its memory */ /* manager. */ /* */ /*------------------------------------------------------------------------*/ template class TStackAsVector : public TMStackAsVector { public: TStackAsVector( unsigned max = DEFAULT_STACK_SIZE ) : TMStackAsVector( max ) { } }; template class TStackAsVectorIterator : public TMStackAsVectorIterator { public: TStackAsVectorIterator( const TStackAsVector& s ) : TMStackAsVectorIterator(s) { } }; #if defined( BI_OLDNAMES ) #define BI_StackAsVector TStackAsVector #define BI_StackAsVectorIterator TStackAsVectorIterator #endif /*------------------------------------------------------------------------*/ /* */ /* template class TMIStackAsVector */ /* template class TMIStackAsVectorIterator */ /* */ /* Implements a managed stack of pointers to objects of type T, */ /* using a vector as the underlying implementation. */ /* */ /*------------------------------------------------------------------------*/ template class TMIStackAsVectorIterator; template class TMIStackAsVector : public TStackAsVectorImp, T * >, public TShouldDelete { typedef TStackAsVectorImp, T * > Parent; public: friend class TMIStackAsVectorIterator; typedef void (*IterFunc)(T&, void *); typedef int (*CondFunc)(const T&, void *); TMIStackAsVector( unsigned max = DEFAULT_STACK_SIZE ) : TStackAsVectorImp,T *>( max ) { } ~TMIStackAsVector() { Flush(); } void Push( T *t ) { PRECONDITION( Current < Data.Limit() ); Data[Current++] = t; } T *Pop() { PRECONDITION( Current > 0 ); return Data[--Current]; } T *const& Top() const { PRECONDITION( Current > 0 ); return Data[Current-1]; } void Flush( DeleteType dt = DefDelete ) { Data.Flush( DelObj(dt), Current ); Current = 0; } void ForEach( IterFunc iter, void *args ) { if( !IsEmpty() ) Data.ForEach( iter, args, 0, Current ); } T *FirstThat( CondFunc cond, void *args ) const { if( IsEmpty() ) return 0; return Data.FirstThat( cond, args, 0, Current ); } T *LastThat( CondFunc cond, void *args ) const { if( IsEmpty() ) return 0; return Data.LastThat( cond, args, 0, Current ); } #if defined( BI_OLDNAMES ) void push( T *t ) { Push(t); } T *pop() { return Pop(); } T *top() const { return Top(); } void flush( DeleteType dt = DefDelete ) { Flush(dt); } 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 }; template class TMIStackAsVectorIterator : public TMIVectorIteratorImp { public: TMIStackAsVectorIterator( const TMIStackAsVector& s ) : TMIVectorIteratorImp(s.Data,0,s.Current) { } }; #if defined( BI_OLDNAMES ) #define BI_MIStackAsVector TMIStackAsVector #define BI_MIStackAsVectorIterator TMIStackAsVectorIterator #endif /*------------------------------------------------------------------------*/ /* */ /* template class TIStackAsVector */ /* template class TIStackAsVectorIterator */ /* */ /* Implements a stack of pointers to objects of type T, */ /* using a vector as the underlying implementation and */ /* TStandardAllocator as its memory manager. */ /* */ /*------------------------------------------------------------------------*/ template class TIStackAsVector : public TMIStackAsVector { public: TIStackAsVector( unsigned max = DEFAULT_STACK_SIZE ) : TMIStackAsVector( max ) { } }; template class TIStackAsVectorIterator : public TMIStackAsVectorIterator { public: TIStackAsVectorIterator( const TIStackAsVector& s ) : TMIStackAsVectorIterator(s) { } }; #if defined( BI_OLDNAMES ) #define BI_IStackAsVector TIStackAsVector #define BI_IStackAsVectorIterator TIStackAsVectorIterator #endif /*------------------------------------------------------------------------*/ /* */ /* template class TStackAsListImp */ /* */ /* Implements the fundamental stack operations, using a stack */ /* as the underlying implementation. The type Stk specifies the */ /* form of the stack, either a TStackImp or a */ /* TIStackImp. The type T specifies the type of the */ /* objects to be put on the stack. When using TStackImp, */ /* T should be the same as T0. When using TIStackImp, T */ /* should be of type pointer to T0. See TStackAsList and */ /* TIStackAsList for examples. */ /* */ /*------------------------------------------------------------------------*/ template class TStackAsListImp { public: TStackAsListImp() { } int IsEmpty() const { return Data.IsEmpty(); } int IsFull() const { return 0; } int GetItemsInContainer() const { return Data.GetItemsInContainer(); } #if defined( BI_OLDNAMES ) int isEmpty() const { return IsEmpty(); } int isFull() const { return IsFull(); } int getItemsInContainer() const { return GetItemsInContainer(); } #endif // BI_OLDNAMES protected: Stk Data; }; #if defined( BI_OLDNAMES ) #define BI_StackAsListImp TStackAsListImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TMStackAsList */ /* template class TMStackAsListIterator */ /* */ /* Implements a managed stack of objects of type T, using a list as */ /* the underlying implementation. */ /* */ /*------------------------------------------------------------------------*/ template class TMStackAsListIterator; template class TMStackAsList : public TStackAsListImp,T> { typedef TStackAsListImp,T> Parent; public: friend class TMStackAsListIterator; typedef void (*IterFunc)(T&, void *); typedef int (*CondFunc)(const T&, void *); void Push( const T& t ) { Data.Add( t ); } T Pop(); const T& Top() const { PRECONDITION( !Data.IsEmpty() ); return Data.PeekHead(); } void ForEach( IterFunc iter, void *args ) { Data.ForEach( iter, args ); } T *FirstThat( CondFunc cond, void *args ) const { return Data.FirstThat( cond, args ); } T *LastThat( CondFunc cond, void *args ) const { return Data.LastThat( cond, args ); } void Flush() { Data.Flush(); } #if defined( BI_OLDNAMES ) void push( const T& t ) { Push(t); } T pop() { return Pop(); } const T& top() const { return Top(); } 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 ); } void flush( TShouldDelete::DeleteType = TShouldDelete::DefDelete ) { Flush(); } #endif // BI_OLDNAMES }; template T TMStackAsList::Pop() { T t = Top(); Data.Detach( t ); return t; } template class TMStackAsListIterator : public TMListIteratorImp { public: TMStackAsListIterator( const TMStackAsList& s ) : TMListIteratorImp(s.Data) { } }; #if defined( BI_OLDNAMES ) #define BI_MStackAsList TMStackAsList #define BI_MStackAsListIterator TMStackAsListIterator #endif /*------------------------------------------------------------------------*/ /* */ /* template class TStackAsList */ /* template class TStackAsListIterator */ /* */ /* Implements a stack of objects of type T, using a list as */ /* the underlying implementation and TStandardAllocator as its memory */ /* manager. */ /* */ /*------------------------------------------------------------------------*/ template class TStackAsList : public TMStackAsList { }; template class TStackAsListIterator : public TMStackAsListIterator { public: TStackAsListIterator( const TStackAsList& s ) : TMStackAsListIterator(s) { } }; #if defined( BI_OLDNAMES ) #define BI_StackAsList TStackAsList #define BI_StackAsListIterator TStackAsListIterator #endif /*------------------------------------------------------------------------*/ /* */ /* template class TMIStackAsList */ /* template class TMIStackAsListIterator */ /* */ /* Implements a managed stack of pointers to objects of type T, */ /* using a linked list as the underlying implementation. */ /* */ /*------------------------------------------------------------------------*/ template class TMIStackAsListIterator; template class TMIStackAsList : public TStackAsListImp,T *>, public TShouldDelete { typedef TStackAsListImp,T *> Parent; public: friend class TMIStackAsListIterator; typedef void (*IterFunc)(T&, void *); typedef int (*CondFunc)(const T&, void *); ~TMIStackAsList() { Flush(); } void Push( T *t ) { Data.Add( t ); } T *Pop(); T *Top() const { PRECONDITION( !Data.IsEmpty() ); return Data.PeekHead(); } void Flush( DeleteType dt = DefDelete ) { Data.Flush( DelObj(dt) ); } void ForEach( IterFunc iter, void *args ) { Data.ForEach( iter, args ); } T *FirstThat( CondFunc cond, void *args ) const { return Data.FirstThat( cond, args ); } T *LastThat( CondFunc cond, void *args ) const { return Data.LastThat( cond, args ); } #if defined( BI_OLDNAMES ) void push( T *t ) { Push(t); } T *pop() { return Pop(); } T *top() const { return Top(); } Parent::isEmpty; Parent::isFull; Parent::getItemsInContainer; void flush( DeleteType dt = DefDelete ) { Flush(dt); } 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 }; template T *TMIStackAsList::Pop() { T * t = Top(); Data.Detach( t ); return t; } template class TMIStackAsListIterator : public TMIListIteratorImp { public: TMIStackAsListIterator( const TMIStackAsList& s ) : TMIListIteratorImp(s.Data) { } }; #if defined( BI_OLDNAMES ) #define BI_MIStackAsList TMIStackAsList #define BI_MIStackAsListIterator TMIStackAsListIterator #endif /*------------------------------------------------------------------------*/ /* */ /* template class TIStackAsList */ /* template class TIStackAsListIterator */ /* */ /* Implements a stack of pointers to objects of type T, */ /* using a linked list as the underlying implementation and */ /* TStandardAllocator as its memory manager. */ /* */ /*------------------------------------------------------------------------*/ template class TIStackAsList : public TMIStackAsList { }; template class TIStackAsListIterator : public TMIStackAsListIterator { public: TIStackAsListIterator( const TIStackAsList& s ) : TMIStackAsListIterator(s) { } }; #if defined( BI_OLDNAMES ) #define BI_IStackAsList TIStackAsList #define BI_IStackAsListIterator TIStackAsListIterator #endif /*------------------------------------------------------------------------*/ /* */ /* template class TStack */ /* template class TStackIterator */ /* */ /* Easy names for TStackAsVector and TStackAsVectorIterator. */ /* */ /*------------------------------------------------------------------------*/ template class TStack : public TStackAsVector { public: TStack( unsigned max = DEFAULT_STACK_SIZE ) : TStackAsVector( max ) { } } template class TStackIterator : public TStackAsVectorIterator { public: TStackIterator( const TStack& a ) : TStackAsVectorIterator(a) { } }; #if defined( BI_CLASSLIB_NO_po ) #pragma option -po. #endif #pragma option -Vo. #endif // CLASSLIB_STACKS_H