Started optimization with unordered_map hash table

This commit is contained in:
sam-astro 2022-01-03 11:51:49 -05:00 committed by GitHub
parent 2880d7e007
commit 392879fbad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,13 +10,13 @@
#include "strops.h" #include "strops.h"
#include "builtin.h" #include "builtin.h"
#include "main.h" #include "main.h"
#include <boost/any.hpp>
using namespace std; using namespace std;
using namespace boost;
vector<string> globalVariables; unordered_map<string, any> globalVariableValues;
vector<string> globalVariableValues; unordered_map<string, vector<string>> functionValues;
vector<string> functions;
vector<vector<string>> functionValues;
bool isNumber(const string& str) bool isNumber(const string& str)
{ {
@ -81,112 +81,81 @@ string RMParenthesis(string str)
return withoutParenthesis; return withoutParenthesis;
} }
string GetVariableValue(string varName, vector<string>& variables, vector<string>& variableVals) string GetVariableValue(string varName, unordered_map<string, any>& variableVals)
{ {
string typ = "string"; auto iA = variableVals.find(varName);
bool isVar = false; if (iA != variableVals.end())
if (count(varName, '\"') == 0)
{ {
// Checks against global vars return iA->second;
for (int v = 0; v < (int)globalVariables.size(); v++)
if (varName == split(globalVariables[v], ' ')[1])
{
typ = split(globalVariables[v], ' ')[0];
isVar = true;
}
// Checks against local vars
for (int v = 0; v < (int)variables.size(); v++)
if (varName == split(variables[v], ' ')[1])
{
typ = split(variables[v], ' ')[0];
isVar = true;
}
} }
else
// If it is a var
if (isVar)
{ {
// Checks against global vars auto iB = globalVariableValues.find(varName);
for (int v = 0; v < (int)globalVariables.size(); v++) if (iB != globalVariableValues.end())
if (varName == split(globalVariables[v], ' ')[1]) {
{ return iB->second;
return globalVariableValues[v]; }
} else
// Checks against local vars {
for (int v = 0; v < (int)variables.size(); v++) return varName;
if (varName == split(variables[v], ' ')[1]) }
{
return variableVals[v];
}
} }
return varName;
} }
vector<string> VarValues(vector<string> varNames, vector<string>& variables, vector<string>& variableVals) bool IsVar(string varName, unordered_map<string, any>& variableVals)
{ {
vector<string> realValues; auto iA = variableVals.find(varName);
if (iA != variableVals.end())
{
return true;
}
return false;
}
vector<string> VarValues(vector<string> varNames, unordered_map<string, any>& variableVals)
{
vector<any> realValues;
for (int varIndex = 0; varIndex < varNames.size(); varIndex++) for (int varIndex = 0; varIndex < varNames.size(); varIndex++)
{ {
varNames[varIndex] = trim(varNames[varIndex]); varNames[varIndex] = trim(varNames[varIndex]);
string typ = "string"; auto iA = variableVals.find(varNames[varIndex]);
bool isVar = false; if (iA != variableVals.end())
if (count(varNames[varIndex], '\"') == 0)
{ {
// Checks against global vars realValues.push_back(iA->second);
for (int v = 0; v < (int)globalVariables.size(); v++)
if (varNames[varIndex] == split(globalVariables[v], ' ')[1])
{
typ = split(globalVariables[v], ' ')[0];
isVar = true;
}
// Checks against local vars
for (int v = 0; v < (int)variables.size(); v++)
if (varNames[varIndex] == split(variables[v], ' ')[1])
{
typ = split(variables[v], ' ')[0];
isVar = true;
}
}
// If it is a var
if (isVar)
{
// Checks against global vars
for (int v = 0; v < (int)globalVariables.size(); v++)
if (varNames[varIndex] == split(globalVariables[v], ' ')[1])
{
realValues.push_back(globalVariableValues[v]);
}
// Checks against local vars
for (int v = 0; v < (int)variables.size(); v++)
if (varNames[varIndex] == split(variables[v], ' ')[1])
{
realValues.push_back(variableVals[v]);
}
} }
else else
realValues.push_back(varNames[varIndex]); {
auto iB = globalVariableValues.find(varNames[varIndex]);
if (iB != globalVariableValues.end())
{
realValues.push_back(iB->second);
}
else
{
realValues.push_back(varNames[varIndex]);
}
}
} }
return realValues; return realValues;
} }
int IsFunction(string funcName) string IsFunction(string funcName)
{ {
// Iterate through all functions auto iA = functionValues.find(funcName);
for (int t = 0; t < (int)functions.size(); t++) if (iA != functionValues.end())
if (trim(funcName) == split(functions[t], ' ')[0]) {
return t; return iA->first;
}
return -1; else
{
return "";
}
} }
string EvalExpression(string expression, vector<string>& variables, vector<string>& variableVals) string EvalExpression(string expression, unordered_map<string, any>& variableVals)
{ {
expression = trim(expression); expression = trim(expression);
bool inQuotes = false; bool inQuotes = false;
@ -207,7 +176,7 @@ string EvalExpression(string expression, vector<string>& variables, vector<strin
y++; y++;
} }
//cout << split(expression, '(')[0] << " " << argContents << endl; //cout << split(expression, '(')[0] << " " << argContents << endl;
string returnVal = ExecuteFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variables, variableVals), funcIndex); string returnVal = ExecuteFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableVals), funcIndex);
return returnVal; return returnVal;
} }
else if (split(expression, '.')[0] == "CPP" && !inQuotes) else if (split(expression, '.')[0] == "CPP" && !inQuotes)
@ -221,11 +190,11 @@ string EvalExpression(string expression, vector<string>& variables, vector<strin
y++; y++;
} }
//cout << split(expression, '(')[0] << " " << unWrapVec(VarValues(split(argContents, ','), variables, variableVals)) << endl; //cout << split(expression, '(')[0] << " " << unWrapVec(VarValues(split(argContents, ','), variables, variableVals)) << endl;
string returnVal = CPPFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variables, variableVals)); string returnVal = CPPFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableVals));
return returnVal; return returnVal;
} }
else else
return GetVariableValue(expression, variables, variableVals); return GetVariableValue(expression, variableVals);
} }
string newExpression = ""; string newExpression = "";
@ -247,7 +216,7 @@ string EvalExpression(string expression, vector<string>& variables, vector<strin
} }
//string varVal = GetVariableValue(name, variables, variableVals); //string varVal = GetVariableValue(name, variables, variableVals);
int funcIndex = IsFunction(name); any funcPointer = IsFunction(name);
if (funcIndex != -1 && !inQuotes) if (funcIndex != -1 && !inQuotes)
{ {
string argContents = ""; string argContents = "";
@ -258,7 +227,7 @@ string EvalExpression(string expression, vector<string>& variables, vector<strin
i++; i++;
} }
string returnVal = ExecuteFunction(name, VarValues(split(argContents, ','), variables, variableVals), funcIndex); string returnVal = ExecuteFunction(name, VarValues(split(argContents, ','), variableVals), funcIndex);
newExpression += returnVal; newExpression += returnVal;
//cout << newExpression << endl; //cout << newExpression << endl;
} }
@ -273,7 +242,7 @@ string EvalExpression(string expression, vector<string>& variables, vector<strin
y++; y++;
} }
//cout << split(expression, '(')[0] << " " << argContents << endl; //cout << split(expression, '(')[0] << " " << argContents << endl;
string returnVal = CPPFunction(split(name, '(')[0], VarValues(split(argContents, ','), variables, variableVals)); string returnVal = CPPFunction(split(name, '(')[0], VarValues(split(argContents, ',') variableVals));
newExpression += returnVal; newExpression += returnVal;
} }
else else
@ -281,7 +250,7 @@ string EvalExpression(string expression, vector<string>& variables, vector<strin
if (inQuotes) if (inQuotes)
newExpression += name; newExpression += name;
else else
newExpression += GetVariableValue(name, variables, variableVals); newExpression += GetVariableValue(name, variableVals);
} }
i--; i--;
@ -324,10 +293,10 @@ string EvalExpression(string expression, vector<string>& variables, vector<strin
return to_string(evaluate(newExpression)); return to_string(evaluate(newExpression));
} }
bool BooleanLogic(string valA, string determinant, string valB, vector<string>& variables, vector<string>& variableVals) bool BooleanLogic(string valA, string determinant, string valB, unordered_map<string, any>& variableVals)
{ {
string valARealValue = EvalExpression(valA, variables, variableVals); string valARealValue = EvalExpression(valA, variableVals);
string valBRealValue = EvalExpression(valB, variables, variableVals); string valBRealValue = EvalExpression(valB, variableVals);
if (determinant == "==") if (determinant == "==")
return valARealValue == valBRealValue; return valARealValue == valBRealValue;
@ -345,51 +314,44 @@ bool BooleanLogic(string valA, string determinant, string valB, vector<string>&
return false; return false;
} }
int evalEqu(vector<string> str, vector<string>& variables, vector<string>& variableValues) int evalEqu(vector<string> str, unordered_map<string, any>& variableValues)
{ {
// Iterate all existing local variable names if (IsVar(str[0], variableValues))
for (int v = 0; v < (int)variables.size(); v++)
{ {
if (str[0] == split(variables[v], ' ')[1]) if (str[1] == "=")
{ variableValues[str[0]] = EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues);
if (str[1] == "=") else if (str[1] == "+=")
variableValues[v] = EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variables, variableValues); variableValues[str[0]] = EvalExpression(variableValues[str[0]] + "+(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variableValues);
else if (str[1] == "+=") else if (str[1] == "-=")
variableValues[v] = EvalExpression(variableValues[v] + "+(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues); variableValues[str[0]] = EvalExpression(variableValues[str[0]] + "-(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variableValues);
else if (str[1] == "-=") else if (str[1] == "*=")
variableValues[v] = EvalExpression(variableValues[v] + "-(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues); variableValues[str[0]] = EvalExpression(variableValues[str[0]] + "*(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variableValues);
else if (str[1] == "*=") else if (str[1] == "/=")
variableValues[v] = EvalExpression(variableValues[v] + "*(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues); variableValues[str[0]] = EvalExpression(variableValues[str[0]] + "/(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variableValues);
else if (str[1] == "/=")
variableValues[v] = EvalExpression(variableValues[v] + "/(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
//cout << variables[v] << " is " << variableValues[v] << endl; //cout << variables[v] << " is " << variableValues[v] << endl;
return 0; return 0;
}
} }
// Iterate all existing global variable names else if (IsVar(str[0], globalVariableValues))
for (int v = 0; v < (int)globalVariables.size(); v++)
{ {
if (str[0] == split(globalVariables[v], ' ')[1]) if (str[1] == "=")
{ globalVariableValues[str[0]] = EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues);
if (str[1] == "=") else if (str[1] == "+=")
globalVariableValues[v] = EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variables, variableValues); globalVariableValues[str[0]] = EvalExpression(variableValues[str[0]] + "+(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variableValues);
else if (str[1] == "+=") else if (str[1] == "-=")
globalVariableValues[v] = EvalExpression(variableValues[v] + "+(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues); globalVariableValues[str[0]] = EvalExpression(variableValues[str[0]] + "-(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variableValues);
else if (str[1] == "-=") else if (str[1] == "*=")
globalVariableValues[v] = EvalExpression(variableValues[v] + "-(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues); globalVariableValues[str[0]] = EvalExpression(variableValues[str[0]] + "*(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variableValues);
else if (str[1] == "*=") else if (str[1] == "/=")
globalVariableValues[v] = EvalExpression(variableValues[v] + "*(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues); globalVariableValues[str[0]] = EvalExpression(variableValues[str[0]] + "/(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variableValues);
else if (str[1] == "/=")
globalVariableValues[v] = EvalExpression(variableValues[v] + "/(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
//cout << variables[v] << " is " << variableValues[v] << endl; //cout << variables[v] << " is " << variableValues[v] << endl;
return 0; return 0;
}
} }
return 1;
} }
string ProcessLine(vector<vector<string>> words, int lineNum, vector<string>& variables, vector<string>& variableValues) string ProcessLine(vector<vector<string>> words, int lineNum, unordered_map<string, any>& variableValues)
{ {
if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/') if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/')
return ""; return "";
@ -595,8 +557,7 @@ string ExecuteFunction(string functionName, vector<string> inputVarVals, int fun
else else
functionLines = functionValues[functionIndex]; functionLines = functionValues[functionIndex];
vector<string> variables; unordered_map<string, any> variableValues;
vector<string> variableValues;
vector<string> functionNameParts = split(replace(functions[functionIndex], functionName + " ", ""), ','); vector<string> functionNameParts = split(replace(functions[functionIndex], functionName + " ", ""), ',');
for (int i = 0; i < (int)inputVarVals.size(); i++) for (int i = 0; i < (int)inputVarVals.size(); i++)
{ {