Source: classlib/include/classlib/arrays.h


Annotated List
Files
Globals
Hierarchy
Index
/* **************************************************************************
    arrays.h  -  array template
                             -------------------
    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_ARRAYS_H )
#define __CLASSLIB_ARRAYS_H

//#define TEMPLATES

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

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

#if !defined( __CLASSLIB_SHDDEL_H )
#include "classlib/shddel.h"
#endif  // __CLASSLIB_SHDDEL_H

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

#if !defined( __CLASSLIB_VECTIMP_H )
#include "classlib/vectimp.h"
#endif  // __CLASSLIB_VECTIMP_H

/**
  [INTERNAL USE ONLY]

  template  class TArrayAsVectorImp

  Implements the type-independent array operations, using a vector
  as the underlying implementation.  The type Vect specifies the
  form of the vector, either a TCVectorImp, a TSVectorImp, a
  TICVectorImp, or a TISVectorImp.  The type T specifies the
  type of the objects to be put in the array.  When using
  TCVectorImp or a TSVectorImp T should be the same as T0. When
  using TICVectorImp or TISVectorImp T should be of type
  pointer to T0.  See TArrayAsVector and
  TIArrayAsVector for examples.
*/

template  class TArrayAsVectorImp
{

public:

    TArrayAsVectorImp( int upper, int lower, int delta ) :
        Data( upper-lower+1,delta ),
        Lowerbound( lower )
        {
        }

    int LowerBound() const
        {
        return Lowerbound;
        }

    int UpperBound() const
        {
        return BoundBase( Data.Limit() )-1;
        }

    unsigned ArraySize() const
        {
        return Data.Limit();
        }

    int IsFull() const
        {
        return Data.GetDelta() == 0 && Data.Count() >= Data.Limit();
        }

    int IsEmpty() const
        {
        return Data.Count() == 0;
        }

    unsigned GetItemsInContainer() const
        {
        return Data.Count();
        }

    void Reallocate( unsigned sz, unsigned offset = 0 )
        {
        Data.Resize( sz, offset );
        }

    void SetData( int loc, const T& t )
        {
        PRECONDITION( loc >= Lowerbound && loc <= UpperBound() );
        Data[ ZeroBase(loc) ] = t;
        }

    void RemoveEntry( int loc )
        {
        SqueezeEntry( ZeroBase(loc) );
        }

    void SqueezeEntry( unsigned loc )
        {
        PRECONDITION( loc < Data.Count() );
        Data.Detach( loc );
        }

    unsigned ZeroBase( int loc ) const
        {
        return loc - Lowerbound;
        }

    int BoundBase( unsigned loc ) const
        {
        return loc == UINT_MAX ? INT_MAX : loc + Lowerbound;
        }

    void Grow( int loc )
        {
        if( loc < LowerBound() )
            Reallocate( ArraySize() + (loc - Lowerbound) );
        else if( loc >= BoundBase( Data.Limit()) )
            Reallocate( ZeroBase(loc) );
        }

	/**
		copy elements from another array
	*/
    const TArrayAsVectorImp& Copy(TArrayAsVectorImp& v)
    	{
    	Data.Copy(v.Data);
    	return *this;
    	}

    Vect Data;
    int Lowerbound;
};

/**
  [INTERNAL USE ONLY]

  template  class TDArrayAsVectorImp

  Implements the fundamental array operations for direct arrays, using
  a vector as the underlying implementation.
*/

