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) {
......
This diff is collapsed.
...@@ -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