summaryrefslogtreecommitdiff
path: root/libs/cassowary/cassowary/ClSymbolicWeight.h
blob: 1c0339c887f145dd87d1fabbbb0dca91f747be11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// $Id$
//
// Cassowary Incremental Constraint Solver
// Original Smalltalk Implementation by Alan Borning
// This C++ Implementation by Greg J. Badros, <gjb@cs.washington.edu>
// http://www.cs.washington.edu/homes/gjb
// (C) 1998, 1999 Greg J. Badros and Alan Borning
// See ../LICENSE for legal details regarding this software
//
// ClSymbolicWeight.h

#ifndef ClSymbolicWeight_H
#define ClSymbolicWeight_H

#if defined(HAVE_CONFIG_H) && !defined(CONFIG_H_INCLUDED) && !defined(CONFIG_INLINE_H_INCLUDED)
#include <cassowary/config-inline.h>
#define CONFIG_INLINE_H_INCLUDED
#endif

#include "Cassowary.h"
#include "ClErrors.h"
#include <vector>
#include <iostream>

using std::vector;
using std::ostream;

class ClSymbolicWeight {
 public:
  ClSymbolicWeight(unsigned int CLevels = 3, Number value = 0.0);

  ClSymbolicWeight(Number w1, Number w2, Number w3);

  ClSymbolicWeight(const vector<Number> &weights);

  static ClSymbolicWeight &Zero();

  ClSymbolicWeight &negated();

  ClSymbolicWeight &MultiplyMe(Number n);

  ClSymbolicWeight Times(Number n) const
    { ClSymbolicWeight cl = *this; cl.MultiplyMe(n); return cl; }

  ClSymbolicWeight DivideBy(Number n) const;

  ClSymbolicWeight &addtoMe(const ClSymbolicWeight &cl);

  ClSymbolicWeight Add(const ClSymbolicWeight &cl) const
    { ClSymbolicWeight clRet = *this; clRet.addtoMe(cl); return clRet; }

  ClSymbolicWeight Subtract(const ClSymbolicWeight &cl) const;

  ClSymbolicWeight operator*(const Number &n) const
    { return Times(n); }

  ClSymbolicWeight operator/(const Number &n) const
    { return DivideBy(n); }

  // FIXGJB: can we express this statically?
  ClSymbolicWeight operator*(ClSymbolicWeight &w) const
    { throw ExCLInternalError("Multiplication of symbolic weights encountered"); 
      return w; }
  ClSymbolicWeight &operator*=(ClSymbolicWeight &w)
    { throw ExCLInternalError("Multiplicative assignment of symbolic weights encountered"); 
      return w; }

  // FIXGJB: can we express this statically?
  ClSymbolicWeight operator-() const
    { throw ExCLInternalError("Can not negate a symbolic weight");
      return ClSymbolicWeight::Zero(); }

  friend ClSymbolicWeight ReciprocalOf(const ClSymbolicWeight &);

  ClSymbolicWeight &operator*=(const Number &n)
    { return MultiplyMe(n); }

  ClSymbolicWeight operator+(const ClSymbolicWeight &cl) const
    { return Add(cl); }

  ClSymbolicWeight operator+=(const ClSymbolicWeight &cl)
    { return addtoMe(cl); }

  ClSymbolicWeight operator*(const Number &n)
    { ClSymbolicWeight answer(*this);
      answer *= n;
      return answer; }

  bool lessThan(const ClSymbolicWeight &cl) const;
  bool lessThanOrEqual(const ClSymbolicWeight &cl) const;
  bool equal(const ClSymbolicWeight &cl) const;
  bool greaterThan(const ClSymbolicWeight &cl) const;
  bool greaterThanOrEqual(const ClSymbolicWeight &cl) const;
  bool isNegative() const;

  friend bool operator==(const ClSymbolicWeight &cl1, const ClSymbolicWeight &cl2)
    { return cl1.equal(cl2); }

  friend bool operator!=(const ClSymbolicWeight &cl1, const ClSymbolicWeight &cl2)
    { return !(cl1 == cl2); }

  friend bool operator<(const ClSymbolicWeight &cl1, const ClSymbolicWeight &cl2)
    { return cl1.lessThan(cl2); }

  friend bool operator>(const ClSymbolicWeight &cl1, const ClSymbolicWeight &cl2)
  { return (cl2 < cl1); }

  // function.h provides operator>, >=, <= from operator<

  double AsDouble() const
    {
    vector<Number>::const_reverse_iterator i = _values.rbegin();
    Number sum  = 0;
    Number factor = 1;
    // A. Beurive' Wed Jul  7 11:07:47 CEST 1999
    Number multiplier = 1000000;
    for ( ; i != _values.rend(); ++i) 
      {
      sum += *i * factor;
      factor *= multiplier;
      }
    return sum;
    }

#ifndef CL_NO_IO
  ostream &PrintOn(ostream &xo) const
    { 
    vector<Number>::const_iterator i = _values.begin();
    if (i == _values.end())
      return xo;

    xo << *i;
    for (++i; i != _values.end(); ++i) 
      {
      xo << "," << *i;
      }
    return xo;
    }

  // FIXGJB: use a template function to generate these automatically
  friend ostream& operator<<(ostream &xos, const ClSymbolicWeight &clsw)
    { clsw.PrintOn(xos); return xos; }
#endif

  int CLevels() const
    { return _values.size(); }

  friend bool ClApprox(const ClSymbolicWeight &cl, Number n);
  friend bool ClApprox(const ClSymbolicWeight &cl1, const ClSymbolicWeight &cl2);

 private:
  vector<Number> _values;

  void push_back(Number d) 
    { _values.push_back(d); }

};

inline bool ClApprox(const ClSymbolicWeight &cl, Number n)
{
  vector<Number>::const_iterator it = cl._values.begin();
  if (!ClApprox(*it,n))
    return false;

  ++it;
  for (; it != cl._values.end(); ++it)
    {
    if (!ClApprox(*it,0))
      return false;
    }

  return true;
}

inline bool ClApprox(const ClSymbolicWeight &cl1, const ClSymbolicWeight &cl2)
{
  vector<Number>::const_iterator it1 = cl1._values.begin();
  vector<Number>::const_iterator it2 = cl2._values.begin();

  for (; it1 != cl1._values.end() && it2 != cl2._values.end(); 
       ++it1, ++it2)
    {
    if (!ClApprox(*it1,*it2))
      return false;
    }

  if (it1 == cl1._values.end() && it2 == cl2._values.end())
    return true;

  return false;
}

inline ClSymbolicWeight ReciprocalOf(const ClSymbolicWeight &)
{ throw(ExCLInternalError("Cannot take ReciprocalOf symbolic weight"));
  return ClSymbolicWeight::Zero(); }

#endif