template  class TDArrayAsVectorImp :
    public TArrayAsVectorImp
{

public:

    typedef void (*IterFunc)(T&, void *);
    typedef int  (*CondFunc)(const T&, void *);

    TDArrayAsVectorImp( int upper, int lower, int delta ) :
        TArrayAsVectorImp( upper, lower, delta )
        {
        }

    int Add( const T& t )
        {
        return Data.Add(t);
        }

	/**	remove an element from array
	*/
    int Detach( const T& t )
        {
        return Data.Detach(t);
        }

	/**	remove the element at index
	*/
    int Detach( int loc )
        {
        return Data.Detach( ZeroBase(loc) );
        }

	/**	remove the element from array
	*/
    int Destroy( const T& t )
        {
        return Detach(t);
        }

	/** remove the element from array at specifiet index
	*/
    int Destroy( int loc )
        {
        return Detach(loc);
        }

	/** true if the element is in the array
	*/
    int HasMember( const T& t ) const
        {
        return Data.Find(t) != UINT_MAX;
        }

	/** find an element in the array; return the index or UINT_MAX if not found
	*/
    int Find( const T& t ) const
        {
        return BoundBase( Data.Find( t ) );
        }

	/** convenience operator for extract elements from array
	*/
    T& operator []( int loc )
        {
        Grow( loc+1 );
        return Data[ZeroBase(loc)];
        }

	/** convenience operator for extract elements from array
	*/
    T& operator []( int loc ) const
        {
        PRECONDITION( loc >= Lowerbound && ZeroBase(loc) < Data.Limit() );		
        return Data[ZeroBase(loc)];
        }

	/** auto iterator: use an iterator function calling it for every element of the array;
		the function must have a prototype like this: void iterFunc(T&, void* args); */
    void ForEach( IterFunc iter, void *args )
        {
        if( !IsEmpty() )
            Data.ForEach( iter, args );
        }

	/** auto iterator: use a test function calling it for every element of the array;
	    the function must have a prototype like this: int testFunc(const T&, void* args);
		the function must return 0 if the test fail, != 0 if test is ok; the first element
		tested successful will be returned or NULL if no elements is ok */
    T *FirstThat( CondFunc cond, void *args ) const
        {
        if( IsEmpty() )
            return 0;
        return Data.FirstThat( cond, args );
        }

	/** auto iterator: use a test function calling it for every element of the array;
	    the function must have a prototype like this: int testFunc(const T&, void* args);
		the function must return 0 if the test fail, != 0 if test is ok; the last element
		tested successful will be returned or NULL if no elements is ok */
    T *LastThat( CondFunc cond, void *args ) const
        {
        if( IsEmpty() )
            return 0;
        return Data.LastThat( cond, args );
        }

	/** discarge every element in the array ad reinit to initial state */
    void Flush()
        {
        Data.Flush();
        }

protected:

    const T& ItemAt( int i ) const
        {
        return Data[ ZeroBase(i) ];
        }

};

/**
  [INTERNAL USE ONLY]

  template  class TIArrayAsVectorImp

  Implements the fundamental array operations for indirect arrays,
  using a vector as the underlying implementation.

  The array is by default autodelete: the destructor
  destroy every element in the array trough the delete operator.
  The default behavory can be changed using the OwnsElements(int)
  member function inherit form TShouldDelete.

*/

