Commit 7dcd8115 authored by Chinmay Garde's avatar Chinmay Garde

Avoid exposing internal classes from the cassowary library

parent 891085b7
// Copyright (c) 2015, <your name>. All rights reserved. Use of this source code // Copyright (c) 2015 The Chromium Authors. All rights reserved.
// is governed by a BSD-style license that can be found in the LICENSE file. // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/// The cassowary library.
library cassowary; library cassowary;
part 'constraint.dart'; part 'constraint.dart';
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
part of cassowary; part of cassowary;
class ConstantMember extends EquationMember { class ConstantMember extends _EquationMember {
double value = 0.0; double value = 0.0;
bool get isConstant => true; bool get isConstant => true;
......
...@@ -4,26 +4,26 @@ ...@@ -4,26 +4,26 @@
part of cassowary; part of cassowary;
abstract class EquationMember { abstract class _EquationMember {
Expression asExpression(); Expression asExpression();
bool get isConstant; bool get isConstant;
double get value; double get value;
Constraint operator >=(EquationMember m) => asExpression() >= m; Constraint operator >=(_EquationMember m) => asExpression() >= m;
Constraint operator <=(EquationMember m) => asExpression() <= m; Constraint operator <=(_EquationMember m) => asExpression() <= m;
/* Constraint */ operator ==(EquationMember m) => asExpression() == m; /* Constraint */ operator ==(_EquationMember m) => asExpression() == m;
Expression operator +(EquationMember m) => asExpression() + m; Expression operator +(_EquationMember m) => asExpression() + m;
Expression operator -(EquationMember m) => asExpression() - m; Expression operator -(_EquationMember m) => asExpression() - m;
Expression operator *(EquationMember m) => asExpression() * m; Expression operator *(_EquationMember m) => asExpression() * m;
Expression operator /(EquationMember m) => asExpression() / m; Expression operator /(_EquationMember m) => asExpression() / m;
int get hashCode => int get hashCode =>
throw "An equation member is not comparable and cannot be added to collections"; throw "An equation member is not comparable and cannot be added to collections";
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
part of cassowary; part of cassowary;
class Expression extends EquationMember { class Expression extends _EquationMember {
final List<Term> terms; final List<Term> terms;
final double constant; final double constant;
...@@ -21,7 +21,7 @@ class Expression extends EquationMember { ...@@ -21,7 +21,7 @@ class Expression extends EquationMember {
Expression asExpression() => this; Expression asExpression() => this;
Constraint _createConstraint( Constraint _createConstraint(
EquationMember /* rhs */ value, Relation relation) { _EquationMember /* rhs */ value, Relation relation) {
if (value is ConstantMember) { if (value is ConstantMember) {
return new Constraint( return new Constraint(
new Expression(new List.from(terms), constant - value.value), new Expression(new List.from(terms), constant - value.value),
...@@ -51,16 +51,16 @@ class Expression extends EquationMember { ...@@ -51,16 +51,16 @@ class Expression extends EquationMember {
return null; return null;
} }
Constraint operator >=(EquationMember value) => Constraint operator >=(_EquationMember value) =>
_createConstraint(value, Relation.greaterThanOrEqualTo); _createConstraint(value, Relation.greaterThanOrEqualTo);
Constraint operator <=(EquationMember value) => Constraint operator <=(_EquationMember value) =>
_createConstraint(value, Relation.lessThanOrEqualTo); _createConstraint(value, Relation.lessThanOrEqualTo);
operator ==(EquationMember value) => operator ==(_EquationMember value) =>
_createConstraint(value, Relation.equalTo); _createConstraint(value, Relation.equalTo);
Expression operator +(EquationMember m) { Expression operator +(_EquationMember m) {
if (m is ConstantMember) { if (m is ConstantMember) {
return new Expression(new List.from(terms), constant + m.value); return new Expression(new List.from(terms), constant + m.value);
} }
...@@ -83,7 +83,7 @@ class Expression extends EquationMember { ...@@ -83,7 +83,7 @@ class Expression extends EquationMember {
return null; return null;
} }
Expression operator -(EquationMember m) { Expression operator -(_EquationMember m) {
if (m is ConstantMember) { if (m is ConstantMember) {
return new Expression(new List.from(terms), constant - m.value); return new Expression(new List.from(terms), constant - m.value);
} }
...@@ -109,13 +109,13 @@ class Expression extends EquationMember { ...@@ -109,13 +109,13 @@ class Expression extends EquationMember {
return null; return null;
} }
EquationMember _applyMultiplicand(double m) { _EquationMember _applyMultiplicand(double m) {
var newTerms = terms.fold(new List<Term>(), (list, term) => list var newTerms = terms.fold(new List<Term>(), (list, term) => list
..add(new Term(term.variable, term.coefficient * m))); ..add(new Term(term.variable, term.coefficient * m)));
return new Expression(newTerms, constant * m); return new Expression(newTerms, constant * m);
} }
_Pair<Expression, double> _findMulitplierAndMultiplicand(EquationMember m) { _Pair<Expression, double> _findMulitplierAndMultiplicand(_EquationMember m) {
// At least on of the the two members must be constant for the resulting // At least on of the the two members must be constant for the resulting
// expression to be linear // expression to be linear
...@@ -135,7 +135,7 @@ class Expression extends EquationMember { ...@@ -135,7 +135,7 @@ class Expression extends EquationMember {
return null; return null;
} }
EquationMember operator *(EquationMember m) { _EquationMember operator *(_EquationMember m) {
_Pair<Expression, double> args = _findMulitplierAndMultiplicand(m); _Pair<Expression, double> args = _findMulitplierAndMultiplicand(m);
if (args == null) { if (args == null) {
...@@ -147,7 +147,7 @@ class Expression extends EquationMember { ...@@ -147,7 +147,7 @@ class Expression extends EquationMember {
return args.first._applyMultiplicand(args.second); return args.first._applyMultiplicand(args.second);
} }
EquationMember operator /(EquationMember m) { _EquationMember operator /(_EquationMember m) {
if (!m.isConstant) { if (!m.isConstant) {
throw new ParserException( throw new ParserException(
"The divisor was not a constant expression", [this, m]); "The divisor was not a constant expression", [this, m]);
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
part of cassowary; part of cassowary;
class Param extends EquationMember { class Param extends _EquationMember {
final Variable variable; final Variable variable;
Param.withVariable(this.variable); Param.withVariable(this.variable);
......
...@@ -6,7 +6,7 @@ part of cassowary; ...@@ -6,7 +6,7 @@ part of cassowary;
class ParserException implements Exception { class ParserException implements Exception {
final String message; final String message;
List<EquationMember> members; List<_EquationMember> members;
ParserException(this.message, this.members); ParserException(this.message, this.members);
String toString() { String toString() {
......
...@@ -4,18 +4,18 @@ ...@@ -4,18 +4,18 @@
part of cassowary; part of cassowary;
class Row { class _Row {
final Map<Symbol, double> cells; final Map<_Symbol, double> cells;
double constant = 0.0; double constant = 0.0;
Row(this.constant) : this.cells = new Map<Symbol, double>(); _Row(this.constant) : this.cells = new Map<_Symbol, double>();
Row.fromRow(Row row) _Row.fromRow(_Row row)
: this.cells = new Map<Symbol, double>.from(row.cells), : this.cells = new Map<_Symbol, double>.from(row.cells),
this.constant = row.constant; this.constant = row.constant;
double add(double value) => constant += value; double add(double value) => constant += value;
void insertSymbol(Symbol symbol, [double coefficient = 1.0]) { void insertSymbol(_Symbol symbol, [double coefficient = 1.0]) {
double val = _elvis(cells[symbol], 0.0) + coefficient; double val = _elvis(cells[symbol], 0.0) + coefficient;
if (_nearZero(val)) { if (_nearZero(val)) {
...@@ -25,18 +25,18 @@ class Row { ...@@ -25,18 +25,18 @@ class Row {
} }
} }
void insertRow(Row other, [double coefficient = 1.0]) { void insertRow(_Row other, [double coefficient = 1.0]) {
constant += other.constant * coefficient; constant += other.constant * coefficient;
other.cells.forEach((s, v) => insertSymbol(s, v * coefficient)); other.cells.forEach((s, v) => insertSymbol(s, v * coefficient));
} }
void removeSymbol(Symbol symbol) { void removeSymbol(_Symbol symbol) {
cells.remove(symbol); cells.remove(symbol);
} }
void reverseSign() => cells.forEach((s, v) => cells[s] = -v); void reverseSign() => cells.forEach((s, v) => cells[s] = -v);
void solveForSymbol(Symbol symbol) { void solveForSymbol(_Symbol symbol) {
assert(cells.containsKey(symbol)); assert(cells.containsKey(symbol));
double coefficient = -1.0 / cells[symbol]; double coefficient = -1.0 / cells[symbol];
cells.remove(symbol); cells.remove(symbol);
...@@ -44,14 +44,14 @@ class Row { ...@@ -44,14 +44,14 @@ class Row {
cells.forEach((s, v) => cells[s] = v * coefficient); cells.forEach((s, v) => cells[s] = v * coefficient);
} }
void solveForSymbols(Symbol lhs, Symbol rhs) { void solveForSymbols(_Symbol lhs, _Symbol rhs) {
insertSymbol(lhs, -1.0); insertSymbol(lhs, -1.0);
solveForSymbol(rhs); solveForSymbol(rhs);
} }
double coefficientForSymbol(Symbol symbol) => _elvis(cells[symbol], 0.0); double coefficientForSymbol(_Symbol symbol) => _elvis(cells[symbol], 0.0);
void substitute(Symbol symbol, Row row) { void substitute(_Symbol symbol, _Row row) {
double coefficient = cells[symbol]; double coefficient = cells[symbol];
if (coefficient == null) { if (coefficient == null) {
......
...@@ -5,13 +5,13 @@ ...@@ -5,13 +5,13 @@
part of cassowary; part of cassowary;
class Solver { class Solver {
final Map<Constraint, Tag> _constraints = new Map<Constraint, Tag>(); final Map<Constraint, _Tag> _constraints = new Map<Constraint, _Tag>();
final Map<Symbol, Row> _rows = new Map<Symbol, Row>(); final Map<_Symbol, _Row> _rows = new Map<_Symbol, _Row>();
final Map<Variable, Symbol> _vars = new Map<Variable, Symbol>(); final Map<Variable, _Symbol> _vars = new Map<Variable, _Symbol>();
final Map<Variable, EditInfo> _edits = new Map<Variable, EditInfo>(); final Map<Variable, _EditInfo> _edits = new Map<Variable, _EditInfo>();
final List<Symbol> _infeasibleRows = new List<Symbol>(); final List<_Symbol> _infeasibleRows = new List<_Symbol>();
final Row _objective = new Row(0.0); final _Row _objective = new _Row(0.0);
Row _artificial = new Row(0.0); _Row _artificial = new _Row(0.0);
int tick = 0; int tick = 0;
Result addConstraint(Constraint constraint) { Result addConstraint(Constraint constraint) {
...@@ -19,12 +19,12 @@ class Solver { ...@@ -19,12 +19,12 @@ class Solver {
return Result.duplicateConstraint; return Result.duplicateConstraint;
} }
Tag tag = new Tag( _Tag tag = new _Tag(
new Symbol(SymbolType.invalid, 0), new Symbol(SymbolType.invalid, 0)); new _Symbol(SymbolType.invalid, 0), new _Symbol(SymbolType.invalid, 0));
Row row = _createRow(constraint, tag); _Row row = _createRow(constraint, tag);
Symbol subject = _chooseSubjectForRow(row, tag); _Symbol subject = _chooseSubjectForRow(row, tag);
if (subject.type == SymbolType.invalid && _allDummiesInRow(row)) { if (subject.type == SymbolType.invalid && _allDummiesInRow(row)) {
if (!_nearZero(row.constant)) { if (!_nearZero(row.constant)) {
...@@ -50,28 +50,28 @@ class Solver { ...@@ -50,28 +50,28 @@ class Solver {
} }
Result removeConstraint(Constraint constraint) { Result removeConstraint(Constraint constraint) {
Tag tag = _constraints[constraint]; _Tag tag = _constraints[constraint];
if (tag == null) { if (tag == null) {
return Result.unknownConstraint; return Result.unknownConstraint;
} }
tag = new Tag.fromTag(tag); tag = new _Tag.fromTag(tag);
_constraints.remove(constraint); _constraints.remove(constraint);
_removeConstraintEffects(constraint, tag); _removeConstraintEffects(constraint, tag);
Row row = _rows[tag.marker]; _Row row = _rows[tag.marker];
if (row != null) { if (row != null) {
_rows.remove(tag.marker); _rows.remove(tag.marker);
} else { } else {
_Pair<Symbol, Row> rowPair = _Pair<_Symbol, _Row> rowPair =
_getLeavingRowPairForMarkerSymbol(tag.marker); _getLeavingRowPairForMarkerSymbol(tag.marker);
if (rowPair == null) { if (rowPair == null) {
return Result.internalSolverError; return Result.internalSolverError;
} }
Symbol leaving = rowPair.first; _Symbol leaving = rowPair.first;
row = rowPair.second; row = rowPair.second;
var removed = _rows.remove(rowPair.first); var removed = _rows.remove(rowPair.first);
assert(removed != null); assert(removed != null);
...@@ -102,7 +102,7 @@ class Solver { ...@@ -102,7 +102,7 @@ class Solver {
return Result.internalSolverError; return Result.internalSolverError;
} }
EditInfo info = new EditInfo(); _EditInfo info = new _EditInfo();
info.tag = _constraints[constraint]; info.tag = _constraints[constraint];
info.constraint = constraint; info.constraint = constraint;
info.constant = 0.0; info.constant = 0.0;
...@@ -113,7 +113,7 @@ class Solver { ...@@ -113,7 +113,7 @@ class Solver {
} }
Result removeEditVariable(Variable variable) { Result removeEditVariable(Variable variable) {
EditInfo info = _edits[variable]; _EditInfo info = _edits[variable];
if (info == null) { if (info == null) {
return Result.unknownEditVariable; return Result.unknownEditVariable;
} }
...@@ -142,8 +142,8 @@ class Solver { ...@@ -142,8 +142,8 @@ class Solver {
void updateVariable() { void updateVariable() {
for (Variable variable in _vars.keys) { for (Variable variable in _vars.keys) {
Symbol symbol = _vars[variable]; _Symbol symbol = _vars[variable];
Row row = _rows[symbol]; _Row row = _rows[symbol];
if (row == null) { if (row == null) {
variable.value = 0.0; variable.value = 0.0;
} else { } else {
...@@ -154,28 +154,28 @@ class Solver { ...@@ -154,28 +154,28 @@ class Solver {
Solver operator <<(Constraint c) => this..addConstraint(c); Solver operator <<(Constraint c) => this..addConstraint(c);
Symbol _getSymbolForVariable(Variable variable) { _Symbol _getSymbolForVariable(Variable variable) {
Symbol symbol = _vars[variable]; _Symbol symbol = _vars[variable];
if (symbol != null) { if (symbol != null) {
return symbol; return symbol;
} }
symbol = new Symbol(SymbolType.external, tick++); symbol = new _Symbol(SymbolType.external, tick++);
_vars[variable] = symbol; _vars[variable] = symbol;
return symbol; return symbol;
} }
Row _createRow(Constraint constraint, Tag tag) { _Row _createRow(Constraint constraint, _Tag tag) {
Expression expr = new Expression.fromExpression(constraint.expression); Expression expr = new Expression.fromExpression(constraint.expression);
Row row = new Row(expr.constant); _Row row = new _Row(expr.constant);
expr.terms.forEach((term) { expr.terms.forEach((term) {
if (!_nearZero(term.coefficient)) { if (!_nearZero(term.coefficient)) {
Symbol symbol = _getSymbolForVariable(term.variable); _Symbol symbol = _getSymbolForVariable(term.variable);
Row foundRow = _rows[symbol]; _Row foundRow = _rows[symbol];
if (foundRow != null) { if (foundRow != null) {
row.insertRow(foundRow, term.coefficient); row.insertRow(foundRow, term.coefficient);
...@@ -192,12 +192,12 @@ class Solver { ...@@ -192,12 +192,12 @@ class Solver {
double coefficient = double coefficient =
constraint.relation == Relation.lessThanOrEqualTo ? 1.0 : -1.0; constraint.relation == Relation.lessThanOrEqualTo ? 1.0 : -1.0;
Symbol slack = new Symbol(SymbolType.slack, tick++); _Symbol slack = new _Symbol(SymbolType.slack, tick++);
tag.marker = slack; tag.marker = slack;
row.insertSymbol(slack, coefficient); row.insertSymbol(slack, coefficient);
if (!constraint.required) { if (!constraint.required) {
Symbol error = new Symbol(SymbolType.error, tick++); _Symbol error = new _Symbol(SymbolType.error, tick++);
tag.other = error; tag.other = error;
row.insertSymbol(error, -coefficient); row.insertSymbol(error, -coefficient);
_objective.insertSymbol(error, constraint.priority); _objective.insertSymbol(error, constraint.priority);
...@@ -206,8 +206,8 @@ class Solver { ...@@ -206,8 +206,8 @@ class Solver {
break; break;
case Relation.equalTo: case Relation.equalTo:
if (!constraint.required) { if (!constraint.required) {
Symbol errPlus = new Symbol(SymbolType.error, tick++); _Symbol errPlus = new _Symbol(SymbolType.error, tick++);
Symbol errMinus = new Symbol(SymbolType.error, tick++); _Symbol errMinus = new _Symbol(SymbolType.error, tick++);
tag.marker = errPlus; tag.marker = errPlus;
tag.other = errMinus; tag.other = errMinus;
row.insertSymbol(errPlus, -1.0); row.insertSymbol(errPlus, -1.0);
...@@ -215,7 +215,7 @@ class Solver { ...@@ -215,7 +215,7 @@ class Solver {
_objective.insertSymbol(errPlus, constraint.priority); _objective.insertSymbol(errPlus, constraint.priority);
_objective.insertSymbol(errMinus, constraint.priority); _objective.insertSymbol(errMinus, constraint.priority);
} else { } else {
Symbol dummy = new Symbol(SymbolType.dummy, tick++); _Symbol dummy = new _Symbol(SymbolType.dummy, tick++);
tag.marker = dummy; tag.marker = dummy;
row.insertSymbol(dummy); row.insertSymbol(dummy);
} }
...@@ -229,8 +229,8 @@ class Solver { ...@@ -229,8 +229,8 @@ class Solver {
return row; return row;
} }
Symbol _chooseSubjectForRow(Row row, Tag tag) { _Symbol _chooseSubjectForRow(_Row row, _Tag tag) {
for (Symbol symbol in row.cells.keys) { for (_Symbol symbol in row.cells.keys) {
if (symbol.type == SymbolType.external) { if (symbol.type == SymbolType.external) {
return symbol; return symbol;
} }
...@@ -250,11 +250,11 @@ class Solver { ...@@ -250,11 +250,11 @@ class Solver {
} }
} }
return new Symbol(SymbolType.invalid, 0); return new _Symbol(SymbolType.invalid, 0);
} }
bool _allDummiesInRow(Row row) { bool _allDummiesInRow(_Row row) {
for (Symbol symbol in row.cells.keys) { for (_Symbol symbol in row.cells.keys) {
if (symbol.type != SymbolType.dummy) { if (symbol.type != SymbolType.dummy) {
return false; return false;
} }
...@@ -262,10 +262,10 @@ class Solver { ...@@ -262,10 +262,10 @@ class Solver {
return true; return true;
} }
bool _addWithArtificialVariableOnRow(Row row) { bool _addWithArtificialVariableOnRow(_Row row) {
Symbol artificial = new Symbol(SymbolType.slack, tick++); _Symbol artificial = new _Symbol(SymbolType.slack, tick++);
_rows[artificial] = new Row.fromRow(row); _rows[artificial] = new _Row.fromRow(row);
_artificial = new Row.fromRow(row); _artificial = new _Row.fromRow(row);
Result result = _optimizeObjectiveRow(_artificial); Result result = _optimizeObjectiveRow(_artificial);
...@@ -275,16 +275,16 @@ class Solver { ...@@ -275,16 +275,16 @@ class Solver {
} }
bool success = _nearZero(_artificial.constant); bool success = _nearZero(_artificial.constant);
_artificial = new Row(0.0); _artificial = new _Row(0.0);
Row foundRow = _rows[artificial]; _Row foundRow = _rows[artificial];
if (foundRow != null) { if (foundRow != null) {
_rows.remove(artificial); _rows.remove(artificial);
if (foundRow.cells.isEmpty) { if (foundRow.cells.isEmpty) {
return success; return success;
} }
Symbol entering = _anyPivotableSymbol(foundRow); _Symbol entering = _anyPivotableSymbol(foundRow);
if (entering.type == SymbolType.invalid) { if (entering.type == SymbolType.invalid) {
return false; return false;
} }
...@@ -294,29 +294,29 @@ class Solver { ...@@ -294,29 +294,29 @@ class Solver {
_rows[entering] = foundRow; _rows[entering] = foundRow;
} }
for (Row row in _rows.values) { for (_Row row in _rows.values) {
row.removeSymbol(artificial); row.removeSymbol(artificial);
} }
_objective.removeSymbol(artificial); _objective.removeSymbol(artificial);
return success; return success;
} }
Result _optimizeObjectiveRow(Row objective) { Result _optimizeObjectiveRow(_Row objective) {
while (true) { while (true) {
Symbol entering = _getEnteringSymbolForObjectiveRow(objective); _Symbol entering = _getEnteringSymbolForObjectiveRow(objective);
if (entering.type == SymbolType.invalid) { if (entering.type == SymbolType.invalid) {
return Result.success; return Result.success;
} }
_Pair<Symbol, Row> leavingPair = _Pair<_Symbol, _Row> leavingPair =
_getLeavingRowForEnteringSymbol(entering); _getLeavingRowForEnteringSymbol(entering);
if (leavingPair == null) { if (leavingPair == null) {
return Result.internalSolverError; return Result.internalSolverError;
} }
Symbol leaving = leavingPair.first; _Symbol leaving = leavingPair.first;
Row row = leavingPair.second; _Row row = leavingPair.second;
_rows.remove(leavingPair.first); _rows.remove(leavingPair.first);
row.solveForSymbols(leaving, entering); row.solveForSymbols(leaving, entering);
_substitute(entering, row); _substitute(entering, row);
...@@ -324,21 +324,21 @@ class Solver { ...@@ -324,21 +324,21 @@ class Solver {
} }
} }
Symbol _getEnteringSymbolForObjectiveRow(Row objective) { _Symbol _getEnteringSymbolForObjectiveRow(_Row objective) {
Map<Symbol, double> cells = objective.cells; Map<_Symbol, double> cells = objective.cells;
for (Symbol symbol in cells.keys) { for (_Symbol symbol in cells.keys) {
if (symbol.type != SymbolType.dummy && cells[symbol] < 0.0) { if (symbol.type != SymbolType.dummy && cells[symbol] < 0.0) {
return symbol; return symbol;
} }
} }
return new Symbol(SymbolType.invalid, 0); return new _Symbol(SymbolType.invalid, 0);
} }
_Pair<Symbol, Row> _getLeavingRowForEnteringSymbol(Symbol entering) { _Pair<_Symbol, _Row> _getLeavingRowForEnteringSymbol(_Symbol entering) {
double ratio = double.MAX_FINITE; double ratio = double.MAX_FINITE;
_Pair<Symbol, Row> result = new _Pair(null, null); _Pair<_Symbol, _Row> result = new _Pair(null, null);
_rows.forEach((symbol, row) { _rows.forEach((symbol, row) {
if (symbol.type != SymbolType.external) { if (symbol.type != SymbolType.external) {
...@@ -363,7 +363,7 @@ class Solver { ...@@ -363,7 +363,7 @@ class Solver {
return result; return result;
} }
void _substitute(Symbol symbol, Row row) { void _substitute(_Symbol symbol, _Row row) {
_rows.forEach((first, second) { _rows.forEach((first, second) {
second.substitute(symbol, row); second.substitute(symbol, row);
if (first.type != SymbolType.external && second.constant < 0.0) { if (first.type != SymbolType.external && second.constant < 0.0) {
...@@ -377,16 +377,16 @@ class Solver { ...@@ -377,16 +377,16 @@ class Solver {
} }
} }
Symbol _anyPivotableSymbol(Row row) { _Symbol _anyPivotableSymbol(_Row row) {
for (Symbol symbol in row.cells.keys) { for (_Symbol symbol in row.cells.keys) {
if (symbol.type == SymbolType.slack || symbol.type == SymbolType.error) { if (symbol.type == SymbolType.slack || symbol.type == SymbolType.error) {
return symbol; return symbol;
} }
} }
return new Symbol(SymbolType.invalid, 0); return new _Symbol(SymbolType.invalid, 0);
} }
void _removeConstraintEffects(Constraint cn, Tag tag) { void _removeConstraintEffects(Constraint cn, _Tag tag) {
if (tag.marker.type == SymbolType.error) { if (tag.marker.type == SymbolType.error) {
_removeMarkerEffects(tag.marker, cn.priority); _removeMarkerEffects(tag.marker, cn.priority);
} }
...@@ -395,8 +395,8 @@ class Solver { ...@@ -395,8 +395,8 @@ class Solver {
} }
} }
void _removeMarkerEffects(Symbol marker, double strength) { void _removeMarkerEffects(_Symbol marker, double strength) {
Row row = _rows[marker]; _Row row = _rows[marker];
if (row != null) { if (row != null) {
_objective.insertRow(row, -strength); _objective.insertRow(row, -strength);
} else { } else {
...@@ -404,11 +404,11 @@ class Solver { ...@@ -404,11 +404,11 @@ class Solver {
} }
} }
_Pair<Symbol, Row> _getLeavingRowPairForMarkerSymbol(Symbol marker) { _Pair<_Symbol, _Row> _getLeavingRowPairForMarkerSymbol(_Symbol marker) {
double r1 = double.MAX_FINITE; double r1 = double.MAX_FINITE;
double r2 = double.MAX_FINITE; double r2 = double.MAX_FINITE;
_Pair<Symbol, Row> first, second, third; _Pair<_Symbol, _Row> first, second, third;
_rows.forEach((symbol, row) { _rows.forEach((symbol, row) {
double c = row.coefficientForSymbol(marker); double c = row.coefficientForSymbol(marker);
...@@ -444,13 +444,13 @@ class Solver { ...@@ -444,13 +444,13 @@ class Solver {
} }
void _suggestValueForEditInfoWithoutDualOptimization( void _suggestValueForEditInfoWithoutDualOptimization(
EditInfo info, double value) { _EditInfo info, double value) {
double delta = value - info.constant; double delta = value - info.constant;
info.constant = value; info.constant = value;
{ {
Symbol symbol = info.tag.marker; _Symbol symbol = info.tag.marker;
Row row = _rows[info.tag.marker]; _Row row = _rows[info.tag.marker];
if (row != null) { if (row != null) {
if (row.add(-delta) < 0.0) { if (row.add(-delta) < 0.0) {
...@@ -470,8 +470,8 @@ class Solver { ...@@ -470,8 +470,8 @@ class Solver {
} }
} }
for (Symbol symbol in _rows.keys) { for (_Symbol symbol in _rows.keys) {
Row row = _rows[symbol]; _Row row = _rows[symbol];
double coeff = row.coefficientForSymbol(info.tag.marker); double coeff = row.coefficientForSymbol(info.tag.marker);
if (coeff != 0.0 && if (coeff != 0.0 &&
row.add(delta * coeff) < 0.0 && row.add(delta * coeff) < 0.0 &&
...@@ -483,11 +483,11 @@ class Solver { ...@@ -483,11 +483,11 @@ class Solver {
Result _dualOptimize() { Result _dualOptimize() {
while (_infeasibleRows.length != 0) { while (_infeasibleRows.length != 0) {
Symbol leaving = _infeasibleRows.removeLast(); _Symbol leaving = _infeasibleRows.removeLast();
Row row = _rows[leaving]; _Row row = _rows[leaving];
if (row != null && row.constant < 0.0) { if (row != null && row.constant < 0.0) {
Symbol entering = _getDualEnteringSymbolForRow(row); _Symbol entering = _getDualEnteringSymbolForRow(row);
if (entering.type == SymbolType.invalid) { if (entering.type == SymbolType.invalid) {
return Result.internalSolverError; return Result.internalSolverError;
...@@ -503,14 +503,14 @@ class Solver { ...@@ -503,14 +503,14 @@ class Solver {
return Result.success; return Result.success;
} }
Symbol _getDualEnteringSymbolForRow(Row row) { _Symbol _getDualEnteringSymbolForRow(_Row row) {
Symbol entering; _Symbol entering;
double ratio = double.MAX_FINITE; double ratio = double.MAX_FINITE;
Map<Symbol, double> rowCells = row.cells; Map<_Symbol, double> rowCells = row.cells;
for (Symbol symbol in rowCells.keys) { for (_Symbol symbol in rowCells.keys) {
double value = rowCells[symbol]; double value = rowCells[symbol];
if (value > 0.0 && symbol.type != SymbolType.dummy) { if (value > 0.0 && symbol.type != SymbolType.dummy) {
...@@ -523,22 +523,22 @@ class Solver { ...@@ -523,22 +523,22 @@ class Solver {
} }
} }
return _elvis(entering, new Symbol(SymbolType.invalid, 0)); return _elvis(entering, new _Symbol(SymbolType.invalid, 0));
} }
} }
class Tag { class _Tag {
Symbol marker; _Symbol marker;
Symbol other; _Symbol other;
Tag(this.marker, this.other); _Tag(this.marker, this.other);
Tag.fromTag(Tag tag) _Tag.fromTag(_Tag tag)
: this.marker = tag.marker, : this.marker = tag.marker,
this.other = tag.other; this.other = tag.other;
} }
class EditInfo { class _EditInfo {
Tag tag; _Tag tag;
Constraint constraint; Constraint constraint;
double constant; double constant;
} }
......
...@@ -6,9 +6,9 @@ part of cassowary; ...@@ -6,9 +6,9 @@ part of cassowary;
enum SymbolType { invalid, external, slack, error, dummy, } enum SymbolType { invalid, external, slack, error, dummy, }
class Symbol { class _Symbol {
final SymbolType type; final SymbolType type;
int tick; int tick;
Symbol(this.type, this.tick); _Symbol(this.type, this.tick);
} }
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
part of cassowary; part of cassowary;
class Term extends EquationMember { class Term extends _EquationMember {
final Variable variable; final Variable variable;
final double coefficient; final double coefficient;
......
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