remove BaseDynamicTable, everything in template class

This commit is contained in:
Joachim Schöberl 2020-08-29 09:58:33 +02:00
parent f8dd4be8d6
commit 73846f23ae

View File

@ -349,32 +349,32 @@ public:
};
/// Base class to generic DynamicTable.
template <class IndexType = size_t>
class BaseDynamicTable
/**
A dynamic table class.
A DynamicTable contains entries of variable size. Entry sizes can
be increased dynamically.
*/
template <class T, class IndexType = size_t>
class DynamicTable
{
protected:
static constexpr IndexType BASE = IndexBASE<IndexType>();
///
struct linestruct
{
///
int size;
///
int maxsize;
///
void * col;
T * col;
};
///
Array<linestruct, IndexType> data;
///
char * oneblock;
T * oneblock;
public:
///
BaseDynamicTable (int size)
/// Creates table of size size
DynamicTable (int size = 0)
: data(size)
{
for (auto & d : data)
@ -386,53 +386,60 @@ template <class IndexType = size_t>
oneblock = nullptr;
}
///
BaseDynamicTable (const Array<int, IndexType> & entrysizes, int elemsize)
/// Creates table with a priori fixed entry sizes.
DynamicTable (const Array<int, IndexType> & entrysizes)
: data(entrysizes.Size())
{
int cnt = 0;
int n = entrysizes.Size();
size_t cnt = 0;
size_t n = entrysizes.Size();
for (auto es : entrysizes)
cnt += es;
oneblock = new char[elemsize * cnt];
oneblock = new T[cnt];
cnt = 0;
for (auto i : Range(data))
{
data[i].maxsize = entrysizes[i];
data[i].size = 0;
data[i].col = &oneblock[elemsize * cnt];
data[i].col = &oneblock[cnt];
cnt += entrysizes[i];
}
}
///
~BaseDynamicTable ()
~DynamicTable ()
{
if (oneblock)
delete [] oneblock;
else
for (auto & d : data)
delete [] static_cast<char*> (d.col);
delete [] d.col;
}
DynamicTable & operator= (DynamicTable && tab2)
{
Swap (data, tab2.data);
Swap (oneblock, tab2.oneblock);
return *this;
}
/// Changes Size of table to size, deletes data
void SetSize (int size)
{
for (auto & d : data)
delete [] static_cast<char*> (d.col);
delete [] d.col;
data.SetSize(size);
for (auto & d : data)
{
d.maxsize = 0;
d.size = 0;
d.col = NULL;
d.col = nullptr;
}
}
///
void IncSize (IndexType i, int elsize)
void IncSize (IndexType i)
{
NETGEN_CHECK_RANGE(i,BASE,data.Size()+BASE);
@ -440,10 +447,11 @@ template <class IndexType = size_t>
if (line.size == line.maxsize)
{
void * p = new char [(2*line.maxsize+5) * elsize];
memcpy (p, line.col, line.maxsize * elsize);
delete [] static_cast<char*> (line.col);
T * p = new T[(2*line.maxsize+5)];
for (size_t i = 0; i < line.maxsize; i++)
p[i] = std::move(line.col[i]);
// memcpy (p, line.col, line.maxsize * sizeof(T));
delete [] line.col;
line.col = p;
line.maxsize = 2*line.maxsize+5;
}
@ -454,66 +462,32 @@ template <class IndexType = size_t>
void DecSize (IndexType i)
{
NETGEN_CHECK_RANGE(i,BASE,data.Size()+BASE);
/*
if (i < 0 || i >= data.Size())
{
std::cerr << "BaseDynamicTable::Dec: Out of range" << std::endl;
return;
}
*/
linestruct & line = data[i];
#ifdef NETGEN_ENABLE_CHECK_RANGE
if (line.size == 0)
throw Exception ("BaseDynamicTable::Dec: EntrySize < 0");
#endif
line.size--;
}
};
/**
A dynamic table class.
A DynamicTable contains entries of variable size. Entry sizes can
be increased dynamically.
*/
template <class T, class IndexType = size_t>
class DynamicTable : public BaseDynamicTable<IndexType>
{
using BaseDynamicTable<IndexType>::data;
using BaseDynamicTable<IndexType>::oneblock;
public:
/// Creates table of size size
DynamicTable (int size = 0)
: BaseDynamicTable<IndexType> (size) { }
/// Creates table with a priori fixed entry sizes.
DynamicTable (const Array<int, IndexType> & entrysizes)
: BaseDynamicTable<IndexType> (entrysizes, sizeof(T)) { }
DynamicTable & operator= (DynamicTable && tab2)
{
Swap (data, tab2.data);
Swap (oneblock, tab2.oneblock);
return *this;
}
/// Inserts element acont into row i. Does not test if already used.
void Add (IndexType i, const T & acont)
{
if (data[i].size == data[i].maxsize)
this->IncSize (i, sizeof (T));
this->IncSize (i);
else
data[i].size++;
static_cast<T*> (data[i].col) [data[i].size-1] = acont;
data[i].col[data[i].size-1] = acont;
}
/// Inserts element acont into row i, iff not yet exists.
void AddUnique (IndexType i, const T & cont)
{
int es = EntrySize (i);
int * line = const_cast<int*> (GetLine (i));
T * line = data[i].col;
for (int j = 0; j < es; j++)
if (line[j] == cont)
return;
@ -524,41 +498,54 @@ class DynamicTable : public BaseDynamicTable<IndexType>
/// Inserts element acont into row i. Does not test if already used.
void AddEmpty (IndexType i)
{
IncSize (i, sizeof (T));
IncSize (i);
}
/** Set the nr-th element in the i-th row to acont.
Does not check for overflow. */
void Set (IndexType i, int nr, const T & acont)
{ static_cast<T*> (data[i].col)[nr] = acont; }
{
data[i].col[nr] = acont;
}
/** Returns the nr-th element in the i-th row.
Does not check for overflow. */
const T & Get (IndexType i, int nr) const
{ return static_cast<T*> (data[i].col)[nr]; }
{
return data[i].col[nr];
}
/** Returns pointer to the first element in row i. */
const T * GetLine (IndexType i) const
{ return static_cast<T*> (data[i].col); }
{
return data[i].col;
}
/// Returns size of the table.
int Size () const
{ return data.Size(); }
size_t Size () const
{
return data.Size();
}
/// Returns size of the i-th row.
int EntrySize (IndexType i) const
{ return data[i].size; }
{
return data[i].size;
}
///
void DecEntrySize (IndexType i)
{ DecSize(i); }
{
DecSize(i);
}
/// Access entry i
FlatArray<T> operator[] (IndexType i)
{ return FlatArray<T> (data[i].size, static_cast<T*> (data[i].col)); }
{
return FlatArray<T> (data[i].size, data[i].col);
}
/*
typedef const FlatArray<T> ConstFlatArray;
@ -567,12 +554,12 @@ class DynamicTable : public BaseDynamicTable<IndexType>
{ return FlatArray<T> (data[i].size, static_cast<T*> (data[i].col)); }
*/
FlatArray<T> operator[] (IndexType i) const
{ return FlatArray<T> (data[i].size, static_cast<T*> (data[i].col)); }
{
return FlatArray<T> (data[i].size, data[i].col);
}
};
/// Print table
template <class T>
inline ostream & operator<< (ostream & s, const DynamicTable<T> & table)