/*------------------------------------------------------------------------*/ /* */ /* VECTIMP.H */ /* */ /* Copyright (c) 1991, 1994 Borland International */ /* All Rights Reserved */ /* */ /*------------------------------------------------------------------------*/ #if !defined( CLASSLIB_VECTIMP_H ) #define CLASSLIB_VECTIMP_H #if !defined( __LIMITS_H ) #include #endif #if !defined( __CHECKS_H ) #include #endif #if !defined( CLASSLIB_DEFS_H ) #include #endif #if !defined( CLASSLIB_STDTEMPL_H ) #include #endif #if !defined( CLASSLIB_ALLOCTR_H ) #include #endif #if !defined( CLASSLIB_MEMMGR_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 TVectorImpBase */ /* */ /* Implements the base functionality for a managed vector of objects of */ /* type T. Assumes that T has meaningful copy semantics and a default */ /* constructor. */ /* */ /*------------------------------------------------------------------------*/ template class TVectorImpBase : public Alloc { public: TVectorImpBase() : Data(0), Lim(0) {} TVectorImpBase( unsigned sz, unsigned d ) : Data( new(*this)T[sz] ), Lim(sz) { } TVectorImpBase( const TVectorImpBase& ); const TVectorImpBase& operator = ( const TVectorImpBase& ); ~TVectorImpBase() { delete [] Data; } unsigned Limit() const { return Lim; } virtual unsigned Top() const { return Lim; } virtual unsigned Count() const { return Lim; } int Resize( unsigned, unsigned = 0 ); virtual unsigned GetDelta() const { return 0; } #if defined( BI_OLDNAMES ) unsigned limit() const { return Limit(); } int resize( unsigned sz, unsigned off = 0 ) { return Resize(sz,off); } #endif protected: T * Data; unsigned Lim; virtual void Zero( unsigned, unsigned ) { } }; template TVectorImpBase::TVectorImpBase( const TVectorImpBase& v ) : Data( new(*this)T[v.Lim] ), Lim(v.Lim) { PRECONDITION( Lim == 0 || (Data != 0 && v.Data != 0) ); for( unsigned i = 0; i < Lim; i++ ) Data[i] = v.Data[i]; } template const TVectorImpBase& TVectorImpBase::operator = ( const TVectorImpBase& v ) { if( Data != v.Data ) { delete [] Data; Data = new(*this)T[v.Lim]; CHECK( Data != 0 ); Lim = v.Lim; for( unsigned i = 0; i < Lim; i++ ) Data[i] = v.Data[i]; } return *this; } inline unsigned NextDelta( unsigned sz, unsigned delta ) { return (sz%delta) ? ((sz+delta)/delta)*delta : sz; } template int TVectorImpBase::Resize( unsigned newSz, unsigned offset ) { if( newSz <= Lim || GetDelta() == 0 ) return 0; unsigned sz = Lim + NextDelta( newSz - Lim, GetDelta() ); T *temp = new(*this)T[sz]; unsigned last = min( sz-offset, Lim ); for( unsigned i = 0; i < last; i++ ) temp[i+offset] = Data[i]; delete [] Data; Data = temp; Lim = sz; Zero( last+offset, sz ); return 1; } /*------------------------------------------------------------------------*/ /* */ /* template class TMVectorImp */ /* */ /* Implements a managed vector of objects of type T. Assumes that */ /* T has meaningful copy semantics and a default constructor. */ /* */ /*------------------------------------------------------------------------*/ template class TMVectorIteratorImp; template class TMVectorImp : public TVectorImpBase { public: typedef void (*IterFunc)(T&, void *); typedef int (*CondFunc)(const T&, void *); friend TMVectorIteratorImp; TMVectorImp() : TVectorImpBase() {} TMVectorImp( unsigned sz, unsigned d = 0 ) : TVectorImpBase(sz,d) {} T& operator [] ( unsigned index ) { PRECONDITION( Lim == 0 || Data != 0 && index < Lim ); return Data[index]; } T& operator [] ( unsigned index ) const { PRECONDITION( Lim > 0 && Data != 0 && index < Lim ); return Data[index]; } void Flush( unsigned = UINT_MAX, unsigned = 0 ) {} void ForEach( IterFunc iter, void *args ) { ForEach( iter, args, 0, Count() ); } void ForEach( IterFunc iter, void *args, unsigned start, unsigned stop ); T *FirstThat( CondFunc cond, void *args, unsigned start, unsigned ) const; T *FirstThat( CondFunc cond, void *args ) const { return FirstThat( cond, args, 0, Count() ); } T *LastThat( CondFunc cond, void *args, unsigned start, unsigned stop ) const; T *LastThat( CondFunc cond, void *args ) const { return LastThat( cond, args, 0, Count() ); } #if defined( BI_OLDNAMES ) void flush( unsigned = 0, unsigned = UINT_MAX, unsigned = 0 ) {} void forEach( IterFunc iter, void *args ) { ForEach( iter, args ); } void forEach( IterFunc iter, void *args, unsigned start, unsigned stop ) { ForEach( iter, args, start, stop ); } T *firstThat( CondFunc cond, void *args, unsigned start, unsigned stop ) const { return FirstThat( cond, args, start, stop ); } T *firstThat( CondFunc cond, void *args ) const { return FirstThat( cond, args ); } T *lastThat( CondFunc cond, void *args, unsigned start, unsigned stop ) const { return LastThat( cond, args, start, stop ); } T *lastThat( CondFunc cond, void *args ) const { return LastThat( cond, args ); } #endif // BI_OLDNAMES }; template void TMVectorImp::ForEach( IterFunc iter, void *args, unsigned start, unsigned stop ) { for( unsigned cur = start; cur < stop; cur++ ) iter( Data[cur], args ); } template T *TMVectorImp::FirstThat( CondFunc cond, void *args, unsigned start, unsigned stop ) const { for( unsigned cur = start; cur < stop; cur++ ) if( cond( Data[cur], args ) != 0 ) return &(T&)Data[cur]; return 0; } template T *TMVectorImp::LastThat( CondFunc cond, void *args, unsigned start, unsigned stop ) const { T *res = 0; for( unsigned cur = start; cur < stop; cur++ ) if( cond( Data[cur], args ) != 0 ) res = &(T&)Data[cur]; return res; } #if defined( BI_OLDNAMES ) #define BI_MVectorImp TMVectorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TMVectorIteratorImp */ /* */ /* Implements a vector iterator. This iterator works with any direct, */ /* managed vector. For indirect vectors, see TMIVectorIteratorImp. */ /* */ /*------------------------------------------------------------------------*/ template class TMVectorIteratorImp { public: TMVectorIteratorImp( const TMVectorImp&v ) { Vect = &v; Restart(0,v.Top()); } TMVectorIteratorImp( const TMVectorImp&v, unsigned start, unsigned stop ) { Vect = &v; Restart( start, stop ); } operator int() const { return Cur < Upper; } const T& Current() const { PRECONDITION( Cur < Upper ); return (*Vect)[Cur]; } const T& operator ++ ( int ) { const T& temp = Current(); Cur++; return temp; } const T& operator ++ () { PRECONDITION( Cur < Upper ); Cur++; return Current(); } void Restart() { Restart(Lower,Upper); } void Restart( unsigned start, unsigned stop ) { Cur = Lower = start; Upper = stop; } #if defined( BI_OLDNAMES ) const T& current() { return Current(); } void restart() { Restart(); } void restart( unsigned start, unsigned stop ) { Restart( start, stop ); } #endif // BI_OLDNAMES private: const TMVectorImp *Vect; unsigned Cur; unsigned Lower, Upper; }; #if defined( BI_OLDNAMES ) #define BI_MVectorIteratorImp TMVectorIteratorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TVectorImp */ /* template class TVectorIteratorImp */ /* */ /* Implements a vector of objects of type T using TStandardAllocator as */ /* its memory manager. Assumes that T has meaningful copy semantics and */ /* a default constructor. */ /* */ /*------------------------------------------------------------------------*/ template class TVectorImp : public TMVectorImp { public: TVectorImp() { } TVectorImp( unsigned sz, unsigned = 0 ) : TMVectorImp( sz ) { } TVectorImp( const TVectorImp& v ) : TMVectorImp( v ) { } }; template class TVectorIteratorImp : public TMVectorIteratorImp { public: TVectorIteratorImp( const TVectorImp& v ) : TMVectorIteratorImp(v) { } TVectorIteratorImp( const TVectorImp& v, unsigned start, unsigned stop ) : TMVectorIteratorImp(v,start,stop) { } }; #if defined( BI_OLDNAMES ) #define BI_VectorImp TVectorImp #define BI_VectorIteratorImp TVectorIteratorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TMCVectorImp */ /* template class TMCVectorIteratorImp */ /* */ /* Implements a managed, counted vector of objects of type T. Assumes */ /* that T has meaningful copy semantics and a default constructor. */ /* */ /*------------------------------------------------------------------------*/ template class TMCVectorImp : public TMVectorImp { public: TMCVectorImp() : Count_(0), Delta(0) { } TMCVectorImp( unsigned sz, unsigned d = 0 ) : TMVectorImp( sz ), Count_(0), Delta(d) { } int Add( const T& ); int AddAt( const T&, unsigned ); int Detach( const T& t ) { return Detach( Find(t) ); } int Detach( unsigned loc ); int IsEmpty() const { return Count_ == 0; } void Flush( unsigned stop = UINT_MAX, unsigned start = 0 ) { TMVectorImp::Flush( stop, start ); Count_ = 0; } virtual unsigned Find( const T& ) const; virtual unsigned Top() const { return Count_; } virtual unsigned Count() const { return Count_; } virtual unsigned GetDelta() const { return Delta; } #if defined( BI_OLDNAMES ) void add( const T& t ) { Add(t); } void addAt( const T& t, unsigned loc ) { AddAt( t, loc ); } void detach( const T& t, int del = 0 ) { Detach( t ); } void detach( unsigned loc, int del = 0 ) { Detach( loc ); } int isEmpty() const { return IsEmpty(); } void flush( unsigned del = 0, unsigned stop = UINT_MAX, unsigned start = 0 ) { Flush( stop, start ); } unsigned count() const { return Count(); } #endif // BI_OLDNAMES protected: unsigned Count_; unsigned Delta; }; template class TMCVectorIteratorImp : public TMVectorIteratorImp { public: TMCVectorIteratorImp( const TMCVectorImp& v ) : TMVectorIteratorImp(v) { } TMCVectorIteratorImp( const TMCVectorImp& v, unsigned start, unsigned stop ) : TMVectorIteratorImp(v,start,stop) { } }; template int TMCVectorImp::Add( const T& t ) { if( Count_ >= Lim && !Resize( Count_+1 ) ) return 0; Data[Count_++] = t; return 1; } template int TMCVectorImp::AddAt( const T& t, unsigned loc ) { if( loc >= Lim && !Resize(loc+1) ) return 0; if( Count_ == Lim && !Resize(Lim+1) ) return 0; if( loc > Count_ ) Count_ = loc; for( unsigned cur = Count_; cur > loc; cur-- ) Data[cur] = Data[cur-1]; Data[loc] = t; Count_++; return 1; } template int TMCVectorImp::Detach( unsigned loc ) { if( loc >= Lim ) return 0; if( loc >= Count_ ) { Zero( loc, loc+1 ); // removing an element that's not return 1; // in the counted portion } Count_--; for( unsigned cur = loc; cur < Count_; cur++ ) Data[cur] = Data[cur+1]; return 1; } template unsigned TMCVectorImp::Find( const T& t ) const { for( unsigned loc = 0; loc < Count_; loc++ ) if( Data[loc] == t ) return loc; return UINT_MAX; } #if defined( BI_OLDNAMES ) #define BI_MCVectorImp TMCVectorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TCVectorImp */ /* template class TCVectorIteratorImp */ /* */ /* Implements a counted vector of objects of type T using */ /* TStandardAllocator as its memory manager. Assumes */ /* that T has meaningful copy semantics and a default constructor. */ /* */ /*------------------------------------------------------------------------*/ template class TCVectorImp : public TMCVectorImp { public: TCVectorImp() { } TCVectorImp( unsigned sz, unsigned d = 0 ) : TMCVectorImp( sz, d ) { } }; template class TCVectorIteratorImp : public TMCVectorIteratorImp { public: TCVectorIteratorImp( const TCVectorImp& v ) : TMCVectorIteratorImp(v) { } TCVectorIteratorImp( const TCVectorImp& v, unsigned start, unsigned stop ) : TMCVectorIteratorImp(v,start,stop) { } }; #if defined( BI_OLDNAMES ) #define BI_CVectorImp TCVectorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TMSVectorImp */ /* template class TMSVectorIteratorImp */ /* */ /* Implements a managed, sorted vector of objects of type T. Assumes */ /* that T has meaningful copy semantics, a meaningful < operator, */ /* and a default constructor. */ /* */ /*------------------------------------------------------------------------*/ template class TMSVectorImp : public TMCVectorImp { public: TMSVectorImp() { } TMSVectorImp( unsigned sz, unsigned d = 0 ) : TMCVectorImp( sz, d ) { } int Add( const T& ); virtual unsigned Find( const T& ) const; }; template class TMSVectorIteratorImp : public TMCVectorIteratorImp { public: TMSVectorIteratorImp( const TMSVectorImp& v ) : TMCVectorIteratorImp(v) { } TMSVectorIteratorImp( const TMSVectorImp& v, unsigned start, unsigned stop ) : TMCVectorIteratorImp(v,start,stop) { } }; template int TMSVectorImp::Add( const T& t ) { unsigned loc = Count_++; if( Count_ > Lim ) if( !Resize( Count_ ) ) { --Count_; return 0; } while( loc > 0 && t < Data[loc-1] ) { Data[loc] = Data[loc-1]; loc--; } Data[loc] = t; return 1; } template unsigned TMSVectorImp::Find( const T& t ) const { if( Count_ == 0 ) return UINT_MAX; unsigned lower = 0; unsigned upper = Count_-1; while( lower < upper && upper != UINT_MAX ) { unsigned middle = (lower+upper)/2; if( (T&)Data[middle] == (T&)t ) return middle; if( (T&)Data[middle] < (T&)t ) lower = middle+1; else upper = middle-1; } if( lower == upper && (T&)Data[lower] == (T&)t ) return lower; else return UINT_MAX; } #if defined( BI_OLDNAMES ) #define BI_MSVectorImp TMSVectorImp #define BI_MSVectorIteratorImp TMSVectorIteratorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TSVectorImp */ /* template class TSVectorIteratorImp */ /* */ /* Implements a sorted vector 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 TSVectorImp : public TMSVectorImp { public: TSVectorImp() { } TSVectorImp( unsigned sz, unsigned d = 0 ) : TMSVectorImp( sz, d ) { } }; template class TSVectorIteratorImp : public TMSVectorIteratorImp { public: TSVectorIteratorImp( const TSVectorImp& v ) : TMSVectorIteratorImp(v) { } TSVectorIteratorImp( const TSVectorImp& v, unsigned start, unsigned stop ) : TMSVectorIteratorImp(v,start,stop) { } }; #if defined( BI_OLDNAMES ) #define BI_SVectorImp TSVectorImp #define BI_SVectorIteratorImp TSVectorIteratorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TMIVectorImp */ /* */ /* Implements a managed vector of pointers to objects of type T. */ /* Since pointers always have meaningful copy semantics, this class */ /* can handle any type of object. */ /* */ /*------------------------------------------------------------------------*/ template class TMIVectorImp : public TVectorImpBase { public: typedef void (*IterFunc)(T&, void *); typedef int (*CondFunc)(const T&, void *); TMIVectorImp( unsigned sz, unsigned d = 0 ) : TVectorImpBase(sz,d) {} T *& operator [] ( unsigned index ) { PRECONDITION( Lim == 0 || Data != 0 && index < Lim ); return *STATIC_CAST(T **,STATIC_CAST(void *,&Data[index])); } T *& operator [] ( unsigned index ) const { PRECONDITION( Lim > 0 && Data != 0 && index < Lim ); return *STATIC_CAST(T **,STATIC_CAST(void *,&Data[index])); } void Flush( unsigned del = 0, unsigned stop = UINT_MAX, unsigned start = 0 ); void ForEach( IterFunc iter, void *args ) { ForEach( iter, args, 0, Count() ); } void ForEach( IterFunc iter, void *args, unsigned start, unsigned stop ); T *FirstThat( CondFunc cond, void *args ) const { return FirstThat( cond, args, 0, Count() ); } T *FirstThat( CondFunc cond, void *args, unsigned start, unsigned stop ) const; T *LastThat( CondFunc cond, void *args ) const { return LastThat( cond, args, 0, Count() ); } T *LastThat( CondFunc cond, void *args, unsigned start, unsigned stop ) const; #if defined( BI_OLDNAMES ) void flush( unsigned del = 0, unsigned upr = UINT_MAX, unsigned lwr = 0 ) { Flush( del, upr, lwr ); } void forEach( IterFunc iter, void *args ) { ForEach( iter, args ); } void forEach( IterFunc cond, void *args, unsigned start, unsigned stop) { ForEach( cond, args, start, stop ); } T *firstThat( CondFunc cond, void *args ) const { return FirstThat( cond, args ); } T *firstThat( CondFunc cond, void *args, unsigned start, unsigned stop ) const { return FirstThat( cond, args, start, stop ); } T *lastThat( CondFunc cond, void *args ) const { return LastThat( cond, args ); } T *lastThat( CondFunc cond, void *args, unsigned start, unsigned stop ) const { return LastThat( cond, args, start, stop ); } #endif // BI_OLDNAMES protected: void Zero( unsigned, unsigned ); private: static void DelObj( T&, void * ); }; template void TMIVectorImp::DelObj( T& tRef, void * ) { delete &tRef; } template void TMIVectorImp::Flush( unsigned del, unsigned upr, unsigned lwr ) { upr = min( upr, Limit() ); if( del ) ForEach( DelObj, 0, lwr, upr ); Zero( lwr, upr ); } template void TMIVectorImp::ForEach( IterFunc iter, void *args, unsigned start, unsigned stop ) { for( unsigned cur = start; cur < stop; cur++ ) if( Data[cur] != 0 ) iter( *STATIC_CAST(T *,STATIC_CAST(void *,Data[cur])), args ); } template T *TMIVectorImp::FirstThat( CondFunc cond, void *args, unsigned start, unsigned stop ) const { for( unsigned cur = start; cur < stop; cur++ ) if( Data[cur] != 0 && cond( *STATIC_CAST(T *,STATIC_CAST(void *,Data[cur])), args ) != 0 ) return STATIC_CAST(T *,STATIC_CAST(void *,Data[cur])); return 0; } template T *TMIVectorImp::LastThat( CondFunc cond, void *args, unsigned start, unsigned stop ) const { T *res = 0; for( unsigned cur = start; cur < stop; cur++ ) if( Data[cur] != 0 && cond( *STATIC_CAST(T *,STATIC_CAST(void *,Data[cur])), args ) != 0 ) res = STATIC_CAST(T *,STATIC_CAST(void *,Data[cur])); return res; } template void TMIVectorImp::Zero( unsigned lwr, unsigned upr ) { for( unsigned i = lwr; i < min( Limit(), upr ); i++ ) Data[i] = 0; } #if defined( BI_OLDNAMES ) #define BI_MIVectorImp TMIVectorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TMIVectorIteratorImp */ /* */ /* Implements an iterator for a managed indirect vector of pointers to */ /* objects of type T. */ /* */ /*------------------------------------------------------------------------*/ template class TMIVectorIteratorImp { public: TMIVectorIteratorImp( const TMIVectorImp& v ) { Vect = &v; Restart(0,v.Top()); } TMIVectorIteratorImp( const TMIVectorImp& v, unsigned start, unsigned stop ) { Vect = &v; Restart( start, stop ); } operator int() const { return Cur < Upper; } T *Current() const { PRECONDITION( Cur < Upper ); return STATIC_CAST(T *,STATIC_CAST(void *,(*Vect)[Cur])); } T *operator ++ ( int ) { T *temp = Current(); Cur++; return temp; } T *operator ++ () { PRECONDITION( Cur < Upper ); Cur++; return Current(); } void Restart() { Restart(Lower,Upper); } void Restart( unsigned start, unsigned stop ) { Cur = Lower = start; Upper = stop; } #if defined( BI_OLDNAMES ) const T *current() { return Current(); } void restart() { Restart(); } void restart( unsigned start, unsigned stop ) { Restart( start, stop ); } #endif // BI_OLDNAMES private: const TMIVectorImp *Vect; unsigned Cur; unsigned Lower, Upper; }; #if defined( BI_OLDNAMES ) #define BI_MIVectorIteratorImp TMIVectorIteratorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TIVectorImp */ /* template class TIVectorIteratorImp */ /* */ /* Implements a vector of pointers to objects of type T using */ /* TStandardAllocator as its memory manager. */ /* Since pointers always have meaningful copy semantics, this class */ /* can handle any type of object. */ /* */ /*------------------------------------------------------------------------*/ template class TIVectorImp : public TMIVectorImp { public: TIVectorImp( unsigned sz, unsigned d = 0 ) : TMIVectorImp(sz,d) { } }; template class TIVectorIteratorImp : public TMIVectorIteratorImp { public: TIVectorIteratorImp( const TIVectorImp& v ) : TMIVectorIteratorImp(v) { } TIVectorIteratorImp( const TIVectorImp& v, unsigned l, unsigned u ) : TMIVectorIteratorImp(v,l,u) { } }; #if defined( BI_OLDNAMES ) #define BI_IVectorImp TIVectorImp #define BI_IVectorIteratorImp TIVectorIteratorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TMICVectorImp */ /* template class TMICVectorIteratorImp */ /* */ /* Implements a managed, counted vector of pointers to objects of type T.*/ /* Since pointers always have meaningful copy semantics, this class */ /* can handle any type of object. */ /* */ /*------------------------------------------------------------------------*/ template class TMICVectorImp : public TMIVectorImp { public: TMICVectorImp( unsigned sz, unsigned d = 0 ) : TMIVectorImp(sz), Delta(d), Count_(0) {} int Add( T *t ); int AddAt( T *, unsigned ); int Detach( const T *t, int del = 0 ) { return Detach( Find(t), del ); } int Detach( unsigned loc, int del = 0 ); int IsEmpty() const { return Count_ == 0; } void Flush( int del = 0, unsigned stop = UINT_MAX, unsigned start = 0 ) { TMIVectorImp::Flush( del, stop, start ); Count_ = 0; } unsigned Find( const T *t ) const; virtual unsigned Top() const { return Count_; } virtual unsigned Count() const { return Count_; } virtual unsigned GetDelta() const { return Delta; } #if defined( BI_OLDNAMES ) unsigned find( T *t ) const { return Find(t); } void add( T *t ) { Add(t); } #endif // BI_OLDNAMES protected: unsigned Count_; unsigned Delta; }; template class TMICVectorIteratorImp : public TMIVectorIteratorImp { public: TMICVectorIteratorImp( const TMICVectorImp& v ) : TMIVectorIteratorImp(v) { } TMICVectorIteratorImp( const TMICVectorImp& v, unsigned start, unsigned stop ) : TMIVectorIteratorImp(v,start,stop) { } }; template int TMICVectorImp::AddAt( T *t, unsigned loc ) { if( loc >= Lim && !Resize(loc+1) ) return 0; if( Count_ == Lim && !Resize(Lim+1) ) return 0; if( loc > Count_ ) Count_ = loc; for( unsigned cur = Count_; cur > loc; cur-- ) Data[cur] = Data[cur-1]; Data[loc] = t; Count_++; return 1; } template int TMICVectorImp::Detach( unsigned loc, int del ) { if( loc >= Lim ) return 0; if( del ) delete STATIC_CAST(T *,STATIC_CAST(void *,Data[loc])); if( loc >= Count_ ) { Zero( loc, loc+1 ); // removing an element that's not return 1; // in the counted portion } Count_--; for( unsigned cur = loc; cur < Count_; cur++ ) Data[cur] = Data[cur+1]; Zero( Count_, Count_+1 ); return 1; } template unsigned TMICVectorImp::Find( const T *t ) const { if( Top() != 0 ) { for( unsigned loc = 0; loc < Top(); loc++ ) if( Data[loc] && *STATIC_CAST(T *,STATIC_CAST(void *,Data[loc])) == *t ) return loc; } return UINT_MAX; } template int TMICVectorImp::Add( T *t ) { while( Count_ < Limit() && (*this)[Count_] != 0 ) Count_++; if( Count_ >= Lim && !Resize( Count_+1 ) ) return 0; Data[Count_++] = t; return 1; } #if defined( BI_OLDNAMES ) #define BI_MICVectorImp TMICVectorImp #define BI_MICVectorIteratorImp TMICVectorIteratorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TICVectorImp */ /* template class TICVectorIteratorImp */ /* */ /* Implements a counted vector of pointers to objects of type T using */ /* TStandardAllocator as its memory manager. */ /* Since pointers always have meaningful copy semantics, this class */ /* can handle any type of object. */ /* */ /*------------------------------------------------------------------------*/ template class TICVectorImp : public TMICVectorImp { public: TICVectorImp( unsigned sz, unsigned d = 0 ) : TMICVectorImp( sz, d ) { } }; template class TICVectorIteratorImp : public TMICVectorIteratorImp { public: TICVectorIteratorImp( const TICVectorImp& v ) : TMICVectorIteratorImp(v) { } TICVectorIteratorImp( const TICVectorImp& v, unsigned l, unsigned u ) : TMICVectorIteratorImp(v,l,u) { } }; #if defined( BI_OLDNAMES ) #define BI_ICVectorImp TICVectorImp #define BI_ICVectorVectorImp TICVectorIteratorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TMISVectorImp */ /* template class TMISVectorIteratorImp */ /* */ /* Implements a managed, sorted vector of pointers to objects of type T. */ /* This is implemented through the template TInternalIVectorImp. */ /* Since pointers always have meaningful copy semantics, this class */ /* can handle any type of object. */ /* */ /*------------------------------------------------------------------------*/ template class TMISVectorImp : public TMICVectorImp { public: TMISVectorImp( unsigned sz, unsigned d = 0 ) : TMICVectorImp(sz) { Delta = d; } unsigned Find( const T *t ) const; int Add( T *t ); #if defined( BI_OLDNAMES ) unsigned find( T *t ) const { return Find(t); } void add( T *t ) { Add(t); } #endif // BI_OLDNAMES }; template class TMISVectorIteratorImp : public TMICVectorIteratorImp { public: TMISVectorIteratorImp( const TMISVectorImp& v ) : TMICVectorIteratorImp(v) { } TMISVectorIteratorImp( const TMISVectorImp& v, unsigned start, unsigned stop ) : TMICVectorIteratorImp(v,start,stop) { } }; template unsigned TMISVectorImp::Find( const T *t ) const { if( Count_ == 0 ) return UINT_MAX; unsigned lower = 0; unsigned upper = Count_-1; while( lower < upper && upper != UINT_MAX ) { unsigned middle = (lower+upper)/2; if( *STATIC_CAST(T *,STATIC_CAST(void *,Data[middle])) == *t ) return middle; if( *STATIC_CAST(T *,STATIC_CAST(void *,Data[middle])) < *t ) lower = middle+1; else upper = middle-1; } if( lower == upper && *STATIC_CAST(T *,STATIC_CAST(void *,Data[lower])) == *t ) return lower; else return UINT_MAX; } template int TMISVectorImp::Add( T *t ) { unsigned loc = Count_++; if( Count_ > Lim ) if( !Resize( Count_ ) ) { --Count_; return 0; } while( loc > 0 && *t < *STATIC_CAST(T *,STATIC_CAST(void *,(*this)[loc-1])) ) { Data[loc] = Data[loc-1]; loc--; } Data[loc] = t; return 1; } #if defined( BI_OLDNAMES ) #define BI_MISVectorImp TMISVectorImp #define BI_MISVectorVectorImp TMISVectorIteratorImp #endif /*------------------------------------------------------------------------*/ /* */ /* template class TISVectorImp */ /* template class TISVectorIteratorImp */ /* */ /* Implements a sorted vector of pointers to objects of type T using */ /* TStandardAllocator as its memory manager. */ /* This is implemented through the template TInternalIVectorImp. */ /* Since pointers always have meaningful copy semantics, this class */ /* can handle any type of object. */ /* */ /*------------------------------------------------------------------------*/ template class TISVectorImp : public TMISVectorImp { public: TISVectorImp( unsigned sz, unsigned d = 0 ) : TMISVectorImp( sz, d ) { } }; template class TISVectorIteratorImp : public TMISVectorIteratorImp { public: TISVectorIteratorImp( const TISVectorImp& v ) : TMISVectorIteratorImp(v) { } TISVectorIteratorImp( const TISVectorImp& v, unsigned l, unsigned u ) : TMISVectorIteratorImp(v,l,u) { } }; #if defined( BI_OLDNAMES ) #define BI_ISVectorImp TISVectorImp #define BI_ISVectorIteratorImp TISVectorIteratorImp #endif #if defined( BI_CLASSLIB_NO_po ) #pragma option -po. #endif #pragma option -Vo. #endif // CLASSLIB_VECTIMP_H