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
// is governed by a BSD-style license that can be found in the LICENSE file.
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
// 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;
part 'constraint.dart';
......
......@@ -4,7 +4,7 @@
part of cassowary;
class ConstantMember extends EquationMember {
class ConstantMember extends _EquationMember {
double value = 0.0;
bool get isConstant => true;
......
......@@ -4,26 +4,26 @@
part of cassowary;
abstract class EquationMember {
abstract class _EquationMember {
Expression asExpression();
bool get isConstant;
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 =>
throw "An equation member is not comparable and cannot be added to collections";
......
......@@ -4,7 +4,7 @@
part of cassowary;
class Expression extends EquationMember {
class Expression extends _EquationMember {
final List<Term> terms;
final double constant;
......@@ -21,7 +21,7 @@ class Expression extends EquationMember {
Expression asExpression() => this;
Constraint _createConstraint(
EquationMember /* rhs */ value, Relation relation) {
_EquationMember /* rhs */ value, Relation relation) {
if (value is ConstantMember) {
return new Constraint(
new Expression(new List.from(terms), constant - value.value),
......@@ -51,16 +51,16 @@ class Expression extends EquationMember {
return null;
}
Constraint operator >=(EquationMember value) =>
Constraint operator >=(_EquationMember value) =>
_createConstraint(value, Relation.greaterThanOrEqualTo);
Constraint operator <=(EquationMember value) =>
Constraint operator <=(_EquationMember value) =>
_createConstraint(value, Relation.lessThanOrEqualTo);
operator ==(EquationMember value) =>
operator ==(_EquationMember value) =>
_createConstraint(value, Relation.equalTo);
Expression operator +(EquationMember m) {
Expression operator +(_EquationMember m) {
if (m is ConstantMember) {
return new Expression(new List.from(terms), constant + m.value);
}
......@@ -83,7 +83,7 @@ class Expression extends EquationMember {
return null;
}
Expression operator -(EquationMember m) {
Expression operator -(_EquationMember m) {
if (m is ConstantMember) {
return new Expression(new List.from(terms), constant - m.value);
}
......@@ -109,13 +109,13 @@ class Expression extends EquationMember {
return null;
}
EquationMember _applyMultiplicand(double m) {
_EquationMember _applyMultiplicand(double m) {
var newTerms = terms.fold(new List<Term>(), (list, term) => list
..add(new Term(term.variable, term.coefficient * 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
// expression to be linear
......@@ -135,7 +135,7 @@ class Expression extends EquationMember {
return null;
}
EquationMember operator *(EquationMember m) {
_EquationMember operator *(_EquationMember m) {
_Pair<Expression, double> args = _findMulitplierAndMultiplicand(m);
if (args == null) {
......@@ -147,7 +147,7 @@ class Expression extends EquationMember {
return args.first._applyMultiplicand(args.second);
}
EquationMember operator /(EquationMember m) {
_EquationMember operator /(_EquationMember m) {
if (!m.isConstant) {
throw new ParserException(
"The divisor was not a constant expression", [this, m]);
......
......@@ -4,7 +4,7 @@
part of cassowary;
class Param extends EquationMember {
class Param extends _EquationMember {
final Variable variable;
Param.withVariable(this.variable);
......
......@@ -6,7 +6,7 @@ part of cassowary;
class ParserException implements Exception {
final String message;
List<EquationMember> members;
List<_EquationMember> members;
ParserException(this.message, this.members);
String toString() {
......
......@@ -4,18 +4,18 @@
part of cassowary;
class Row {
final Map<Symbol, double> cells;
class _Row {
final Map<_Symbol, double> cells;
double constant = 0.0;
Row(this.constant) : this.cells = new Map<Symbol, double>();
Row.fromRow(Row row)
: this.cells = new Map<Symbol, double>.from(row.cells),
_Row(this.constant) : this.cells = new Map<_Symbol, double>();
_Row.fromRow(_Row row)
: this.cells = new Map<_Symbol, double>.from(row.cells),
this.constant = row.constant;
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;
if (_nearZero(val)) {
......@@ -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;
other.cells.forEach((s, v) => insertSymbol(s, v * coefficient));
}
void removeSymbol(Symbol symbol) {
void removeSymbol(_Symbol symbol) {
cells.remove(symbol);
}
void reverseSign() => cells.forEach((s, v) => cells[s] = -v);
void solveForSymbol(Symbol symbol) {
void solveForSymbol(_Symbol symbol) {
assert(cells.containsKey(symbol));
double coefficient = -1.0 / cells[symbol];
cells.remove(symbol);
......@@ -44,14 +44,14 @@ class Row {
cells.forEach((s, v) => cells[s] = v * coefficient);
}
void solveForSymbols(Symbol lhs, Symbol rhs) {
void solveForSymbols(_Symbol lhs, _Symbol rhs) {
insertSymbol(lhs, -1.0);
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];
if (coefficient == null) {
......
This diff is collapsed.
......@@ -6,9 +6,9 @@ part of cassowary;
enum SymbolType { invalid, external, slack, error, dummy, }
class Symbol {
class _Symbol {
final SymbolType type;
int tick;
Symbol(this.type, this.tick);
_Symbol(this.type, this.tick);
}
......@@ -4,7 +4,7 @@
part of cassowary;
class Term extends EquationMember {
class Term extends _EquationMember {
final Variable variable;
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