#ifndef VARIABLE_ASSIGNMENT_HPP #define VARIABLE_ASSIGNMENT_HPP #include "Expression.hpp" #include "IdMap.hpp" template struct VariableAssignment { virtual ~VariableAssignment() { } virtual const Domain& operator[](const Variable&) const = 0; virtual const Domain& operator[](const Variable& x) { return (*const_cast(this))[x]; } }; #include "EquationSystem.hpp" template struct DynamicMaxStrategy; template struct DynamicVariableAssignment : public VariableAssignment { DynamicVariableAssignment( const EquationSystem& system, DynamicMaxStrategy& strat ) : _system(system), _strategy(strat), _values(system.variableCount(), unknown(infinity())), _stable(system.variableCount()), _influence(system.variableCount(), IdSet >(system.variableCount())), _frozen(false) { } void freeze() { _frozen = true; } void thaw() { _frozen = false; } bool is_frozen() { return _frozen; } const Domain& operator[](const Variable& var) const { // slightly hacky return const_cast&>(*this)[var]; } const Domain& operator[](const Variable& var) { if (!_frozen) solve(var); return _values[var]; } void invalidate(const Variable& x) { log::fixpoint << indent() << "Invalidating " << x << std::endl; if (_stable.contains(x)) { _stable.remove(x); _values[x] = unknown(infinity()); solve(x); /* IdSet > infl = _influence[x]; _influence[x].clear(); for (typename IdSet >::iterator it = infl.begin(), ei = infl.end(); it != ei; ++it) { invalidate(_system.variable(*it)); } */ } } private: void solve(const Variable& x) { if (!_stable.contains(x)) { _stable.insert(x); log::fixpoint << indent() << "Stabilise " << x << std::endl; stack_depth++; Domain val = _system[x]->eval(DependencyAssignment(*this, x), _strategy); stack_depth--; if (val != _values[x]) { log::fixpoint << x << " = " << val << std::endl; IdSet > oldInfluence = _influence[x]; _influence[x].clear(); _values[x] = val; _strategy.invalidate(x); _stable.filter(oldInfluence); for (typename IdSet >::iterator it = oldInfluence.begin(); it != oldInfluence.end(); ++it) { solve(_system.variable(*it)); } } else { log::fixpoint << indent() << x << " did not change: " << x << " = " << val << std::endl; } } else { log::fixpoint << indent() << x << " is stable: " << x << " = " << _values[x] << std::endl; } } struct DependencyAssignment : public VariableAssignment { DependencyAssignment(DynamicVariableAssignment& assignment, const Variable& var) : _assignment(assignment), _var(var) { } const Domain& operator[](const Variable& x) const { const Domain& result = _assignment[x]; _assignment._influence[x].insert(_var); return result; } private: DynamicVariableAssignment& _assignment; const Variable& _var; }; const EquationSystem& _system; DynamicMaxStrategy& _strategy; IdMap, Domain> _values; IdSet > _stable; IdMap,IdSet > > _influence; bool _frozen; }; #endif