Source: classlib/include/classlib/staticarray.h


Annotated List
Files
Globals
Hierarchy
Index
/***************************************************************************
    staticarrays.h  -  arrays statici di oggetti semplici (tipi fondamentali o strutture)
                             -------------------
    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_STATICARRAY_H )
#define __CLASSLIB_STATICARRAY_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

/**

  template  class TStaticArray			

  Implements a complete static array of the supplied dimension.
  It can be used to minimize the heap fragmentation.

  Assumes that T has meaningful copy semantics and an appropriate == operator.

  For a more exaustive description of the metods see TArray.
*/

template  class TStaticArray
{
public:
    typedef void (*IterFunc)(T&, void *);
    typedef int  (*CondFunc)(const T&, void *);

    TStaticArray()
        {
				Count = 0;
				Limit = Size;
        }

    int LowerBound() const
        {
        return 0;
        }

    int UpperBound() const
        {
        return Limit;
        }

    unsigned ArraySize() const
        {
        return Limit;
        }

    int IsFull() const
        {
        return Count >= Limit;
        }

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

    unsigned GetItemsInContainer() const
        {
        return Count;
        }

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

    void RemoveEntry( int loc )
        {
		PRECONDITION( loc >=0 && loc < Count );
        SqueezeEntry( loc );
        }

    int Add( const T& t )
        {
			PRECONDITION(Count < Limit);
			Data[Count++] = t;
			return Count;
        }

    int AddAt( const T& t, int loc )
        {
			PRECONDITION(loc >= 0 && loc < Count && Count < Limit);
			for(int i=Count ; i>loc ; i--)
				Data[i] = Data[i-1];
			Data[loc] = t;
			return ++Count;
        }

    int Destroy( const T& t )
        {
			int loc = Find(t);
			if(loc != INT_MAX) {
				SqueezeEntry(loc);
			}
			return -1;
        }

    void Destroy( int loc )
        {
        RemoveEntry(loc);
        }

    int HasMember( const T& t ) const
        {
        return Find(t) != INT_MAX;
        }

    int Find( const T& t ) const;

    T& operator []( int loc )
        {
        PRECONDITION( loc >= 0 && loc < Limit );		
        return Data[loc];
        }

    const T& operator []( int loc ) const
        {
        PRECONDITION( loc >= 0 && loc < Limit );		
        return Data[loc];
        }

    void ForEach( IterFunc iter, void *args, unsigned start=0, unsigned stop=0  );
    T *FirstThat( CondFunc cond, void *args, unsigned start=0, unsigned stop=0 ) const;
    T *LastThat( CondFunc cond, void *args, unsigned start=0, unsigned stop=0 ) const;

    void Flush()
        {
        Count = 0;
        }

	/** Set the item counter to the specified value.
	*/
    void Crop(int size)
        {
		PRECONDITION(size>=0 && size < Limit);
        Count = size;
        }

    T& ItemAt( int loc )
        {
        PRECONDITION( loc >= 0 && loc < Limit );		
        return Data[ loc ];
        }

    const T& ItemAt( int loc ) const
        {
        PRECONDITION( loc >= 0 && loc < Limit );		
        return Data[ loc ];
        }

	/** Copy the contents of another array of the same type.
	*/
	void CopyFrom(const TStaticArray& s);

	/** Return the base address of the array data.
	*/
	T* AddressBase() {return &Data[0];}

	/** Return the dimension of an item inserted into the array.
	*/
	int SizeItem() const {return sizeof(Data[0]);}

protected:
    void SqueezeEntry( unsigned loc );

protected:
    int Limit, Count;
    T Data[Size];
};

