~jozilla/uiml.net/uiml.net-tp

« back to all changes in this revision

Viewing changes to Cassowary/ClSymbolicWeight.cs

  • Committer: Jo Vermeulen
  • Date: 2007-09-05 14:26:30 UTC
  • mfrom: (249.1.16 Uiml.net)
  • Revision ID: jo.vermeulen@uhasselt.be-20070905142630-w5eafpsfntbgz0ko
Merged with Jo's main branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
  Cassowary.net: an incremental constraint solver for .NET
3
 
  (http://lumumba.uhasselt.be/jo/projects/cassowary.net/)
4
 
  
5
 
  Copyright (C) 2005  Jo Vermeulen (jo.vermeulen@uhasselt.be)
6
 
  
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.
11
 
 
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.
16
 
 
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.
20
 
*/
21
 
 
22
 
using System;
23
 
 
24
 
namespace Cassowary
25
 
{
26
 
  public class ClSymbolicWeight : ICloneable
27
 
  {
28
 
    public ClSymbolicWeight(int cLevels)
29
 
    {
30
 
      _values = new double[cLevels];
31
 
    }
32
 
 
33
 
    public ClSymbolicWeight(double w1, double w2, double w3)
34
 
    {
35
 
      _values = new double[3];
36
 
      _values[0] = w1;
37
 
      _values[1] = w2;
38
 
      _values[2] = w3;
39
 
    }
40
 
 
41
 
    public ClSymbolicWeight(double[] weights)
42
 
    {
43
 
      int cLevels = weights.Length;
44
 
      _values = new double[cLevels];
45
 
      
46
 
      for (int i = 0; i < cLevels; i++)
47
 
      {
48
 
        _values[i] = weights[i];
49
 
      }
50
 
    }
51
 
 
52
 
    public virtual object Clone()
53
 
    {
54
 
      return new ClSymbolicWeight(_values);
55
 
    }
56
 
 
57
 
    public static ClSymbolicWeight operator*(ClSymbolicWeight clsw, double n)
58
 
    {
59
 
      return clsw.Times(n);
60
 
    }
61
 
 
62
 
    public static ClSymbolicWeight operator*(double n, ClSymbolicWeight clsw)
63
 
    {
64
 
      return clsw.Times(n);
65
 
    }
66
 
 
67
 
    public ClSymbolicWeight Times(double n)
68
 
    {
69
 
      ClSymbolicWeight clsw = (ClSymbolicWeight) Clone();
70
 
      
71
 
      for (int i = 0; i < _values.Length; i++) 
72
 
      {
73
 
        clsw._values[i] *= n;
74
 
      }
75
 
      
76
 
      return clsw;
77
 
    }
78
 
 
79
 
    public static ClSymbolicWeight operator/(ClSymbolicWeight clsw, double n)
80
 
    {
81
 
      return clsw.DivideBy(n);
82
 
    }
83
 
 
84
 
    public static ClSymbolicWeight operator/(double n, ClSymbolicWeight clsw)
85
 
    {
86
 
      return clsw.DivideBy(n);
87
 
    }
88
 
 
89
 
    public ClSymbolicWeight DivideBy(double n)
90
 
    {
91
 
      // Assert(n != 0);
92
 
      ClSymbolicWeight clsw = (ClSymbolicWeight) Clone();
93
 
      
94
 
      for (int i = 0; i < _values.Length; i++)
95
 
      {
96
 
        clsw._values[i] /= n;
97
 
      }
98
 
 
99
 
      return clsw;
100
 
    }
101
 
 
102
 
    public static ClSymbolicWeight operator+(ClSymbolicWeight clsw1, ClSymbolicWeight clsw2)
103
 
    {
104
 
      return clsw1.Add(clsw2);
105
 
    }
106
 
 
107
 
    public ClSymbolicWeight Add(ClSymbolicWeight clsw1)
108
 
    {
109
 
      // Assert(clws.CLevels == CLevels);
110
 
      ClSymbolicWeight clsw = (ClSymbolicWeight) Clone();
111
 
      
112
 
      for (int i = 0; i < _values.Length; i++)
113
 
      {
114
 
        clsw._values[i] += clsw1._values[i];
115
 
      }
116
 
      
117
 
      return clsw;
118
 
    }
119
 
 
120
 
    public static ClSymbolicWeight operator-(ClSymbolicWeight clsw1, ClSymbolicWeight clsw2)
121
 
    {
122
 
      return clsw1.Subtract(clsw2);
123
 
    }
124
 
 
125
 
    public ClSymbolicWeight Subtract(ClSymbolicWeight clsw1)
126
 
    {
127
 
      // Assert(clsw1.CLevels == CLevels);
128
 
      ClSymbolicWeight clsw = (ClSymbolicWeight) Clone();
129
 
      
130
 
      for (int i = 0; i < _values.Length; i++)
131
 
      {
132
 
        clsw._values[i] -= clsw1._values[i];
133
 
      }
134
 
      
135
 
      return clsw;
136
 
    }
137
 
 
138
 
    // TODO: comparison operators (<, <=, >, >=, ==)
139
 
    public bool LessThan(ClSymbolicWeight clsw1)
140
 
    {
141
 
      // Assert(clsw1.CLevels == CLevels);
142
 
 
143
 
      for (int i = 0; i < _values.Length; i++)
144
 
      {
145
 
        if (_values[i] < clsw1._values[i])
146
 
          return true;
147
 
        else if (_values[i] > clsw1._values[i])
148
 
          return false;
149
 
      }
150
 
 
151
 
      return false; // they are equal
152
 
    }
153
 
 
154
 
    public bool LessThanOrEqual(ClSymbolicWeight clsw1)
155
 
    {
156
 
      // Assert(clsw1.CLevels == CLevels);
157
 
 
158
 
      for (int i = 0; i < _values.Length; i++)
159
 
      {
160
 
        if (_values[i] < clsw1._values[i])
161
 
          return true;
162
 
        else if (_values[i] > clsw1._values[i])
163
 
          return false;
164
 
      }
165
 
 
166
 
      return true; // they are equal
167
 
    }
168
 
 
169
 
    public bool Equal(ClSymbolicWeight clsw1)
170
 
    {
171
 
      for (int i = 0; i < _values.Length; i++)
172
 
      {
173
 
        if (_values[i] != clsw1._values[i])
174
 
          return false;
175
 
      }
176
 
      
177
 
      return true; // they are equal
178
 
    }
179
 
 
180
 
    public bool GreaterThan(ClSymbolicWeight clsw1)
181
 
    {
182
 
      return !LessThan(clsw1);
183
 
    }
184
 
 
185
 
    public bool IsNegative()
186
 
    {
187
 
      return LessThan(ClsZero);
188
 
    }
189
 
 
190
 
    public double AsDouble()
191
 
    {
192
 
      double sum = 0;
193
 
      double factor = 1;
194
 
      double multiplier = 1000;
195
 
 
196
 
      for (int i = _values.Length - 1; i >= 0; i--)
197
 
      {
198
 
        sum += _values[i] * factor;
199
 
        factor *= multiplier;
200
 
      }
201
 
 
202
 
      return sum;
203
 
    }
204
 
 
205
 
    public override string ToString()
206
 
    {
207
 
      string result = "[";
208
 
 
209
 
      for (int i = 0; i < _values.Length - 1; i++)
210
 
      {
211
 
        result += _values[i] + ",";
212
 
      }
213
 
      
214
 
      result += _values[_values.Length - 1] + "]";
215
 
      
216
 
      return result;
217
 
    }
218
 
    
219
 
    public int CLevels
220
 
    {
221
 
      get { return _values.Length; }
222
 
    }
223
 
    
224
 
    public static ClSymbolicWeight ClsZero
225
 
    {
226
 
      get { return _clsZero; }
227
 
    }
228
 
 
229
 
    private double[] _values;
230
 
    
231
 
    private static readonly ClSymbolicWeight _clsZero = new ClSymbolicWeight(0.0, 0.0, 0.0);
232
 
  }
233
 
}