#ifndef MAX_EXPRESSION_HPP #define MAX_EXPRESSION_HPP #include #include "Expression.hpp" #include "EquationSystem.hpp" #include "IdSet.hpp" template struct MaxStrategy { virtual ~MaxStrategy() { } virtual unsigned int get(const MaxExpression& e) const = 0; }; unsigned int stack_depth = 1; std::string indent() { std::string result = ""; for (unsigned int i = 0; i < stack_depth; ++i) { result += '\t'; } return result; } #include "VariableAssignment.hpp" template struct DynamicVariableAssignment; template struct DynamicMaxStrategy : public MaxStrategy { DynamicMaxStrategy( const EquationSystem& system ) : _system(system), _rho(NULL), _values(system.maxExpressionCount(), 0), _stable(system.maxExpressionCount()), _influence(system.maxExpressionCount(), IdSet >(system.maxExpressionCount())), _var_influence(system.variableCount(), IdSet >(system.maxExpressionCount())) {} void setRho(DynamicVariableAssignment& rho) { _rho = ρ } unsigned int get(const MaxExpression& e) const { solve(e); return _values[e]; } void invalidate(const Variable& v) const { log::strategy << indent() << "Invalidating " << v << " - " << *_system[v] << std::endl; _stable.filter(_var_influence[v]); for (typename IdSet >::iterator it = _var_influence[v].begin(), end = _var_influence[v].end(); it != end; ++it) { solve(_system.maxExpression(*it)); } } private: void solve(const MaxExpression& x) const { if (!_stable.contains(x)) { _stable.insert(x); log::strategy << indent() << "Stabilise " << x << std::endl; stack_depth++; unsigned int val = x.bestStrategy(DependencyAssignment(*this, *_rho, x), DependencyStrategy(*this, x)); stack_depth--; if (val != _values[x]) { log::strategy << x << " => " << *x.arguments()[val] << std::endl; IdSet > oldInfluence = _influence[x]; _influence[x].clear(); _values[x] = val; _rho->invalidate(*_system.varFromExpr(x)); _stable.filter(oldInfluence); for (typename IdSet >::iterator it = oldInfluence.begin(), end = oldInfluence.end(); it != end; ++it) { solve(_system.maxExpression(*it)); } } else { log::strategy << indent() << x << " did not change" << std::endl; } } else { log::strategy << indent() << x << " is stable" << std::endl; } } struct DependencyAssignment : public VariableAssignment{ DependencyAssignment(const DynamicMaxStrategy& strat, VariableAssignment& rho, const MaxExpression& expr) : _strat(strat), _rho(rho), _expr(expr) { } const Domain& operator[](const Variable& var) const { _strat._var_influence[var].insert(_expr); return _rho[var]; } private: const DynamicMaxStrategy& _strat; VariableAssignment& _rho; const MaxExpression& _expr; }; struct DependencyStrategy : public MaxStrategy { DependencyStrategy(const DynamicMaxStrategy& strat, const MaxExpression& expr) : _strat(strat), _expr(expr) { } unsigned int get(const MaxExpression& e) const { _strat.solve(e); if (&_expr != &e) { _strat._influence[e].insert(_expr); } return _strat._values[e]; } private: const DynamicMaxStrategy& _strat; const MaxExpression& _expr; }; private: const EquationSystem& _system; mutable DynamicVariableAssignment* _rho; mutable IdMap,unsigned int> _values; mutable IdSet > _stable; mutable IdMap,IdSet > > _influence; mutable IdMap,IdSet > > _var_influence; }; /*template std::ostream& operator<<(std::ostream& cout, const MaxStrategy& strat) { strat.print(cout); return cout; }*/ #endif