Commit e9335659 authored by Chinmay Garde's avatar Chinmay Garde

Add bulk edit update options to the solver

parent 149a2ca1
...@@ -18,28 +18,10 @@ class Solver { ...@@ -18,28 +18,10 @@ class Solver {
/// add any for some reason, a cleanup is attempted so that either all /// add any for some reason, a cleanup is attempted so that either all
/// constraints will be added or none. /// constraints will be added or none.
Result addConstraints(List<Constraint> constraints) { Result addConstraints(List<Constraint> constraints) {
List<Constraint> added = new List<Constraint>(); _SolverBulkUpdate applier = (Constraint c) => addConstraint(c);
bool needsCleanup = false; _SolverBulkUpdate undoer = (Constraint c) => removeConstraint(c);
Result result = Result.success;
for (Constraint constraint in constraints) {
result = addConstraint(constraint);
if (result == Result.success) {
added.add(constraint);
} else {
needsCleanup = true;
break;
}
}
if (needsCleanup) { return _bulkEdit(constraints, applier, undoer);
for (Constraint constraint in added) {
removeConstraint(constraint);
}
}
return result;
} }
Result addConstraint(Constraint constraint) { Result addConstraint(Constraint constraint) {
...@@ -113,6 +95,13 @@ class Solver { ...@@ -113,6 +95,13 @@ class Solver {
return _constraints.containsKey(constraint); return _constraints.containsKey(constraint);
} }
Result addEditVariables(List<Variable> variables, double priority) {
_SolverBulkUpdate applier = (Variable v) => addEditVariable(v, priority);
_SolverBulkUpdate undoer = (Variable v) => removeEditVariable(v);
return _bulkEdit(variables, applier, undoer);
}
Result addEditVariable(Variable variable, double priority) { Result addEditVariable(Variable variable, double priority) {
if (_edits.containsKey(variable)) { if (_edits.containsKey(variable)) {
return Result.duplicateEditVariable; return Result.duplicateEditVariable;
...@@ -189,6 +178,33 @@ class Solver { ...@@ -189,6 +178,33 @@ class Solver {
return updates; return updates;
} }
Result _bulkEdit(Iterable items,
_SolverBulkUpdate applier,
_SolverBulkUpdate undoer) {
List applied = new List();
bool needsCleanup = false;
Result result = Result.success;
for (dynamic item in items) {
result = applier(item);
if (result == Result.success) {
applied.add(item);
} else {
needsCleanup = true;
break;
}
}
if (needsCleanup) {
for (dynamic item in applied.reversed) {
undoer(item);
}
}
return result;
}
_Symbol _symbolForVariable(Variable variable) { _Symbol _symbolForVariable(Variable variable) {
_Symbol symbol = _vars[variable]; _Symbol symbol = _vars[variable];
...@@ -617,3 +633,5 @@ class _EditInfo { ...@@ -617,3 +633,5 @@ class _EditInfo {
bool _isValidNonRequiredPriority(double priority) { bool _isValidNonRequiredPriority(double priority) {
return (priority >= 0.0 && priority < Priority.required); return (priority >= 0.0 && priority < Priority.required);
} }
typedef Result _SolverBulkUpdate(dynamic item);
...@@ -592,4 +592,15 @@ void main() { ...@@ -592,4 +592,15 @@ void main() {
expect((left == right).runtimeType, Constraint); expect((left == right).runtimeType, Constraint);
}); });
test('bulk_add_edit_variables', () {
Solver s = new Solver();
var left = new Param(0.0);
var right = new Param(100.0);
var mid = new Param(0.0);
expect(s.addEditVariables(
[left.variable, right.variable, mid.variable], 999.0), Result.success);
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment