From 8840c519d3b8fc00be4b5744dda56e69c470b697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20Sch=C3=B6berl?= Date: Sat, 29 Aug 2020 11:04:47 +0200 Subject: [PATCH] Min/Max of FlatArray, DynamicTable::ChangeSize --- libsrc/core/array.hpp | 16 +++++++++++++++ libsrc/core/table.hpp | 46 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/libsrc/core/array.hpp b/libsrc/core/array.hpp index 3a775eea..28868d91 100644 --- a/libsrc/core/array.hpp +++ b/libsrc/core/array.hpp @@ -599,6 +599,22 @@ namespace ngcore template FlatArray View (FlatArray fa) { return fa; } + template + auto Max (FlatArray array, T max = std::numeric_limits::min()) -> T + { + for (auto & v : array) + if (v > max) max = v; + return max; + } + + template + auto Min (FlatArray array, T min = std::numeric_limits::max()) -> T + { + for (auto & v : array) + if (v < min) min = v; + return min; + } + /// print array template inline ostream & operator<< (ostream & s, const FlatArray & a) diff --git a/libsrc/core/table.hpp b/libsrc/core/table.hpp index a1aac67e..2bcd739a 100644 --- a/libsrc/core/table.hpp +++ b/libsrc/core/table.hpp @@ -370,7 +370,7 @@ namespace ngcore }; Array data; - T * oneblock; + T * oneblock = nullptr; public: /// Creates table of size size @@ -398,7 +398,7 @@ namespace ngcore oneblock = new T[cnt]; cnt = 0; - for (auto i : Range(data)) + for (auto i : data.Range()) { data[i].maxsize = entrysizes[i]; data[i].size = 0; @@ -407,6 +407,12 @@ namespace ngcore } } + DynamicTable (DynamicTable && tab2) + { + Swap (data, tab2.data); + Swap (oneblock, tab2.oneblock); + } + ~DynamicTable () { if (oneblock) @@ -437,7 +443,32 @@ namespace ngcore d.col = nullptr; } } - + + void ChangeSize (size_t size) + { + if (oneblock) + throw Exception ("cannot change size of oneblock dynamic table"); + + size_t oldsize = data.Size(); + if (size == oldsize) + return; + + if (size < oldsize) + for (int i = size; i < oldsize; i++) + delete [] data[i+BASE].col; + + data.SetSize(size); + + for (int i = oldsize; i < size; i++) + { + data[i+BASE].maxsize = 0; + data[i+BASE].size = 0; + data[i+BASE].col = nullptr; + } + } + + + /// void IncSize (IndexType i) { @@ -528,7 +559,12 @@ namespace ngcore { return data.Size(); } - + + auto Range () const + { + return data.Range(); + } + /// Returns size of the i-th row. int EntrySize (IndexType i) const { @@ -564,7 +600,7 @@ namespace ngcore template inline ostream & operator<< (ostream & s, const DynamicTable & table) { - for (int i = 0; i < table.Size(); i++) + for (auto i : Range(table)) { s << i << ":"; for (int j = 0; j < table[i].Size(); j++)