Source: classlib/include/classlib/vectimp.h


Annotated List
Files
Globals
Hierarchy
Index
/***************************************************************************
    vectimp.h  -  vettori di oggetti
                             -------------------
    begin                : ven dic  7 17:40:01 CET 2001
    copyright            : (C) 2001 by Nicola De Nisco
    email                : nicola@winada.it
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#if !defined( __CLASSLIB_VECTIMP_H )
#define __CLASSLIB_VECTIMP_H

#if !defined( __LIMITS_H )
#include 
#endif  // __LIMITS_H

#if !defined( __CHECKS_H )
#include 
#endif  // __CHECKS_H

#if !defined( __CLASSLIB_DEFS_H )
#include "classlib/defs.h"
#endif  // __CLASSLIB_DEFS_H

#if !defined( __CLASSLIB_STDTEMPL_H )
#include "classlib/stdtempl.h"
#endif  // __CLASSLIB_STDTEMPL_H

#if !defined( __CLASSLIB_ALLOCTR_H )
#include "classlib/alloctr.h"
#endif  // __CLASSLIB_ALLOCTR_H

#if !defined( __CLASSLIB_MEMMGR_H )
#include "classlib/memmgr.h"
#endif  // __CLASSLIB_MEMMGR_H

#if !defined( __CLASSLIB_VOIDP_H )
#include "classlib/voidp.h"
#endif  // __CLASSLIB_VOIDP_H

/**

  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:

    typedef int (*SortFunc)(const T *, const T *);

    TVectorImpBase() : Data(0), Lim(0) {}

    TVectorImpBase( unsigned sz, unsigned )
        {
		Data = new T[sz];
		Lim  = sz;
		OrSize = sz;
		PRECONDITION( Lim == 0 || (Data != 0) );
        }

    TVectorImpBase( const TVectorImpBase& );

    const TVectorImpBase& operator = (const TVectorImpBase& v)
    	{
    	return Copy(v);
    	}

    virtual ~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 );

    void Flush( unsigned = UINT_MAX, unsigned = 0 );

    const TVectorImpBase& Copy(const TVectorImpBase& );

    virtual unsigned GetDelta() const
        {
        return 0;
        }

protected:

    T * Data;
    unsigned Lim, OrSize;

    virtual void Zero( unsigned, unsigned )
        {
        }

    typedef int (*compareQsort)(const void *, const void *);

    void InternalSort( SortFunc funcCompare )
    	{
    	qsort(Data, Count(), sizeof(T), (compareQsort)funcCompare);
    	}
};

template 
TVectorImpBase::TVectorImpBase( const TVectorImpBase& v )
{
    Data = new T[v.Lim];
    Lim  = v.Lim;
	OrSize = v.OrSize;
    PRECONDITION( Lim == 0 || (Data != 0 && v.Data != 0) );
    for( unsigned i = 0; i < Lim; i++ )
        Data[i] = v.Data[i];
}

template 
const TVectorImpBase& TVectorImpBase::Copy( const TVectorImpBase& v )
{
    if( Data != v.Data )
        {
        delete [] Data;
        Data = new T[v.Lim];
        CHECK( Data != 0 );
        Lim = v.Lim;
		OrSize = v.OrSize;
        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 T[sz];
    unsigned last = tmin( 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 
void TVectorImpBase::Flush( unsigned, unsigned )
{
    delete [] Data;
    Data = new T[OrSize];
	Lim  = OrSize;
    PRECONDITION( Lim == 0 || (Data != 0) );
}

/**

  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 class 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 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() );
        }
};

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;
}

/**

  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;
        }

private:

    const TMVectorImp *Vect;
    unsigned Cur;
    unsigned Lower, Upper;

};

/**

  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)
        {
        }

};

/**

  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;
        }

    const TMCVectorImp& Copy(const TMCVectorImp& v)
    	{
    	TMVectorImp::Copy(v);
    	Count_ = v.Count_;
    	Delta  = v.Delta;
    	return *this;
    	}

    virtual unsigned Find( const T& ) const;

    virtual unsigned Top() const
        {
        return Count_;
        }

    virtual unsigned Count() const
        {
        return Count_;
        }

    virtual unsigned GetDelta() const
        {
        return Delta;
        }

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;
}

/**

  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)
        {
        }

};

/**

  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;
}

/**

  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)
        {
        }

};

/**

  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 int  (*SortFunc)(const T *, const T *);
    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;

    const TMIVectorImp& Copy(const TMIVectorImp& v)
    	{
    	TVectorImpBase::Copy(v);
    	return *this;
    	}

    void Sort( SortFunc funcCompare )
    	{
    	funcCom = (void*)funcCompare;
    	InternalSort(CompareQSort);
    	}
    	
protected:

    void Zero( unsigned, unsigned );

private:

    static void DelObj( T&, void * );
	static int CompareQSort(const TVoidPointer *, const TVoidPointer *);
	static void* funcCom;
};

template 
void TMIVectorImp::DelObj( T& tRef, void * )
{
    delete &tRef;
}

template 
void* TMIVectorImp::funcCom = 0;

template 
int TMIVectorImp::CompareQSort(const TVoidPointer *p1, const TVoidPointer *p2)
{
	return ((SortFunc)(funcCom))(
		STATIC_CAST(T *, STATIC_CAST(void *, *p1 )),
		STATIC_CAST(T *, STATIC_CAST(void *, *p2 ))
	);
}

template 
void TMIVectorImp::Flush( unsigned del, unsigned upr, unsigned lwr )
{
    upr = tmin( 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( ((void*)Data[cur]) != NULL )
            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] != NULL &&
            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] != NULL &&
            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 < tmin( Limit(), upr ); i++ )
        Data[i] = NULL;
}

/**

  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;
        }

private:

    const TMIVectorImp *Vect;
    unsigned Cur;
    unsigned Lower, Upper;

};


/**

  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)
        {
        }

};

/**

  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;
        }

    const TMICVectorImp& Copy(const TMICVectorImp& v)
    	{
		TMIVectorImp::Copy(v);
		Count_ = v.Count_;
		Delta  = v.Delta;
		return *this;
    	}

protected:

    unsigned Delta;
    unsigned Count_;

};

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;
}

/**

  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)
        {
        }

};

/**

  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 );
};

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;
}

/**

  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)
        {
        }

};

#endif  // __CLASSLIB_VECTIMP_H


Generated by: nicola on gulliver.wadahome.it on Sun May 25 13:54:34 2003, using kdoc 2.0a53.