From a9de4273bf8377aeab7d3f76a6d8bc0c89b42f79 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Fri, 20 Apr 2012 01:19:54 +1000 Subject: Start on the max-strategy stuff. Also more BF. --- impl/EquationSystem.hpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 impl/EquationSystem.hpp (limited to 'impl/EquationSystem.hpp') diff --git a/impl/EquationSystem.hpp b/impl/EquationSystem.hpp new file mode 100644 index 0000000..efd34b3 --- /dev/null +++ b/impl/EquationSystem.hpp @@ -0,0 +1,64 @@ +#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)); + } + /** + 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; + } + + 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 -- cgit v1.2.3