NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
table.cpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2018 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 "table.hpp"
20#include "../utils/tools.hpp"
21#include "tablecolumnimpl.hpp"
22#include <cmath>
23#include <cstdio>
24
25using namespace std;
26
27namespace NumeRe
28{
33 {
34 sTableName.clear();
35 }
36
37
45 Table::Table(int nLines, int nCols) : Table()
46 {
47 setSize(nLines, nCols);
48 }
49
50
57 Table::Table(const Table& _table) : sTableName(_table.sTableName), m_meta(_table.m_meta)
58 {
59 vTableData.resize(_table.vTableData.size());
60
61 for (size_t i = 0; i < vTableData.size(); i++)
62 {
63 if (_table.vTableData[i])
64 vTableData[i].reset(_table.vTableData[i]->copy());
65 }
66 }
67
68
75 Table::Table(Table&& _table) : sTableName(_table.sTableName), m_meta(_table.m_meta)
76 {
77 // We move by using the std::swap() functions
78 std::swap(vTableData, _table.vTableData);
79 }
80
81
86 {
87 vTableData.clear();
88 }
89
90
99 {
100 // We move by using the std::swap() functions
101 std::swap(vTableData, _table.vTableData);
102 sTableName = _table.sTableName;
103 m_meta = _table.m_meta;
104
105 return *this;
106 }
107
108
119 void Table::setMinSize(size_t i, size_t j)
120 {
121 if (vTableData.size() <= j)
122 vTableData.resize(j);
123 }
124
125
136 bool Table::isNumerical(const std::string& sValue) const
137 {
138 return isConvertible(sValue, CONVTYPE_VALUE);
139 }
140
141
150 {
151 vTableData.clear();
152 sTableName.clear();
153 }
154
155
165 void Table::setSize(size_t i, size_t j)
166 {
167 this->setMinSize(i, j);
168
169 for (auto& col : vTableData)
170 {
171 if (col)
172 col->resize(i);
173 else
174 col.reset(new ValueColumn(i));
175 }
176 }
177
178
186 void Table::setName(const std::string& _sName)
187 {
188 sTableName = _sName;
189 }
190
191
201 {
202 m_meta = meta;
203 }
204
205
213 void Table::setComment(const std::string& _comment)
214 {
215 m_meta.comment = _comment;
216 }
217
218
229 void Table::setHead(size_t i, const std::string& _sHead)
230 {
231 if (i >= vTableData.size())
232 vTableData.resize(i+1);
233
234 if (!vTableData[i])
235 vTableData[i].reset(new ValueColumn);
236
237 vTableData[i]->m_sHeadLine = _sHead;
238 }
239
240
254 void Table::setHeadPart(size_t i, size_t part, const std::string& _sHead)
255 {
256 if (i >= vTableData.size())
257 vTableData.resize(i+1);
258
259 if (!vTableData[i])
260 vTableData[i].reset(new ValueColumn);
261
262 std::string head;
263
264 // Compose the new headline using the different
265 // parts
266 for (int j = 0; j < getHeadCount(); j++)
267 {
268 if (j == (int)part)
269 head += _sHead + '\n';
270 else
271 head += getCleanHeadPart(i, j) + '\n';
272 }
273
274 head.pop_back();
275
276 vTableData[i]->m_sHeadLine = head;
277 }
278
279
291 void Table::setValue(size_t i, size_t j, const mu::value_type& _dValue)
292 {
293 this->setMinSize(i+1, j+1);
294
296 vTableData[j]->setValue(i, _dValue);
297 }
298
299
312 void Table::setValueAsString(size_t i, size_t j, const std::string& _sValue)
313 {
314 // Resize the table if needed
315 this->setMinSize(i+1, j+1);
316
317 if (vTableData[j])
318 vTableData[j]->deleteElements(VectorIndex(i));
319
320 // Empty value means only deletion
321 if (!_sValue.length())
322 return;
323
324 // Is it a numerical value?
325 if (isConvertible(_sValue, CONVTYPE_DATE_TIME))
326 {
328 vTableData[j]->setValue(i, to_double(StrToTime(_sValue)));
329 }
330 else if (isConvertible(_sValue, CONVTYPE_VALUE))
331 {
333 vTableData[j]->setValue(i, StrToCmplx(_sValue));
334 }
335 else if (isConvertible(_sValue, CONVTYPE_LOGICAL))
336 {
338 vTableData[j]->setValue(i, StrToLogical(_sValue));
339 }
340 else
341 {
343 vTableData[j]->setValue(i, _sValue);
344 }
345 }
346
347
357 void Table::setColumn(size_t j, TableColumn* column)
358 {
359 if (vTableData.size() <= j)
360 vTableData.resize(j+1);
361
362 vTableData[j].reset(column);
363 }
364
365
376 {
377 if (j >= vTableData.size())
378 return false;
379
380 return convert_if_needed(vTableData[j], j, _type);
381 }
382
383
390 std::string Table::getName() const
391 {
392 return sTableName;
393 }
394
395
404 {
405 return m_meta;
406 }
407
408
415 std::string Table::getComment() const
416 {
417 return m_meta.comment;
418 }
419
420
430 {
431 int nHeadlineCount = 1;
432
433 // Get the dimensions of the complete headline (i.e. including possible linebreaks)
434 for (size_t j = 0; j < vTableData.size(); j++)
435 {
436 if (!vTableData[j])
437 continue;
438
439 // No linebreak? Continue
440 if (vTableData[j]->m_sHeadLine.find('\n') == std::string::npos)
441 continue;
442
443 int nLinebreak = 0;
444
445 // Count all linebreaks
446 for (unsigned int n = 0; n < vTableData[j]->m_sHeadLine.length() - 2; n++)
447 {
448 if (vTableData[j]->m_sHeadLine[n] == '\n')
449 nLinebreak++;
450 }
451
452 // Save the maximal number
453 if (nLinebreak + 1 > nHeadlineCount)
454 nHeadlineCount = nLinebreak + 1;
455 }
456
457 return nHeadlineCount;
458 }
459
460
469 std::string Table::getHead(size_t i) const
470 {
471 if (vTableData.size() > i && vTableData[i])
472 return vTableData[i]->m_sHeadLine;
473
475 }
476
477
487 std::string Table::getCleanHead(size_t i) const
488 {
489 if (vTableData.size() > i && vTableData[i])
490 return vTableData[i]->m_sHeadLine;
491
493 }
494
495
507 std::string Table::getCleanHeadPart(size_t i, size_t part) const
508 {
509 if (vTableData.size() > i && vTableData[i] && part < (size_t)getHeadCount())
510 {
511 // get the cleaned headline
512 std::string head = getCleanHead(i);
513
514 // Simple case: only one line
515 if (head.find('\n') == string::npos)
516 {
517 if (part)
518 return "";
519 return head;
520 }
521
522 size_t pos = 0;
523
524 // Complex case: find the selected part
525 for (size_t j = 0; j < head.length(); j++)
526 {
527 if (head[j] == '\n')
528 {
529 if (!part)
530 return head.substr(pos, j - pos);
531
532 part--;
533 pos = j+1;
534 }
535 }
536
537 // part is zero and position not: return
538 // the last part
539 if (!part && pos)
540 return head.substr(pos);
541
542 return "";
543 }
544
545 if (!part)
547
548 return "";
549 }
550
551
561 mu::value_type Table::getValue(size_t i, size_t j) const
562 {
563 if (vTableData.size() > j && vTableData[j])
564 return vTableData[j]->getValue(i);
565
566 return NAN;
567 }
568
569
580 std::string Table::getValueAsString(size_t i, size_t j) const
581 {
582 if (vTableData.size() > j && vTableData[j])
583 {
584 // Invalid number
585 if (!vTableData[j]->isValid(i))
586 return "---";
587
588 return vTableData[j]->getValueAsStringLiteral(i);
589 }
590
591 return "---";
592 }
593
594
605 {
606 if (j < vTableData.size() && vTableData[j])
607 return vTableData[j]->copy();
608
609 return nullptr;
610 }
611
612
622 {
623 if (j < vTableData.size() && vTableData[j])
624 return vTableData[j]->m_type;
625
627 }
628
629
636 size_t Table::getLines() const
637 {
638 size_t lines = 0;
639
640 for (const TblColPtr& col : vTableData)
641 {
642 if (col && col->size() > lines)
643 lines = col->size();
644 }
645
646 return lines;
647 }
648
649
656 size_t Table::getCols() const
657 {
658 return vTableData.size();
659 }
660
661
668 bool Table::isEmpty() const
669 {
670 return vTableData.empty();
671 }
672
673
683 bool Table::insertLines(size_t nPos, size_t nNum)
684 {
685 nPos -= getHeadCount();
686
687 for (TblColPtr& col : vTableData)
688 {
689 if (col)
690 col->insertElements(nPos, nNum);
691 }
692
693 return true;
694 }
695
696
704 bool Table::appendLines(size_t nNum)
705 {
706 for (TblColPtr& col : vTableData)
707 {
708 if (col)
709 col->appendElements(nNum);
710 }
711
712 return true;
713 }
714
715
725 bool Table::deleteLines(size_t nPos, size_t nNum)
726 {
727 nPos -= getHeadCount();
728
729 for (TblColPtr& col : vTableData)
730 {
731 if (col)
732 col->removeElements(nPos, nNum);
733 }
734
735 return true;
736 }
737
738
748 bool Table::insertCols(size_t nPos, size_t nNum)
749 {
750 TableColumnArray arr(nNum);
751 vTableData.insert(vTableData.begin()+nPos, std::make_move_iterator(arr.begin()), std::make_move_iterator(arr.end()));
752 return true;
753 }
754
755
763 bool Table::appendCols(size_t nNum)
764 {
765 TableColumnArray arr(nNum);
766 vTableData.insert(vTableData.end(), std::make_move_iterator(arr.begin()), std::make_move_iterator(arr.end()));
767 return true;
768 }
769
770
780 bool Table::deleteCols(size_t nPos, size_t nNum)
781 {
782 vTableData.erase(vTableData.begin()+nPos, vTableData.begin()+nPos+nNum);
783 return true;
784 }
785
786}
787
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 setName(const std::string &_sName)
Setter function for the table name.
Definition: table.cpp:186
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
TableMetaData m_meta
Definition: table.hpp:91
bool isEmpty() const
Return, whether the table is empty.
Definition: table.cpp:668
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
void setComment(const std::string &_comment)
Setter function for the table comment.
Definition: table.cpp:213
void setValue(size_t i, size_t j, const mu::value_type &_dValue)
This member function sets the data to the table. It will resize the table automatically,...
Definition: table.cpp:291
~Table()
Destructor.
Definition: table.cpp:85
void setSize(size_t i, size_t j)
This member function simply redirects the control to setMinSize().
Definition: table.cpp:165
void setColumn(size_t j, TableColumn *column)
Assigns a whole column to the internal array.
Definition: table.cpp:357
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
Table()
Default constructor.
Definition: table.cpp:32
std::string getHead(size_t i) const
Getter function for the selected column's headline.
Definition: table.cpp:469
std::string getName() const
Getter function for the table name.
Definition: table.cpp:390
int getHeadCount() const
Getter function for the needed number of headlines (depending on the number of linebreaks found in th...
Definition: table.cpp:429
void setHead(size_t i, const std::string &_sHead)
Setter function for the selected column headline. Will create missing headlines automatically.
Definition: table.cpp:229
Table & operator=(Table _table)
Move assignment operator.
Definition: table.cpp:98
size_t getCols() const
Get the number of columns.
Definition: table.cpp:656
TableMetaData getMetaData() const
Getter function for the table meta data.
Definition: table.cpp:403
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
std::string getComment() const
Getter function for the table comment.
Definition: table.cpp:415
std::string sTableName
Definition: table.hpp:90
bool isNumerical(const std::string &sValue) const
This member function is a simple helper function to determine, whether a passed value may be parsed i...
Definition: table.cpp:136
void setMetaData(const TableMetaData &meta)
Setter function for the table meta data.
Definition: table.cpp:200
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 setColumnType(size_t j, TableColumn::ColumnType _type)
Tries to change the column type of the selected column.
Definition: table.cpp:375
bool deleteLines(size_t nPos=0, size_t nNum=1)
This member function deletes lines at the desired position.
Definition: table.cpp:725
void setMinSize(size_t i, size_t j)
This member function prepares a table with the minimal size of the selected lines and columns.
Definition: table.cpp:119
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
TableColumnArray vTableData
Definition: table.hpp:89
std::string getCleanHead(size_t i) const
Getter function for the selected column's headline. Underscores and masked headlines are replaced on-...
Definition: table.cpp:487
A table column containing only numerical values.
This class abstracts all the index logics, i.e. the logical differences between single indices and in...
Definition: structures.hpp:42
double to_double(sys_time_point tp)
Convert a sys_time_point to a double. The double is returned in units of seconds.
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:251
sys_time_point StrToTime(const std::string &sString)
Convert a string to a sys_time_point.
double StrToLogical(const std::string &sString)
Converts a string into a double considering logical values.
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_DATE_TIME
Definition: stringtools.hpp:45
@ CONVTYPE_LOGICAL
Definition: stringtools.hpp:46
@ CONVTYPE_VALUE
Definition: stringtools.hpp:44
Encapsulating structure to gather all table meta data information.
Definition: table.hpp:32
std::string comment
Definition: table.hpp:33
Abstract table column, which allows using it to compose the data table in each Memory instance.
Definition: tablecolumn.hpp:34
static std::string getDefaultColumnHead(size_t colNo)
Creates a default column headline for a column, which can be used without an instance of this class.
std::unique_ptr< TableColumn > TblColPtr
Typedef for simplifying the usage of a smart pointer in combination with a TableColumn instance.
std::vector< TblColPtr > TableColumnArray
This typedef represents the actual table, which is implemented using a std::vector.
void convert_if_empty(TblColPtr &col, size_t colNo, TableColumn::ColumnType type)
Tries to convert a column if the column does not contain any data (with the exception of the header).
bool convert_if_needed(TblColPtr &col, size_t colNo, TableColumn::ColumnType type)
Tries to convert a column into the selected column, if possible.