21#include <gsl/gsl_statistics.h>
22#include <gsl/gsl_sort.h>
25#include "../utils/tools.hpp"
26#include "../version.h"
27#include "../maths/resampler.h"
28#include "../../kernel.hpp"
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;";
68 for (
size_t i = 0; i <
vMemory.size(); i++)
91 for (
size_t i = 0; i <
vMemory.size(); i++)
116 for (
size_t i = 0; i <
vMemory.size(); i++)
139 for (
size_t i = 0; i <
vMemory.size(); i++)
159 for (
size_t i = 0; i <
vMemory.size(); i++)
161 vMemory[i]->setSaveStatus(_bIsSaved);
177 long long int nLastSaved = 0;
182 nLastSaved =
vMemory[0]->getLastSaved();
184 for (
size_t i = 1; i <
vMemory.size(); i++)
187 nLastSaved =
vMemory[i]->getLastSaved();
207 return vector<int>();
210 string sSortingExpression =
"-set";
220 sSortingExpression +=
" desc";
229 sSortingExpression +=
" desc";
231 sCache = iter->first;
236 sCache = iter->first;
248 sSortingExpression +=
" index";
270 return vMemory[
findTable(sCache)]->sortElements(i1, i2, j1, j2, sSortingExpression);
284 if (_sFileName.length())
318 if (iter->first ==
"data")
321 nLines =
vMemory[iter->second.first]->getLines(
false);
322 nCols =
vMemory[iter->second.first]->getCols(
false);
325 cacheFile.
setData(&
vMemory[iter->second.first]->memArray, nLines, nCols);
381 for (
size_t i = 0; i <
vMemory.size(); i++)
387 for (
size_t i = 0; i < nCaches; i++)
426 char*** cHeadLine = 0;
427 char* cCachesMap = 0;
429 bool* bValidData = 0;
430 long long int nCols = 0;
431 long long int nLines = 0;
432 long long int nLayers = 0;
438 size_t cachemapssize = 0;
439 long long int nLayerIndex = 0;
449 cache_file.read((
char*)&nMajor,
sizeof(
long int));
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));
464 if (nMajor*100+nMinor*10+nBuild >= 107 && nLines < 0 && nCols < 0)
468 cache_file.read((
char*)&nLayers,
sizeof(
long long int));
470 cache_file.read((
char*)&cachemapssize,
sizeof(
size_t));
472 for (
size_t i = 0; i <
vMemory.size(); i++)
478 for (
size_t i = 0; i < cachemapssize; i++)
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));
487 sTemp.resize(nLength-1);
489 for (
unsigned int n = 0; n < nLength-1; n++)
491 sTemp[n] = cCachesMap[n];
496 mCachesMap[sTemp] = std::pair<size_t,size_t>(nLayerIndex, nLayerIndex);
503 cHeadLine =
new char**[nLayers];
504 dCache =
new double[nLayers];
505 bValidData =
new bool[nLayers];
507 for (
long long int i = 0; i < nLayers; i++)
509 cHeadLine[i] =
new char*[nCols];
511 for (
long long int j = 0; j < nCols; j++)
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);
519 for (
unsigned int k = 0; k < nLength-1; k++)
521 sHead += cHeadLine[i][j][k];
524 if (i < cachemapssize)
525 vMemory[i]->setHeadLineElement(j, sHead);
529 cache_file.seekg(
sizeof(
long long int)*nLayers*nCols, ios_base::cur);
531 for (
long long int i = 0; i < nLines; i++)
533 for (
long long int j = 0; j < nCols; j++)
535 cache_file.read((
char*)dCache,
sizeof(
double)*nLayers);
537 for (
long long int k = 0; k < cachemapssize; k++)
538 vMemory[k]->writeData(i, j, dCache[k]);
542 for (
long long int i = 0; i < nLines; i++)
544 for (
long long int j = 0; j < nCols; j++)
546 cache_file.read((
char*)bValidData,
sizeof(
bool)*nLayers);
548 for (
long long int k = 0; k < cachemapssize; k++)
551 vMemory[k]->writeData(i, j, NAN);
556 for (
size_t i = 0; i <
vMemory.size(); i++)
570 for (
long long int i = 0; i < nLayers; i++)
572 for (
long long int j = 0; j < nCols; j++)
573 delete[] cHeadLine[i][j];
575 delete[] cHeadLine[i];
591 for (
long long int i = 0; i < nLayers; i++)
593 for (
long long int j = 0; j < nCols; j++)
594 delete[] cHeadLine[i][j];
596 delete[] cHeadLine[i];
630 for (
size_t i = 0; i < cols.
size(); i++)
673 if (sDir.find(
"every=") != string::npos && (sDir.find(
"cols") != string::npos || sDir.find(
"lines") != string::npos))
675 string sEvery =
getArgAtPos(sDir, sDir.find(
"every=")+6);
679 sDir.erase(sDir.find(
"every="));
681 if (sEvery.front() ==
'(' && sEvery.back() ==
')')
682 sEvery = sEvery.substr(1, sEvery.length()-2);
684 if (sEvery.front() ==
'{' && sEvery.back() ==
'}')
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]))
712 else if (nResults == 2)
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]))
747 vector<mu::value_type> vResults;
748 long long int nlines =
getLines(sTableName,
false);
749 long long int ncols =
getCols(sTableName,
false);
752 long long int nGridOffset = sDir.find(
"grid") != string::npos ? 2 : 0;
759 nlines = vSize.front().real();
760 ncols = vSize.back().real()+nGridOffset;
768 if (sDir.find(
"cols") != string::npos)
770 for (
size_t i = 0; i < _idx.
size(); i++)
772 if (_idx[i]+nGridOffset < 0 || _idx[i]+nGridOffset >= ncols)
775 vResults.push_back((this->*MAF)(sTableName, 0, nlines-1, _idx[i]+nGridOffset, -1));
778 else if (sDir.find(
"lines") != string::npos)
780 for (
size_t i = 0; i < _idx.
size(); i++)
782 if (_idx[i] < 0 || _idx[i] >= nlines)
785 vResults.push_back((this->*MAF)(sTableName, _idx[i], -1, nGridOffset, ncols-1));
789 vResults.push_back((this->*MAF)(sTableName, 0, nlines-1, nGridOffset, ncols-1));
791 if (!vResults.size())
792 vResults.push_back(NAN);
849 if (!
exists(target.substr(0, target.find(
'('))))
855 *targetTable = *sourceTable;
875 if (!_mem || !_mem->
memArray.size())
885 else if (overrideTarget)
896 size_t nCols = _existingMem->
memArray.size();
903 for (
size_t j = 0; j < _mem->
memArray.size(); j++)
932 if (sTableName !=
"string")
996 if (sExpression.find(
'(') == string::npos)
1000 for (
size_t i = 0; i < sExpression.length(); i++)
1003 if (sExpression[i] ==
'"' && (!i || sExpression[i-1] !=
'\\'))
1012 if (isalpha(sExpression[i]) || sExpression[i] ==
'_' || sExpression[i] ==
'~')
1014 size_t nStartPos = i;
1020 while (isalnum(sExpression[i]) || sExpression[i] ==
'_' || sExpression[i] ==
'~');
1022 if (sExpression[i] ==
'(')
1047 string sCacheName = sCache.substr(0,sCache.find(
'('));
1048 static const string sVALIDCHARACTERS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";
1052 if ((sCacheName[0] >=
'0' && sCacheName[0] <=
'9') || sCacheName[0] ==
'~' || sCacheName ==
"data" || sCacheName ==
"string" || sCacheName.find_first_not_of(sVALIDCHARACTERS) != string::npos)
1069 if (iter->first == sCacheName)
1099 if (sCache ==
"table")
1104 if (iter->first == sCache)
1106 if (
vMemory.size() > iter->second.first)
1108 delete vMemory[iter->second.first];
1116 if (iter2->second.first > iter->second.first)
1117 iter2->second.first--;
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;
1136 remove(sCachefile.c_str());
std::string getProgramPath() const
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...
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 ...
This class represents a single table in memory, or a - so to say - single memory page to be handled b...
NumeRe::TableMetaData getMetaData() const
Return the internal meta data structure.
int getCols(bool _bFull=false) const
This member function will return the number of columns, which are currently available in this table.
TableColumnArray memArray
void setSaveStatus(bool _bIsSaved)
This member function changes the saved state to the passed value.
int getElemsInColumn(size_t col) const
Returns the number of elements in the selected column (but might contain invalid values).
bool resizeMemory(size_t _nLines, size_t _nCols)
This member function will handle all memory grow operations by doubling the base size,...
void setMetaData(const NumeRe::TableMetaData &meta)
Update the internal meta data with the passed one.
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
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
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....
void readCacheHeader()
This member function will read the cache file header and ensure that the version of the file is not n...
virtual bool read() override
Pure virtual declaration of the read access method. Has to be implemented in all derived classes and ...
void setNumberOfTables(size_t nTables)
Sets the number of tables to be stored in the referenced cache file.
virtual bool write() override
Pure virtual declaration of the write access method. Has to be implemented in all derived classes and...
void writeCacheHeader()
This member function will write the standard cache file header to the cache file.
size_t getNumberOfTables()
Returns the number of tables stored in the referenced cache file.
bool containsClusters(const std::string &sCmdLine) const
This member function detects, whether any cluster is used in the current expression.
void getData(TableColumnArray *data)
This method copies the internal data to the passed memory address. The target memory must already exi...
void close()
Wrapper for fstream::close(). Will also clear the internal memory.
void setTableName(const std::string &name)
Set the table's name.
std::string getTableName()
Returns the table name referenced in the file. Will default to the file name with non-alnum character...
std::string getComment()
Returns the comment stored with the referenced file.
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...
long long int getRows()
Returns the number of rows.
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...
void setComment(const std::string &comment)
Sets the comment to be written to the referencedfile.
long long int getCols()
Returns the number of columns.
static NumeReKernel * getInstance()
This static member function returns a a pointer to the singleton instance of the kernel.
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...
This class manages the setting values of the internal (kernel) settings of this application.
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.
@ FUNCTION_ALREADY_EXISTS
static size_t invalid_position
This class abstracts all the index logics, i.e. the logical differences between single indices and in...
void setOpenEndIndex(int nLast) const
This member function can be used to replace the open end state with a defined index value although th...
size_t size() const
This member function returns the size of the indices stored in this class.
bool isOpenEnd() const
This member function determines, whether the internal index set has an open end.
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.
bool fileExists(const string &)
This function checks, whether the file with the passed file name exists.
MUP_BASETYPE value_type
The numeric datatype used by the parser.
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,...
long long int intCast(const std::complex< double > &)
Casts the real part of the complex number to an integer and avoids rounding errors.
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...