template 
void TStaticArray::SqueezeEntry( unsigned loc )
{
	if( loc >=0 && loc < Count )
	{
		if(loc < Count-1) {
			for(int i=loc ; i
int TStaticArray::Find( const T& t ) const
{
    for( unsigned loc = 0; loc < Count; loc++ )
        if( Data[loc] == t )
            return loc;
    return INT_MAX;

}

template 
void TStaticArray::ForEach( IterFunc iter, void *args,
                                    unsigned start, unsigned stop )
{
	if(!stop)	stop = Count;
    for( unsigned cur = start; cur < stop; cur++ )
        iter( Data[cur], args );
}

template 
T *TStaticArray::FirstThat( CondFunc cond, void *args,
                                    unsigned start, unsigned stop ) const
{
	if(!stop)	stop = Count;
    for( unsigned cur = start; cur < stop; cur++ )
        if( cond( Data[cur], args ) != 0 )
            return &((T&)Data[cur]);
    return 0;
}

template 
T *TStaticArray::LastThat( CondFunc cond, void *args,
                                   unsigned start, unsigned stop ) const
{
    T *res = 0;
	if(!stop)	stop = Count;
    for( unsigned cur = start; cur < stop; cur++ )
        if( cond( Data[cur], args ) != 0 )
            res = &((T&)Data[cur]);
    return res;
}

template 
void TStaticArray::CopyFrom(const TStaticArray& s)
{
	PRECOND(Limit == s.Limit);
	Flush();
	Count = s.Count;
	for(int i=0 ; i class TStaticArrayFast

  Implements a complete static array of the supplied dimension.
  It can be used to minimize the heap fragmentation.

  IMPORTANT NOTE: THIS ARRAY MUST BE USED ONLY WITH STRUCTURE
  OR DATA THAT HAVE NOT COSTRUCTOR OR DESTRUCTOR.

  Implementa un array completamente statico delle dimensioni indicate.
  Da utilizzarsi per diminuire la frammentazione dell'heap.

  Assumes that T has meaningful copy semantics and an appropriate == operator.

  For a more exaustive description of the metods see TArray.
*/

template  class TStaticArrayFast
{
public:
    typedef void (*IterFunc)(T&, void *);
    typedef int  (*CondFunc)(const T&, void *);

    TStaticArrayFast() 
        {
			Count = 0;
			Limit = Size;
        }

    int LowerBound() const
        {
        return 0;
        }

    int UpperBound() const
        {
        return Limit;
        }

    unsigned ArraySize() const
        {
        return Limit;
        }

    int IsFull() const
        {
        return Count >= Limit;
        }

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

    unsigned GetItemsInContainer() const
        {
        return Count;
        }

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

    void RemoveEntry( int loc )
        {
		PRECONDITION( loc >=0 && loc < Count );
        SqueezeEntry( loc );
        }

    int Add( const T& t )
        {
			PRECONDITION(Count < Limit);
			Data[Count++] = t;
			return Count;
        }

    int AddAt( const T& t, int loc )
        {
			PRECONDITION(loc >= 0 && loc < Count && Count < Limit);
			memmove(&Data[loc+1], &Data[loc], (Count - loc) * sizeof(Data[0]));
			Data[loc] = t;
			return ++Count;
        }

    int Destroy( const T& t )
        {
			int loc = Find(t);
			if(loc != INT_MAX) {
				SqueezeEntry(loc);
			}
			return -1;
        }

    void Destroy( int loc )
        {
        RemoveEntry(loc);
        }

    int HasMember( const T& t ) const
        {
        return Find(t) != INT_MAX;
        }

    int Find( const T& t ) const;

    T& operator []( int loc )
        {
        PRECONDITION( loc >= 0 && loc < Limit );		
        return Data[loc];
        }

    const T& operator []( int loc ) const
        {
        PRECONDITION( loc >= 0 && loc < Limit );		
        return Data[loc];
        }

    void ForEach( IterFunc iter, void *args, unsigned start=0, unsigned stop=0  );
    T *FirstThat( CondFunc cond, void *args, unsigned start=0, unsigned stop=0 ) const;
    T *LastThat( CondFunc cond, void *args, unsigned start=0, unsigned stop=0 ) const;

    void Flush()
        {
        Count = 0;
        }

    void Crop(int size)
        {
		PRECONDITION(size>=0 && size < Limit);
        Count = size;
        }

    T& ItemAt( int loc )
        {
        PRECONDITION( loc >= 0 && loc < Limit );		
        return Data[ loc ];
        }

    const T& ItemAt( int loc ) const
        {
        PRECONDITION( loc >= 0 && loc < Limit );		
        return Data[ loc ];
        }

	void CopyFrom(const TStaticArrayFast& s)
		{
		PRECONDITION(Limit == s.Limit);
		Count = s.Count;
		memcpy(&Data[0], &s.Data[0], Count * sizeof(Data[0]));
		}

	T* AddressBase() {return &Data[0];}

	int SizeItem() const {return sizeof(Data[0]);}

protected:
    void SqueezeEntry( unsigned loc )
		{
			if( loc >=0 && loc < Count )
			{
				if(loc < Count-1) {
					memmove(&Data[loc], &Data[loc+1], (Count-loc-1)*sizeof(Data[0]));
				}
				Count--;
			}
		}

protected:
    int Limit, Count;
    T Data[Size];
};

template  
int TStaticArrayFast::Find( const T& t ) const
{
    for( unsigned loc = 0; loc < Count; loc++ )
        if( Data[loc] == t )
            return loc;
    return INT_MAX;
}

template  
void TStaticArrayFast::ForEach( IterFunc iter, void *args,
                                    unsigned start, unsigned stop )
{
	if(!stop)	stop = Count;
    for( unsigned cur = start; cur < stop; cur++ )
        iter( Data[cur], args );
}

template  
T *TStaticArrayFast::FirstThat( CondFunc cond, void *args,
                                    unsigned start, unsigned stop ) const
{
	if(!stop)	stop = Count;
    for( unsigned cur = start; cur < stop; cur++ )
        if( cond( Data[cur], args ) != 0 )
            return &((T&)Data[cur]);
    return 0;
}

template  
T *TStaticArrayFast::LastThat( CondFunc cond, void *args,
                                   unsigned start, unsigned stop ) const
{
    T *res = 0;
	if(!stop)	stop = Count;
    for( unsigned cur = start; cur < stop; cur++ )
        if( cond( Data[cur], args ) != 0 )
            res = &((T&)Data[cur]);
    return res;
}

#endif // __CLASSLIB_STATICARRAY_H


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