2009-01-13 04:40:13 +05:00
|
|
|
#ifndef FILE_TABLE
|
|
|
|
#define FILE_TABLE
|
|
|
|
|
|
|
|
/**************************************************************************/
|
|
|
|
/* File: table.hpp */
|
|
|
|
/* Author: Joachim Schoeberl */
|
|
|
|
/* Date: 01. Jun. 95 */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
2009-07-20 14:36:36 +06:00
|
|
|
namespace netgen
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2009-01-13 04:40:13 +05:00
|
|
|
/// Base class to generic class TABLE.
|
|
|
|
class BASE_TABLE
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
|
|
|
|
///
|
|
|
|
class linestruct
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
///
|
|
|
|
int size;
|
|
|
|
///
|
|
|
|
int maxsize;
|
|
|
|
///
|
|
|
|
void * col;
|
|
|
|
};
|
|
|
|
|
|
|
|
///
|
2019-07-09 13:39:16 +05:00
|
|
|
NgArray<linestruct> data;
|
2009-01-13 04:40:13 +05:00
|
|
|
char * oneblock;
|
|
|
|
|
|
|
|
public:
|
|
|
|
///
|
2017-02-25 23:48:37 +05:00
|
|
|
BASE_TABLE (BASE_TABLE && table2)
|
|
|
|
: data(move(table2.data)), oneblock(table2.oneblock)
|
|
|
|
{
|
|
|
|
table2.oneblock = nullptr;
|
|
|
|
}
|
|
|
|
|
2009-01-13 04:40:13 +05:00
|
|
|
BASE_TABLE (int size);
|
|
|
|
///
|
2019-07-09 13:40:35 +05:00
|
|
|
BASE_TABLE (const NgFlatArray<int> & entrysizes, int elemsize);
|
2009-01-13 04:40:13 +05:00
|
|
|
///
|
|
|
|
~BASE_TABLE ();
|
2017-02-25 23:48:37 +05:00
|
|
|
|
|
|
|
BASE_TABLE & operator= (BASE_TABLE && table2)
|
|
|
|
{
|
|
|
|
data = move(table2.data);
|
|
|
|
Swap (oneblock, table2.oneblock);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-01-13 04:40:13 +05:00
|
|
|
///
|
|
|
|
void SetSize (int size);
|
|
|
|
///
|
|
|
|
void ChangeSize (int size);
|
|
|
|
|
|
|
|
/// increment size of entry i by one, i is 0-based
|
|
|
|
void IncSize (int i, int elsize)
|
|
|
|
{
|
|
|
|
if (data[i].size < data[i].maxsize)
|
|
|
|
data[i].size++;
|
|
|
|
else
|
|
|
|
IncSize2 (i, elsize);
|
|
|
|
}
|
2011-07-15 03:36:19 +06:00
|
|
|
|
|
|
|
void SetEntrySize (int i, int newsize, int elsize)
|
|
|
|
{
|
|
|
|
if (newsize < data[i].maxsize)
|
|
|
|
data[i].size = newsize;
|
|
|
|
else
|
|
|
|
SetEntrySize2 (i, newsize, elsize);
|
|
|
|
}
|
|
|
|
|
2009-01-13 04:40:13 +05:00
|
|
|
///
|
|
|
|
void IncSize2 (int i, int elsize);
|
2011-07-15 03:36:19 +06:00
|
|
|
void SetEntrySize2 (int i, int newsize, int elsize);
|
2009-01-13 04:40:13 +05:00
|
|
|
|
|
|
|
// void DecSize (int i);
|
|
|
|
|
|
|
|
///
|
|
|
|
void AllocateElementsOneBlock (int elemsize);
|
|
|
|
|
2018-01-12 19:55:29 +05:00
|
|
|
size_t AllocatedElements () const;
|
|
|
|
size_t UsedElements () const;
|
2009-01-13 04:40:13 +05:00
|
|
|
|
|
|
|
void SetElementSizesToMaxSizes ();
|
2018-04-28 06:42:04 +05:00
|
|
|
|
2018-11-29 22:35:30 +05:00
|
|
|
void DoArchive (Archive & ar, int elemsize);
|
2009-01-13 04:40:13 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Abstract data type TABLE.
|
|
|
|
|
|
|
|
To an integer i in the range from 1 to size a set of elements of the
|
|
|
|
generic type T is associated.
|
|
|
|
*/
|
|
|
|
template <class T, int BASE = 0>
|
|
|
|
class TABLE : public BASE_TABLE
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// Creates table.
|
|
|
|
inline TABLE () : BASE_TABLE(0) { ; }
|
2017-02-25 23:48:37 +05:00
|
|
|
|
2009-01-13 04:40:13 +05:00
|
|
|
/// Creates table of size size
|
|
|
|
inline TABLE (int size) : BASE_TABLE (size) { ; }
|
|
|
|
|
2020-08-28 11:47:33 +05:00
|
|
|
TABLE (TABLE && tab2)
|
|
|
|
: BASE_TABLE(move(tab2))
|
|
|
|
{ }
|
|
|
|
|
2009-01-13 04:40:13 +05:00
|
|
|
/// Creates fixed maximal element size table
|
2019-07-09 13:40:35 +05:00
|
|
|
inline TABLE (const NgFlatArray<int,BASE> & entrysizes)
|
|
|
|
: BASE_TABLE (NgFlatArray<int> (entrysizes.Size(), const_cast<int*>(&entrysizes[BASE])),
|
2009-01-13 04:40:13 +05:00
|
|
|
sizeof(T))
|
|
|
|
{ ; }
|
2020-08-28 11:47:33 +05:00
|
|
|
|
|
|
|
TABLE & operator= (TABLE && tab2)
|
|
|
|
{
|
|
|
|
BASE_TABLE::operator=(move(tab2));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-01-13 04:40:13 +05:00
|
|
|
|
|
|
|
/// Changes Size of table to size, deletes data
|
|
|
|
inline void SetSize (int size)
|
|
|
|
{
|
|
|
|
BASE_TABLE::SetSize (size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Changes Size of table to size, keep data
|
|
|
|
inline void ChangeSize (int size)
|
|
|
|
{
|
|
|
|
BASE_TABLE::ChangeSize (size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Inserts element acont into row i, BASE-based. Does not test if already used.
|
|
|
|
inline void Add (int i, const T & acont)
|
|
|
|
{
|
|
|
|
IncSize (i-BASE, sizeof (T));
|
|
|
|
((T*)data[i-BASE].col)[data[i-BASE].size-1] = acont;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Inserts element acont into row i, 1-based. Does not test if already used.
|
|
|
|
inline void Add1 (int i, const T & acont)
|
|
|
|
{
|
|
|
|
IncSize (i-1, sizeof (T));
|
|
|
|
((T*)data.Elem(i).col)[data.Elem(i).size-1] = acont;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
void IncSizePrepare (int i)
|
|
|
|
{
|
|
|
|
data[i-BASE].maxsize++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Inserts element acont into row i. BASE-based. Does not test if already used, assumes to have enough memory
|
|
|
|
inline void AddSave (int i, const T & acont)
|
|
|
|
{
|
|
|
|
((T*)data[i-BASE].col)[data[i-BASE].size] = acont;
|
|
|
|
data[i-BASE].size++;
|
|
|
|
}
|
|
|
|
|
2018-01-04 17:00:01 +05:00
|
|
|
inline void ParallelAdd (int i, const T & acont)
|
|
|
|
{
|
|
|
|
auto oldval = AsAtomic (data[i-BASE].size)++;
|
|
|
|
((T*)data[i-BASE].col)[oldval] = acont;
|
|
|
|
}
|
|
|
|
|
2009-01-13 04:40:13 +05:00
|
|
|
/// Inserts element acont into row i. 1-based. Does not test if already used, assumes to have mem
|
|
|
|
inline void AddSave1 (int i, const T & acont)
|
|
|
|
{
|
|
|
|
((T*)data.Elem(i).col)[data.Elem(i).size] = acont;
|
|
|
|
data.Elem(i).size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Inserts element acont into row i. Does not test if already used.
|
|
|
|
inline void AddEmpty (int i)
|
|
|
|
{
|
|
|
|
IncSize (i-BASE, sizeof (T));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set the nr-th element in the i-th row to acont.
|
|
|
|
Does not check for overflow. */
|
|
|
|
inline void Set (int i, int nr, const T & acont)
|
|
|
|
{ ((T*)data.Get(i).col)[nr-1] = acont; }
|
|
|
|
/** Returns the nr-th element in the i-th row.
|
|
|
|
Does not check for overflow. */
|
|
|
|
inline const T & Get (int i, int nr) const
|
|
|
|
{ return ((T*)data.Get(i).col)[nr-1]; }
|
|
|
|
|
|
|
|
|
|
|
|
/** Returns pointer to the first element in row i. */
|
|
|
|
inline const T * GetLine (int i) const
|
|
|
|
{
|
|
|
|
return ((const T*)data.Get(i).col);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns size of the table.
|
|
|
|
inline int Size () const
|
|
|
|
{
|
|
|
|
return data.Size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns size of the i-th row.
|
|
|
|
inline int EntrySize (int i) const
|
|
|
|
{ return data.Get(i).size; }
|
|
|
|
|
|
|
|
/*
|
|
|
|
inline void DecEntrySize (int i)
|
|
|
|
{ DecSize(i); }
|
|
|
|
*/
|
|
|
|
void AllocateElementsOneBlock ()
|
|
|
|
{ BASE_TABLE::AllocateElementsOneBlock (sizeof(T)); }
|
|
|
|
|
|
|
|
|
|
|
|
inline void PrintMemInfo (ostream & ost) const
|
|
|
|
{
|
|
|
|
int els = AllocatedElements();
|
|
|
|
ost << "table: allocaed " << els
|
|
|
|
<< " a " << sizeof(T) << " Byts = "
|
|
|
|
<< els * sizeof(T)
|
|
|
|
<< " bytes in " << Size() << " bags."
|
|
|
|
<< " used: " << UsedElements()
|
|
|
|
<< endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Access entry.
|
2019-07-09 13:40:35 +05:00
|
|
|
NgFlatArray<T> operator[] (int i) const
|
2009-01-13 04:40:13 +05:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (i-BASE < 0 || i-BASE >= data.Size())
|
|
|
|
cout << "table out of range, i = " << i << ", s = " << data.Size() << endl;
|
|
|
|
#endif
|
|
|
|
|
2019-07-09 13:40:35 +05:00
|
|
|
return NgFlatArray<T> (data[i-BASE].size, (T*)data[i-BASE].col);
|
2009-01-13 04:40:13 +05:00
|
|
|
}
|
2018-04-28 06:42:04 +05:00
|
|
|
|
2018-11-29 22:35:30 +05:00
|
|
|
void DoArchive (Archive & ar)
|
2018-04-28 06:42:04 +05:00
|
|
|
{
|
2018-11-29 22:35:30 +05:00
|
|
|
BASE_TABLE::DoArchive(ar, sizeof(T));
|
2018-04-28 06:42:04 +05:00
|
|
|
}
|
|
|
|
|
2009-01-13 04:40:13 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T, int BASE>
|
|
|
|
inline ostream & operator<< (ostream & ost, const TABLE<T,BASE> & table)
|
|
|
|
{
|
|
|
|
for (int i = BASE; i < table.Size()+BASE; i++)
|
|
|
|
{
|
|
|
|
ost << i << ": ";
|
2019-07-09 13:40:35 +05:00
|
|
|
NgFlatArray<T> row = table[i];
|
2009-01-13 04:40:13 +05:00
|
|
|
ost << "(" << row.Size() << ") ";
|
|
|
|
for (int j = 0; j < row.Size(); j++)
|
|
|
|
ost << row[j] << " ";
|
|
|
|
ost << endl;
|
|
|
|
}
|
|
|
|
return ost;
|
|
|
|
}
|
|
|
|
|
2009-07-20 14:36:36 +06:00
|
|
|
}
|
|
|
|
|
2009-01-13 04:40:13 +05:00
|
|
|
#endif
|
|
|
|
|