template  class TIArrayAsVectorImp :
    public TArrayAsVectorImp, public TShouldDelete
{

public:

    typedef void (*IterFunc)(T&, void *);
    typedef int  (*CondFunc)(const T&, void *);

    TIArrayAsVectorImp( int upper, int lower, int delta ) :
        TArrayAsVectorImp( upper, lower, delta )
        {
        }

    ~TIArrayAsVectorImp()
		{
			Flush();
		}

    int Add( T *t )
        {
        return Data.Add(t);
        }

	/** remove an element from array without destroing it if need
	*/
    int Detach( T *t,
                TShouldDelete::DeleteType dt = TShouldDelete::NoDelete )
        {
        return Data.Detach(t,DelObj(dt));
        }

	/** remove an element from array without destroing it if need
	*/
    int Detach( int loc,
                TShouldDelete::DeleteType dt = TShouldDelete::NoDelete )
        {
        return Data.Detach( ZeroBase(loc), DelObj(dt) );
        }

	/** remove an element from array; the element will be destroyed is the ownership is on
	*/
    int Destroy( T *t )
        {
        return Detach(t,TShouldDelete::Delete);
        }

	/** remove an element from array; the element will be destroyed is the ownership is on
	*/
    int Destroy( int loc )
        {
        return Detach(loc,TShouldDelete::Delete);
        }

	/** true if the element is in the array
	*/
    int HasMember( const T *t ) const
        {
        return Data.Find(t) != UINT_MAX;
        }

	/** find an element in the array; return the index or UINT_MAX if not found
	*/
    int Find( const T *t ) const
        {
        return BoundBase( Data.Find( t ) );
        }

	/** convenience operator for extract elements from array
	*/
    T *& operator []( int loc )
        {
        Grow( loc+1 );
        return Data[ZeroBase(loc)];
        }

	/** convenience operator for extract elements from array
	*/
    T *& operator []( int loc ) const
        {
        PRECONDITION( loc >= Lowerbound && ZeroBase(loc) < Data.Limit() );		
        return Data[ZeroBase(loc)];
        }

	/** extract elements from array
	*/
    T *& Get( int loc ) const
        {
        PRECONDITION( loc >= Lowerbound && ZeroBase(loc) < Data.Limit() );		
        return Data[ZeroBase(loc)];
        }

	/** auto iterator: use an iterator function calling it for every element of the array;
		the function must have a prototype like this: void iterFunc(T&, void* args); */
    void ForEach( IterFunc iter, void *args )
        {
        if( !IsEmpty() )
            Data.ForEach( iter, args );
        }

	/** auto iterator: use a test function calling it for every element of the array;
	    the function must have a prototype like this: int testFunc(const T&, void* args);
		the function must return 0 if the test fail, != 0 if test is ok; the first element
		that testing successful will be returned or NULL if no elements is ok */
    T *FirstThat( CondFunc cond, void *args ) const
        {
        if( IsEmpty() )
            return 0;
        return Data.FirstThat( cond, args );
        }

	/** auto iterator: use a test function calling it for every element of the array;
	    the function must have a prototype like this: int testFunc(const T&, void* args);
		the function must return 0 if the test fail, != 0 if test is ok; the last element
		that testing successful will be returned or NULL if no elements is ok */
    T *LastThat( CondFunc cond, void *args ) const
        {
        if( IsEmpty() )
            return 0;
        return Data.LastThat( cond, args );
        }

	/** empty the array; if the ownership is on also the elements in the array will be destroyed
	*/
    void Flush( DeleteType dt = DefDelete )
        {
        Data.Flush(DelObj(dt));
        }

protected:

    T *& ItemAt( int i ) const
        {
        return Data[ ZeroBase(i) ];
        }

};

template  class TMArrayAsVectorIterator;

/**
  template  class TMArrayAsVector
  template  class TMArrayAsVectorIterator

  Implements a managed array of objects of type T, using a vector as
  the underlying implementation.
*/

template  class TMArrayAsVector :
    public TDArrayAsVectorImp,T>
{

    friend class TMArrayAsVectorIterator;

public:

    TMArrayAsVector( int upper, int lower = 0, int delta = 0 ) :
        TDArrayAsVectorImp,T>( upper, lower, delta )
        {
        }

	/** Add an element at specified position in the array
	*/
    int AddAt( const T& t, int loc )
        {
        return Data.AddAt( t, ZeroBase(loc) );
        }

	/** Copy elements from another array
	*/
	const TMArrayAsVector& Copy(const TMArrayAsVector& v)
		{
		Data.Copy(v.Data);
		return *this;
		}
};

/**
  template  class TMArrayAsVector
  template  class TMArrayAsVectorIterator

  Implements a managed array of objects of type T, using a vector as
  the underlying implementation.
*/

template  class TMArrayAsVectorIterator :
    public TMCVectorIteratorImp
{

public:

    TMArrayAsVectorIterator( const TMArrayAsVector& a ) :
        TMCVectorIteratorImp( a.Data ) {}

};

/**
  template  class TArrayAsVector
  template  class TArrayAsVectorIterator

  Implements an array of objects of type T, using a vector as the
  underlying implementation and TStandardAllocator as its memory
  manager.
*/

template  class TArrayAsVector :
    public TMArrayAsVector
{

public:

    TArrayAsVector( int upper, int lower = 0, int delta = 0 ) :
        TMArrayAsVector( upper, lower, delta )
        {
        }

};

/**
  template  class TArrayAsVector
  template  class TArrayAsVectorIterator

  Implements an array of objects of type T, using a vector as the
  underlying implementation.
*/

template  class TArrayAsVectorIterator :
    public TMArrayAsVectorIterator
{

public:

    TArrayAsVectorIterator( const TArrayAsVector& a ) :
        TMArrayAsVectorIterator(a)
        {
        }

};

/**
  template  class TMSArrayAsVector
  template  class TMSArrayAsVectorIterator

  Implements a managed, sorted array of objects of type T, using a
  vector as the underlying implementation.
*/

