/*------------------------------------------------------------------------*/ /* */ /* BAGS.H */ /* */ /* Copyright (c) 1991, 1994 Borland International */ /* All Rights Reserved */ /* */ /*------------------------------------------------------------------------*/ #if !defined( CLASSLIB_BAGS_H ) #define CLASSLIB_BAGS_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 #pragma option -Vo- #if defined( BI_CLASSLIB_NO_po ) #pragma option -po- #endif /*------------------------------------------------------------------------*/ /* */ /* template class TBagAsVectorImp */ /* */ /* Implements a bag, using a vector as the underlying implementation. */ /* The type Vect specifies the form of the vector, either a */ /* TCVectorImp or a TICVectorImp. The type T specifies the */ /* type of the objects to be put in the bag. When using */ /* TVectorImp, T should be the same as T0. When using */ /* TIVectorImp, T should be of type pointer to T0. See */ /* TBagAsVector and TIBagAsVector for examples. */ /* */ /*------------------------------------------------------------------------*/ template class TBagAsVectorImp { public: TBagAsVectorImp( unsigned sz = DEFAULT_BAG_SIZE ) : Data(sz,1) { } int IsEmpty() const { return Data.IsEmpty(); } int IsFull() const { return 0; } int GetItemsInContainer() const { return Data.Top(); } #if defined( BI_OLDNAMES ) int isEmpty() const { return IsEmpty(); } int isFull() const { return IsFull(); } int getItemsInContainer() const { return GetItemsInContainer(); } #endif protected: Vect Data; }; #if defined( BI_OLDNAMES ) #define BI_BagAsVectorImp TBagAsVectorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TMBagAsVector */ /* template class TMBagAsVectorIterator */ /* */ /* Implements a managed bag of objects of type T, using a vector as */ /* the underlying implementation. */ /* */ /*------------------------------------------------------------------------*/ template class TMBagAsVectorIterator; template class TMBagAsVector : public TBagAsVectorImp,T> { typedef TBagAsVectorImp,T> Parent; public: friend class TMBagAsVectorIterator; typedef void (*IterFunc)(T&, void *); typedef int (*CondFunc)(const T&, void *); TMBagAsVector( unsigned sz = DEFAULT_BAG_SIZE ) : TBagAsVectorImp,T>( sz ) { } int Add( const T& t ) { return Data.Add( t ); } int Detach( const T& t ) { return Data.Detach( t ); } int HasMember( const T& t ) const { return Data.Find(t) != UINT_MAX; } const T *Find( const T& t ) const { unsigned loc = Data.Find(t); return ( loc == UINT_MAX ? 0 : &Data[loc] ); } void ForEach( IterFunc iter, void *args ) { Data.ForEach( iter, args, 0, Data.Top() ); } T *FirstThat( CondFunc cond, void *args ) const { return Data.FirstThat( cond, args, 0, Data.Top() ); } T *LastThat( CondFunc cond, void *args ) const { return Data.LastThat( cond, args, 0, Data.Top() ); } void Flush() { Data.Flush(); } #if defined( BI_OLDNAMES ) void add( const T& t ) { Add(t); } void detach( const T& t, TShouldDelete::DeleteType dt = TShouldDelete::NoDelete ) { Detach( t ); } int hasMember( const T& t ) const { return HasMember(t); } T findMember( const T& t ) const { PRECONDITION( HasMember(t) ); return *Find(t); } Parent::isEmpty; Parent::isFull; Parent::getItemsInContainer; 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 dt = TShouldDelete::DefDelete ) { Flush(); } #endif // BI_OLDNAMES }; template class TMBagAsVectorIterator : private TMVectorIteratorImp { typedef TMVectorIteratorImp Parent; public: TMBagAsVectorIterator( const TMBagAsVector& b ) : TMVectorIteratorImp(b.Data) { } void Restart() { Parent::Restart(); } Parent::operator int; Parent::Current; Parent::operator ++; #if defined( BI_OLDNAMES ) Parent::restart; Parent::current; #endif }; #if defined( BI_OLDNAMES ) #define BI_MBagAsVector TMBagAsVector #define BI_MBagAsVectorIterator TMBagAsVectorIterator #endif /*------------------------------------------------------------------------*/ /* */ /* template class TBagAsVector */ /* template class TBagAsVectorIterator */ /* */ /* Implements a bag of objects of type T, using a vector as the */ /* underlying implementation and TStandardAllocator as its memory */ /* manager. */ /* */ /*------------------------------------------------------------------------*/ template class TBagAsVector : public TMBagAsVector { public: TBagAsVector( unsigned sz = DEFAULT_BAG_SIZE ) : TMBagAsVector( sz ) { } }; template class TBagAsVectorIterator : public TMBagAsVectorIterator { public: TBagAsVectorIterator( const TBagAsVector& b ) : TMBagAsVectorIterator(b) { } }; #if defined( BI_OLDNAMES ) #define BI_BagAsVector TBagAsVector #define BI_BagAsVectorIterator TBagAsVectorIterator #endif /*------------------------------------------------------------------------*/ /* */ /* template class TMIBagAsVector */ /* template class TMIBagAsVectorIterator */ /* */ /* Implements a managed bag of pointers to objects of type T, */ /* using a vector as the underlying implementation. */ /* */ /*------------------------------------------------------------------------*/ template class TMIBagAsVectorIterator; template class TMIBagAsVector : public TBagAsVectorImp,T *>, public TShouldDelete { typedef TBagAsVectorImp,T *> Parent; public: typedef void (*IterFunc)(T&, void *); typedef int (*CondFunc)(const T&, void *); friend TMIBagAsVectorIterator; TMIBagAsVector( unsigned sz = DEFAULT_BAG_SIZE ) : TBagAsVectorImp,T *>(sz) { } ~TMIBagAsVector() { Flush(); } int Add( T *t ) { return Data.Add( t ); } int Detach( T *t, DeleteType dt = NoDelete ) { return Data.Detach( t, dt ); } int HasMember( const T *t ) const { return Data.Find(t) != UINT_MAX; } void Flush( TShouldDelete::DeleteType dt = TShouldDelete::DefDelete ) { Data.Flush( DelObj(dt), Data.Top(), 0 ); } T *Find( T *t ) const { unsigned loc = Data.Find(t); return ( loc == UINT_MAX ? 0 : STATIC_CAST(T *,Data[loc]) ); } void ForEach( IterFunc iter, void *args ) { Data.ForEach( iter, args, 0, Data.Top() ); } T *FirstThat( CondFunc cond, void *args ) const { return Data.FirstThat( cond, args, 0, Data.Top() ); } T *LastThat( CondFunc cond, void *args ) const { return Data.LastThat( cond, args, 0, Data.Top() ); } #if defined( BI_OLDNAMES ) void add( T *t ) { Add(t); } void detach( T *t, DeleteType dt = NoDelete ) { Detach( t, dt ); } void flush( TShouldDelete::DeleteType dt = TShouldDelete::DefDelete ) { Flush( dt ); } T *findMember( T *t ) const { return Find(t); } Parent::isEmpty; Parent::isFull; Parent::getItemsInContainer; 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 TMIBagAsVectorIterator : public TMICVectorIteratorImp { public: TMIBagAsVectorIterator( const TMIBagAsVector& s ) : TMICVectorIteratorImp(s.Data,0,s.Data.Top()) {} }; #if defined( BI_OLDNAMES ) #define BI_MIBagAsVector TMIBagAsVector #define BI_MIBagAsVectorIterator TMIBagAsVectorIterator #endif /*------------------------------------------------------------------------*/ /* */ /* template class TIBagAsVector */ /* template class TIBagAsVectorIterator */ /* */ /* Implements a bag of pointers to objects of type T, using a vector as */ /* the underlying implementation and TStandardAllocator as its memory */ /* manager. */ /* */ /*------------------------------------------------------------------------*/ template class TIBagAsVector : public TMIBagAsVector { public: TIBagAsVector( unsigned sz = DEFAULT_BAG_SIZE ) : TMIBagAsVector(sz) { } }; template class TIBagAsVectorIterator : private TMIBagAsVectorIterator { typedef TMIBagAsVectorIterator Parent; public: TIBagAsVectorIterator( const TIBagAsVector& s ) : TMIBagAsVectorIterator(s) { } void Restart() { Parent::Restart(); } Parent::operator int; Parent::Current; Parent::operator ++; #if defined( BI_OLDNAMES ) Parent::restart; Parent::current; #endif }; #if defined( BI_OLDNAMES ) #define BI_IBagAsVector TIBagAsVector #define BI_IBagAsVectorIterator TIBagAsVectorIterator #endif /*------------------------------------------------------------------------*/ /* */ /* template class TBag */ /* template class TBagIterator */ /* */ /* Easy names for TBagAsVector and TBagAsVectorIterator. */ /* */ /*------------------------------------------------------------------------*/ template class TBag : public TBagAsVector { public: TBag( unsigned sz = DEFAULT_BAG_SIZE ) : TBagAsVector( sz ) { } } template class TBagIterator : public TBagAsVectorIterator { public: TBagIterator( const TBag& a ) : TBagAsVectorIterator(a) { } }; #if defined( BI_CLASSLIB_NO_po ) #pragma option -po. #endif #pragma option -Vo. #endif // CLASSLIB_BAGS_H