NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
cluster.hpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2019 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
19#ifndef CLUSTER_HPP
20#define CLUSTER_HPP
21
22#include <string>
23#include <vector>
24#include <map>
25#include <cmath>
26#include "../utils/tools.hpp"
27#include "sorter.hpp"
28
29namespace NumeRe
30{
39 {
40 private:
41 unsigned short nType;
42
43 public:
49 {
54 };
55
56 ClusterItem(unsigned short type) : nType(type) {}
57 virtual ~ClusterItem() {}
58
65 unsigned short getType() const
66 {
67 return nType;
68 }
69
78 {
79 return NAN;
80 }
81
89 virtual void setDouble(const mu::value_type& val) {}
90
98 virtual std::string getString()
99 {
100 return "\"\"";
101 }
102
110 virtual std::string getInternalString()
111 {
112 return "";
113 }
114
122 virtual std::string getParserString()
123 {
124 return "\"" + getInternalString() + "\"";
125 }
126
134 virtual void setString(const std::string& strval) {}
135 };
136
137
138
145 {
146 private:
148
149 public:
151 virtual ~ClusterDoubleItem() override {}
152
159 virtual mu::value_type getDouble() override
160 {
161 return dData;
162 }
163
171 virtual void setDouble(const mu::value_type& val) override
172 {
173 dData = val;
174 }
175
183 virtual std::string getString() override
184 {
186 return toExternalString("nan");
187
188 return toExternalString(toString(dData, 7));
189 }
190
198 virtual std::string getInternalString() override
199 {
201 return "nan";
202
203 return toString(dData, 7);
204 }
205
213 virtual std::string getParserString() override
214 {
215 return getInternalString();
216 }
217
227 virtual void setString(const std::string& strval) override
228 {
229 if (isConvertible(strval, CONVTYPE_VALUE))
231 else
232 dData = NAN;
233 }
234 };
235
236
243 {
244 private:
245 std::string sData;
246
247 public:
248 ClusterStringItem(const std::string& strval) : ClusterItem(ClusterItem::ITEMTYPE_STRING) {setString(strval);}
249 virtual ~ClusterStringItem() override {}
250
258 virtual mu::value_type getDouble() override
259 {
261 return StrToCmplx(sData);
262
263 return NAN;
264 }
265
275 virtual void setDouble(const mu::value_type& val) override
276 {
277 sData = toString(val, 7);
278 }
279
286 virtual std::string getString() override
287 {
288 return toExternalString(sData);
289 }
290
297 virtual std::string getInternalString() override
298 {
299 return sData;
300 }
301
309 virtual void setString(const std::string& strval) override
310 {
311 sData = strval;
312 }
313 };
314
315
324 class Cluster : public Sorter
325 {
326 private:
327 std::vector<ClusterItem*> vClusterArray;
329 mutable int nGlobalType;
330
331 void assign(const Cluster& cluster);
332 void assign(const std::vector<mu::value_type>& vVals);
333 void assign(const std::vector<std::string>& vStrings);
334 void assignVectorResults(Indices _idx, int nNum, mu::value_type* data);
335 virtual int compare(int i, int j, int col) override;
336 virtual bool isValue(int line, int col) override;
337 void reorderElements(std::vector<int> vIndex, int i1, int i2);
338 void reduceSize(size_t z);
339
340 public:
342 {
343 bSortCaseInsensitive = false;
345 }
346 Cluster(const Cluster& cluster)
347 {
348 assign(cluster);
349 }
350 Cluster(const std::vector<mu::value_type>& vVals)
351 {
352 assign(vVals);
353 }
354 Cluster(const std::vector<std::string>& vStrings)
355 {
356 assign(vStrings);
357 }
358
360 {
361 clear();
362 }
363
364 Cluster& operator=(const Cluster& cluster)
365 {
366 assign(cluster);
367 return *this;
368 }
369 Cluster& operator=(const std::vector<mu::value_type>& vVals)
370 {
371 assign(vVals);
372 return *this;
373 }
374 Cluster& operator=(const std::vector<std::string>& vStrings)
375 {
376 assign(vStrings);
377 return *this;
378 }
379
380 void push_back(ClusterItem* item);
381 void push_back(const mu::value_type& val);
382 void push_back(const std::string& strval);
383 void pop_back();
384
385 size_t size() const;
386 size_t getBytes() const;
387 void clear();
388
389 bool isMixed() const;
390 bool isDouble() const;
391 bool isString() const;
392
393 unsigned short getType(size_t i) const;
394
395 mu::value_type getDouble(size_t i) const;
396 void setDouble(size_t i, const mu::value_type& value);
397 std::vector<mu::value_type> getDoubleArray() const;
398 void insertDataInArray(std::vector<mu::value_type>* vTarget, const VectorIndex& _vLine);
399 void setDoubleArray(const std::vector<mu::value_type>& vVals);
400 void setDoubleArray(int nNum, mu::value_type* data);
401 void assignResults(Indices _idx, int nNum, mu::value_type* data);
402
403 std::string getString(size_t i) const;
404 std::string getInternalString(size_t i) const;
405 std::string getParserString(size_t i) const;
406 void setString(size_t i, const std::string& strval);
407 std::vector<std::string> getStringArray() const;
408 std::vector<std::string> getInternalStringArray() const;
409 void setStringArray(const std::vector<std::string>& sVals);
410
411 std::vector<std::string> to_string() const;
412 std::string getVectorRepresentation() const;
413 std::string getShortVectorRepresentation(size_t maxStringLength) const;
414
415 std::vector<int> sortElements(long long int i1, long long int i2, const std::string& sSortingExpression);
416 void deleteItems(long long int i1, long long int i2);
417 void deleteItems(const VectorIndex& vLines);
418
419 mu::value_type std(const VectorIndex& _vLine);
420 mu::value_type avg(const VectorIndex& _vLine);
421 mu::value_type max(const VectorIndex& _vLine);
422 std::string strmax(const VectorIndex& _vLine);
423 mu::value_type min(const VectorIndex& _vLine);
424 std::string strmin(const VectorIndex& _vLine);
425 mu::value_type prd(const VectorIndex& _vLine);
426 mu::value_type sum(const VectorIndex& _vLine);
427 std::string strsum(const VectorIndex& _vLine);
428 mu::value_type num(const VectorIndex& _vLine);
429 mu::value_type and_func(const VectorIndex& _vLine);
430 mu::value_type or_func(const VectorIndex& _vLine);
431 mu::value_type xor_func(const VectorIndex& _vLine);
432 mu::value_type cnt(const VectorIndex& _vLine);
433 mu::value_type norm(const VectorIndex& _vLine);
434 mu::value_type cmp(const VectorIndex& _vLine, mu::value_type dRef, int _nType);
435 mu::value_type med(const VectorIndex& _vLine);
436 mu::value_type pct(const VectorIndex& _vLine, mu::value_type dPct);
437
438 };
439
440
447 {
448 private:
449 std::map<std::string, Cluster> mClusterMap;
450
451 std::string validateClusterName(const std::string& sCluster);
452 std::map<std::string, Cluster>::iterator mapStringViewFind(StringView view);
453 std::map<std::string, Cluster>::const_iterator mapStringViewFind(StringView view) const;
454
455 public:
458
460
461 bool containsClusters(const std::string& sCmdLine) const;
462 bool isCluster(StringView sCluster) const;
463 bool isCluster(const std::string& sCluster) const;
464 Cluster& getCluster(StringView sCluster);
465 Cluster& getCluster(const std::string& sCluster);
466 const Cluster& getCluster(const std::string& sCluster) const;
467 Cluster& newCluster(const std::string& sCluster);
468 void appendCluster(const std::string& sCluster, const Cluster& cluster);
469 void removeCluster(const std::string& sCluster);
470 std::string createTemporaryCluster(const std::string& suffix = "");
472 void clearAllClusters();
474
475 const std::map<std::string, Cluster>& getClusterMap() const
476 {
477 return mClusterMap;
478 }
479 };
480
481}
482
483
484#endif // CLUSTER_HPP
485
486
487
This is a cluster item, which contains a double. It features conversions to and from strings on-the-f...
Definition: cluster.hpp:145
virtual std::string getParserString() override
Returns the internal value converted to a string.
Definition: cluster.hpp:213
virtual std::string getString() override
Returns the internal value converted to a string.
Definition: cluster.hpp:183
virtual void setDouble(const mu::value_type &val) override
Overwrites the internal value.
Definition: cluster.hpp:171
virtual void setString(const std::string &strval) override
Overwrites the internal value with the passed string, which will converted to a value first.
Definition: cluster.hpp:227
mu::value_type dData
Definition: cluster.hpp:147
ClusterDoubleItem(const mu::value_type &value)
Definition: cluster.hpp:150
virtual std::string getInternalString() override
Returns the internal value converted to a string.
Definition: cluster.hpp:198
virtual ~ClusterDoubleItem() override
Definition: cluster.hpp:151
virtual mu::value_type getDouble() override
Returns the internal value.
Definition: cluster.hpp:159
This class represents a whole cluster. The single items are stored as pointers to the abstract cluste...
Definition: cluster.hpp:325
void setStringArray(const std::vector< std::string > &sVals)
This member function assigns values as data for the all cluster items in memory. The type of the clus...
Definition: cluster.cpp:871
bool isString() const
This member function returns, whether the data in the cluster have only string as type.
Definition: cluster.cpp:487
bool bSortCaseInsensitive
Definition: cluster.hpp:328
size_t size() const
This member function returns the size of the internal memory buffer as items.
Definition: cluster.cpp:356
void clear()
This member function clears the internal memory buffer and frees the associated memory.
Definition: cluster.cpp:395
std::string getParserString(size_t i) const
This member function returns the data of the i-th cluster item in memory as a parser string.
Definition: cluster.cpp:781
std::string getString(size_t i) const
This member function returns the data of the i-th cluster item in memory as a string.
Definition: cluster.cpp:745
void setDouble(size_t i, const mu::value_type &value)
This member function assigns a value as data for the i-th cluster item in memory. The type of the i-t...
Definition: cluster.cpp:561
void push_back(ClusterItem *item)
This member function appends an arbitrary cluster item at the back of the internal cluster array buff...
Definition: cluster.cpp:274
mu::value_type med(const VectorIndex &_vLine)
This member function calculates the median value of the data in memory. Cluster items,...
Definition: cluster.cpp:1784
void setString(size_t i, const std::string &strval)
This member function assigns a string as data for the i-th cluster item in memory....
Definition: cluster.cpp:801
void assignVectorResults(Indices _idx, int nNum, mu::value_type *data)
Private result assignment function for values using vectors as indices.
Definition: cluster.cpp:119
mu::value_type min(const VectorIndex &_vLine)
This member function calculates the minimal value of the data in memory. Cluster items,...
Definition: cluster.cpp:1306
Cluster(const Cluster &cluster)
Definition: cluster.hpp:346
Cluster & operator=(const std::vector< std::string > &vStrings)
Definition: cluster.hpp:374
void assignResults(Indices _idx, int nNum, mu::value_type *data)
This member function assigns calculation results as data for the cluster items in memory,...
Definition: cluster.cpp:720
std::vector< std::string > getInternalStringArray() const
This member function returns the data of all cluster items memory as a value vector.
Definition: cluster.cpp:848
mu::value_type cnt(const VectorIndex &_vLine)
This member function counts the number of valid cluster items in memory. Cluster items of any type ar...
Definition: cluster.cpp:1612
std::vector< ClusterItem * > vClusterArray
Definition: cluster.hpp:327
std::vector< std::string > to_string() const
Converts all contents of this cluster to a vector of strings. Intended to be used for data transfer.
Definition: cluster.cpp:903
mu::value_type and_func(const VectorIndex &_vLine)
This member function applies an "AND" to the data in memory. Cluster items, which do not have the typ...
Definition: cluster.cpp:1506
mu::value_type num(const VectorIndex &_vLine)
This member function counts the number of valid cluster items in memory. Cluster items of any type ar...
Definition: cluster.cpp:1475
Cluster(const std::vector< std::string > &vStrings)
Definition: cluster.hpp:354
mu::value_type xor_func(const VectorIndex &_vLine)
This member function applies an "exclusive OR" to the data in memory. Cluster items,...
Definition: cluster.cpp:1572
Cluster & operator=(const Cluster &cluster)
Definition: cluster.hpp:364
mu::value_type avg(const VectorIndex &_vLine)
This member function calculates the average of the data in memory. Cluster items, which do not have t...
Definition: cluster.cpp:1198
Cluster(const std::vector< mu::value_type > &vVals)
Definition: cluster.hpp:350
void setDoubleArray(const std::vector< mu::value_type > &vVals)
This member function assigns values as data for the all cluster items in memory. The type of the clus...
Definition: cluster.cpp:648
void insertDataInArray(std::vector< mu::value_type > *vTarget, const VectorIndex &_vLine)
This member function inserts the data of all cluster items memory into the pointer passed to the func...
Definition: cluster.cpp:612
std::string getVectorRepresentation() const
This member function constructs a plain vector from the data in memory, which can be inserted in the ...
Definition: cluster.cpp:929
mu::value_type max(const VectorIndex &_vLine)
This member function calculates the maximal value of the data in memory. Cluster items,...
Definition: cluster.cpp:1234
mu::value_type getDouble(size_t i) const
This member function returns the data of the i-th cluster item in memory as a value.
Definition: cluster.cpp:541
bool isMixed() const
This member function returns, whether the data in the cluster have mixed type.
Definition: cluster.cpp:414
void reduceSize(size_t z)
Reduces the size of this cluster to the specified number of elements. Does not create anything,...
Definition: cluster.cpp:253
virtual bool isValue(int line, int col) override
This private member function is an override for the sorter object.
Definition: cluster.cpp:207
mu::value_type std(const VectorIndex &_vLine)
This member function calculates the standard deviation of the data in memory. Cluster items,...
Definition: cluster.cpp:1160
mu::value_type pct(const VectorIndex &_vLine, mu::value_type dPct)
This member function calculates the p-th percentile of the data in memory. Cluster items,...
Definition: cluster.cpp:1851
void assign(const Cluster &cluster)
Private cluster copy assignment function.
Definition: cluster.cpp:39
std::vector< int > sortElements(long long int i1, long long int i2, const std::string &sSortingExpression)
This public member function provides access to the sorting algorithm for the cluster object.
Definition: cluster.cpp:1009
std::string getInternalString(size_t i) const
This member function returns the data of the i-th cluster item in memory as a string.
Definition: cluster.cpp:763
mu::value_type or_func(const VectorIndex &_vLine)
This member function applies an "OR" to the data in memory. Cluster items, which do not have the type...
Definition: cluster.cpp:1542
mu::value_type norm(const VectorIndex &_vLine)
This member function calculates the euclidic vector norm of the data in memory. Cluster items,...
Definition: cluster.cpp:1639
std::string strsum(const VectorIndex &_vLine)
This member function calculates the string concatenation of the data in memory. Cluster items of all ...
Definition: cluster.cpp:1444
void reorderElements(std::vector< int > vIndex, int i1, int i2)
This private member function reorders the elements in the cluster based upon the passed index vector.
Definition: cluster.cpp:230
virtual int compare(int i, int j, int col) override
This private member function is an override for the sorter object.
Definition: cluster.cpp:160
mu::value_type cmp(const VectorIndex &_vLine, mu::value_type dRef, int _nType)
This member function compares the values in memory with the referenced value and returns indices or v...
Definition: cluster.cpp:1675
mu::value_type sum(const VectorIndex &_vLine)
This member function calculates the sum of the data in memory. Cluster items, which do not have the t...
Definition: cluster.cpp:1411
std::vector< mu::value_type > getDoubleArray() const
This member function returns the data of all cluster items memory as a value vector.
Definition: cluster.cpp:588
Cluster & operator=(const std::vector< mu::value_type > &vVals)
Definition: cluster.hpp:369
void pop_back()
This member function removes the last item in the internal memory buffer and frees the associated mem...
Definition: cluster.cpp:337
mu::value_type prd(const VectorIndex &_vLine)
This member function calculates the product of the data in memory. Cluster items, which do not have t...
Definition: cluster.cpp:1378
size_t getBytes() const
This member function returns the size of the associated memory as bytes.
Definition: cluster.cpp:369
std::string strmin(const VectorIndex &_vLine)
This member function calculates the minimal string value of the data in memory. Cluster items of all ...
Definition: cluster.cpp:1343
std::string getShortVectorRepresentation(size_t maxStringLength) const
This member function constructs a short version of a plain vector from the data in memory,...
Definition: cluster.cpp:962
bool isDouble() const
This member function returns, whether the data in the cluster have only double as type.
Definition: cluster.cpp:451
unsigned short getType(size_t i) const
This member function returns the type of the i-th cluster item in the internal memory buffer.
Definition: cluster.cpp:524
std::string strmax(const VectorIndex &_vLine)
This member function calculates the maximal string value of the data in memory. Cluster items of all ...
Definition: cluster.cpp:1271
std::vector< std::string > getStringArray() const
This member function returns the data of all cluster items memory as a value vector.
Definition: cluster.cpp:828
void deleteItems(long long int i1, long long int i2)
This public member function erases elements located from the index i1 to i2.
Definition: cluster.cpp:1072
This is an abstract cluster item. It is used as root class of any cluster items and only contains the...
Definition: cluster.hpp:39
virtual std::string getParserString()
Base implementation. Returns a string with quotation marks.
Definition: cluster.hpp:122
virtual void setString(const std::string &strval)
Base implementation. Does nothing.
Definition: cluster.hpp:134
virtual void setDouble(const mu::value_type &val)
Base implementation. Does nothing.
Definition: cluster.hpp:89
virtual std::string getString()
Base implementation. Always returns an empty string.
Definition: cluster.hpp:98
unsigned short getType() const
Returns the ClusterItemType.
Definition: cluster.hpp:65
virtual std::string getInternalString()
Base implementation. Returns an empty string.
Definition: cluster.hpp:110
virtual mu::value_type getDouble()
Base implementation. Returns always NaN.
Definition: cluster.hpp:77
ClusterItem(unsigned short type)
Definition: cluster.hpp:56
ClusterItemType
Defines the available types of clusters.
Definition: cluster.hpp:49
virtual ~ClusterItem()
Definition: cluster.hpp:57
unsigned short nType
Definition: cluster.hpp:41
This class is the management class for the different clusters, which are currently available in memor...
Definition: cluster.hpp:447
bool containsClusters(const std::string &sCmdLine) const
This member function detects, whether any cluster is used in the current expression.
Definition: cluster.cpp:1989
void appendCluster(const std::string &sCluster, const Cluster &cluster)
This member function appends the passed cluster to the internal cluster map using the passed string a...
Definition: cluster.cpp:2158
void removeTemporaryClusters()
This member function returns all temporary clusters from the internal map. Temporary clusters are ind...
Definition: cluster.cpp:2209
mu::value_type dClusterElementsCount
Definition: cluster.hpp:459
void removeCluster(const std::string &sCluster)
This member function removes the cluster from memory, which corresponds to the passed cluster identif...
Definition: cluster.cpp:2173
Cluster & getCluster(StringView sCluster)
This member function returns a reference to the cluster indicated by the passed cluster identifier.
Definition: cluster.cpp:2077
bool updateClusterSizeVariables(StringView sCluster)
This member function updates the dimension variable reserved for cluster accesses with the size of th...
Definition: cluster.cpp:2245
Cluster & newCluster(const std::string &sCluster)
This member function creates a new cluster from the passed cluster identifier and returns a reference...
Definition: cluster.cpp:2139
std::map< std::string, Cluster > mClusterMap
Definition: cluster.hpp:449
bool isCluster(StringView sCluster) const
This member function returns true, if the passed cluster identifier can be found in the internal map.
Definition: cluster.cpp:2041
std::string validateClusterName(const std::string &sCluster)
This member function creates a valid cluster identifier name, which can be used to create or append a...
Definition: cluster.cpp:1924
std::map< std::string, Cluster >::iterator mapStringViewFind(StringView view)
This private member function returns an iterator to the referenced cluster or std::map::end().
Definition: cluster.cpp:1967
std::string createTemporaryCluster(const std::string &suffix="")
This member function creates a temporary cluster with a unique name and returns this name to the call...
Definition: cluster.cpp:2191
const std::map< std::string, Cluster > & getClusterMap() const
Definition: cluster.hpp:475
void clearAllClusters()
Clear all clusters currently in memory.
Definition: cluster.cpp:2229
This is a cluster item, which contains a string. It features conversions to and from doubles on-the-f...
Definition: cluster.hpp:243
virtual void setDouble(const mu::value_type &val) override
Overwrites the internal string with the passed value, which will be converted to a string first.
Definition: cluster.hpp:275
virtual std::string getString() override
Returns the internal string.
Definition: cluster.hpp:286
ClusterStringItem(const std::string &strval)
Definition: cluster.hpp:248
virtual mu::value_type getDouble() override
Returns the internal string converted to a value.
Definition: cluster.hpp:258
virtual ~ClusterStringItem() override
Definition: cluster.hpp:249
virtual void setString(const std::string &strval) override
Overwrites the internal string.
Definition: cluster.hpp:309
virtual std::string getInternalString() override
Returns the internal string.
Definition: cluster.hpp:297
Abstract parent class to implement the sorting functionality (using Quicksort) on a more generic leve...
Definition: sorter.hpp:31
This class is the immutable (const) version of a string view. It can be constructed from a MutableStr...
This class abstracts all the index logics, i.e. the logical differences between single indices and in...
Definition: structures.hpp:42
CONSTCD11 std::chrono::duration< Rep, Period > abs(std::chrono::duration< Rep, Period > d)
Definition: date.h:1317
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:251
bool isnan(const value_type &v)
Definition: muParserDef.h:379
std::string toInternalString(std::string sStr)
Converts a string literal to the internal representation in tables and clusters.
std::string toExternalString(std::string sStr)
Converts an internal string to the external representation in the terminal.
bool isConvertible(const std::string &sStr, ConvertibleType type)
This function checks, whether a string can be converted to the selected ConvertibleType.
std::complex< double > StrToCmplx(const std::string &sString)
Converts a string into a complex number.
@ CONVTYPE_VALUE
Definition: stringtools.hpp:44
This structure is central for managing the indices of a table or cluster read or write data access....
std::string toString(int)
Converts an integer to a string without the Settings bloat.