From 392879fbad6d620349fc46e12494465dccc6452e Mon Sep 17 00:00:00 2001 From: sam-astro <77079540+sam-astro@users.noreply.github.com> Date: Mon, 3 Jan 2022 11:51:49 -0500 Subject: [PATCH] Started optimization with unordered_map hash table --- Slang/Main.cpp | 231 ++++++++++++++++++++----------------------------- 1 file changed, 96 insertions(+), 135 deletions(-) diff --git a/Slang/Main.cpp b/Slang/Main.cpp index 968dbb7..f392206 100644 --- a/Slang/Main.cpp +++ b/Slang/Main.cpp @@ -10,13 +10,13 @@ #include "strops.h" #include "builtin.h" #include "main.h" +#include using namespace std; +using namespace boost; -vector globalVariables; -vector globalVariableValues; -vector functions; -vector> functionValues; +unordered_map globalVariableValues; +unordered_map> functionValues; bool isNumber(const string& str) { @@ -81,112 +81,81 @@ string RMParenthesis(string str) return withoutParenthesis; } -string GetVariableValue(string varName, vector& variables, vector& variableVals) +string GetVariableValue(string varName, unordered_map& variableVals) { - string typ = "string"; - bool isVar = false; - - if (count(varName, '\"') == 0) + auto iA = variableVals.find(varName); + if (iA != variableVals.end()) { - // Checks against global vars - 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; - } + return iA->second; } - - // If it is a var - if (isVar) + else { - // Checks against global vars - for (int v = 0; v < (int)globalVariables.size(); v++) - if (varName == split(globalVariables[v], ' ')[1]) - { - return globalVariableValues[v]; - } - // Checks against local vars - for (int v = 0; v < (int)variables.size(); v++) - if (varName == split(variables[v], ' ')[1]) - { - return variableVals[v]; - } + auto iB = globalVariableValues.find(varName); + if (iB != globalVariableValues.end()) + { + return iB->second; + } + else + { + return varName; + } } - - return varName; } -vector VarValues(vector varNames, vector& variables, vector& variableVals) +bool IsVar(string varName, unordered_map& variableVals) { - vector realValues; + auto iA = variableVals.find(varName); + if (iA != variableVals.end()) + { + return true; + } + return false; +} + +vector VarValues(vector varNames, unordered_map& variableVals) +{ + vector realValues; for (int varIndex = 0; varIndex < varNames.size(); varIndex++) { varNames[varIndex] = trim(varNames[varIndex]); - string typ = "string"; - bool isVar = false; - - if (count(varNames[varIndex], '\"') == 0) + auto iA = variableVals.find(varNames[varIndex]); + if (iA != variableVals.end()) { - // Checks against global vars - 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]); - } + realValues.push_back(iA->second); } 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; } -int IsFunction(string funcName) +string IsFunction(string funcName) { - // Iterate through all functions - for (int t = 0; t < (int)functions.size(); t++) - if (trim(funcName) == split(functions[t], ' ')[0]) - return t; - - return -1; + auto iA = functionValues.find(funcName); + if (iA != functionValues.end()) + { + return iA->first; + } + else + { + return ""; + } } -string EvalExpression(string expression, vector& variables, vector& variableVals) +string EvalExpression(string expression, unordered_map& variableVals) { expression = trim(expression); bool inQuotes = false; @@ -207,7 +176,7 @@ string EvalExpression(string expression, vector& variables, vector& variables, vector& variables, vector& variables, vector& variables, vector& variables, vector& variables, vector& variables, vector& variableVals) +bool BooleanLogic(string valA, string determinant, string valB, unordered_map& variableVals) { - string valARealValue = EvalExpression(valA, variables, variableVals); - string valBRealValue = EvalExpression(valB, variables, variableVals); + string valARealValue = EvalExpression(valA, variableVals); + string valBRealValue = EvalExpression(valB, variableVals); if (determinant == "==") return valARealValue == valBRealValue; @@ -345,51 +314,44 @@ bool BooleanLogic(string valA, string determinant, string valB, vector& return false; } -int evalEqu(vector str, vector& variables, vector& variableValues) +int evalEqu(vector str, unordered_map& variableValues) { - // Iterate all existing local variable names - for (int v = 0; v < (int)variables.size(); v++) + if (IsVar(str[0], variableValues)) { - if (str[0] == split(variables[v], ' ')[1]) - { - if (str[1] == "=") - variableValues[v] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variables, variableValues); - else if (str[1] == "+=") - variableValues[v] = EvalExpression(variableValues[v] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); - else if (str[1] == "-=") - variableValues[v] = EvalExpression(variableValues[v] + "-(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); - else if (str[1] == "*=") - variableValues[v] = EvalExpression(variableValues[v] + "*(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); - else if (str[1] == "/=") - variableValues[v] = EvalExpression(variableValues[v] + "/(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); + if (str[1] == "=") + variableValues[str[0]] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); + else if (str[1] == "+=") + variableValues[str[0]] = EvalExpression(variableValues[str[0]] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + else if (str[1] == "-=") + variableValues[str[0]] = EvalExpression(variableValues[str[0]] + "-(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + else if (str[1] == "*=") + variableValues[str[0]] = EvalExpression(variableValues[str[0]] + "*(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + else if (str[1] == "/=") + variableValues[str[0]] = EvalExpression(variableValues[str[0]] + "/(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); - //cout << variables[v] << " is " << variableValues[v] << endl; - return 0; - } + //cout << variables[v] << " is " << variableValues[v] << endl; + return 0; } - // Iterate all existing global variable names - for (int v = 0; v < (int)globalVariables.size(); v++) + else if (IsVar(str[0], globalVariableValues)) { - if (str[0] == split(globalVariables[v], ' ')[1]) - { - if (str[1] == "=") - globalVariableValues[v] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variables, variableValues); - else if (str[1] == "+=") - globalVariableValues[v] = EvalExpression(variableValues[v] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); - else if (str[1] == "-=") - globalVariableValues[v] = EvalExpression(variableValues[v] + "-(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); - else if (str[1] == "*=") - globalVariableValues[v] = EvalExpression(variableValues[v] + "*(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); - else if (str[1] == "/=") - globalVariableValues[v] = EvalExpression(variableValues[v] + "/(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variables, variableValues); + if (str[1] == "=") + globalVariableValues[str[0]] = EvalExpression(unWrapVec(vector(str.begin() + 2, str.end())), variableValues); + else if (str[1] == "+=") + globalVariableValues[str[0]] = EvalExpression(variableValues[str[0]] + "+(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + else if (str[1] == "-=") + globalVariableValues[str[0]] = EvalExpression(variableValues[str[0]] + "-(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + else if (str[1] == "*=") + globalVariableValues[str[0]] = EvalExpression(variableValues[str[0]] + "*(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); + else if (str[1] == "/=") + globalVariableValues[str[0]] = EvalExpression(variableValues[str[0]] + "/(" + unWrapVec(vector(str.begin() + 2, str.end())) + ")", variableValues); - //cout << variables[v] << " is " << variableValues[v] << endl; - return 0; - } + //cout << variables[v] << " is " << variableValues[v] << endl; + return 0; } + return 1; } -string ProcessLine(vector> words, int lineNum, vector& variables, vector& variableValues) +string ProcessLine(vector> words, int lineNum, unordered_map& variableValues) { if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/') return ""; @@ -595,8 +557,7 @@ string ExecuteFunction(string functionName, vector inputVarVals, int fun else functionLines = functionValues[functionIndex]; - vector variables; - vector variableValues; + unordered_map variableValues; vector functionNameParts = split(replace(functions[functionIndex], functionName + " ", ""), ','); for (int i = 0; i < (int)inputVarVals.size(); i++) { @@ -716,4 +677,4 @@ int main(int argc, char* argv[]) parseSlang(scriptString.str()); return 0; -} \ No newline at end of file +}