indirect arrays

This commit is contained in:
Joachim Schoeberl 2013-04-02 20:26:21 +00:00
parent 15b7f3d119
commit 8d7debaf9b
3 changed files with 94 additions and 56 deletions

View File

@ -12,6 +12,7 @@ namespace netgen
{ {
// template <class T, int B1, int B2> class IndirectArray; // template <class T, int B1, int B2> class IndirectArray;
template <class TA1, class TA2> class IndirectArray;
@ -23,7 +24,7 @@ namespace netgen
Optional range check by macro RANGE_CHECK Optional range check by macro RANGE_CHECK
*/ */
template <class T, int BASE = 0> template <typename T, int BASE = 0, typename TIND = int>
class FlatArray class FlatArray
{ {
protected: protected:
@ -32,6 +33,7 @@ namespace netgen
/// the data /// the data
T * data; T * data;
public: public:
typedef T TELEM;
/// provide size and memory /// provide size and memory
FlatArray (int asize, T * adata) FlatArray (int asize, T * adata)
@ -40,11 +42,11 @@ namespace netgen
/// the size /// the size
int Size() const { return size; } int Size() const { return size; }
int Begin() const { return BASE; } TIND Begin() const { return TIND(BASE); }
int End() const { return size+BASE; } TIND End() const { return TIND(size+BASE); }
/// Access array. BASE-based /// Access array. BASE-based
T & operator[] (int i) const T & operator[] (TIND i) const
{ {
#ifdef DEBUG #ifdef DEBUG
if (i-BASE < 0 || i-BASE >= size) if (i-BASE < 0 || i-BASE >= size)
@ -54,6 +56,14 @@ namespace netgen
return data[i-BASE]; return data[i-BASE];
} }
template <typename T2, int B2>
IndirectArray<FlatArray, FlatArray<T2,B2> > operator[] (const FlatArray<T2,B2> & ia) const
{
return IndirectArray<FlatArray, FlatArray<T2,B2> > (*this, ia);
}
/// Access array, one-based (old fashioned) /// Access array, one-based (old fashioned)
T & Elem (int i) T & Elem (int i)
{ {
@ -113,16 +123,16 @@ namespace netgen
} }
/// takes range starting from position start of end-start elements /// takes range starting from position start of end-start elements
const FlatArray<T> Range (int start, int end) const FlatArray<T> Range (TIND start, TIND end)
{ {
return FlatArray<T> (end-start, data+start); return FlatArray<T> (end-start, data+start);
} }
/// first position of element elem, returns -1 if element not contained in array /// first position of element elem, returns -1 if element not contained in array
int Pos(const T & elem) const TIND Pos(const T & elem) const
{ {
int pos = -1; TIND pos = -1;
for(int i=0; pos==-1 && i < this->size; i++) for(TIND i=0; pos==-1 && i < this->size; i++)
if(elem == data[i]) pos = i; if(elem == data[i]) pos = i;
return pos; return pos;
} }
@ -137,10 +147,10 @@ namespace netgen
// print array // print array
template <class T, int BASE> template <typename T, int BASE, typename TIND>
inline ostream & operator<< (ostream & s, const FlatArray<T,BASE> & a) inline ostream & operator<< (ostream & s, const FlatArray<T,BASE,TIND> & a)
{ {
for (int i = a.Begin(); i < a.End(); i++) for (TIND i = a.Begin(); i < a.End(); i++)
s << i << ": " << a[i] << endl; s << i << ": " << a[i] << endl;
return s; return s;
} }
@ -155,12 +165,12 @@ namespace netgen
Either the container takes care of memory allocation and deallocation, Either the container takes care of memory allocation and deallocation,
or the user provides one block of data. or the user provides one block of data.
*/ */
template <class T, int BASE = 0> template <class T, int BASE = 0, typename TIND = int>
class Array : public FlatArray<T, BASE> class Array : public FlatArray<T, BASE, TIND>
{ {
protected: protected:
using FlatArray<T,BASE>::size; using FlatArray<T,BASE,TIND>::size;
using FlatArray<T,BASE>::data; using FlatArray<T,BASE,TIND>::data;
/// physical size of array /// physical size of array
int allocsize; int allocsize;
@ -171,34 +181,34 @@ namespace netgen
/// Generate array of logical and physical size asize /// Generate array of logical and physical size asize
explicit Array() explicit Array()
: FlatArray<T, BASE> (0, NULL) : FlatArray<T, BASE, TIND> (0, NULL)
{ {
allocsize = 0; allocsize = 0;
ownmem = 1; ownmem = 1;
} }
explicit Array(int asize) explicit Array(int asize)
: FlatArray<T, BASE> (asize, new T[asize]) : FlatArray<T, BASE, TIND> (asize, new T[asize])
{ {
allocsize = asize; allocsize = asize;
ownmem = 1; ownmem = 1;
} }
/// Generate array in user data /// Generate array in user data
Array(int asize, T* adata) Array(TIND asize, T* adata)
: FlatArray<T, BASE> (asize, adata) : FlatArray<T, BASE, TIND> (asize, adata)
{ {
allocsize = asize; allocsize = asize;
ownmem = 0; ownmem = 0;
} }
/// array copy /// array copy
explicit Array (const Array<T> & a2) explicit Array (const Array<T,BASE,TIND> & a2)
: FlatArray<T, BASE> (a2.Size(), a2.Size() ? new T[a2.Size()] : 0) : FlatArray<T, BASE, TIND> (a2.Size(), a2.Size() ? new T[a2.Size()] : 0)
{ {
allocsize = size; allocsize = size;
ownmem = 1; ownmem = 1;
for (int i = BASE; i < size+BASE; i++) for (TIND i = BASE; i < size+BASE; i++)
(*this)[i] = a2[i]; (*this)[i] = a2[i];
} }
@ -249,7 +259,7 @@ namespace netgen
/// Delete element i (0-based). Move last element to position i. /// Delete element i (0-based). Move last element to position i.
void Delete (int i) void Delete (TIND i)
{ {
#ifdef CHECK_Array_RANGE #ifdef CHECK_Array_RANGE
RangeCheck (i+1); RangeCheck (i+1);
@ -262,7 +272,7 @@ namespace netgen
/// Delete element i (1-based). Move last element to position i. /// Delete element i (1-based). Move last element to position i.
void DeleteElement (int i) void DeleteElement (TIND i)
{ {
#ifdef CHECK_Array_RANGE #ifdef CHECK_Array_RANGE
RangeCheck (i); RangeCheck (i);
@ -290,7 +300,7 @@ namespace netgen
/// Fill array with val /// Fill array with val
Array & operator= (const T & val) Array & operator= (const T & val)
{ {
FlatArray<T, BASE>::operator= (val); FlatArray<T, BASE, TIND>::operator= (val);
return *this; return *this;
} }
@ -298,7 +308,7 @@ namespace netgen
Array & operator= (const Array & a2) Array & operator= (const Array & a2)
{ {
SetSize (a2.Size()); SetSize (a2.Size());
for (int i = BASE; i < size+BASE; i++) for (TIND i (BASE); i < size+BASE; i++)
(*this)[i] = a2[i]; (*this)[i] = a2[i];
return *this; return *this;
} }
@ -307,7 +317,7 @@ namespace netgen
Array & operator= (const FlatArray<T> & a2) Array & operator= (const FlatArray<T> & a2)
{ {
SetSize (a2.Size()); SetSize (a2.Size());
for (int i = BASE; i < size+BASE; i++) for (TIND i = BASE; i < size+BASE; i++)
(*this)[i] = a2[i]; (*this)[i] = a2[i];
return *this; return *this;
} }
@ -389,29 +399,49 @@ namespace netgen
/* /*
template <class T, int B1, int B2> template <class T, int B1, int B2>
class IndirectArray class IndirectArray
{ {
const FlatArray<T, B1> & array; const FlatArray<T, B1> & array;
const FlatArray<int, B2> & ia; const FlatArray<int, B2> & ia;
public: public:
IndirectArray (const FlatArray<T,B1> & aa, const FlatArray<int, B2> & aia) IndirectArray (const FlatArray<T,B1> & aa, const FlatArray<int, B2> & aia)
: array(aa), ia(aia) { ; } : array(aa), ia(aia) { ; }
int Size() const { return ia.Size(); } int Size() const { return ia.Size(); }
const T & operator[] (int i) const { return array[ia[i]]; } const T & operator[] (int i) const { return array[ia[i]]; }
}; };
*/ */
template <class TA1, class TA2>
class IndirectArray
{
const TA1 & array;
const TA2 & ia;
public:
IndirectArray (const TA1 & aa, const TA2 & aia)
: array(aa), ia(aia) { ; }
int Size() const { return ia.Size(); }
int Begin() const { return ia.Begin(); }
int End() const { return ia.End(); }
const typename TA1::TELEM & operator[] (int i) const { return array[ia[i]]; }
};
template <typename T1, typename T2>
inline ostream & operator<< (ostream & s, const IndirectArray<T1,T2> & ia)
{
for (int i = ia.Begin(); i < ia.End(); i++)
s << i << ": " << ia[i] << endl;
return s;
}
/*
/// ///
template <class T, int BASE = 0> template <class T, int BASE = 0>
@ -566,7 +596,7 @@ namespace netgen
ost << i << ": " << a[i] << endl; ost << i << ": " << a[i] << endl;
return ost; return ost;
} }
*/
/// bubble sort array /// bubble sort array

View File

@ -139,7 +139,7 @@ namespace netgen
BASE_INDEX_CLOSED_HASHTABLE (int size) BASE_INDEX_CLOSED_HASHTABLE (int size)
: hash(size) : hash(size)
{ {
hash.SetName ("index-hashtable, hash"); // hash.SetName ("index-hashtable, hash");
invalid = -1; invalid = -1;
for (int i = 1; i <= size; i++) for (int i = 1; i <= size; i++)
@ -216,7 +216,7 @@ namespace netgen
BASE_INDEX_2_CLOSED_HASHTABLE (int size) BASE_INDEX_2_CLOSED_HASHTABLE (int size)
: hash(size) : hash(size)
{ {
hash.SetName ("i2-hashtable, hash"); // hash.SetName ("i2-hashtable, hash");
invalid = -1; invalid = -1;
for (int i = 1; i <= size; i++) for (int i = 1; i <= size; i++)

View File

@ -468,7 +468,8 @@ class BASE_INDEX_CLOSED_HASHTABLE
{ {
protected: protected:
/// ///
MoveableArray<INDEX> hash; // MoveableArray<INDEX> hash;
Array<INDEX> hash;
/// ///
int invalid; int invalid;
public: public:
@ -543,14 +544,15 @@ template <class T>
class INDEX_CLOSED_HASHTABLE : public BASE_INDEX_CLOSED_HASHTABLE class INDEX_CLOSED_HASHTABLE : public BASE_INDEX_CLOSED_HASHTABLE
{ {
/// ///
MoveableArray<T> cont; // MoveableArray<T> cont;
Array<T> cont;
public: public:
/// ///
INDEX_CLOSED_HASHTABLE (int size) INDEX_CLOSED_HASHTABLE (int size)
: BASE_INDEX_CLOSED_HASHTABLE(size), cont(size) : BASE_INDEX_CLOSED_HASHTABLE(size), cont(size)
{ {
cont.SetName ("ind-hashtable, contents"); ; // cont.SetName ("ind-hashtable, contents");
} }
@ -617,8 +619,8 @@ public:
void SetName (const char * aname) void SetName (const char * aname)
{ {
cont.SetName(aname); // cont.SetName(aname);
hash.SetName(aname); // hash.SetName(aname);
} }
}; };
@ -634,7 +636,8 @@ class BASE_INDEX_2_CLOSED_HASHTABLE
{ {
protected: protected:
/// ///
MoveableArray<INDEX_2> hash; // MoveableArray<INDEX_2> hash;
Array<INDEX_2> hash;
/// ///
int invalid; int invalid;
public: public:
@ -695,7 +698,8 @@ template <class T>
class INDEX_2_CLOSED_HASHTABLE : public BASE_INDEX_2_CLOSED_HASHTABLE class INDEX_2_CLOSED_HASHTABLE : public BASE_INDEX_2_CLOSED_HASHTABLE
{ {
/// ///
MoveableArray<T> cont; // MoveableArray<T> cont;
Array<T> cont;
public: public:
/// ///
@ -726,8 +730,9 @@ public:
void SetName (const char * aname) void SetName (const char * aname)
{ {
cont.SetName(aname); ;
hash.SetName(aname); // cont.SetName(aname);
// hash.SetName(aname);
} }
}; };
@ -755,14 +760,15 @@ inline ostream & operator<< (ostream & ost, const INDEX_2_CLOSED_HASHTABLE<T> &
class BASE_INDEX_3_CLOSED_HASHTABLE class BASE_INDEX_3_CLOSED_HASHTABLE
{ {
protected: protected:
MoveableArray<INDEX_3> hash; // MoveableArray<INDEX_3> hash;
Array<INDEX_3> hash;
int invalid; int invalid;
protected: protected:
BASE_INDEX_3_CLOSED_HASHTABLE (int size) BASE_INDEX_3_CLOSED_HASHTABLE (int size)
: hash(size) : hash(size)
{ {
hash.SetName ("i3-hashtable, hash"); // hash.SetName ("i3-hashtable, hash");
invalid = -1; invalid = -1;
for (int i = 0; i < size; i++) for (int i = 0; i < size; i++)
hash[i].I1() = invalid; hash[i].I1() = invalid;
@ -848,13 +854,14 @@ protected:
template <class T> template <class T>
class INDEX_3_CLOSED_HASHTABLE : public BASE_INDEX_3_CLOSED_HASHTABLE class INDEX_3_CLOSED_HASHTABLE : public BASE_INDEX_3_CLOSED_HASHTABLE
{ {
MoveableArray<T,0> cont; // MoveableArray<T,0> cont;
Array<T,0> cont;
public: public:
INDEX_3_CLOSED_HASHTABLE (int size) INDEX_3_CLOSED_HASHTABLE (int size)
: BASE_INDEX_3_CLOSED_HASHTABLE(size), cont(size) : BASE_INDEX_3_CLOSED_HASHTABLE(size), cont(size)
{ {
cont.SetName ("i3-hashtable, contents"); ; //cont.SetName ("i3-hashtable, contents");
} }
void Set (const INDEX_3 & ahash, const T & acont) void Set (const INDEX_3 & ahash, const T & acont)
@ -923,8 +930,9 @@ public:
void SetName (const char * aname) void SetName (const char * aname)
{ {
cont.SetName(aname); ;
hash.SetName(aname); // cont.SetName(aname);
// hash.SetName(aname);
} }
}; };
@ -1167,7 +1175,7 @@ inline INDEX_2_CLOSED_HASHTABLE<T> ::
INDEX_2_CLOSED_HASHTABLE (int size) INDEX_2_CLOSED_HASHTABLE (int size)
: BASE_INDEX_2_CLOSED_HASHTABLE(size), cont(size) : BASE_INDEX_2_CLOSED_HASHTABLE(size), cont(size)
{ {
cont.SetName ("i2-hashtable, contents"); // cont.SetName ("i2-hashtable, contents");
} }
template<class T> template<class T>