2
Cassowary.net: an incremental constraint solver for .NET
3
(http://lumumba.uhasselt.be/jo/projects/cassowary.net/)
5
Copyright (C) 2005 Jo Vermeulen (jo.vermeulen@uhasselt.be)
7
This program is free software; you can redistribute it and/or
8
modify it under the terms of the GNU Lesser General Public License
9
as published by the Free Software Foundation; either version 2.1
10
of the License, or (at your option) any later version.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU Lesser General Public License for more details.
17
You should have received a copy of the GNU Lesser General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
public class ClSymbolicWeight : ICloneable
28
public ClSymbolicWeight(int cLevels)
30
_values = new double[cLevels];
33
public ClSymbolicWeight(double w1, double w2, double w3)
35
_values = new double[3];
41
public ClSymbolicWeight(double[] weights)
43
int cLevels = weights.Length;
44
_values = new double[cLevels];
46
for (int i = 0; i < cLevels; i++)
48
_values[i] = weights[i];
52
public virtual object Clone()
54
return new ClSymbolicWeight(_values);
57
public static ClSymbolicWeight operator*(ClSymbolicWeight clsw, double n)
62
public static ClSymbolicWeight operator*(double n, ClSymbolicWeight clsw)
67
public ClSymbolicWeight Times(double n)
69
ClSymbolicWeight clsw = (ClSymbolicWeight) Clone();
71
for (int i = 0; i < _values.Length; i++)
79
public static ClSymbolicWeight operator/(ClSymbolicWeight clsw, double n)
81
return clsw.DivideBy(n);
84
public static ClSymbolicWeight operator/(double n, ClSymbolicWeight clsw)
86
return clsw.DivideBy(n);
89
public ClSymbolicWeight DivideBy(double n)
92
ClSymbolicWeight clsw = (ClSymbolicWeight) Clone();
94
for (int i = 0; i < _values.Length; i++)
102
public static ClSymbolicWeight operator+(ClSymbolicWeight clsw1, ClSymbolicWeight clsw2)
104
return clsw1.Add(clsw2);
107
public ClSymbolicWeight Add(ClSymbolicWeight clsw1)
109
// Assert(clws.CLevels == CLevels);
110
ClSymbolicWeight clsw = (ClSymbolicWeight) Clone();
112
for (int i = 0; i < _values.Length; i++)
114
clsw._values[i] += clsw1._values[i];
120
public static ClSymbolicWeight operator-(ClSymbolicWeight clsw1, ClSymbolicWeight clsw2)
122
return clsw1.Subtract(clsw2);
125
public ClSymbolicWeight Subtract(ClSymbolicWeight clsw1)
127
// Assert(clsw1.CLevels == CLevels);
128
ClSymbolicWeight clsw = (ClSymbolicWeight) Clone();
130
for (int i = 0; i < _values.Length; i++)
132
clsw._values[i] -= clsw1._values[i];
138
// TODO: comparison operators (<, <=, >, >=, ==)
139
public bool LessThan(ClSymbolicWeight clsw1)
141
// Assert(clsw1.CLevels == CLevels);
143
for (int i = 0; i < _values.Length; i++)
145
if (_values[i] < clsw1._values[i])
147
else if (_values[i] > clsw1._values[i])
151
return false; // they are equal
154
public bool LessThanOrEqual(ClSymbolicWeight clsw1)
156
// Assert(clsw1.CLevels == CLevels);
158
for (int i = 0; i < _values.Length; i++)
160
if (_values[i] < clsw1._values[i])
162
else if (_values[i] > clsw1._values[i])
166
return true; // they are equal
169
public bool Equal(ClSymbolicWeight clsw1)
171
for (int i = 0; i < _values.Length; i++)
173
if (_values[i] != clsw1._values[i])
177
return true; // they are equal
180
public bool GreaterThan(ClSymbolicWeight clsw1)
182
return !LessThan(clsw1);
185
public bool IsNegative()
187
return LessThan(ClsZero);
190
public double AsDouble()
194
double multiplier = 1000;
196
for (int i = _values.Length - 1; i >= 0; i--)
198
sum += _values[i] * factor;
199
factor *= multiplier;
205
public override string ToString()
209
for (int i = 0; i < _values.Length - 1; i++)
211
result += _values[i] + ",";
214
result += _values[_values.Length - 1] + "]";
221
get { return _values.Length; }
224
public static ClSymbolicWeight ClsZero
226
get { return _clsZero; }
229
private double[] _values;
231
private static readonly ClSymbolicWeight _clsZero = new ClSymbolicWeight(0.0, 0.0, 0.0);