NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
gridtable.cpp
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#include "gridtable.hpp"
20#include "../../kernel/core/utils/stringtools.hpp"
21#include "../../kernel/core/datamanagement/tablecolumnimpl.hpp"
22
23
28{
30}
31
32
41GridNumeReTable::GridNumeReTable(int numRows, int numCols)
42{
43 _table = NumeRe::Table(numRows-2, numCols-1);
44}
45
46
55{
56 _table = std::move(_extTable);
57}
58
59
69{
70 return _table.getHeadCount();
71}
72
73
82{
83 NumeRe::Table tableCopy(_table);
84 return tableCopy;
85}
86
87
96{
97 return _table;
98}
99
100
110{
111 return std::max(_table.getLines(), 1u) + getNumHeadlines() + 1;
112}
113
114
124{
125 return _table.getCols() + 1;
126}
127
128
140bool GridNumeReTable::CanGetValueAs(int row, int col, const wxString& sTypeName)
141{
142 // Headlines
143 if (row == 0 && (sTypeName == wxGRID_VALUE_FLOAT || sTypeName == "complex"))
144 return false;
145 else if (row < getNumHeadlines() && (sTypeName == wxGRID_VALUE_STRING || sTypeName == "plain"))
146 return true;
147
148 // Regular cells
149 if (sTypeName == "complex" && _table.getColumnType(col) == TableColumn::TYPE_VALUE)
150 return true;
151
152 if (sTypeName == "datetime" && _table.getColumnType(col) == TableColumn::TYPE_DATETIME)
153 return true;
154
155 if (sTypeName == wxGRID_VALUE_FLOAT
157 && (_table.getValue(row-getNumHeadlines(), col).imag() == 0 || mu::isnan(_table.getValue(row-getNumHeadlines(), col))))
158 return true;
159
160 if (sTypeName == wxGRID_VALUE_BOOL
162 && _table.getColumn(col)->isValid(row-getNumHeadlines()))
163 return true;
164
165 if (sTypeName == "plain" && _table.getColumnType(col) == TableColumn::TYPE_STRING)
166 return true;
167
168 if (sTypeName == wxGRID_VALUE_STRING && _table.getColumnType(col) != TableColumn::TYPE_NONE)
169 return true;
170
171 return false;
172}
173
174
184double GridNumeReTable::GetValueAsDouble(int row, int col)
185{
186 // Return NAN, if this is not a numeric cell
187 if (row < getNumHeadlines())
188 return NAN;
189
190 if (row - getNumHeadlines() >= (int)_table.getLines() || col >= (int)_table.getCols())
191 return NAN;
192
193 return _table.getValue(row - getNumHeadlines(), col).real();
194}
195
196
207{
208 // Return NAN, if this is not a numeric cell
209 if (row < getNumHeadlines())
210 return false;
211
212 if (row - getNumHeadlines() >= (int)_table.getLines() || col >= (int)_table.getCols())
213 return false;
214
215 return _table.getValue(row - getNumHeadlines(), col).real() != 0.0;
216}
217
218
230void* GridNumeReTable::GetValueAsCustom(int row, int col, const wxString& sTypeName)
231{
232 value = _table.getValue(row - getNumHeadlines(), col);
233 return static_cast<void*>(&value);
234}
235
236
246wxString GridNumeReTable::GetValue(int row, int col)
247{
248 if (row < getNumHeadlines() && col < (int)_table.getCols())
249 return _table.getCleanHeadPart(col, row);
250 else if (row - getNumHeadlines() >= (int)_table.getLines() || col >= (int)_table.getCols())
251 return "";
252 else
254}
255
256
270void GridNumeReTable::SetValue(int row, int col, const wxString& value)
271{
272 int nHeadRows = getNumHeadlines();
273
274 // Set the value
275 if (row < nHeadRows)
276 _table.setHeadPart(col, row, value.ToStdString());
277 else
278 _table.setValueAsString(row - nHeadRows, col, value.ToStdString());
279
280 // If the number of headlines changed, notify the
281 // grid to draw the newly added cells
282 if (nHeadRows != getNumHeadlines() && GetView())
283 {
284 if (nHeadRows < getNumHeadlines())
285 {
286 wxGridTableMessage msg(this, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, nHeadRows, getNumHeadlines() - nHeadRows);
287 GetView()->ProcessTableMessage(msg);
288 }
289 else
290 {
291 wxGridTableMessage msg(this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, getNumHeadlines(), nHeadRows - getNumHeadlines());
292 GetView()->ProcessTableMessage(msg);
293 }
294 }
295}
296
297
307{
308 _table.Clear();
309}
310
311
322bool GridNumeReTable::InsertRows(size_t pos, size_t numRows)
323{
324 _table.insertLines(pos, numRows);
325
326 // Notify the grid that the number of elements have
327 // changed and that the grid has to be redrawn
328 if (GetView())
329 {
330 wxGridTableMessage msg(this, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, pos, numRows);
331 GetView()->ProcessTableMessage(msg);
332 }
333
334 return true;
335}
336
337
347bool GridNumeReTable::AppendRows(size_t numRows)
348{
349 _table.appendLines(numRows);
350
351 // Notify the grid that the number of elements have
352 // changed and that the grid has to be redrawn
353 if (GetView())
354 {
355 wxGridTableMessage msg(this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, numRows);
356 GetView()->ProcessTableMessage(msg);
357 }
358
359 return true;
360}
361
362
373bool GridNumeReTable::DeleteRows(size_t pos, size_t numRows)
374{
375 _table.deleteLines(pos, numRows);
376
377 // Notify the grid that the number of elements have
378 // changed and that the grid has to be redrawn
379 if (GetView())
380 {
381 wxGridTableMessage msg(this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, pos, numRows);
382 GetView()->ProcessTableMessage(msg);
383 }
384
385 return true;
386}
387
388
399bool GridNumeReTable::InsertCols(size_t pos, size_t numRows)
400{
401 _table.insertCols(pos, numRows);
402
403 // Notify the grid that the number of elements have
404 // changed and that the grid has to be redrawn
405 if (GetView())
406 {
407 wxGridTableMessage msg(this, wxGRIDTABLE_NOTIFY_COLS_INSERTED, pos, numRows);
408 GetView()->ProcessTableMessage(msg);
409 }
410
411 return true;
412}
413
414
424bool GridNumeReTable::AppendCols(size_t numRows)
425{
426 _table.appendCols(numRows);
427
428 // Notify the grid that the number of elements have
429 // changed and that the grid has to be redrawn
430 if (GetView())
431 {
432 wxGridTableMessage msg(this, wxGRIDTABLE_NOTIFY_COLS_APPENDED, numRows);
433 GetView()->ProcessTableMessage(msg);
434 }
435
436 return true;
437}
438
439
450bool GridNumeReTable::DeleteCols(size_t pos, size_t numRows)
451{
452 _table.deleteCols(pos, numRows);
453
454 // Notify the grid that the number of elements have
455 // changed and that the grid has to be redrawn
456 if (GetView())
457 {
458 wxGridTableMessage msg(this, wxGRIDTABLE_NOTIFY_COLS_DELETED, pos, numRows);
459 GetView()->ProcessTableMessage(msg);
460 }
461
462 return true;
463}
464
465
476{
477 if (row < getNumHeadlines())
478 return "#";
479 else
480 return toString(row - (getNumHeadlines()-1));
481}
482
483
494{
495 return toString(col+1);
496}
497
498
508{
509 double dMin = NAN;
510 const int nHeadLines = getNumHeadlines();
511 const wxGridCellsExtent& cellExtent = coords.getExtent();
512
513 for (int i = cellExtent.m_topleft.GetRow(); i <= cellExtent.m_bottomright.GetRow(); i++)
514 {
515 for (int j = cellExtent.m_topleft.GetCol(); j <= cellExtent.m_bottomright.GetCol(); j++)
516 {
517 if (!coords.contains(i, j))
518 continue;
519
520 double val = _table.getValue(i - nHeadLines, j).real();
521
522 if (isnan(dMin) || val < dMin)
523 dMin = val;
524 }
525 }
526
527 return dMin;
528}
529
530
540{
541 double dMax = NAN;
542 const int nHeadLines = getNumHeadlines();
543 const wxGridCellsExtent& cellExtent = coords.getExtent();
544
545 for (int i = cellExtent.m_topleft.GetRow(); i <= cellExtent.m_bottomright.GetRow(); i++)
546 {
547 for (int j = cellExtent.m_topleft.GetCol(); j <= cellExtent.m_bottomright.GetCol(); j++)
548 {
549 if (!coords.contains(i, j))
550 continue;
551
552 double val = _table.getValue(i - nHeadLines, j).real();
553
554 if (isnan(dMax) || val > dMax)
555 dMax = val;
556 }
557 }
558
559 return dMax;
560}
561
562
572{
573 mu::value_type dSum = 0;
574 size_t nCount = 0;
575 const int nHeadLines = getNumHeadlines();
576 const wxGridCellsExtent& cellExtent = coords.getExtent();
577
578 for (int i = cellExtent.m_topleft.GetRow(); i <= cellExtent.m_bottomright.GetRow(); i++)
579 {
580 for (int j = cellExtent.m_topleft.GetCol(); j <= cellExtent.m_bottomright.GetCol(); j++)
581 {
582 if (!coords.contains(i, j))
583 continue;
584
585 mu::value_type val = _table.getValue(i - nHeadLines, j);
586
587 if (!mu::isnan(val))
588 {
589 nCount++;
590 dSum += val;
591 }
592 }
593 }
594
595 if (nCount)
596 return dSum / (double)nCount;
597
598 return 0.0;
599}
600
601
611{
612 mu::value_type dSum = 0;
613 const int nHeadLines = getNumHeadlines();
614 const wxGridCellsExtent& cellExtent = coords.getExtent();
615
616 for (int i = cellExtent.m_topleft.GetRow(); i <= cellExtent.m_bottomright.GetRow(); i++)
617 {
618 for (int j = cellExtent.m_topleft.GetCol(); j <= cellExtent.m_bottomright.GetCol(); j++)
619 {
620 if (!coords.contains(i, j))
621 continue;
622
623 mu::value_type val = _table.getValue(i - nHeadLines, j);
624
625 if (!mu::isnan(val))
626 dSum += val;
627 }
628 }
629
630 return dSum;
631}
632
633
640std::vector<int> GridNumeReTable::getColumnTypes() const
641{
642 std::vector<int> vTypes;
643
644 for (size_t i = 0; i < _table.getCols(); i++)
645 vTypes.push_back(_table.getColumnType(i));
646
647 return vTypes;
648}
649
650
659std::vector<std::string> GridNumeReTable::getCategories(int col) const
660{
662 return static_cast<CategoricalColumn*>(_table.getColumn(col))->getCategories();
663
664 return std::vector<std::string>();
665}
666
A table column containing categorical values.
mu::value_type sum(const wxGridCellCoordsContainer &coords) const
This member function calculates the sum of the range (r1,c1)->(r2,c2).
Definition: gridtable.cpp:610
mu::value_type avg(const wxGridCellCoordsContainer &coords) const
This member function calculates the average value of the range (r1,c1)->(r2,c2).
Definition: gridtable.cpp:571
virtual wxString GetRowLabelValue(int row)
This virtual member function will return the label for the selected row as a string.
Definition: gridtable.cpp:475
virtual void Clear()
This virtual function redirects the control directly to the internal buffer and cleares it contents.
Definition: gridtable.cpp:306
virtual bool AppendCols(size_t numRows=1)
This virtual function redirects the control directly to the internal buffer and appends columns.
Definition: gridtable.cpp:424
std::vector< std::string > getCategories(int col) const
Returns the categories in the selected column, if this is a categorical column.
Definition: gridtable.cpp:659
virtual bool GetValueAsBool(int row, int col)
This virtual member function returns the selected cell value as a bool.
Definition: gridtable.cpp:206
virtual wxString GetColLabelValue(int col)
This virtual member function will return the label for the selected column as a string.
Definition: gridtable.cpp:493
virtual bool AppendRows(size_t numRows=1)
This virtual function redirects the control directly to the internal buffer and appends rows.
Definition: gridtable.cpp:347
virtual bool DeleteRows(size_t pos=0, size_t numRows=1)
This virtual function redirects the control directly to the internal buffer and deletes rows.
Definition: gridtable.cpp:373
GridNumeReTable()
Default constructor.
Definition: gridtable.cpp:27
virtual int GetNumberRows()
This member function will return the number of lines including the headlines handled by this data pro...
Definition: gridtable.cpp:109
double max(const wxGridCellCoordsContainer &coords) const
This member function calculates the maximal value in the range (r1,c1)->(r2,c2).
Definition: gridtable.cpp:539
NumeRe::Table getTableCopy()
This member function returns a copy of the internal table.
Definition: gridtable.cpp:81
std::vector< int > getColumnTypes() const
Returns the types of the handled table.
Definition: gridtable.cpp:640
virtual bool InsertRows(size_t pos=0, size_t numRows=1)
This virtual function redirects the control directly to the internal buffer and inserts rows.
Definition: gridtable.cpp:322
virtual bool InsertCols(size_t pos=0, size_t numRows=1)
This virtual function redirects the control directly to the internal buffer and inserts columns.
Definition: gridtable.cpp:399
virtual bool DeleteCols(size_t pos=0, size_t numRows=1)
This virtual function redirects the control directly to the internal buffer and deletes columns.
Definition: gridtable.cpp:450
virtual int GetNumberCols()
This member function will return the number of columns handled by this data provider class.
Definition: gridtable.cpp:123
NumeRe::Table _table
Definition: gridtable.hpp:36
virtual bool CanGetValueAs(int row, int col, const wxString &sTypeName)
This virtual member function will tell the grid, which data types may be returned by the current cell...
Definition: gridtable.cpp:140
double min(const wxGridCellCoordsContainer &coords) const
This member function calculates the minimal value in the range (r1,c1)->(r2,c2).
Definition: gridtable.cpp:507
virtual void SetValue(int row, int col, const wxString &value)
This virtual member function sets the passed value in the internal buffer. It's decided automatically...
Definition: gridtable.cpp:270
NumeRe::Table & getTableRef()
This member function returns a reference to the internal table.
Definition: gridtable.cpp:95
virtual double GetValueAsDouble(int row, int col)
This virtual member function returns the selected cell value as a double.
Definition: gridtable.cpp:184
int getNumHeadlines() const
This member function will return the number of headlines available in the internal buffer.
Definition: gridtable.cpp:68
mu::value_type value
Definition: gridtable.hpp:38
virtual wxString GetValue(int row, int col)
This virtual member function returns the value of the selected cell as string.
Definition: gridtable.cpp:246
virtual void * GetValueAsCustom(int row, int col, const wxString &sTypeName)
This member function will return the internal data as a void pointer referencing the internal mu::val...
Definition: gridtable.cpp:230
This data container is a copy- efficient table to interchange data between Kernel and GUI.
Definition: table.hpp:87
bool insertLines(size_t nPos=0, size_t nNum=1)
This member function inserts lines at the desired position.
Definition: table.cpp:683
TableColumn::ColumnType getColumnType(size_t j) const
Returns the type of the selected column.
Definition: table.cpp:621
void Clear()
This member function cleares the contents of this table.
Definition: table.cpp:149
bool appendLines(size_t nNum=1)
This member function appends lines.
Definition: table.cpp:704
std::string getValueAsString(size_t i, size_t j) const
Getter function for the value of the selected cell. The value is converted into a string.
Definition: table.cpp:580
bool deleteCols(size_t nPos=0, size_t nNum=1)
This member function delets columns at the desired position.
Definition: table.cpp:780
std::string getCleanHeadPart(size_t i, size_t part=0) const
Getter function for the selected part of the selected column's headline. Underscores and masked headl...
Definition: table.cpp:507
bool insertCols(size_t nPos=0, size_t nNum=1)
This member function inserts columns at the desired position.
Definition: table.cpp:748
int getHeadCount() const
Getter function for the needed number of headlines (depending on the number of linebreaks found in th...
Definition: table.cpp:429
size_t getCols() const
Get the number of columns.
Definition: table.cpp:656
void setValueAsString(size_t i, size_t j, const std::string &_sValue)
This member function sets the data, which is passed as a string, to the table. If the data is not a n...
Definition: table.cpp:312
size_t getLines() const
Get the number of lines.
Definition: table.cpp:636
mu::value_type getValue(size_t i, size_t j) const
Getter function for the value of the selected cell.
Definition: table.cpp:561
bool deleteLines(size_t nPos=0, size_t nNum=1)
This member function deletes lines at the desired position.
Definition: table.cpp:725
bool appendCols(size_t nNum=1)
This member function appends columns.
Definition: table.cpp:763
void setHeadPart(size_t i, size_t part, const std::string &_sHead)
Setter function for the selected column headline and the selected part of the headline (split using l...
Definition: table.cpp:254
TableColumn * getColumn(size_t j) const
Returns a copy of the internal column array or a nullptr, if the column does not exist or is empty.
Definition: table.cpp:604
A class to simplify the access to different types of grid cell coords. Especially useful in the conte...
const wxGridCellsExtent & getExtent() const
Get the maximal needed enclosing box in terms of wxGridCellCoordinates.
bool contains(const wxGridCellCoords &cell) const
Does this wxGridCellCoordsContainer contain the passed coordinates?
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::vector< double > imag(const std::vector< value_type > &vVec)
#define max(a, b)
Definition: resampler.cpp:30
std::string replaceControlCharacters(std::string sToModify)
This function is a simple wrapper for replaceAll() and specialized to remove control characters from ...
virtual bool isValid(int elem) const =0
std::string toString(int)
Converts an integer to a string without the Settings bloat.
A simple structure to define the needed grid space to enclose all cells contained in the wxGridCellCo...
wxGridCellCoords m_bottomright