template  class TMSArrayAsVectorIterator;

template  class TMSArrayAsVector :
    public TDArrayAsVectorImp,T>
{

    friend class TMSArrayAsVectorIterator;

public:

    TMSArrayAsVector( int upper, int lower = 0, int delta = 0 ) :
        TDArrayAsVectorImp,T>( upper, lower, delta )
        {
        }

};

template  class TMSArrayAsVectorIterator :
    public TMSVectorIteratorImp
{

public:

    TMSArrayAsVectorIterator( const TMSArrayAsVector& a ) :
        TMSVectorIteratorImp( a.Data ) {}

};

/**
  template  class TSArrayAsVector
  template  class TSArrayAsVectorIterator

  Implements a sorted array of objects of type T, using a vector as
  the underlying implementation.
*/

template  class TSArrayAsVector :
    public TMSArrayAsVector
{

public:

    TSArrayAsVector( int upper, int lower = 0, int delta = 0 ) :
        TMSArrayAsVector( upper, lower, delta )
        {
        }

};

template  class TSArrayAsVectorIterator :
    public TMSArrayAsVectorIterator
{

public:

    TSArrayAsVectorIterator( const TSArrayAsVector& a ) :
        TMSArrayAsVectorIterator( a ) {}

};

template  class TMIArrayAsVectorIterator;

/**
  template  class TMIArrayAsVector
  template  class TMIArrayAsVectorIterator

  Implements a managed indirect array of objects of type T, using a
  vector as the underlying implementation.
*/

template  class TMIArrayAsVector :
    public TIArrayAsVectorImp,T>
{

    friend class TMIArrayAsVectorIterator;

public:

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

    TMIArrayAsVector( int upper, int lower = 0, int delta = 0 ) :
        TIArrayAsVectorImp,T>( upper, lower, delta )
        {
        }

	/** add an element at a specified position in the array
	*/
    int AddAt( T *t, int loc )
        {
        return Data.AddAt( t, ZeroBase(loc) );
        }

	/** copy elements form another array; before copy elements
		this array is flushed; the object in the array are dostroyed
		if the ownership is on
	*/
	const TMIArrayAsVector& Copy(const TMIArrayAsVector& v)
		{
		Flush();
		Data.Copy(v.Data);
		return *this;
		}

	/** sort the array; the sort function must be compare two elements
		and return a value appropriate for sorting; the quick sort alogritm is used; the prototype
		of the sorting funcion must be int cmpTwoEle(const T *t1, const T *t2);
	*/
	void Sort( SortFunc funcCompare )
		{
		Data.Sort(funcCompare);
		}
};

/**
  template  class TMIArrayAsVector
  template  class TMIArrayAsVectorIterator

  Implements a managed indirect array of objects of type T, using a
  vector as the underlying implementation.
*/

template  class TMIArrayAsVectorIterator :
    public TMICVectorIteratorImp
{

public:

    TMIArrayAsVectorIterator( const TMIArrayAsVector& a ) :
        TMICVectorIteratorImp( a.Data ) {}

};

/**
  template  class TIArrayAsVector
  template  class TIArrayAsVectorIterator

  Implements an indirect array of objects of type T, using a vector as
  the underlying implementation.
*/

template  class TIArrayAsVector :
    public TMIArrayAsVector
{

public:

    TIArrayAsVector( int upper, int lower = 0, int delta = 0 ) :
        TMIArrayAsVector( upper, lower, delta )
        {
        }

};

/**
  template  class TIArrayAsVector
  template  class TIArrayAsVectorIterator

  Implements an indirect array of objects of type T, using a vector as
  the underlying implementation.
*/

template  class TIArrayAsVectorIterator :
    public TMIArrayAsVectorIterator
{

public:

    TIArrayAsVectorIterator( const TIArrayAsVector& a ) :
        TMIArrayAsVectorIterator(a)
        {
        }

};

template  class TMISArrayAsVectorIterator;

/**
  template  class TMISArrayAsVector
  template  class TMISArrayAsVectorIterator

  Implements a managed, indirect sorted array of objects of type T,
  using a vector as the underlying implementation.
*/

