From 049a16d1b1a683487a0c17014e9f7c477820a132 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Mon, 9 Jul 2012 13:10:32 +1000 Subject: Fixed up the newer strategy iteration stuff Trivial 100000 var case in 15s on my Uni machine. --- impl/EquationSystem.hpp | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'impl/EquationSystem.hpp') diff --git a/impl/EquationSystem.hpp b/impl/EquationSystem.hpp index d09bef2..c7e5f4c 100644 --- a/impl/EquationSystem.hpp +++ b/impl/EquationSystem.hpp @@ -7,12 +7,16 @@ #include "Operator.hpp" #include "Expression.hpp" #include "VariableAssignment.hpp" +#include "IdMap.hpp" template struct MaxStrategy; template struct EquationSystem { + EquationSystem() + : _expr_to_var(NULL) { } + virtual ~EquationSystem() { for (typename std::set*>::iterator it = _expressions.begin(); it != _expressions.end(); @@ -24,6 +28,8 @@ struct EquationSystem { ++it) { delete *it; } + if (_expr_to_var) + delete _expr_to_var; } MaxExpression& maxExpression(const std::vector*>& arguments) { @@ -86,7 +92,7 @@ struct EquationSystem { StableVariableAssignment* assignment(const Domain& value) const { return new StableVariableAssignment(_variables.size(), value); } - VariableAssignment* eval(const VariableAssignment& rho) const { + StableVariableAssignment* eval(const VariableAssignment& rho) const { StableVariableAssignment* result = this->assignment(-infinity()); for (unsigned int i = 0, length = _variables.size(); i < length; @@ -97,7 +103,7 @@ struct EquationSystem { } return result; } - VariableAssignment* eval(const VariableAssignment& rho, const MaxStrategy& strat) const { + StableVariableAssignment* eval(const VariableAssignment& rho, const MaxStrategy& strat) const { StableVariableAssignment* result = this->assignment(-infinity()); for (unsigned int i = 0, length = _variables.size(); i < length; @@ -109,12 +115,28 @@ struct EquationSystem { return result; } - Variable* varFromExpr(const Expression& expr) const { + void indexMaxExpressions() { + _expr_to_var = new IdMap,Variable*>(maxExpressionCount(), NULL); for (unsigned int i = 0, length = _right_sides.size(); i < length; ++i) { - if (_right_sides[i] == &expr) - return _variables[i]; + (*_expr_to_var)[*_right_sides[i]] = _variables[i]; + } + } + + Variable* varFromExpr(const Expression& expr) const { + if (_expr_to_var) { // we've indexed: + auto* maxExpr = dynamic_cast*>(&expr); + if (maxExpr) { + return (*_expr_to_var)[*maxExpr]; + } else { + return NULL; + } + } else { + for (unsigned int i = 0, length = _right_sides.size(); i < length; ++i) { + if (_right_sides[i] == &expr) + return _variables[i]; + } + return NULL; } - return NULL; } virtual bool equalAssignments(const VariableAssignment& l, const VariableAssignment& r) const { @@ -141,6 +163,7 @@ struct EquationSystem { std::set*> _expressions; std::vector*> _variables; std::map*> _variable_names; + IdMap, Variable*>* _expr_to_var; std::vector*> _max_expressions; std::vector*> _right_sides; }; -- cgit v1.2.3