20#include "../../kernel.hpp"
21#include "../structures.hpp"
22#define DEFAULT_NUM_ARG INT_MIN
24#define NEWSTRING (char)23
45 if (sString.find(
'"') == string::npos && sString.find(
NEWSTRING) == string::npos && sString.back() !=
'\\')
47 string sRet = sString;
51 for (
size_t i = 1; i < sRet.length() - 1; i++)
54 if (sRet[i] ==
'\\' && sRet[i + 1] !=
'"' && sRet[i + 1] !=
' ')
56 sRet.insert(i + 1,
" ");
61 if (sRet[i] ==
'"' && sRet[i - 1] !=
'\\' && sRet[i + 1] !=
NEWSTRING && sRet.find(
'"', i + 1) != string::npos)
71 if (sRet[i + 1] ==
'"')
106 size_t nStartPosition = 0;
107 size_t nEndPosition = 0;
110 while ((nStartPosition =
findNextFunction(sFuncName, sLine, nStartPosition, nEndPosition)) != string::npos)
114 std::vector<s_vect> vReturnValues;
117 bool bLogicalOnly =
false;
120 s_vect sStringArg1, sStringArg2, sStringArg3;
121 n_vect nIntArg1, nIntArg2;
139 nMaxArgs =
argumentParser(sFunctionArgument, sStringArg1, bLogicalOnly);
148 && (sFuncName ==
"min(" || sFuncName ==
"max(" || sFuncName ==
"cnt(" || sFuncName ==
"num(" || sFuncName ==
"sum("))
155 nMaxArgs =
argumentParser(sFunctionArgument, sStringArg1, dValArg);
157 nMaxArgs =
argumentParser(sFunctionArgument, sStringArg1, nIntArg1, nIntArg2);
159 nMaxArgs =
argumentParser(sFunctionArgument, sStringArg1, nIntArg1, nIntArg2, sStringArg2);
161 nMaxArgs =
argumentParser(sFunctionArgument, sStringArg1, sStringArg2, nIntArg1, nIntArg2);
163 nMaxArgs =
argumentParser(sFunctionArgument, sStringArg1, sStringArg2, sStringArg3, nIntArg1, nIntArg2);
175 vReturnValues.push_back(funcHandle.
fHandle(stringArgs));
179 nMaxArgs =
max(
max(
max(
max(sStringArg2.size(), sStringArg3.size()), nIntArg1.size()), nIntArg2.size()), 1u);
182 vReturnValues =
callMultiFunction(funcHandle, sStringArg1, sStringArg2, sStringArg3, nIntArg1, nIntArg2, dValArg, nMaxArgs);
184 vReturnValues =
callMultiFunctionParallel(funcHandle, sStringArg1, sStringArg2, sStringArg3, nIntArg1, nIntArg2, dValArg, nMaxArgs);
190 vReturnValues =
callFunction(funcHandle, sStringArg1, sStringArg2, sStringArg3, nIntArg1, nIntArg2, dValArg, nMaxArgs);
192 vReturnValues =
callFunctionParallel(funcHandle, sStringArg1, sStringArg2, sStringArg3, nIntArg1, nIntArg2, dValArg, nMaxArgs);
199 sLine.replace(nStartPosition, nEndPosition + 1 - nStartPosition, sFuncReturnValue);
225 std::string sFuncArgument = __sFuncArgument.
to_string();
246 for (
size_t i = 0; i < strRes.
vResult.size(); i++)
249 v = _parser.
Eval(nReturn);
251 for (
int n = 0; n < nReturn; n++)
263 _parser.
SetExpr(sFuncArgument);
264 v = _parser.
Eval(nReturn);
266 for (
int i = 0; i < nReturn; i++)
271 return (
size_t)nReturn;
294 std::string sFuncArgument = __sFuncArgument.
to_string();
314 for (
size_t i = 0; i < strRes.
vResult.size(); i++)
317 v = _parser.
Eval(nReturn);
318 dArg.insert(dArg.end(), v, v+nReturn);
329 _parser.
SetExpr(sFuncArgument);
330 v = _parser.
Eval(nReturn);
331 dArg.insert(dArg.end(), v, v+nReturn);
333 return (
size_t)nReturn;
355 size_t nMaxLength = 0;
375 if (nMaxLength < nReturn)
376 nMaxLength = nReturn;
403 std::string sFuncArgument = __sFuncArgument.
to_string();
437 if (sFuncArgument.find(
'{') != string::npos || sFuncArgument.find(
',') != string::npos)
443 while (sFuncArgument.length())
469 size_t nMaxLength = 0;
470 bool bLogicalOnly =
false;
490 if (nMaxLength < nReturn)
491 nMaxLength = nReturn;
512 size_t nMaxLength = 0;
513 bool bLogicalOnly =
false;
534 if (nMaxLength < nReturn)
535 nMaxLength = nReturn;
547 if (nMaxLength < nReturn)
548 nMaxLength = nReturn;
572 size_t nMaxLength = 0;
573 bool bLogicalOnly =
false;
595 if (nMaxLength < nReturn)
596 nMaxLength = nReturn;
608 if (nMaxLength < nReturn)
609 nMaxLength = nReturn;
621 if (nMaxLength < nReturn)
622 nMaxLength = nReturn;
645 size_t nMaxLength = 0;
646 bool bLogicalOnly =
false;
668 if (nMaxLength < nReturn)
669 nMaxLength = nReturn;
681 if (nMaxLength < nReturn)
682 nMaxLength = nReturn;
694 if (nMaxLength < nReturn)
695 nMaxLength = nReturn;
719 size_t nMaxLength = 0;
720 bool bLogicalOnly =
false;
743 if (nMaxLength < nReturn)
744 nMaxLength = nReturn;
756 if (nMaxLength < nReturn)
757 nMaxLength = nReturn;
769 if (nMaxLength < nReturn)
770 nMaxLength = nReturn;
782 if (nMaxLength < nReturn)
783 nMaxLength = nReturn;
813 return std::vector<s_vect>(1, funcHandle.
fHandle(stringArgs));
815 std::vector<s_vect> vReturnValues(nMaxArgs);
817 for (
size_t i = 0; i < nMaxArgs; i++)
823 if (i < nIntArg1.size())
824 stringArgs.
nArg1 = nIntArg1[i];
825 else if (nIntArg1.size() == 1)
826 stringArgs.
nArg1 = nIntArg1[0];
830 if (i < nIntArg2.size())
831 stringArgs.
nArg2 = nIntArg2[i];
832 else if (nIntArg2.size() == 1)
833 stringArgs.
nArg2 = nIntArg2[0];
837 if (i < dValArg.size())
838 stringArgs.
dArg1 = dValArg[i];
839 else if (dValArg.size() == 1)
840 stringArgs.
dArg1 = dValArg[0];
842 stringArgs.
dArg1 = 0.0;
844 vReturnValues[i] = funcHandle.
fHandle(stringArgs);
847 return vReturnValues;
869 std::vector<s_vect> vReturnValues(nMaxArgs);
874 #pragma omp parallel for firstprivate(stringArgs)
875 for (
size_t i = 0; i < nMaxArgs; i++)
881 if (i < nIntArg1.size())
882 stringArgs.
nArg1 = nIntArg1[i];
883 else if (nIntArg1.size() == 1)
884 stringArgs.
nArg1 = nIntArg1[0];
888 if (i < nIntArg2.size())
889 stringArgs.
nArg2 = nIntArg2[i];
890 else if (nIntArg2.size() == 1)
891 stringArgs.
nArg2 = nIntArg2[0];
895 if (i < dValArg.size())
896 stringArgs.
dArg1 = dValArg[i];
897 else if (dValArg.size() == 1)
898 stringArgs.
dArg1 = dValArg[0];
900 stringArgs.
dArg1 = 0.0;
902 vReturnValues[i] = funcHandle.
fHandle(stringArgs);
905 return vReturnValues;
928 std::vector<s_vect> vReturnValues(nMaxArgs);
934 for (
size_t i = 0; i < nMaxArgs; i++)
939 if (i < nIntArg1.size())
940 stringArgs.
nArg1 = nIntArg1[i];
941 else if (nIntArg1.size() == 1)
942 stringArgs.
nArg1 = nIntArg1[0];
946 if (i < nIntArg2.size())
947 stringArgs.
nArg2 = nIntArg2[i];
948 else if (nIntArg2.size() == 1)
949 stringArgs.
nArg2 = nIntArg2[0];
953 if (i < dValArg.size())
954 stringArgs.
dArg1 = dValArg[i];
955 else if (dValArg.size() == 1)
956 stringArgs.
dArg1 = dValArg[0];
958 stringArgs.
dArg1 = 0.0;
960 vReturnValues[i] = funcHandle.
fHandle(stringArgs);
963 return vReturnValues;
986 std::vector<s_vect> vReturnValues(nMaxArgs);
992 #pragma omp parallel for firstprivate(stringArgs)
993 for (
size_t i = 0; i < nMaxArgs; i++)
998 if (i < nIntArg1.size())
999 stringArgs.
nArg1 = nIntArg1[i];
1000 else if (nIntArg1.size() == 1)
1001 stringArgs.
nArg1 = nIntArg1[0];
1005 if (i < nIntArg2.size())
1006 stringArgs.
nArg2 = nIntArg2[i];
1007 else if (nIntArg2.size() == 1)
1008 stringArgs.
nArg2 = nIntArg2[0];
1012 if (i < dValArg.size())
1013 stringArgs.
dArg1 = dValArg[i];
1014 else if (dValArg.size() == 1)
1015 stringArgs.
dArg1 = dValArg[0];
1017 stringArgs.
dArg1 = 0.0;
1019 vReturnValues[i] = funcHandle.
fHandle(stringArgs);
1022 return vReturnValues;
1041 size_t nStartPosition = 0;
1042 size_t nEndPosition;
1045 while ((nStartPosition =
findNextFunction(
"string_cast(", sLine, nStartPosition, nEndPosition)) != string::npos)
1049 if (sToString.find(
'"') != string::npos || sToString.find(
'#') != string::npos)
1059 sToString =
"\"" + sToString +
"\"";
1061 sLine = sLine.substr(0, nStartPosition) + sToString + sLine.substr(nEndPosition + 1);
1068 while ((nStartPosition =
findNextFunction(
"to_cmd(", sLine, nStartPosition, nEndPosition)) != string::npos)
1081 sCmdString = strRes.
vResult.front();
1088 sLine = sLine.substr(0, nStartPosition) +
removeQuotationMarks(sCmdString) + sLine.substr(nEndPosition + 1);
1094 while ((nStartPosition =
findNextFunction(
"to_value(", sLine, nStartPosition, nEndPosition)) != string::npos)
1106 vector<string> vToValueResults;
1109 for (
size_t i = 0; i < strRes.
vResult.size(); i++)
1111 std::string sComponent = strRes.
vResult[i].to_string();
1113 while (sComponent.length())
1127 sLine = sLine.substr(0, nStartPosition) + sToValue + sLine.substr(nEndPosition + 1);
1133 while ((nStartPosition =
findNextFunction(
"is_string(", sLine, nStartPosition, nEndPosition)) != string::npos)
1145 _parser.
Eval(nResults);
1146 std::vector<std::string> vIsString(nResults,
"false");
1156 std::vector<std::string> vIsString;
1158 for (
size_t i = 0; i < strRes.
vResult.size(); i++)
1166 sLine = sLine.substr(0, nStartPosition) + sArgument + sLine.substr(nEndPosition + 1);
1167 nStartPosition += sArgument.length();
1172 while ((nStartPosition =
findNextFunction(
"getindices(", sLine, nStartPosition, nEndPosition)) != string::npos)
1181 _sObject = strRes.
vResult[0].to_string();
1184 if (strRes.
vResult.size() > 1)
1186 std::string sType = strRes.
vResult[1].to_string();
1199 sType = res.
vResult[0].to_string();
1205 if (nType < -1 || nType > 2)
1218 sLine = sLine.substr(0, nStartPosition) +
"nan" + sLine.substr(nEndPosition + 1);
1276 vector<mu::value_type> vIndices;
1282 sLine = sLine.substr(0, nStartPosition) +
"_~indices[" +
replaceToVectorname(_sObject) +
"]" + sLine.substr(nEndPosition + 1);
1289 while ((nStartPosition =
findNextFunction(
"is_data(", sLine, nStartPosition, nEndPosition)) != string::npos)
1301 sData = strRes.
vResult[0].to_string();
1307 sLine = sLine.substr(0, nStartPosition) +
"true" + sLine.substr(nEndPosition + 1);
1309 sLine = sLine.substr(0, nStartPosition) +
"false" + sLine.substr(nEndPosition + 1);
1316 while ((nStartPosition =
findNextFunction(
"is_table(", sLine, nStartPosition, nEndPosition)) != string::npos)
1328 sData = strRes.
vResult[0].to_string();
1334 sLine = sLine.substr(0, nStartPosition) +
"true" + sLine.substr(nEndPosition + 1);
1336 sLine = sLine.substr(0, nStartPosition) +
"false" + sLine.substr(nEndPosition + 1);
1343 while ((nStartPosition =
findNextFunction(
"is_cluster(", sLine, nStartPosition, nEndPosition)) != string::npos)
1355 sData = strRes.
vResult[0].to_string();
1361 sLine = sLine.substr(0, nStartPosition) +
"true" + sLine.substr(nEndPosition + 1);
1363 sLine = sLine.substr(0, nStartPosition) +
"false" + sLine.substr(nEndPosition + 1);
1370 while ((nStartPosition =
findNextFunction(
"findcolumn(", sLine, nStartPosition, nEndPosition)) != string::npos)
1383 sData = strRes.
vResult[0].to_string();
1384 sHeadline = strRes.
vResult[1].to_string();
1387 if (!sHeadline.length())
1395 sData.erase(sData.find(
"("));
1398 for (
long long int i = 0; i < _data.
getCols(sData,
false); i++)
1402 if (sResult.length())
1409 if (!sResult.length())
1412 if (sResult.find(
',') != string::npos)
1413 sResult =
"{" + sResult +
"}";
1415 sLine = sLine.substr(0, nStartPosition) + sResult + sLine.substr(nEndPosition + 1);
1418 sLine = sLine.substr(0, nStartPosition) +
"nan" + sLine.substr(nEndPosition + 1);
1425 while ((nStartPosition =
findNextFunction(
"valtostr(", sLine, nStartPosition, nEndPosition)) != string::npos)
1430 std::vector<mu::value_type> vCounts;
1432 if (sToString.length())
1444 sChar = strRes.
vResult[0].to_string();
1457 v = _parser.
Eval(nCount);
1458 vCounts.assign(v, v+nCount);
1471 v = _parser.
Eval(nResults);
1472 vector<string> vToString;
1473 string sElement =
"";
1476 for (
int n = 0; n < nResults; n++)
1480 if (n < (
int)vCounts.size())
1481 nLen =
intCast(fabs(vCounts[n]));
1483 while (sElement.length() < nLen && sChar.length())
1484 sElement.insert(0, sChar);
1486 vToString.push_back(
"\"" + sElement +
"\"");
1499 for (
size_t i = 0; i < strRes.
vResult.size(); i++)
1501 if (i < vCounts.size())
1502 nLen =
intCast(fabs(vCounts[i]));
1504 while (strRes.
vResult[i].length() < nLen && sChar.length())
1514 sLine = sLine.substr(0, nStartPosition) + sToString + sLine.substr(nEndPosition + 1);
1539 if (sLine.find(iter->first +
"(") != string::npos)
1543 if (sLine.find(
'(') == string::npos)
1584 while ((nStartPos = sLine.
find(sFunc, nStartPos)) != string::npos)
1587 if ((nStartPos && !(
isDelimiter(sLine[nStartPos-1]) || sLine[nStartPos-1] ==
'~'))
1600 nEndPosition += nStartPos + sFunc.length() - 1;
1603 if (searchForMethods && nEndPosition+1 < sLine.
length() && sLine[nEndPosition+1] ==
'.')
1605 for (
size_t i = nEndPosition+1; i < sLine.
length(); i++)
1607 if (sLine[i] ==
'(' || sLine[i] ==
'{')
1609 else if (sLine[i] ==
')' || sLine[i] ==
'}')
1630 return string::npos;
1648 return sLine.
subview(nStartPosition + sFunc.length(),
1649 nEndPosition - nStartPosition - sFunc.length());
1665 if (value.imag() == 0.0)
1667 if (fabs(
rint(value.real()) - value.real()) < 1e-14 && fabs(value.real()) >= 1.0)
1670 else if (value.real() == 0.0)
1672 if (fabs(
rint(value.imag()) - value.imag()) < 1e-14 && fabs(value.imag()) >= 1.0)
This class is defined to abstrahize the determination of the correct data object and the calculation ...
Indices & getIndices()
Returns a reference to the stored indices.
bool isCluster() const
Determines, whether the data access references a cluster.
std::string & getDataObject()
Returns a reference to the data object identifier.
This class represents the central memory managing instance. It will handle all tables and clusters,...
int getLines(StringView sTable, bool _bFull=false) const
bool isTable(const std::string &sTable) const
This member function returns, whether the passed table name corresponds to a known table.
std::string getHeadLineElement(int _i, const std::string &_sTable) const
bool containsTablesOrClusters(const std::string &sCmdLine)
This member function evaluates, whether the passed command line contains tables or clusters.
int getCols(StringView sTable, bool _bFull=false) const
size_t size() const
This member function returns the size of the internal memory buffer as items.
bool containsClusters(const std::string &sCmdLine) const
This member function detects, whether any cluster is used in the current expression.
Cluster & getCluster(StringView sCluster)
This member function returns a reference to the cluster indicated by the passed cluster identifier.
bool isCluster(StringView sCluster) const
This member function returns true, if the passed cluster identifier can be found in the internal map.
std::string applyStringFuncs(std::string sLine)
This member function searches for an occurence of a known string function in the passed command line ...
std::string applySpecialStringFuncs(std::string sLine)
This member function applies special string functions in the expression, which cannot be implemented ...
size_t findNextFunction(const std::string &sFunc, StringView sLine, size_t nStartPos, size_t &nEndPosition, bool searchForMethods=false)
Finds the position of the next function occurence in the passed string including the position of the ...
virtual bool isStringExpression(const std::string &sExpression)=0
virtual StringResult eval(std::string &sLine, std::string sCache, bool bParseNumericals=true)=0
std::vector< s_vect > callMultiFunction(StringFuncHandle, s_vect &, s_vect &, s_vect &, n_vect &, n_vect &, d_vect &, size_t)
Calls the selected string function accepting multiple values as first argument using the passed strin...
void evalFunction(std::string &sLine, const std::string &sFuncName, StringFuncHandle)
This member function evaluates the passed call sequence to the string function.
StringView getFunctionArgumentList(const std::string &sFunc, StringView sLine, size_t nStartPosition, size_t nEndPosition)
Returns the contents of the argument parentheses of the function starting at nStartPosition.
std::vector< s_vect > callFunctionParallel(StringFuncHandle, s_vect &, s_vect &, s_vect &, n_vect &, n_vect &, d_vect &, size_t)
Calls the selected string function using the passed string function arguments parallel using OpenMP.
std::string addMaskedStrings(const std::string &sString)
This member function adds escape characters into the passed string and transforms the newstring chara...
void declareStringFuncs(const std::map< std::string, StringFuncHandle > &mStringFuncs)
This member function is used to fill the internal map of declared string functions with the data of t...
size_t argumentParser(StringView, n_vect &)
This member function is the string function argument parser for numerical arguments.
std::vector< s_vect > callFunction(StringFuncHandle, s_vect &, s_vect &, s_vect &, n_vect &, n_vect &, d_vect &, size_t)
Calls the selected string function using the passed string function arguments sequentially.
std::map< std::string, StringFuncHandle > m_mStringFuncs
std::vector< s_vect > callMultiFunctionParallel(StringFuncHandle, s_vect &, s_vect &, s_vect &, n_vect &, n_vect &, d_vect &, size_t)
Calls the selected string function accepting multiple values as first argument using the passed strin...
std::string printValue(const mu::value_type &value)
Prints a value to a string respecting possible integer optimizations.
StringVector expandStringVectorComponents(std::vector< StringVector > &vStringVector)
This member function expands the internal multi-expressions in the veector into separate components b...
std::string createStringVectorVar(const std::vector< std::string > &vStringVector)
This member function is used to create a string vector variable.
static NumeReKernel * getInstance()
This static member function returns a a pointer to the singleton instance of the kernel.
MemoryManager & getMemoryManager()
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 an extension to the std::vector<std::string> to provide the vector-like functionalities...
void push_back(const std::string &sStr)
Append a string to the end of this vector. Will be stored as local string.
std::string & getRef(size_t i)
Return a reference to the i-th element in this string.
void convert_to_string(size_t i, size_t minChars=0)
Convert the i-th element to a string.
void push_generic(const std::string &sStr)
Append a generic string value to the end of this vector. Depending on the existence of surrounding qu...
bool is_string(size_t i) const
Check whether the i-th element represents a string or a numeric value.
StringArg getArg(size_t i) const
Create a StringArg instance from the i-th or the 1st element, if this vector is a singleton.
std::string to_string() const
This member function returns a copy of the viewed section of the string (via std::string::substr)....
size_t find(const std::string &findstr, size_t pos=0) const
Wrapper member function for std::string::find()
size_t length() const
This member function simply returns the length of the viewed section.
This class is the immutable (const) version of a string view. It can be constructed from a MutableStr...
StringView subview(size_t pos=0, size_t len=std::string::npos) const
This member function creates a new StringView class instance using the selected position and length a...
Common exception class for all exceptions thrown in NumeRe.
static size_t invalid_position
void linearize()
This member function linearizes the contents of a vector-described index set. The vectorial informati...
int last() const
This member function returns the last index value, which can be reached by the values stored internal...
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 setRange(int nMin, int nMax)
This member function can be used to force the indices stored internally to be in a defined interval....
int & back()
This member function returns a reference to the final index value stored internally.
int & front()
This member function returns a reference to the first index value stored internally.
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.
void SetVectorVar(const std::string &sVarName, const std::vector< mu::value_type > &vVar, bool bAddVectorType=false)
This member function copies the passed vector into the internal storage referencing it with the passe...
Mathematical expressions parser.
string getDataElements(string &sLine, Parser &_parser, MemoryManager &_data, const Settings &_option, int options)
Searches the passed string for calls to any table or cluster and replaces them with internal vectors ...
bool isValidIndexSet(const Indices &_idx)
unsigned int getMatchingParenthesis(const StringView &)
Returns the position of the closing parenthesis.
MUP_BASETYPE value_type
The numeric datatype used by the parser.
value_type rint(value_type v)
void convertVectorToExpression(string &sLine, const Settings &_option)
This function replaces vector expressions with their corresponding multi- expression equation.
void StripSpaces(std::string &)
Removes leading and trailing white spaces and tabulator characters.
std::vector< long long int > n_vect
Simple abbreviation.
@ PARSER_STRING_INT_INT_STRING
@ PARSER_STRING_STRING_STRING_INT_INT
@ PARSER_STRING_STRING_INT_INT
std::vector< mu::value_type > d_vect
Simple abbreviation.
string addQuotationMarks(const string &sString)
This function simply adds the surrounding quotation marks.
string removeQuotationMarks(const string &sString)
This function simply removes the surrounding quotation marks.
This structure combines all string function's arguments into a single structure to align all string f...
This structure defines the internal string function signature. It contains the pointer to the actual ...
FunctionSignatureType fType
bool bTakesMultiArguments
This structure contains all possible return values of the central string parser in single combined st...
std::vector< mu::value_type > vNumericalValues
long long int intCast(const std::complex< double > &)
Casts the real part of the complex number to an integer and avoids rounding errors.
std::string toString(int)
Converts an integer to a string without the Settings bloat.