NAME
IndexSet - base class index set
INCLUDE
include "IndexSet.h"
SYNTAX
//-----------------------------------------------------------------------------
class IndexSet : public HandleId
//-----------------------------------------------------------------------------
{
public:
enum IndexSetType { BOX, GENERAL };
protected:
int* iptr; // (*iptr) is pointing to (*itIndex)(itdim)
int dummy_idx; // iptr points to dummy_idx (when not in use)
String info; // Description of this indexset
Ptv(int) max_ind; // maximum index
Ptv(int) min_ind; // minimum index
int nindices; // number of indices
int nsd; // number of dimensions
int ibase, itop ,itdim; // 'index line'
Ptv(int)* itIndex; // pointer to index
Ptv(int) dummy; // itIndex points to dummy (when not in use)
bool iteration_flag; // if true : under iteration
void setDim(int dim); // initialize with dimension 'dim'
void setItDim(int d); // set the iteration dimension
// of the current index line
int step; // the no. of steps
IndexSetType state;
virtual int getLineNumber (const Ptv(int)& pt) const =0;
virtual void computeNoIndices () =0;
virtual void specialStartIterator () =0;
virtual void specialReverseStartIterator () =0;
public :
IndexSet ();
virtual ~IndexSet ();
void startIterator (const Ptv(int)& allocation);
void startIterator (int& allocation);
void startIterator (const Ptv(int)& allocation_lattice,
int& allocation_general);
void startReverseIterator (const Ptv(int)& allocation);
void startReverseIterator (int& allocation);
void startReverseIterator (const Ptv(int)& allocation_lattice,
int& allocation_general);
// iterate :
virtual bool iterate ();
virtual bool reverseIterate ();
bool isIterating () { return iteration_flag;}
virtual void stopIterator ();
// get new index line :
virtual void specialIterate () =0;
virtual void specialReverseIterate () =0;
int base () const; // start of 'line'
int top () const; // end of 'line'
int itDim () const; // iteration dimension
int getStep () const; // stride of 'line'
int minIndex (int d) const;
int maxIndex (int d) const;
int size () const;
int dim () const;
void setDescription (const String& info_);
String getDescription () const;
virtual IndexSetType getIndexSetType () const { return state; }
Ptv(int) minIndex () const;
Ptv(int) maxIndex () const;
virtual void getIndexIterator (Handle(IndexSet)& I) const=0;
virtual void print (Os os) const =0;
virtual void scan (Is is) =0;
friend Os& operator << (Os& os, const IndexSet& X) { X.print(os);return os; }
friend Is& operator << (Is& is, IndexSet& X) { X.scan(is); return is; }
CLASS_INFO
DEF_VIRTUAL_CAST(IndexSetBox)
DEF_VIRTUAL_CAST(IndexSetBoxObstacles)
DEF_VIRTUAL_CAST(IndexSetBoxItOrder)
DEF_VIRTUAL_CAST(IndexSetUnion)
DEF_VIRTUAL_CAST(IndexSetGen)
friend class IndexSetUnion;
};
KEYWORDS
index set, iterator, base class
DESCRIPTION
The class is a base class in a hirarchy of classes defining dif
ferent index sets. The class shall be initialized from a derived
class. Please note that the allocation of the index under itera
tion is given by the argument of startIterator or startReverseIt
erator, otherwise a dummy Ptv(int) object is iterated, typically
leading to subscript errors. If DP_DEBUG is defined the error is
detected by the iterate functions.
The base class contains an index line, given by the integers
ibase, itop and itdim which derived classes shall supply to the
base class. This is accomplished by the pure virtual functions
specialIterate and specialReverseIterate, which are called by the
iterate and reverseIterate functions at the end of each line.
The Ptv(int) object under iteration, *itIndex, must also be
updated correctly by these functions. When iteration is finished
the member function stopIterator shall be called. Below we show
an example of an class derived from the class IndexSet.
CONSTRUCTORS AND INITIALIZATION
The class contains pure virtual functions and must be initialized
by a derived class.
MEMBER FUNCTIONS
startIterator(Ptv(int) index) - starts the iterator, note that
the argument is not set to the first index of the set.
startReverseIterator(Ptv(int) index) - starts the reverse itera
tor.
stopIterator - stops the iterator.
isIterating - returns true if there are more indices, otherwise
false is returned.
bool iterate() - iterates, returns true if there are more
indices.
bool reverseIterate() - iterates in reverse order, returns true
if there are more indices.
int minIndex(int d) - returns the minimum value of the index set
in the dimension d.
int maxIndex(int d) - returns the maximum value of the index set
in the dimension d.
int size() - returns the number of indices contained by the set.
int dim() - returns the number of dimensions of the indices of
the set
int base() - returns the lower index of the index line.
int top() - returns the upper index of the index line.
int itDim() - returns the dimension of the index line.
int getStep() - returns the increment of the index line.
void setDescription() - defines a verbose description of the
index set and its purpose, used for information purposes only.
String getDescription() - returns the verbose description for
this index set.
EXAMPLES
#include <IndexSet.h>
// The class implements a triangle shaped index set
// with vertics (1,1) , (1,length) and (length,1)
class IndexSetTriangle : public IndexSet
{
int length;
protected:
void specialStartIterator()
{
setItDim(1); // index line dimension
ibase = 1; // start of line
itop = length; // end of line
(*itIndex)(1) = 0; // the first call of iterate
(*itIndex)(2) = 1; // will put (*itIndex) to (1,1)
}
void specialReverseStartIterator()
{ errorFP("IndexSetTriangle::specialReverseStartItera
tor",
"not implemented !");}
public:
IndexSetTriangle(int length_)
{ setDim(2);
length = length_;
nindices = (length*(length+1))/2;
max_ind = length;
min_ind = 1;
}
void specialIterate() // supply index lines to
IndexSet
{
if (itop == 1) stopIterator(); // no more indices
else
{
itop--;
(*itIndex)(1) = ibase;
(*itIndex)(2)++;
}
}
void specialReverseIterate()
{ errorFP("IndexSetTriangle::specialReverseIterate",
"not implemented !");}
void getIndexIterator (Handle(IndexSet)& I) const
{ I.rebind(new IndexSetTriangle(length));}
};
void main()
{
IndexSetTriangle triangle(10);
Ptv(int) index(2);
triangle.startIterator(index);
while (triangle.iterate())
{
if (index(1)==1) s_o <<"0;
s_o << index << " ";
}
}
SEE ALSO
class IndexSetBox, class IndexSetBoxObstacles, class IndexSetBox
ItOrder, class ArrayGenSel(Type).
DEVELOPED BY
Numerical Objects AS
AUTHOR
Are Magnus Bruaset, Numerical Objects AS