template  class TMISArrayAsVector :
    public TIArrayAsVectorImp,T>
{

    friend class TMISArrayAsVectorIterator;

public:

    TMISArrayAsVector( int upper, int lower = 0, int delta = 0 ) :
        TIArrayAsVectorImp,T>( upper, lower, delta )
        {
        }

};

template  class TMISArrayAsVectorIterator :
    public TMISVectorIteratorImp
{

public:

    TMISArrayAsVectorIterator( const TMISArrayAsVector& a ) :
        TMISVectorIteratorImp( a.Data ) {}

};

/**
  template  class TISArrayAsVector
  template  class TISArrayAsVectorIterator

  Implements an indirect sorted array of objects of type T, using a
  vector as the underlying implementation.
*/

template  class TISArrayAsVector :
    public TMISArrayAsVector
{

public:

    TISArrayAsVector( int upper, int lower = 0, int delta = 0 ) :
        TMISArrayAsVector( upper, lower, delta )
        {
        }

};

/**
  template  class TISArrayAsVector
  template  class TISArrayAsVectorIterator

  Implements an indirect sorted array of objects of type T, using a
  vector as the underlying implementation.
*/

template  class TISArrayAsVectorIterator :
    public TMISArrayAsVectorIterator
{

public:

    TISArrayAsVectorIterator( const TISArrayAsVector& a ) :
        TMISArrayAsVectorIterator(a)
        {
        }

};

/**
  template  class TArray
  template  class TArrayIterator

  Easy names for TArrayAsVector and TArrayAsVectorIterator
*/

template  class TArray :
    public TArrayAsVector
{

public:

    TArray( int upper, int lower = 0, int delta = 0 ) :
        TArrayAsVector( upper, lower, delta )
        {
        }

};

/**
  template  class TArray
  template  class TArrayIterator

  Easy names for TArrayAsVector and TArrayAsVectorIterator
*/

template  class TArrayIterator :
    public TArrayAsVectorIterator
{

public:


    TArrayIterator( const TArray& a ) :
        TArrayAsVectorIterator(a)
        {
        }

};

/**
  template  class TSArray
  template  class TSArrayIterator

  Easy names for TSArrayAsVector and TSArrayAsVectorIterator
*/

template  class TSArray :
    public TSArrayAsVector
{

public:

    TSArray( int upper, int lower = 0, int delta = 0 ) :
        TSArrayAsVector( upper, lower, delta )
        {
        }

};

/**
  template  class TSArray
  template  class TSArrayIterator

  Easy names for TSArrayAsVector and TSArrayAsVectorIterator
*/

template  class TSArrayIterator :
    public TSArrayAsVectorIterator
{

public:


    TSArrayIterator( const TSArray& a ) :
        TSArrayAsVectorIterator(a)
        {
        }

};


////////////////////////////////////////////////////////////////////////////////

/**
  template  class TIArray
  template  class TIArrayIterator

  Easy names for TIArrayAsVector and TIArrayAsVectorIterator
*/

template  class TIArray :
    public TIArrayAsVector
{

public:

    TIArray( int upper, int lower = 0, int delta = 0 ) :
        TIArrayAsVector( upper, lower, delta )
        {
        }

};

/**
  template  class TIArray
  template  class TIArrayIterator

  Easy names for TIArrayAsVector and TIArrayAsVectorIterator
*/

template  class TIArrayIterator :
    public TIArrayAsVectorIterator
{

public:


    TIArrayIterator( const TIArray& a ) :
        TIArrayAsVectorIterator(a)
        {
        }

};

/**
  template  class TISArray
  template  class TISArrayIterator

  Easy names for TISArrayAsVector and TISArrayAsVectorIterator
*/

template  class TISArray :
    public TISArrayAsVector
{

public:

    TISArray( int upper, int lower = 0, int delta = 0 ) :
        TISArrayAsVector( upper, lower, delta )
        {
        }

};

/**
  template  class TISArray
  template  class TISArrayIterator

  Easy names for TISArrayAsVector and TISArrayAsVectorIterator
*/

template  class TISArrayIterator :
    public TISArrayAsVectorIterator
{

public:


    TISArrayIterator( const TISArray& a ) :
        TISArrayAsVectorIterator(a)
        {
        }

};

#endif  // __CLASSLIB_ARRAYS_H


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