Source: classlib/include/classlib/bags.h
|
|
|
|
/* *************************************************************************
bags.h - contenitori
-------------------
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_BAGS_H )
#define __CLASSLIB_BAGS_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_SHDDEL_H )
#include "classlib/shddel.h"
#endif // __CLASSLIB_SHDDEL_H
#if !defined( __CLASSLIB_VECTIMP_H )
#include "classlib/vectimp.h"
#endif // __CLASSLIB_VECTIMP_H
/**
[INTERNAL USE ONLY]
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();
}
protected:
Vect Data;
};
/**
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 );
}
/** return true if the item is in the bag
*/
int HasMember( const T& t ) const
{
return Data.Find(t) != UINT_MAX;
}
/** find an element in the bag; return the address if found or NULL
*/
const T *Find( const T& t ) const
{
unsigned loc = Data.Find(t);
return ( loc == UINT_MAX ? 0 : &Data[loc] );
}
/** auto iterator: use an iterator function calling it for every element of the bag;
the function must have a prototype like this: void iterFunc(T&, void* args); */
void ForEach( IterFunc iter, void *args )
{
Data.ForEach( iter, args, 0, Data.Top() );
}
/** auto iterator: use a test function calling it for every element of the bag;
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
{
return Data.FirstThat( cond, args, 0, Data.Top() );
}
/** auto iterator: use a test function calling it for every element of the bag;
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
{
return Data.LastThat( cond, args, 0, Data.Top() );
}
/** Discarge every element in the bag
*/
void Flush()
{
Data.Flush();
}
};
/**
template class TMBagAsVector
template class TMBagAsVectorIterator
Implements a managed bag of objects of type T, using a vector as
the underlying implementation.
This is the iterator.
*/
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 ++;
};
/**
template class TBagAsVector
template class TBagAsVectorIterator
Implements a bag of objects of type T, using a vector as the
underlying implementation.
*/
template class TBagAsVector :
public TMBagAsVector
{
public:
TBagAsVector( unsigned sz = DEFAULT_BAG_SIZE ) :
TMBagAsVector( sz )
{
}
};
/**
template class TBagAsVector
template class TBagAsVectorIterator
Implements a bag of objects of type T, using a vector as the
underlying implementation.
*/
template class TBagAsVectorIterator :
public TMBagAsVectorIterator
{
public:
TBagAsVectorIterator( const TBagAsVector& b ) :
TMBagAsVectorIterator(b)
{
}
};
/**
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 class 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;
}
/** empty the bag; if the ownership is on also the elements in the bag will be destroyed
*/
void Flush( TShouldDelete::DeleteType dt = TShouldDelete::DefDelete )
{
Data.Flush( DelObj(dt), Data.Top(), 0 );
}
/** find an element in the bag; return the address if found or NULL
*/
T *Find( T *t ) const
{
unsigned loc = Data.Find(t);
return ( loc == UINT_MAX ? 0 : STATIC_CAST(T *,Data[loc]) );
}
/** auto iterator: use an iterator function calling it for every element of the bag;
the function must have a prototype like this: void iterFunc(T&, void* args); */
void ForEach( IterFunc iter, void *args )
{
Data.ForEach( iter, args, 0, Data.Top() );
}
/** auto iterator: use a test function calling it for every element of the bag;
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
{
return Data.FirstThat( cond, args, 0, Data.Top() );
}
/** auto iterator: use a test function calling it for every element of the bag;
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
{
return Data.LastThat( cond, args, 0, Data.Top() );
}
};
template class TMIBagAsVectorIterator :
public TMICVectorIteratorImp
{
public:
TMIBagAsVectorIterator( const TMIBagAsVector& s ) :
TMICVectorIteratorImp(s.Data,0,s.Data.Top()) {}
};
/**
template class TIBagAsVector
template class TIBagAsVectorIterator
Implements a bag of pointers to objects of type T, using a vector as
the underlying implementation.
*/
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 ++;
};
/**
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 TBag
template class TBagIterator
Easy names for TBagAsVector and TBagAsVectorIterator.
*/
template class TBagIterator :
public TBagAsVectorIterator
{
public:
TBagIterator( const TBag& a ) :
TBagAsVectorIterator(a)
{
}
};
/**
template class TIBag
template class TIBagIterator
Easy names for TIBagAsVector and TIBagAsVectorIterator.
*/
template class TIBag : public TIBagAsVector
{
public:
TIBag( unsigned sz = DEFAULT_BAG_SIZE ) :
TIBagAsVector( sz )
{
}
};
/**
template class TIBag
template class TIBagIterator
Easy names for TIBagAsVector and TIBagAsVectorIterator.
*/
template class TIBagIterator :
public TIBagAsVectorIterator
{
public:
TIBagIterator( const TBag& a ) :
TIBagAsVectorIterator(a)
{
}
};
#endif // __CLASSLIB_BAGS_H
Generated by: nicola on gulliver.wadahome.it on Sun May 25 13:54:34 2003, using kdoc 2.0a53. |