#ifndef EQUATION_SYSTEM_H #define EQUATION_SYSTEM_H #include #include #include "Expression.hpp" template struct EquationSystem { void add(std::string k, Expression v) { equations.insert(std::pair >(k, v)); } unsigned int count() const { return equations.size(); } /** init is the value where it STARTS, it's working the other way if there's no stability then it ends with -init */ std::map solveBF(const T& init) const { std::map solutions; for (typename std::map >::const_iterator it = equations.begin(); it != equations.end(); ++it){ solutions[it->first] = init; } int num = equations.size(); for (int i = 0; i < num; ++i) { solutions = eval(solutions, false, -init); } solutions = eval(solutions, true, -init); for (int i = 0; i < num-1; ++i) { solutions = eval(solutions, false, -init); } return solutions; } EquationSystem maxStrategy(const std::map& assignment) { EquationSystem result; for (typename std::map >::const_iterator it = equations.begin(); it != equations.end(); ++it) { result.add(it->first, it->second.maxStrategyEager(assignment)); } return result; } std::string output() const { std::string output = ""; for (typename std::map >::const_iterator it = equations.begin(); it != equations.end(); ++it) { output += it->first + " = " + it->second.output() + "\n"; } return output; } private: std::map eval(const std::map& assignment, bool infinity, const T& infValue) const { std::map result; for (typename std::map >::const_iterator it = equations.begin(); it != equations.end(); ++it) { T val = it->second.eval(assignment); if (infinity) { typename std::map::const_iterator oldEntry = assignment.find(it->first); result[it->first] = (oldEntry->second == val ? val : infValue); } else { result[it->first] = val; } } return result; } std::map > equations; }; #endif