mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Solver.flushParameterUpdates returns the a collection of updated parameters
This commit is contained in:
parent
df2eb202cc
commit
4568b088e0
@ -7,8 +7,13 @@ part of cassowary;
|
||||
class Param extends _EquationMember {
|
||||
final Variable variable;
|
||||
|
||||
Param.withVariable(this.variable);
|
||||
Param([double value = 0.0]) : this.variable = new Variable(value);
|
||||
Param.withVariable(this.variable) {
|
||||
variable._owner = this;
|
||||
}
|
||||
|
||||
Param([double value = 0.0]) : this.variable = new Variable(value) {
|
||||
variable._owner = this;
|
||||
}
|
||||
|
||||
bool get isConstant => false;
|
||||
|
||||
|
||||
@ -168,16 +168,20 @@ class Solver {
|
||||
return _dualOptimize();
|
||||
}
|
||||
|
||||
void flushVariableUpdates() {
|
||||
List<Param> flushParameterUpdates() {
|
||||
List<Param> updates = new List<Param>();
|
||||
|
||||
for (Variable variable in _vars.keys) {
|
||||
_Symbol symbol = _vars[variable];
|
||||
_Row row = _rows[symbol];
|
||||
if (row == null) {
|
||||
variable.value = 0.0;
|
||||
} else {
|
||||
variable.value = row.constant;
|
||||
|
||||
double updatedValue = row == null ? 0.0 : row.constant;
|
||||
|
||||
if (variable._applyUpdate(updatedValue) && variable._owner != null) {
|
||||
updates.add(variable._owner);
|
||||
}
|
||||
}
|
||||
return updates;
|
||||
}
|
||||
|
||||
_Symbol _symbolForVariable(Variable variable) {
|
||||
|
||||
@ -8,12 +8,18 @@ class Variable {
|
||||
double value = 0.0;
|
||||
String name;
|
||||
|
||||
Param _owner;
|
||||
|
||||
int _tick;
|
||||
static int _total = 0;
|
||||
|
||||
Variable(this.value) : _tick = _total++;
|
||||
|
||||
// TODO(csg): Add external variable update callbacks here
|
||||
bool _applyUpdate(double updated) {
|
||||
bool res = updated != value;
|
||||
value = updated;
|
||||
return res;
|
||||
}
|
||||
|
||||
String get debugName => _elvis(name, "variable${_tick}");
|
||||
|
||||
|
||||
@ -427,7 +427,7 @@ void main() {
|
||||
var left = new Param(-20.0);
|
||||
Solver s = new Solver();
|
||||
s.addConstraint(left >= CM(0.0));
|
||||
s.flushVariableUpdates();
|
||||
s.flushParameterUpdates();
|
||||
expect(left.value, 0.0);
|
||||
});
|
||||
|
||||
@ -443,7 +443,7 @@ void main() {
|
||||
expect(s.addConstraint(right - left >= CM(100.0)), Result.success);
|
||||
expect(s.addConstraint(left >= CM(0.0)), Result.success);
|
||||
|
||||
s.flushVariableUpdates();
|
||||
s.flushParameterUpdates();
|
||||
|
||||
expect(left.value, 0.0);
|
||||
expect(mid.value, 50.0);
|
||||
@ -484,7 +484,7 @@ void main() {
|
||||
expect(s.addEditVariable(mid.variable, Priority.strong), Result.success);
|
||||
expect(s.suggestValueForVariable(mid.variable, 300.0), Result.success);
|
||||
|
||||
s.flushVariableUpdates();
|
||||
s.flushParameterUpdates();
|
||||
|
||||
expect(left.value, 0.0);
|
||||
expect(mid.value, 300.0);
|
||||
@ -494,10 +494,14 @@ void main() {
|
||||
test('test_description', () {
|
||||
var left = new Param(0.0);
|
||||
var right = new Param(100.0);
|
||||
var c = right >= left;
|
||||
var c1 = right >= left;
|
||||
var c2 = right <= left;
|
||||
var c3 = (right == left) as Constraint;
|
||||
|
||||
Solver s = new Solver();
|
||||
expect(s.addConstraint(c), Result.success);
|
||||
expect(s.addConstraint(c1), Result.success);
|
||||
expect(s.addConstraint(c2), Result.success);
|
||||
expect(s.addConstraint(c3), Result.success);
|
||||
|
||||
expect(s.toString() != null, true);
|
||||
});
|
||||
@ -519,7 +523,7 @@ void main() {
|
||||
solver.addConstraint((p2 == CM(2.0) * p1) as Constraint);
|
||||
solver.addConstraint((container == (p1 + p2 + p3)) as Constraint);
|
||||
|
||||
solver.flushVariableUpdates();
|
||||
solver.flushParameterUpdates();
|
||||
|
||||
expect(container.value, 100.0);
|
||||
|
||||
@ -527,4 +531,29 @@ void main() {
|
||||
expect(p2.value, 60.0);
|
||||
expect(p3.value, 10.0);
|
||||
});
|
||||
|
||||
test('test_updates_collection', () {
|
||||
Param left = new Param();
|
||||
Param mid = new Param();
|
||||
Param right = new Param();
|
||||
|
||||
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(0.0)), Result.success);
|
||||
|
||||
expect(s.suggestValueForVariable(mid.variable, 50.0), Result.success);
|
||||
|
||||
var updates = s.flushParameterUpdates();
|
||||
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user