Path: blob/devel/ElmerGUI/netgen/libsrc/general/table.hpp
3206 views
#ifndef FILE_TABLE1#define FILE_TABLE23/**************************************************************************/4/* File: table.hpp */5/* Author: Joachim Schoeberl */6/* Date: 01. Jun. 95 */7/**************************************************************************/89/// Base class to generic class TABLE.10class BASE_TABLE11{12protected:1314///15class linestruct16{17public:18///19int size;20///21int maxsize;22///23void * col;24};2526///27ARRAY<linestruct> data;28char * oneblock;2930public:31///32BASE_TABLE (int size);33///34BASE_TABLE (const FlatArray<int> & entrysizes, int elemsize);35///36~BASE_TABLE ();37///38void SetSize (int size);39///40void ChangeSize (int size);4142/// increment size of entry i by one, i is 0-based43void IncSize (int i, int elsize)44{45if (data[i].size < data[i].maxsize)46data[i].size++;47else48IncSize2 (i, elsize);49}50///51void IncSize2 (int i, int elsize);5253// void DecSize (int i);5455///56void AllocateElementsOneBlock (int elemsize);5758int AllocatedElements () const;59int UsedElements () const;6061void SetElementSizesToMaxSizes ();62};6364656667686970/**71Abstract data type TABLE.7273To an integer i in the range from 1 to size a set of elements of the74generic type T is associated.75*/76template <class T, int BASE = 0>77class TABLE : public BASE_TABLE78{79public:80/// Creates table.81inline TABLE () : BASE_TABLE(0) { ; }8283/// Creates table of size size84inline TABLE (int size) : BASE_TABLE (size) { ; }8586/// Creates fixed maximal element size table87inline TABLE (const FlatArray<int,BASE> & entrysizes)88: BASE_TABLE (FlatArray<int> (entrysizes.Size(), const_cast<int*>(&entrysizes[BASE])),89sizeof(T))90{ ; }9192/// Changes Size of table to size, deletes data93inline void SetSize (int size)94{95BASE_TABLE::SetSize (size);96}9798/// Changes Size of table to size, keep data99inline void ChangeSize (int size)100{101BASE_TABLE::ChangeSize (size);102}103104105/// Inserts element acont into row i, BASE-based. Does not test if already used.106inline void Add (int i, const T & acont)107{108IncSize (i-BASE, sizeof (T));109((T*)data[i-BASE].col)[data[i-BASE].size-1] = acont;110}111112113/// Inserts element acont into row i, 1-based. Does not test if already used.114inline void Add1 (int i, const T & acont)115{116IncSize (i-1, sizeof (T));117((T*)data.Elem(i).col)[data.Elem(i).size-1] = acont;118}119120///121void IncSizePrepare (int i)122{123data[i-BASE].maxsize++;124}125126127/// Inserts element acont into row i. BASE-based. Does not test if already used, assumes to have enough memory128inline void AddSave (int i, const T & acont)129{130((T*)data[i-BASE].col)[data[i-BASE].size] = acont;131data[i-BASE].size++;132}133134/// Inserts element acont into row i. 1-based. Does not test if already used, assumes to have mem135inline void AddSave1 (int i, const T & acont)136{137((T*)data.Elem(i).col)[data.Elem(i).size] = acont;138data.Elem(i).size++;139}140141/// Inserts element acont into row i. Does not test if already used.142inline void AddEmpty (int i)143{144IncSize (i-BASE, sizeof (T));145}146147/** Set the nr-th element in the i-th row to acont.148Does not check for overflow. */149inline void Set (int i, int nr, const T & acont)150{ ((T*)data.Get(i).col)[nr-1] = acont; }151/** Returns the nr-th element in the i-th row.152Does not check for overflow. */153inline const T & Get (int i, int nr) const154{ return ((T*)data.Get(i).col)[nr-1]; }155156157/** Returns pointer to the first element in row i. */158inline const T * GetLine (int i) const159{160return ((const T*)data.Get(i).col);161}162163164/// Returns size of the table.165inline int Size () const166{167return data.Size();168}169170/// Returns size of the i-th row.171inline int EntrySize (int i) const172{ return data.Get(i).size; }173174/*175inline void DecEntrySize (int i)176{ DecSize(i); }177*/178void AllocateElementsOneBlock ()179{ BASE_TABLE::AllocateElementsOneBlock (sizeof(T)); }180181182inline void PrintMemInfo (ostream & ost) const183{184int els = AllocatedElements();185ost << "table: allocaed " << els186<< " a " << sizeof(T) << " Byts = "187<< els * sizeof(T)188<< " bytes in " << Size() << " bags."189<< " used: " << UsedElements()190<< endl;191}192193/// Access entry.194FlatArray<T> operator[] (int i) const195{196#ifdef DEBUG197if (i-BASE < 0 || i-BASE >= data.Size())198cout << "table out of range, i = " << i << ", s = " << data.Size() << endl;199#endif200201return FlatArray<T> (data[i-BASE].size, (T*)data[i-BASE].col);202}203};204205206template <class T, int BASE>207inline ostream & operator<< (ostream & ost, const TABLE<T,BASE> & table)208{209for (int i = BASE; i < table.Size()+BASE; i++)210{211ost << i << ": ";212FlatArray<T> row = table[i];213ost << "(" << row.Size() << ") ";214for (int j = 0; j < row.Size(); j++)215ost << row[j] << " ";216ost << endl;217}218return ost;219}220221#endif222223224225