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 {
/// add any for some reason, a cleanup is attempted so that either all
/// constraints will be added or none.
Result addConstraints(List<Constraint> constraints) {
List<Constraint> added = new List<Constraint>();
bool needsCleanup = false;
Result result = Result.success;
for (Constraint constraint in constraints) {
result = addConstraint(constraint);
if (result == Result.success) {
added.add(constraint);
} else {
needsCleanup = true;
break;
}
}
_SolverBulkUpdate applier = (Constraint c) => addConstraint(c);
_SolverBulkUpdate undoer = (Constraint c) => removeConstraint(c);
if (needsCleanup) {
for (Constraint constraint in added) {
removeConstraint(constraint);
}
}
return result;
return _bulkEdit(constraints, applier, undoer);
}
Result addConstraint(Constraint constraint) {
......@@ -113,6 +95,13 @@ class Solver {
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) {
if (_edits.containsKey(variable)) {
return Result.duplicateEditVariable;
......@@ -189,6 +178,33 @@ class Solver {
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 symbol = _vars[variable];
......@@ -617,3 +633,5 @@ class _EditInfo {
bool _isValidNonRequiredPriority(double priority) {
return (priority >= 0.0 && priority < Priority.required);
}
typedef Result _SolverBulkUpdate(dynamic item);
......@@ -592,4 +592,15 @@ void main() {
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