From 73846f23ae44a1db32dc9cf70b3dfc3831e62fc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20Sch=C3=B6berl?= Date: Sat, 29 Aug 2020 09:58:33 +0200 Subject: [PATCH] remove BaseDynamicTable, everything in template class --- libsrc/core/table.hpp | 545 +++++++++++++++++++++--------------------- 1 file changed, 266 insertions(+), 279 deletions(-) diff --git a/libsrc/core/table.hpp b/libsrc/core/table.hpp index a1033973..a1aac67e 100644 --- a/libsrc/core/table.hpp +++ b/libsrc/core/table.hpp @@ -20,76 +20,76 @@ namespace ngcore template -class FlatTable -{ -protected: - static constexpr IndexType BASE = IndexBASE(); - /// number of rows - size_t size; - /// pointer to first in row - size_t * index; - /// array of data - T * data; - -public: - FlatTable() = delete; - - NETGEN_INLINE FlatTable(size_t as, size_t * aindex, T * adata) - : size(as), index(aindex), data(adata) { ; } - - /// Size of table - NETGEN_INLINE size_t Size() const { return size; } - - /// Access entry - NETGEN_INLINE const FlatArray operator[] (IndexType i) const + class FlatTable { - i = i-BASE; - return FlatArray (index[i+1]-index[i], data+index[i]); - } + protected: + static constexpr IndexType BASE = IndexBASE(); + /// number of rows + size_t size; + /// pointer to first in row + size_t * index; + /// array of data + T * data; - NETGEN_INLINE T * Data() const { return data; } - - NETGEN_INLINE FlatArray AsArray() const - { - return FlatArray (index[size]-index[0], data+index[0]); - } - - NETGEN_INLINE FlatArray IndexArray() const - { - return FlatArray (size+1, index); - } - - /// takes range starting from position start of end-start elements - NETGEN_INLINE FlatTable Range (size_t start, size_t end) const - { - return FlatTable (end-start, index+start-BASE, data); - } - - /// takes range starting from position start of end-start elements - NETGEN_INLINE FlatTable Range (T_Range range) const - { - return FlatTable (range.Size(), index+range.First()-BASE, data); - } - - NETGEN_INLINE T_Range Range () const - { - return T_Range (BASE, size+BASE); - } - - class Iterator - { - const FlatTable & tab; - size_t row; public: - Iterator (const FlatTable & _tab, size_t _row) : tab(_tab), row(_row) { ; } - Iterator & operator++ () { ++row; return *this; } - FlatArray operator* () const { return tab[row]; } - bool operator!= (const Iterator & it2) { return row != it2.row; } - }; + FlatTable() = delete; - Iterator begin() const { return Iterator(*this, BASE); } - Iterator end() const { return Iterator(*this, BASE+size); } -}; + NETGEN_INLINE FlatTable(size_t as, size_t * aindex, T * adata) + : size(as), index(aindex), data(adata) { ; } + + /// Size of table + NETGEN_INLINE size_t Size() const { return size; } + + /// Access entry + NETGEN_INLINE const FlatArray operator[] (IndexType i) const + { + i = i-BASE; + return FlatArray (index[i+1]-index[i], data+index[i]); + } + + NETGEN_INLINE T * Data() const { return data; } + + NETGEN_INLINE FlatArray AsArray() const + { + return FlatArray (index[size]-index[0], data+index[0]); + } + + NETGEN_INLINE FlatArray IndexArray() const + { + return FlatArray (size+1, index); + } + + /// takes range starting from position start of end-start elements + NETGEN_INLINE FlatTable Range (size_t start, size_t end) const + { + return FlatTable (end-start, index+start-BASE, data); + } + + /// takes range starting from position start of end-start elements + NETGEN_INLINE FlatTable Range (T_Range range) const + { + return FlatTable (range.Size(), index+range.First()-BASE, data); + } + + NETGEN_INLINE T_Range Range () const + { + return T_Range (BASE, size+BASE); + } + + class Iterator + { + const FlatTable & tab; + size_t row; + public: + Iterator (const FlatTable & _tab, size_t _row) : tab(_tab), row(_row) { ; } + Iterator & operator++ () { ++row; return *this; } + FlatArray operator* () const { return tab[row]; } + bool operator!= (const Iterator & it2) { return row != it2.row; } + }; + + Iterator begin() const { return Iterator(*this, BASE); } + Iterator end() const { return Iterator(*this, BASE+size); } + }; NGCORE_API extern size_t * TablePrefixSum32 (FlatArray entrysize); NGCORE_API extern size_t * TablePrefixSum64 (FlatArray entrysize); @@ -105,106 +105,106 @@ public: { return TablePrefixSum64 (entrysize); } -/** - A compact Table container. - A table contains size entries of variable size. - The entry sizes must be known at construction. -*/ + /** + A compact Table container. + A table contains size entries of variable size. + The entry sizes must be known at construction. + */ template class Table : public FlatTable -{ -protected: - - using FlatTable::size; - using FlatTable::index; - using FlatTable::data; - -public: - /// - NETGEN_INLINE Table () : FlatTable (0,nullptr,nullptr) { ; } - /// Construct table of uniform entrysize - NETGEN_INLINE Table (size_t asize, size_t entrysize) - : FlatTable( asize, new size_t[asize+1], new T[asize*entrysize] ) { - for (size_t i : IntRange(size+1)) - index[i] = i*entrysize; - } + protected: - /// Construct table of variable entrysize - template - NETGEN_INLINE Table (FlatArray entrysize) - : FlatTable (0, nullptr, nullptr) - { - size = entrysize.Size(); - index = TablePrefixSum (FlatArray (entrysize.Size(), entrysize.Data())); - size_t cnt = index[size]; - data = new T[cnt]; - } + using FlatTable::size; + using FlatTable::index; + using FlatTable::data; - explicit NETGEN_INLINE Table (const Table & tab2) - : FlatTable(0, nullptr, nullptr) - { - size = tab2.Size(); + public: + /// + NETGEN_INLINE Table () : FlatTable (0,nullptr,nullptr) { ; } + /// Construct table of uniform entrysize + NETGEN_INLINE Table (size_t asize, size_t entrysize) + : FlatTable( asize, new size_t[asize+1], new T[asize*entrysize] ) + { + for (size_t i : IntRange(size+1)) + index[i] = i*entrysize; + } - index = new size_t[size+1]; - for (size_t i = 0; i <= size; i++) - index[i] = tab2.index[i]; + /// Construct table of variable entrysize + template + NETGEN_INLINE Table (FlatArray entrysize) + : FlatTable (0, nullptr, nullptr) + { + size = entrysize.Size(); + index = TablePrefixSum (FlatArray (entrysize.Size(), entrysize.Data())); + size_t cnt = index[size]; + data = new T[cnt]; + } - size_t cnt = index[size]; - data = new T[cnt]; - for (size_t i = 0; i < cnt; i++) - data[i] = tab2.data[i]; - } + explicit NETGEN_INLINE Table (const Table & tab2) + : FlatTable(0, nullptr, nullptr) + { + size = tab2.Size(); - NETGEN_INLINE Table (Table && tab2) - : FlatTable(0, nullptr, nullptr) - { - Swap (size, tab2.size); - Swap (index, tab2.index); - Swap (data, tab2.data); - } + index = new size_t[size+1]; + for (size_t i = 0; i <= size; i++) + index[i] = tab2.index[i]; - NETGEN_INLINE Table & operator= (Table && tab2) - { - Swap (size, tab2.size); - Swap (index, tab2.index); - Swap (data, tab2.data); - return *this; - } + size_t cnt = index[size]; + data = new T[cnt]; + for (size_t i = 0; i < cnt; i++) + data[i] = tab2.data[i]; + } + + NETGEN_INLINE Table (Table && tab2) + : FlatTable(0, nullptr, nullptr) + { + Swap (size, tab2.size); + Swap (index, tab2.index); + Swap (data, tab2.data); + } + + NETGEN_INLINE Table & operator= (Table && tab2) + { + Swap (size, tab2.size); + Swap (index, tab2.index); + Swap (data, tab2.data); + return *this; + } - /// Delete data - NETGEN_INLINE ~Table () - { - delete [] data; - delete [] index; - } + /// Delete data + NETGEN_INLINE ~Table () + { + delete [] data; + delete [] index; + } - /// Size of table - using FlatTable::Size; + /// Size of table + using FlatTable::Size; - /// number of elements in all rows - NETGEN_INLINE size_t NElements() const { return index[size]; } + /// number of elements in all rows + NETGEN_INLINE size_t NElements() const { return index[size]; } - using FlatTable::operator[]; -}; + using FlatTable::operator[]; + }; -/// Print table + /// Print table template inline ostream & operator<< (ostream & s, const Table & table) -{ - for (auto i : table.Range()) - { - s << i << ":"; - for (auto el : table[i]) - s << " " << el; - s << "\n"; - } - s << std::flush; - return s; -} + { + for (auto i : table.Range()) + { + s << i << ":"; + for (auto el : table[i]) + s << " " << el; + s << "\n"; + } + s << std::flush; + return s; + } @@ -349,32 +349,32 @@ public: }; -/// Base class to generic DynamicTable. -template - class BaseDynamicTable + + /** + A dynamic table class. + + A DynamicTable contains entries of variable size. Entry sizes can + be increased dynamically. + */ + template + class DynamicTable { protected: - static constexpr IndexType BASE = IndexBASE(); - - /// + static constexpr IndexType BASE = IndexBASE(); + struct linestruct { - /// int size; - /// int maxsize; - /// - void * col; + T * col; }; - /// Array data; - /// - char * oneblock; - + T * oneblock; + public: - /// - BaseDynamicTable (int size) + /// Creates table of size size + DynamicTable (int size = 0) : data(size) { for (auto & d : data) @@ -385,112 +385,36 @@ template } oneblock = nullptr; } - - /// - BaseDynamicTable (const Array & entrysizes, int elemsize) + + /// Creates table with a priori fixed entry sizes. + DynamicTable (const Array & 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 (d.col); + delete [] d.col; } - - /// Changes Size of table to size, deletes data - void SetSize (int size) - { - for (auto & d : data) - delete [] static_cast (d.col); - - data.SetSize(size); - for (auto & d : data) - { - d.maxsize = 0; - d.size = 0; - d.col = NULL; - } - } - - /// - void IncSize (IndexType i, int elsize) - { - NETGEN_CHECK_RANGE(i,BASE,data.Size()+BASE); - - linestruct & line = data[i]; - - if (line.size == line.maxsize) - { - void * p = new char [(2*line.maxsize+5) * elsize]; - - memcpy (p, line.col, line.maxsize * elsize); - delete [] static_cast (line.col); - line.col = p; - line.maxsize = 2*line.maxsize+5; - } - - line.size++; - } - - 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]; - - if (line.size == 0) - throw Exception ("BaseDynamicTable::Dec: EntrySize < 0"); - - line.size--; - } - }; - - - - /** - A dynamic table class. - - A DynamicTable contains entries of variable size. Entry sizes can - be increased dynamically. - */ -template -class DynamicTable : public BaseDynamicTable - { - using BaseDynamicTable::data; - using BaseDynamicTable::oneblock; - public: - /// Creates table of size size - DynamicTable (int size = 0) - : BaseDynamicTable (size) { } - - /// Creates table with a priori fixed entry sizes. - DynamicTable (const Array & entrysizes) - : BaseDynamicTable (entrysizes, sizeof(T)) { } DynamicTable & operator= (DynamicTable && tab2) { @@ -498,81 +422,144 @@ class DynamicTable : public BaseDynamicTable Swap (oneblock, tab2.oneblock); return *this; } + + /// Changes Size of table to size, deletes data + void SetSize (int size) + { + for (auto & d : data) + delete [] d.col; + data.SetSize(size); + for (auto & d : data) + { + d.maxsize = 0; + d.size = 0; + d.col = nullptr; + } + } + + /// + void IncSize (IndexType i) + { + NETGEN_CHECK_RANGE(i,BASE,data.Size()+BASE); + + linestruct & line = data[i]; + + if (line.size == line.maxsize) + { + 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; + } + + line.size++; + } + + void DecSize (IndexType i) + { + NETGEN_CHECK_RANGE(i,BASE,data.Size()+BASE); + linestruct & line = data[i]; + +#ifdef NETGEN_ENABLE_CHECK_RANGE + if (line.size == 0) + throw Exception ("BaseDynamicTable::Dec: EntrySize < 0"); +#endif + + line.size--; + } + + /// 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 (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 (GetLine (i)); + T * line = data[i].col; for (int j = 0; j < es; j++) if (line[j] == cont) return; Add (i, cont); } - + /// 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 (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. */ + Does not check for overflow. */ const T & Get (IndexType i, int nr) const - { return static_cast (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 (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 operator[] (IndexType i) - { return FlatArray (data[i].size, static_cast (data[i].col)); } - + { + return FlatArray (data[i].size, data[i].col); + } + /* - typedef const FlatArray ConstFlatArray; - /// Access entry i - ConstFlatArray operator[] (int i) const - { return FlatArray (data[i].size, static_cast (data[i].col)); } + typedef const FlatArray ConstFlatArray; + /// Access entry i + ConstFlatArray operator[] (int i) const + { return FlatArray (data[i].size, static_cast (data[i].col)); } */ FlatArray operator[] (IndexType i) const - { return FlatArray (data[i].size, static_cast (data[i].col)); } + { + return FlatArray (data[i].size, data[i].col); + } }; - - /// Print table template inline ostream & operator<< (ostream & s, const DynamicTable & table)