From c065ae2bd1176b17d137e0f52df6ef1d9af9e757 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Fri, 26 Oct 2012 16:29:52 +1100 Subject: Try to make the correct solver into a local solver As far as I can tell, it's worked! Hooray! --- impl/VariableAssignment.hpp | 55 ++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 21 deletions(-) (limited to 'impl/VariableAssignment.hpp') diff --git a/impl/VariableAssignment.hpp b/impl/VariableAssignment.hpp index 2a63756..e074f6f 100644 --- a/impl/VariableAssignment.hpp +++ b/impl/VariableAssignment.hpp @@ -7,10 +7,7 @@ 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]; - } + virtual const Domain operator[](const Variable& x) = 0; }; #include "EquationSystem.hpp" @@ -27,11 +24,13 @@ struct DynamicVariableAssignment : public VariableAssignment { ) : _system(system), _strategy(strat), _values(system.variableCount(), value), - _stable(system.variableCount()), + _unstable(system.variableCount()), _influence(system.variableCount(), IdSet >(system.variableCount())), - _frozen(false) - { } + _frozen(false), + _changed(false) + { + } void freeze() { _frozen = true; @@ -41,25 +40,37 @@ struct DynamicVariableAssignment : public VariableAssignment { _frozen = false; } - bool is_frozen() { + bool is_frozen() const { return _frozen; } - const Domain& operator[](const Variable& var) const { - return _values[var]; + void has_changed(bool c) { + _changed = c; + } + + bool has_changed() const { + return _changed; } - const Domain& operator[](const Variable& var) { + const Domain operator[](const Variable& var) { if (!_frozen) solve(var); return _values[var]; } + void stabilise() { + if (!_unstable.empty()) { + Variable& var = _system.variable(*_unstable.begin()); + solve(var); + } + } + void invalidate(const Variable& x) { log::fixpoint << indent() << "Invalidating " << x << std::endl; - if (_stable.contains(x)) { - _stable.remove(x); + if (!_unstable.contains(x)) { + _unstable.insert(x); _values[x] = infinity(); + _changed = true; IdSet > infl = _influence[x]; _influence[x].clear(); @@ -76,13 +87,13 @@ struct DynamicVariableAssignment : public VariableAssignment { private: void solve(const Variable& x) { - if (!_stable.contains(x)) { - _stable.insert(x); + if (_unstable.contains(x)) { + _unstable.remove(x); log::fixpoint << indent() << "Stabilise " << x << std::endl; stack_depth++; - Domain val = _system[x]->eval(DependencyAssignment(*this, x), - _strategy); + DependencyAssignment assignment(*this, x); + Domain val = _system[x]->eval(assignment, _strategy); stack_depth--; if (val != _values[x]) { @@ -91,10 +102,11 @@ private: IdSet > oldInfluence = _influence[x]; _influence[x].clear(); _values[x] = val; + _changed = true; _strategy.invalidate(x); - _stable.filter(oldInfluence); + _unstable.absorb(oldInfluence); for (typename IdSet >::iterator it = oldInfluence.begin(); it != oldInfluence.end(); @@ -114,8 +126,8 @@ private: 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]; + const Domain operator[](const Variable& x) { + const Domain result = _assignment[x]; _assignment._influence[x].insert(_var); return result; } @@ -127,9 +139,10 @@ private: const EquationSystem& _system; DynamicMaxStrategy& _strategy; IdMap, Domain> _values; - IdSet > _stable; + IdSet > _unstable; IdMap,IdSet > > _influence; bool _frozen; + bool _changed; }; #endif -- cgit v1.2.3