Solver.flush update returns the set of context associated with parameters in play
diff --git a/lib/param.dart b/lib/param.dart index 0b2b5c1..f296c9d 100644 --- a/lib/param.dart +++ b/lib/param.dart
@@ -6,12 +6,17 @@ class Param extends _EquationMember { final Variable variable; + final dynamic context; - Param.withVariable(this.variable) { + Param([double value = 0.0]) + : variable = new Variable(value), + context = null { variable._owner = this; } - Param([double value = 0.0]) : this.variable = new Variable(value) { + Param.withContext(ctx) + : variable = new Variable(0.0), + context = ctx { variable._owner = this; }
diff --git a/lib/solver.dart b/lib/solver.dart index 59ee312..f76ea23 100644 --- a/lib/solver.dart +++ b/lib/solver.dart
@@ -168,8 +168,8 @@ return _dualOptimize(); } - List<Param> flushParameterUpdates() { - List<Param> updates = new List<Param>(); + Set flushUpdates() { + Set updates = new Set(); for (Variable variable in _vars.keys) { _Symbol symbol = _vars[variable]; @@ -178,9 +178,14 @@ double updatedValue = row == null ? 0.0 : row.constant; if (variable._applyUpdate(updatedValue) && variable._owner != null) { - updates.add(variable._owner); + dynamic context = variable._owner.context; + + if (context != null) { + updates.add(context); + } } } + return updates; }
diff --git a/test/cassowary_test.dart b/test/cassowary_test.dart index 7c2e60d..1ea67e1 100644 --- a/test/cassowary_test.dart +++ b/test/cassowary_test.dart
@@ -427,7 +427,7 @@ var left = new Param(-20.0); Solver s = new Solver(); s.addConstraint(left >= CM(0.0)); - s.flushParameterUpdates(); + s.flushUpdates(); expect(left.value, 0.0); }); @@ -443,7 +443,7 @@ expect(s.addConstraint(right - left >= CM(100.0)), Result.success); expect(s.addConstraint(left >= CM(0.0)), Result.success); - s.flushParameterUpdates(); + s.flushUpdates(); expect(left.value, 0.0); expect(mid.value, 50.0); @@ -484,7 +484,7 @@ expect(s.addEditVariable(mid.variable, Priority.strong), Result.success); expect(s.suggestValueForVariable(mid.variable, 300.0), Result.success); - s.flushParameterUpdates(); + s.flushUpdates(); expect(left.value, 0.0); expect(mid.value, 300.0); @@ -523,7 +523,7 @@ solver.addConstraint((p2 == CM(2.0) * p1) as Constraint); solver.addConstraint((container == (p1 + p2 + p3)) as Constraint); - solver.flushParameterUpdates(); + solver.flushUpdates(); expect(container.value, 100.0); @@ -533,9 +533,9 @@ }); test('test_updates_collection', () { - Param left = new Param(); - Param mid = new Param(); - Param right = new Param(); + Param left = new Param.withContext("left"); + Param mid = new Param.withContext("mid"); + Param right = new Param.withContext("right"); Solver s = new Solver(); @@ -547,13 +547,36 @@ expect(s.suggestValueForVariable(mid.variable, 50.0), Result.success); - var updates = s.flushParameterUpdates(); + var updates = s.flushUpdates(); expect(updates.length, 2); - expect(updates[0] is Param, true); expect(left.value, 0.0); expect(mid.value, 50.0); expect(right.value, 100.0); }); + + test('test_updates_collection_is_set', () { + Param left = new Param.withContext("a"); + Param mid = new Param.withContext("a"); + Param right = new Param.withContext("a"); + + Solver s = new Solver(); + + expect(s.addEditVariable(mid.variable, Priority.strong), Result.success); + + expect(s.addConstraint((mid * CM(2.0) == left + right) as Constraint), + Result.success); + expect(s.addConstraint(left >= CM(10.0)), Result.success); + + expect(s.suggestValueForVariable(mid.variable, 50.0), Result.success); + + var updates = s.flushUpdates(); + + expect(updates.length, 1); + + expect(left.value, 10.0); + expect(mid.value, 50.0); + expect(right.value, 90.0); + }); }