NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
memorymanager.cpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2014 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 <ctime>
20#include <cmath>
21#include <gsl/gsl_statistics.h>
22#include <gsl/gsl_sort.h>
23
24#include "memorymanager.hpp"
25#include "../utils/tools.hpp"
26#include "../version.h"
27#include "../maths/resampler.h"
28#include "../../kernel.hpp"
29#include "tablecolumnimpl.hpp"
30using namespace std;
31
32
33/*
34 * Realisierung der Cache-Klasse
35 */
36
37
43MemoryManager::MemoryManager() : NumeRe::FileAdapter(), StringMemory(), NumeRe::ClusterManager()
44{
45 bSaveMutex = false;
46 sCache_file = "<>/numere.cache";
49 sPredefinedCommands = ";abort;about;audio;break;compose;cont;cont3d;continue;copy;credits;data;datagrid;define;delete;dens;dens3d;diff;draw;draw3d;edit;else;endcompose;endfor;endif;endprocedure;endwhile;eval;explicit;export;extrema;fft;find;fit;for;get;global;grad;grad3d;graph;graph3d;help;hist;hline;if;ifndef;ifndefined;imread;implot;info;integrate;list;load;matop;mesh;mesh3d;move;mtrxop;namespace;new;odesolve;plot;plot3d;procedure;pulse;quit;random;read;readline;regularize;remove;rename;replaceline;resample;return;save;script;set;smooth;sort;stats;stfa;str;surf;surf3d;swap;taylor;throw;undef;undefine;var;vect;vect3d;while;write;zeroes;";
50 sPluginCommands = "";
51 mCachesMap["table"] = std::make_pair(0u, 0u);
52 vMemory.push_back(new Memory());
53
55 tableLinesCount = 0.0;
56}
57
58
64{
65 if (cache_file.is_open())
66 cache_file.close();
67
68 for (size_t i = 0; i < vMemory.size(); i++)
69 delete vMemory[i];
70}
71
72
82{
83 if (isValid()) // Sind ueberhaupt Daten vorhanden?
84 {
85 if (bSaveMutex)
86 return;
87
88 bSaveMutex = true;
89
90 // --> Speicher, wo denn noetig freigeben <--
91 for (size_t i = 0; i < vMemory.size(); i++)
92 delete vMemory[i];
93
94 vMemory.clear();
95 bSaveMutex = false;
96 mCachesMap.clear();
97 mCachesMap["table"] = std::make_pair(0u, 0u);
98 vMemory.push_back(new Memory());
99 sDataFile.clear();
100 }
101}
102
103
112{
113 if (!vMemory.size())
114 return false;
115
116 for (size_t i = 0; i < vMemory.size(); i++)
117 {
118 if (vMemory[i]->getCols(false))
119 return true;
120 }
121
122 return false;
123}
124
125
135{
136 if (!vMemory.size())
137 return true;
138
139 for (size_t i = 0; i < vMemory.size(); i++)
140 {
141 if (!vMemory[i]->getSaveStatus())
142 return false;
143 }
144
145 return true;
146}
147
148
158{
159 for (size_t i = 0; i < vMemory.size(); i++)
160 {
161 vMemory[i]->setSaveStatus(_bIsSaved);
162 }
163}
164
165
175long long int MemoryManager::getLastSaved() const
176{
177 long long int nLastSaved = 0;
178
179 if (!vMemory.size())
180 return 0;
181
182 nLastSaved = vMemory[0]->getLastSaved();
183
184 for (size_t i = 1; i < vMemory.size(); i++)
185 {
186 if (vMemory[i]->getLastSaved() < nLastSaved)
187 nLastSaved = vMemory[i]->getLastSaved();
188 }
189
190 return nLastSaved;
191}
192
193
204vector<int> MemoryManager::sortElements(const string& sLine)
205{
206 if (!isValid())
207 return vector<int>();
208
209 string sCache;
210 string sSortingExpression = "-set";
211
212 if (findCommand(sLine).sString != "sort")
213 {
214 sCache = findCommand(sLine).sString;
215 }
216
217 if (findParameter(sLine, "sort", '='))
218 {
219 if (getArgAtPos(sLine, findParameter(sLine, "sort", '=')+4) == "desc")
220 sSortingExpression += " desc";
221 }
222 else
223 {
224 for (auto iter = mCachesMap.begin(); iter != mCachesMap.end(); ++iter)
225 {
226 if (findParameter(sLine, iter->first, '='))
227 {
228 if (getArgAtPos(sLine, findParameter(sLine, iter->first, '=')+5) == "desc")
229 sSortingExpression += " desc";
230
231 sCache = iter->first;
232 break;
233 }
234 else if (findParameter(sLine, iter->first))
235 {
236 sCache = iter->first;
237 break;
238 }
239 }
240 }
241
242 if (findParameter(sLine, "cols", '='))
243 sSortingExpression += " cols=" + getArgAtPos(sLine, findParameter(sLine, "cols", '=')+4);
244 else if (findParameter(sLine, "c", '='))
245 sSortingExpression += " cols=" + getArgAtPos(sLine, findParameter(sLine, "c", '=')+1);
246
247 if (findParameter(sLine, "index"))
248 sSortingExpression += " index";
249
250 return vMemory[findTable(sCache)]->sortElements(0, getLines(sCache, false)-1, 0, getCols(sCache, false)-1, sSortingExpression);
251}
252
253
268vector<int> MemoryManager::sortElements(const string& sCache, int i1, int i2, int j1, int j2, const string& sSortingExpression)
269{
270 return vMemory[findTable(sCache)]->sortElements(i1, i2, j1, j2, sSortingExpression);
271}
272
273
282void MemoryManager::setCacheFileName(string _sFileName)
283{
284 if (_sFileName.length())
285 {
286 sCache_file = ValidFileName(_sFileName, ".cache");
287 }
288}
289
290
300{
301 if (bSaveMutex)
302 return false;
303
304 bSaveMutex = true;
305
307
309
310 cacheFile.setNumberOfTables(mCachesMap.size() - isTable("data"));
311 cacheFile.writeCacheHeader();
312
313 int nLines;
314 int nCols;
315
316 for (auto iter = mCachesMap.begin(); iter != mCachesMap.end(); ++iter)
317 {
318 if (iter->first == "data")
319 continue;
320
321 nLines = vMemory[iter->second.first]->getLines(false);
322 nCols = vMemory[iter->second.first]->getCols(false);
323
324 cacheFile.setDimensions(nLines, nCols);
325 cacheFile.setData(&vMemory[iter->second.first]->memArray, nLines, nCols);
326 cacheFile.setTableName(iter->first);
327 cacheFile.setComment(vMemory[iter->second.first]->m_meta.comment);
328
329 cacheFile.write();
330 }
331
332 setSaveStatus(true);
333
334 bSaveMutex = false;
335 return true;
336}
337
338
349{
350 if (bSaveMutex)
351 return false;
352
353 bSaveMutex = true;
355
358
359 return true;
360}
361
362
373{
375
376 try
377 {
378 cacheFile.readCacheHeader();
379 size_t nCaches = cacheFile.getNumberOfTables();
380
381 for (size_t i = 0; i < vMemory.size(); i++)
382 delete vMemory[i];
383
384 vMemory.clear();
385 mCachesMap.clear();
386
387 for (size_t i = 0; i < nCaches; i++)
388 {
389 cacheFile.read();
390
391 mCachesMap[cacheFile.getTableName()] = std::make_pair(vMemory.size(), vMemory.size());
392 vMemory.push_back(new Memory());
393
394 vMemory.back()->resizeMemory(cacheFile.getRows(), cacheFile.getCols());
395 cacheFile.getData(&vMemory.back()->memArray);
396
397 vMemory.back()->shrink();
398
399 if (cacheFile.getComment() != "NO COMMENT")
400 vMemory.back()->m_meta.comment = cacheFile.getComment();
401 }
402
403 bSaveMutex = false;
404 return true;
405
406 }
407 catch (...)
408 {
409 cacheFile.close();
410 }
411
412 return false;
413}
414
415
425{
426 char*** cHeadLine = 0;
427 char* cCachesMap = 0;
428 double* dCache = 0;
429 bool* bValidData = 0;
430 long long int nCols = 0;
431 long long int nLines = 0;
432 long long int nLayers = 0;
433
434 long int nMajor = 0;
435 long int nMinor = 0;
436 long int nBuild = 0;
437 size_t nLength = 0;
438 size_t cachemapssize = 0;
439 long long int nLayerIndex = 0;
440
441 if (!cache_file.is_open())
442 cache_file.close();
443
444 cache_file.open(sCache_file.c_str(), ios_base::in | ios_base::binary);
445
446 if (!isValid() && cache_file.good())
447 {
448 time_t tTime = 0;
449 cache_file.read((char*)&nMajor, sizeof(long int));
450
451 if (cache_file.fail() || cache_file.eof())
452 {
453 cache_file.close();
454 bSaveMutex = false;
455 return false;
456 }
457
458 cache_file.read((char*)&nMinor, sizeof(long int));
459 cache_file.read((char*)&nBuild, sizeof(long int));
460 cache_file.read((char*)&tTime, sizeof(time_t));
461 cache_file.read((char*)&nLines, sizeof(long long int));
462 cache_file.read((char*)&nCols, sizeof(long long int));
463
464 if (nMajor*100+nMinor*10+nBuild >= 107 && nLines < 0 && nCols < 0)
465 {
466 nLines *= -1;
467 nCols *= -1;
468 cache_file.read((char*)&nLayers, sizeof(long long int));
469 nLayers *= -1;
470 cache_file.read((char*)&cachemapssize, sizeof(size_t));
471
472 for (size_t i = 0; i < vMemory.size(); i++)
473 delete vMemory[i];
474
475 vMemory.clear();
476 mCachesMap.clear();
477
478 for (size_t i = 0; i < cachemapssize; i++)
479 {
480 nLength = 0;
481 nLayerIndex = 0;
482 cache_file.read((char*)&nLength, sizeof(size_t));
483 cCachesMap = new char[nLength];
484 cache_file.read(cCachesMap, sizeof(char)*nLength);
485 cache_file.read((char*)&nLayerIndex, sizeof(long long int));
486 string sTemp;
487 sTemp.resize(nLength-1);
488
489 for (unsigned int n = 0; n < nLength-1; n++)
490 {
491 sTemp[n] = cCachesMap[n];
492 }
493
494 delete[] cCachesMap;
495 cCachesMap = 0;
496 mCachesMap[sTemp] = std::pair<size_t,size_t>(nLayerIndex, nLayerIndex);
497 vMemory.push_back(new Memory());
498 }
499 }
500 else
501 nLayers = 1;
502
503 cHeadLine = new char**[nLayers];
504 dCache = new double[nLayers];
505 bValidData = new bool[nLayers];
506
507 for (long long int i = 0; i < nLayers; i++)
508 {
509 cHeadLine[i] = new char*[nCols];
510
511 for (long long int j = 0; j < nCols; j++)
512 {
513 nLength = 0;
514 cache_file.read((char*)&nLength, sizeof(size_t));
515 cHeadLine[i][j] = new char[nLength];
516 cache_file.read(cHeadLine[i][j], sizeof(char)*nLength);
517 string sHead;
518
519 for (unsigned int k = 0; k < nLength-1; k++)
520 {
521 sHead += cHeadLine[i][j][k];
522 }
523
524 if (i < cachemapssize)
525 vMemory[i]->setHeadLineElement(j, sHead);
526 }
527 }
528
529 cache_file.seekg(sizeof(long long int)*nLayers*nCols, ios_base::cur);
530
531 for (long long int i = 0; i < nLines; i++)
532 {
533 for (long long int j = 0; j < nCols; j++)
534 {
535 cache_file.read((char*)dCache, sizeof(double)*nLayers);
536
537 for (long long int k = 0; k < cachemapssize; k++)
538 vMemory[k]->writeData(i, j, dCache[k]);
539 }
540 }
541
542 for (long long int i = 0; i < nLines; i++)
543 {
544 for (long long int j = 0; j < nCols; j++)
545 {
546 cache_file.read((char*)bValidData, sizeof(bool)*nLayers);
547
548 for (long long int k = 0; k < cachemapssize; k++)
549 {
550 if (!bValidData[k])
551 vMemory[k]->writeData(i, j, NAN);
552 }
553 }
554 }
555
556 for (size_t i = 0; i < vMemory.size(); i++)
557 {
558 vMemory[i]->shrink();
559 }
560
561 cache_file.close();
562 setSaveStatus(true);
563 }
564 else
565 {
566 bSaveMutex = false;
567
568 if (cHeadLine)
569 {
570 for (long long int i = 0; i < nLayers; i++)
571 {
572 for (long long int j = 0; j < nCols; j++)
573 delete[] cHeadLine[i][j];
574
575 delete[] cHeadLine[i];
576 }
577
578 delete[] cHeadLine;
579 }
580
581 if (dCache)
582 delete[] dCache;
583
584 return false;
585 }
586
587 bSaveMutex = false;
588
589 if (cHeadLine)
590 {
591 for (long long int i = 0; i < nLayers; i++)
592 {
593 for (long long int j = 0; j < nCols; j++)
594 delete[] cHeadLine[i][j];
595
596 delete[] cHeadLine[i];
597 }
598
599 delete[] cHeadLine;
600 }
601
602 if (dCache)
603 delete[] dCache;
604
605 return true;
606}
607
608
618int MemoryManager::getColElements(const VectorIndex& cols, const std::string& _sTable) const
619{
620 if (!exists(_sTable))
621 return 0;
622
623 Memory* _mem = vMemory[findTable(_sTable)];
624
625 int nElems = 0;
626
627 if (cols.isOpenEnd())
628 cols.setOpenEndIndex(_mem->getCols()-1);
629
630 for (size_t i = 0; i < cols.size(); i++)
631 {
632 if (_mem->getElemsInColumn(cols[i]) > nElems)
633 nElems = _mem->getElemsInColumn(cols[i]);
634 }
635
636 return nElems;
637}
638
639
652void MemoryManager::overwriteColumn(int col, const std::string& _sCache, TableColumn::ColumnType type)
653{
654 if (getCols(_sCache) <= col)
655 return;
656
657 convert_for_overwrite(vMemory[findTable(_sCache)]->memArray[col], col, type);
658}
659
660
671VectorIndex MemoryManager::parseEvery(string& sDir, const string& sTableName) const
672{
673 if (sDir.find("every=") != string::npos && (sDir.find("cols") != string::npos || sDir.find("lines") != string::npos))
674 {
675 string sEvery = getArgAtPos(sDir, sDir.find("every=")+6);
676 StripSpaces(sEvery);
678 _parser.DisableAccessCaching();
679 sDir.erase(sDir.find("every="));
680
681 if (sEvery.front() == '(' && sEvery.back() == ')')
682 sEvery = sEvery.substr(1, sEvery.length()-2);
683
684 if (sEvery.front() == '{' && sEvery.back() == '}')
685 {
686 // Definition contains a vector expression
687 _parser.SetExpr(sEvery);
688 int nResults;
689 mu::value_type* v = _parser.Eval(nResults);
690
691 return VectorIndex(v, nResults, 0);
692 }
693 else
694 {
695 // Usual expression
696 _parser.SetExpr(sEvery);
697 int nResults;
698 mu::value_type* v = _parser.Eval(nResults);
699
700 if (nResults == 1)
701 {
702 // Single result: usual every=a,a representation
703 vector<int> idx;
704
705 for (long long int i = intCast(v[0])-1; i < (sDir.find("cols") != string::npos ? getCols(sTableName, false) : getLines(sTableName, false)); i += intCast(v[0]))
706 {
707 idx.push_back(i);
708 }
709
710 return VectorIndex(idx);
711 }
712 else if (nResults == 2)
713 {
714 // Two results: usual every=a,b representation
715 vector<int> idx;
716
717 for (long long int i = intCast(v[0])-1; i < (sDir.find("cols") != string::npos ? getCols(sTableName, false) : getLines(sTableName, false)); i += intCast(v[1]))
718 {
719 idx.push_back(i);
720 }
721
722 return VectorIndex(idx);
723 }
724 else //arbitrary results: use it as if it was a vector
725 return VectorIndex(v, nResults, 0);
726 }
727 }
728
729 return VectorIndex(0, (sDir.find("cols") != string::npos ? getCols(sTableName, false) : getLines(sTableName, false))-1);
730}
731
732
745vector<mu::value_type> MemoryManager::resolveMAF(const string& sTableName, string sDir, mu::value_type (MemoryManager::*MAF)(const string&, long long int, long long int, long long int, long long int) const) const
746{
747 vector<mu::value_type> vResults;
748 long long int nlines = getLines(sTableName, false);
749 long long int ncols = getCols(sTableName, false);
750
751 // Find the "grid" parameter and use it as an offset
752 long long int nGridOffset = sDir.find("grid") != string::npos ? 2 : 0;
753
754 // If a grid is required, get the grid dimensions
755 // of this table
756 if (nGridOffset)
757 {
758 std::vector<mu::value_type> vSize = vMemory[findTable(sTableName)]->size(VectorIndex(), GRID);
759 nlines = vSize.front().real();
760 ncols = vSize.back().real()+nGridOffset; // compensate the offset
761 }
762
763 // Get the vector index corresponding to a possible
764 // every definition
765 VectorIndex _idx = parseEvery(sDir, sTableName);
766
767 // Resolve the actual call to the MAF
768 if (sDir.find("cols") != string::npos)
769 {
770 for (size_t i = 0; i < _idx.size(); i++)
771 {
772 if (_idx[i]+nGridOffset < 0 || _idx[i]+nGridOffset >= ncols)
773 continue;
774
775 vResults.push_back((this->*MAF)(sTableName, 0, nlines-1, _idx[i]+nGridOffset, -1));
776 }
777 }
778 else if (sDir.find("lines") != string::npos)
779 {
780 for (size_t i = 0; i < _idx.size(); i++)
781 {
782 if (_idx[i] < 0 || _idx[i] >= nlines)
783 continue;
784
785 vResults.push_back((this->*MAF)(sTableName, _idx[i], -1, nGridOffset, ncols-1));
786 }
787 }
788 else
789 vResults.push_back((this->*MAF)(sTableName, 0, nlines-1, nGridOffset, ncols-1));
790
791 if (!vResults.size())
792 vResults.push_back(NAN);
793
794 return vResults;
795}
796
797
806void MemoryManager::removeData(bool bAutoSave)
807{
808 if (vMemory.size() && mCachesMap.find("data") != mCachesMap.end())
809 {
810 deleteTable("data");
811 sDataFile = "";
812 }
813}
814
815
826Memory* MemoryManager::getTable(const string& sTable)
827{
828 if (mCachesMap.find(sTable.substr(0, sTable.find('('))) == mCachesMap.end())
829 return nullptr;
830
831 return vMemory[findTable(sTable.substr(0, sTable.find('(')))];
832}
833
834
845void MemoryManager::copyTable(const std::string& source, const std::string& target)
846{
847 Memory* sourceTable = vMemory[findTable(source.substr(0, source.find('(')))];
848
849 if (!exists(target.substr(0, target.find('('))))
850 addTable(target, NumeReKernel::getInstance()->getSettings());
851
852 Memory* targetTable = vMemory[findTable(target.substr(0, target.find('(')))];
853
854 // Use the assignment operator overload
855 *targetTable = *sourceTable;
856}
857
858
872void MemoryManager::melt(Memory* _mem, const string& sTable, bool overrideTarget)
873{
874 // Ensure that the table exists
875 if (!_mem || !_mem->memArray.size())
876 return;
877
878 // Is a corresponding table known?
879 if (mCachesMap.find(sTable) == mCachesMap.end())
880 {
881 // Append the new table
882 mCachesMap[sTable] = std::make_pair(vMemory.size(), vMemory.size());
883 vMemory.push_back(_mem);
884 }
885 else if (overrideTarget)
886 {
887 // Shall the original table be overwritten?
888 delete vMemory[mCachesMap[sTable].first];
889 vMemory[mCachesMap[sTable].first] = _mem;
890 }
891 else
892 {
893 // Combine both tables
894 Memory* _existingMem = vMemory[mCachesMap[sTable].first];
895
896 size_t nCols = _existingMem->memArray.size();
897
898 // Resize the existing table to fit the contents
899 // of both tables
900 _existingMem->resizeMemory(1, nCols + _mem->memArray.size());
901
902 // Move the contents
903 for (size_t j = 0; j < _mem->memArray.size(); j++)
904 {
905 if (_mem->memArray[j])
906 _existingMem->memArray[j+nCols].reset(_mem->memArray[j].release());
907 }
908
909 _existingMem->setSaveStatus(false);
910 _existingMem->nCalcLines = -1;
911 _existingMem->setMetaData(_existingMem->getMetaData().melt(_mem->getMetaData()));
912
913 // Delete the passed instance (it is not
914 // needed any more).
915 delete _mem;
916 }
917}
918
919
930{
931 // Determine the type of table
932 if (sTableName != "string")
933 {
934 // Update the dimensions for the selected
935 // numerical table
936 tableLinesCount = getLines(sTableName, false);
937 tableColumnsCount = getCols(sTableName, false);
938 }
939 else
940 {
941 // Update the dimensions for the selected
942 // string table
945 }
946
947 return true;
948}
949
950
960bool MemoryManager::containsTablesOrClusters(const string& sCmdLine)
961{
962 return containsTables(sCmdLine) || containsClusters(sCmdLine);
963}
964
965
975bool MemoryManager::isTable(const string& sTable) const
976{
977 if (mCachesMap.find(sTable.substr(0, sTable.find('('))) != mCachesMap.end())
978 return true;
979
980 return false;
981}
982
983
992bool MemoryManager::containsTables(const string& sExpression)
993{
994 size_t nQuotes = 0;
995
996 if (sExpression.find('(') == string::npos)
997 return false;
998
999 // Search through the expression
1000 for (size_t i = 0; i < sExpression.length(); i++)
1001 {
1002 // Consider quotation marks
1003 if (sExpression[i] == '"' && (!i || sExpression[i-1] != '\\'))
1004 nQuotes++;
1005
1006 if (!(nQuotes % 2))
1007 {
1008 // If the current character might probably be an
1009 // identifier for a table, search for the next
1010 // nonmatching character and try to find the obtained
1011 // string in the internal map
1012 if (isalpha(sExpression[i]) || sExpression[i] == '_' || sExpression[i] == '~')
1013 {
1014 size_t nStartPos = i;
1015
1016 do
1017 {
1018 i++;
1019 }
1020 while (isalnum(sExpression[i]) || sExpression[i] == '_' || sExpression[i] == '~');
1021
1022 if (sExpression[i] == '(')
1023 {
1024 if (mCachesMap.find(sExpression.substr(nStartPos, i - nStartPos)) != mCachesMap.end())
1025 return true;
1026 }
1027 }
1028 }
1029 }
1030
1031 return false;
1032}
1033
1034
1045bool MemoryManager::addTable(const string& sCache, const Settings& _option)
1046{
1047 string sCacheName = sCache.substr(0,sCache.find('('));
1048 static const string sVALIDCHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
1049
1050 // Ensure that the name of the new table contains only
1051 // valid characters
1052 if ((sCacheName[0] >= '0' && sCacheName[0] <= '9') || sCacheName[0] == '~' || sCacheName == "data" || sCacheName == "string" || sCacheName.find_first_not_of(sVALIDCHARACTERS) != string::npos)
1054
1055 // Ensure that the new table does not match a
1056 // predefined function
1057 if (sPredefinedFuncs.find(","+sCacheName+"()") != string::npos)
1059
1060 // Ensure that the new table does not match a
1061 // user-defined function
1062 if (sUserdefinedFuncs.length() && sUserdefinedFuncs.find(";"+sCacheName+";") != string::npos)
1064
1065 // Ensure that the new table does not already
1066 // exist
1067 for (auto iter = mCachesMap.begin(); iter != mCachesMap.end(); ++iter)
1068 {
1069 if (iter->first == sCacheName)
1071 }
1072
1073 // Warn, if the new table equals an existing command
1074 if (sPredefinedCommands.find(";"+sCacheName+";") != string::npos)
1075 NumeReKernel::print(LineBreak(_lang.get("CACHE_WARNING_CMD_OVERLAP", sCacheName), _option));
1076
1077 // Warn, if the new table equals a plugin command
1078 if (sPluginCommands.length() && sPluginCommands.find(";"+sCacheName+";") != string::npos)
1079 NumeReKernel::print(LineBreak(_lang.get("CACHE_WARNING_PLUGIN_OVERLAP"), _option));
1080
1081 // Actually create the new table
1082 mCachesMap[sCacheName] = std::make_pair(vMemory.size(), vMemory.size());
1083 vMemory.push_back(new Memory());
1084
1085 return true;
1086}
1087
1088
1097bool MemoryManager::deleteTable(const string& sCache)
1098{
1099 if (sCache == "table")
1100 return false;
1101
1102 for (auto iter = mCachesMap.begin(); iter != mCachesMap.end(); ++iter)
1103 {
1104 if (iter->first == sCache)
1105 {
1106 if (vMemory.size() > iter->second.first)
1107 {
1108 delete vMemory[iter->second.first];
1109 vMemory.erase(vMemory.begin() + iter->second.first);
1110 }
1111 else
1112 return false;
1113
1114 for (auto iter2 = mCachesMap.begin(); iter2 != mCachesMap.end(); ++iter2)
1115 {
1116 if (iter2->second.first > iter->second.first)
1117 iter2->second.first--;
1118
1119 if (iter2->second.second > iter->second.first)
1120 iter2->second.second--;
1121 else if (iter2->second.second == iter->second.first)
1122 iter2->second.second = iter2->second.first;
1123
1124 mCachesMap[iter2->first] = iter2->second;
1125 }
1126
1127 mCachesMap.erase(iter);
1128
1130 setSaveStatus(false);
1131 else if (!MemoryManager::isValid())
1132 {
1133 if (fileExists(getProgramPath()+"/numere.cache"))
1134 {
1135 string sCachefile = getProgramPath() + "/numere.cache";
1136 remove(sCachefile.c_str());
1137 }
1138 }
1139
1140 return true;
1141 }
1142 }
1143
1144 return false;
1145}
1146
1147
std::string getProgramPath() const
Definition: filesystem.hpp:127
std::string ValidFileName(std::string _sFileName, const std::string sExtension=".dat", bool checkExtension=true, bool doCleanPath=true) const
This member function evaluates, whether the passed filename is a valid filename. One may supply a pre...
Definition: filesystem.cpp:280
std::string get(const std::string &sMessage, const std::vector< std::string > &vTokens) const
This member function returns the language string for the passed language identifier and replaces all ...
Definition: language.cpp:292
This class represents a single table in memory, or a - so to say - single memory page to be handled b...
Definition: memory.hpp:68
NumeRe::TableMetaData getMetaData() const
Return the internal meta data structure.
Definition: memory.cpp:1202
int getCols(bool _bFull=false) const
This member function will return the number of columns, which are currently available in this table.
Definition: memory.cpp:241
TableColumnArray memArray
Definition: memory.hpp:89
void setSaveStatus(bool _bIsSaved)
This member function changes the saved state to the passed value.
Definition: memory.cpp:1518
int getElemsInColumn(size_t col) const
Returns the number of elements in the selected column (but might contain invalid values).
Definition: memory.cpp:293
bool resizeMemory(size_t _nLines, size_t _nCols)
This member function will handle all memory grow operations by doubling the base size,...
Definition: memory.cpp:221
void setMetaData(const NumeRe::TableMetaData &meta)
Update the internal meta data with the passed one.
Definition: memory.cpp:1109
int nCalcLines
Definition: memory.hpp:92
This class represents the central memory managing instance. It will handle all tables and clusters,...
void removeData(bool bAutoSave=false)
Removes the "data()" table, if it is available.
bool loadFromNewCacheFile()
This member function tries to load the contents of the cache file in the new cache file format....
std::string sPluginCommands
bool saveToCacheFile()
This member function saves the contents of this class to the cache file so that they may be restored ...
std::string sUserdefinedFuncs
mu::value_type tableLinesCount
bool getSaveStatus() const
Returns, whether there's at least a single table in memory, which has not been saved yet.
mu::value_type tableColumnsCount
std::vector< int > sortElements(const std::string &sLine)
This member function wraps the sorting functionality and evaluates the passed parameter string before...
long long int getLastSaved() const
Returns the earliest time-point, when a table was saved. This value is used to determine the elapsed ...
virtual void melt(Memory *_mem, const std::string &sTable, bool overrideTarget=false) override
This member function either combines the contents of the passed Memory instance with an existing one ...
void removeTablesFromMemory()
Removes all tables in memory and re- initializes the MemoryManager with the default table.
void copyTable(const std::string &source, const std::string &target)
Copy one table to another one (and create the missing table automatically, if needed).
int getLines(StringView sTable, bool _bFull=false) const
bool exists(const std::string &sTable) const
std::vector< Memory * > vMemory
bool isTable(const std::string &sTable) const
This member function returns, whether the passed table name corresponds to a known table.
size_t findTable(const std::string &sTable) const
std::fstream cache_file
bool deleteTable(const std::string &sCache)
This member function removes the selected table.
void setCacheFileName(std::string _sFileName)
This member function updates the name of the cache file.
void overwriteColumn(int col, const std::string &_sCache, TableColumn::ColumnType type)
This member function converts the selected column to the needed type, if this column shall be overwri...
bool containsTables(const std::string &sExpression)
This member function detects, whether a table is used in the current expression.
bool isValid() const
Evaluates, whether there's at least a single non-empty table.
void setSaveStatus(bool _bIsSaved)
Changes the save status of all tables in memory.
bool loadFromLegacyCacheFile()
This member function loads the contents of the cache file assuming the legacy format.
bool addTable(const std::string &sCache, const Settings &_option)
This member function creates a new table. It is checked, whether its name is valid and not already us...
bool updateDimensionVariables(StringView sTableName)
This member function updates the dimension variables for the selected table to be used in expressions...
Memory * getTable(const std::string &sTable)
This member function returns a pointer to an existing Memory instance representing the selected table...
VectorIndex parseEvery(std::string &sDir, const std::string &sTableName) const
This member function extracts and parses the every expression part of a MAF call.
bool containsTablesOrClusters(const std::string &sCmdLine)
This member function evaluates, whether the passed command line contains tables or clusters.
~MemoryManager()
MemoryManager destructor. Clears all created tables.
std::string sPredefinedFuncs
std::vector< mu::value_type > resolveMAF(const std::string &sTableName, std::string sDir, mu::value_type(MemoryManager::*MAF)(const std::string &, long long int, long long int, long long int, long long int) const) const
This member function is the abstract implementation of a MAF function call. Most of the MAFs use this...
std::string sPredefinedCommands
MemoryManager()
Default MemoryManager constructor. Creates the default table and initializes the list of predefined c...
bool loadFromCacheFile()
This member function wraps the loading of the tables from the cache file. It will automatically detec...
std::map< std::string, std::pair< size_t, size_t > > mCachesMap
std::string sCache_file
int getCols(StringView sTable, bool _bFull=false) const
int getColElements(const VectorIndex &cols, const std::string &_sTable) const
Returns the maximal number of elements in the selected column range.
This class resembles the cache file used to autosave and recover the tables in memory....
Definition: file.hpp:1576
void readCacheHeader()
This member function will read the cache file header and ensure that the version of the file is not n...
Definition: file.cpp:1732
virtual bool read() override
Pure virtual declaration of the read access method. Has to be implemented in all derived classes and ...
Definition: file.hpp:1590
void setNumberOfTables(size_t nTables)
Sets the number of tables to be stored in the referenced cache file.
Definition: file.hpp:1625
virtual bool write() override
Pure virtual declaration of the write access method. Has to be implemented in all derived classes and...
Definition: file.hpp:1596
void writeCacheHeader()
This member function will write the standard cache file header to the cache file.
Definition: file.cpp:1784
size_t getNumberOfTables()
Returns the number of tables stored in the referenced cache file.
Definition: file.hpp:1612
bool containsClusters(const std::string &sCmdLine) const
This member function detects, whether any cluster is used in the current expression.
Definition: cluster.cpp:1989
std::string sDataFile
Definition: fileadapter.hpp:44
void getData(TableColumnArray *data)
This method copies the internal data to the passed memory address. The target memory must already exi...
Definition: file.hpp:1113
void close()
Wrapper for fstream::close(). Will also clear the internal memory.
Definition: file.hpp:880
void setTableName(const std::string &name)
Set the table's name.
Definition: file.hpp:1170
std::string getTableName()
Returns the table name referenced in the file. Will default to the file name with non-alnum character...
Definition: file.hpp:977
std::string getComment()
Returns the comment stored with the referenced file.
Definition: file.hpp:1008
void setData(TableColumnArray *data, long long int rows, long long int cols)
This method refernces the passed external data internally. The data is not copied and must exist as l...
Definition: file.hpp:1229
long long int getRows()
Returns the number of rows.
Definition: file.hpp:1032
void setDimensions(long long int rows, long long int cols)
Sets the dimensions of the data table, which will be used in the future. Clears the internal memory i...
Definition: file.hpp:1155
void setComment(const std::string &comment)
Sets the comment to be written to the referencedfile.
Definition: file.hpp:1021
long long int getCols()
Returns the number of columns.
Definition: file.hpp:1043
static NumeReKernel * getInstance()
This static member function returns a a pointer to the singleton instance of the kernel.
Definition: kernel.hpp:221
mu::Parser & getParser()
Definition: kernel.hpp:281
static void print(const std::string &__sLine, bool printingEnabled=true)
This member function appends the passed string as a new output line to the buffer and informs the ter...
Definition: kernel.cpp:2636
This class manages the setting values of the internal (kernel) settings of this application.
Definition: settings.hpp:663
unsigned int getStringCols() const
unsigned int getStringElements(unsigned int nCol=std::string::npos) const
This class is the immutable (const) version of a string view. It can be constructed from a MutableStr...
Common exception class for all exceptions thrown in NumeRe.
Definition: error.hpp:32
@ CACHE_ALREADY_EXISTS
Definition: error.hpp:45
@ FUNCTION_IS_PREDEFINED
Definition: error.hpp:111
@ INVALID_CACHE_NAME
Definition: error.hpp:124
@ FUNCTION_ALREADY_EXISTS
Definition: error.hpp:107
static size_t invalid_position
Definition: error.hpp:235
This class abstracts all the index logics, i.e. the logical differences between single indices and in...
Definition: structures.hpp:42
void setOpenEndIndex(int nLast) const
This member function can be used to replace the open end state with a defined index value although th...
Definition: structures.hpp:756
size_t size() const
This member function returns the size of the indices stored in this class.
Definition: structures.hpp:314
bool isOpenEnd() const
This member function determines, whether the internal index set has an open end.
Definition: structures.hpp:614
void SetExpr(StringView a_sExpr)
Set the expression. Triggers first time calculation thus the creation of the bytecode and scanning of...
value_type Eval()
Single-value wrapper around the vectorized overload of this member function.
void DisableAccessCaching()
Disable the data access caching for this position.
Mathematical expressions parser.
Definition: muParser.h:51
Language _lang
Definition: kernel.cpp:39
bool fileExists(const string &)
This function checks, whether the file with the passed file name exists.
Definition: tools.cpp:2500
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:251
void StripSpaces(std::string &)
Removes leading and trailing white spaces and tabulator characters.
int findParameter(const std::string &sCmd, const std::string &sParam, const char cFollowing)
This function searches the passed parameter in the passed command string. If something is found,...
Definition: tools.cpp:113
std::string sString
TableMetaData melt(const TableMetaData &meta)
Definition: table.hpp:53
long long int intCast(const std::complex< double > &)
Casts the real part of the complex number to an integer and avoids rounding errors.
Definition: tools.cpp:1824
void convert_for_overwrite(TblColPtr &col, size_t colNo, TableColumn::ColumnType type)
This function deletes the contents of a column, if necessary, and creates a new column with the corre...
Match findCommand(StringView sCmd, const std::string &sCommand)
This function is very important for the command handler.
Definition: tools.cpp:1275
string getArgAtPos(const string &sCmd, unsigned int nPos, int extraction)
Extracts a options value at the selected position and applies automatic parsing, if necessary.
Definition: tools.cpp:1598
std::string LineBreak(std::string sOutput, const Settings &_option, bool bAllowDashBreaks, int nFirstIndent, int nIndent)
This function takes a string, splits it into multiple lines if it is too long and returns the result.
Definition: tools.cpp:2205