Commit 20908034 authored by Chinmay Garde's avatar Chinmay Garde

Make constraint priority setup more expressive

parent 2f3e5aa7
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
library cassowary; library cassowary;
import 'dart:math';
part 'constraint.dart'; part 'constraint.dart';
part 'expression.dart'; part 'expression.dart';
part 'term.dart'; part 'term.dart';
...@@ -17,3 +19,4 @@ part 'utils.dart'; ...@@ -17,3 +19,4 @@ part 'utils.dart';
part 'result.dart'; part 'result.dart';
part 'parser_exception.dart'; part 'parser_exception.dart';
part 'param.dart'; part 'param.dart';
part 'priority.dart';
...@@ -9,19 +9,9 @@ enum Relation { equalTo, lessThanOrEqualTo, greaterThanOrEqualTo, } ...@@ -9,19 +9,9 @@ enum Relation { equalTo, lessThanOrEqualTo, greaterThanOrEqualTo, }
class Constraint { class Constraint {
final Relation relation; final Relation relation;
final Expression expression; final Expression expression;
final bool required; double priority = Priority.required;
static const double requiredPriority = 1000.0; Constraint(this.expression, this.relation);
double _priority = requiredPriority - 1.0;
Constraint(this.expression, this.relation) : this.required = false;
Constraint.Required(this.expression, this.relation) : this.required = true {
this.priority = requiredPriority;
}
double get priority => required ? requiredPriority : _priority;
set priority(double p) => _priority =
required ? requiredPriority : p.clamp(0.0, requiredPriority - 1.0);
Constraint operator |(double p) => this..priority = p; Constraint operator |(double p) => this..priority = p;
...@@ -31,7 +21,7 @@ class Constraint { ...@@ -31,7 +21,7 @@ class Constraint {
switch (relation) { switch (relation) {
case Relation.equalTo: case Relation.equalTo:
buffer.write(" <= 0 "); buffer.write(" == 0 ");
break; break;
case Relation.greaterThanOrEqualTo: case Relation.greaterThanOrEqualTo:
buffer.write(" >= 0 "); buffer.write(" >= 0 ");
...@@ -43,7 +33,7 @@ class Constraint { ...@@ -43,7 +33,7 @@ class Constraint {
buffer.write(" | priority = ${priority}"); buffer.write(" | priority = ${priority}");
if (required) { if (priority == Priority.required) {
buffer.write(" (required)"); buffer.write(" (required)");
} }
......
// 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.
part of cassowary;
class Priority {
static final double required = create(1e3, 1e3, 1e3);
static final double strong = create(1.0, 0.0, 0.0);
static final double medium = create(0.0, 1.0, 0.0);
static final double weak = create(0.0, 0.0, 1.0);
static double create(double a, double b, double c) {
double result = 0.0;
result += max(0.0, min(1e3, a)) * 1e6;
result += max(0.0, min(1e3, b)) * 1e3;
result += max(0.0, min(1e3, c));
return result;
}
static double clamp(double value) {
return max(0.0, min(required, value));
}
}
...@@ -16,9 +16,9 @@ class _Row { ...@@ -16,9 +16,9 @@ class _Row {
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);
if (_nearZero(val)) { if (_nearZero(val + coefficient)) {
cells.remove(symbol); cells.remove(symbol);
} else { } else {
cells[symbol] = val + coefficient; cells[symbol] = val + coefficient;
......
...@@ -221,7 +221,7 @@ class Solver { ...@@ -221,7 +221,7 @@ class Solver {
tag.marker = slack; tag.marker = slack;
row.insertSymbol(slack, coefficient); row.insertSymbol(slack, coefficient);
if (!constraint.required) { if (constraint.priority < Priority.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);
...@@ -230,7 +230,7 @@ class Solver { ...@@ -230,7 +230,7 @@ class Solver {
} }
break; break;
case Relation.equalTo: case Relation.equalTo:
if (!constraint.required) { if (constraint.priority < Priority.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;
...@@ -605,5 +605,5 @@ class _EditInfo { ...@@ -605,5 +605,5 @@ class _EditInfo {
} }
bool _isValidNonRequiredPriority(double priority) { bool _isValidNonRequiredPriority(double priority) {
return (priority >= 0.0 && priority < Constraint.requiredPriority); return (priority >= 0.0 && priority < Priority.required);
} }
...@@ -17,5 +17,5 @@ class Variable { ...@@ -17,5 +17,5 @@ class Variable {
String get debugName => _elvis(name, "variable${_tick}"); String get debugName => _elvis(name, "variable${_tick}");
String toString() => "${debugName}(=${value})"; String toString() => debugName;
} }
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