Min/Max of FlatArray, DynamicTable::ChangeSize

This commit is contained in:
Joachim Schöberl 2020-08-29 11:04:47 +02:00
parent 73846f23ae
commit 8840c519d3
2 changed files with 57 additions and 5 deletions

View File

@ -599,6 +599,22 @@ namespace ngcore
template <typename T> template <typename T>
FlatArray<T> View (FlatArray<T> fa) { return fa; } FlatArray<T> View (FlatArray<T> fa) { return fa; }
template <typename T, typename TI>
auto Max (FlatArray<T,TI> array, T max = std::numeric_limits<T>::min()) -> T
{
for (auto & v : array)
if (v > max) max = v;
return max;
}
template <typename T, typename TI>
auto Min (FlatArray<T,TI> array, T min = std::numeric_limits<T>::max()) -> T
{
for (auto & v : array)
if (v < min) min = v;
return min;
}
/// print array /// print array
template <class T, class TIND> template <class T, class TIND>
inline ostream & operator<< (ostream & s, const FlatArray<T, TIND> & a) inline ostream & operator<< (ostream & s, const FlatArray<T, TIND> & a)

View File

@ -370,7 +370,7 @@ namespace ngcore
}; };
Array<linestruct, IndexType> data; Array<linestruct, IndexType> data;
T * oneblock; T * oneblock = nullptr;
public: public:
/// Creates table of size size /// Creates table of size size
@ -398,7 +398,7 @@ namespace ngcore
oneblock = new T[cnt]; oneblock = new T[cnt];
cnt = 0; cnt = 0;
for (auto i : Range(data)) for (auto i : data.Range())
{ {
data[i].maxsize = entrysizes[i]; data[i].maxsize = entrysizes[i];
data[i].size = 0; data[i].size = 0;
@ -407,6 +407,12 @@ namespace ngcore
} }
} }
DynamicTable (DynamicTable && tab2)
{
Swap (data, tab2.data);
Swap (oneblock, tab2.oneblock);
}
~DynamicTable () ~DynamicTable ()
{ {
if (oneblock) if (oneblock)
@ -437,7 +443,32 @@ namespace ngcore
d.col = nullptr; 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) void IncSize (IndexType i)
{ {
@ -528,7 +559,12 @@ namespace ngcore
{ {
return data.Size(); return data.Size();
} }
auto Range () const
{
return data.Range();
}
/// Returns size of the i-th row. /// Returns size of the i-th row.
int EntrySize (IndexType i) const int EntrySize (IndexType i) const
{ {
@ -564,7 +600,7 @@ namespace ngcore
template <class T> template <class T>
inline ostream & operator<< (ostream & s, const DynamicTable<T> & table) inline ostream & operator<< (ostream & s, const DynamicTable<T> & table)
{ {
for (int i = 0; i < table.Size(); i++) for (auto i : Range(table))
{ {
s << i << ":"; s << i << ":";
for (int j = 0; j < table[i].Size(); j++) for (int j = 0; j < table[i].Size(); j++)