summaryrefslogtreecommitdiff
path: root/libs/cassowary/cassowary/ClConstraint.h
blob: 0b670e6c0712291ee2f651117d5dca60abda6b01 (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
198
// $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
//
// ClConstraint.h

#ifndef ClConstraint_H
#define ClConstraint_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 "debug.h"

#include "Cassowary.h"
#include "ClLinearExpression.h"
#include "ClStrength.h"
#include <string>

using std::string;

class ClSimplexSolver;
class ClFDSolver;
class ClBlueSolver;

// enum setup so additive inverse flips the direction of the inequality
enum ClCnRelation {cnEQ = 0, cnNEQ = 100, cnLEQ = 2, cnGEQ = -2, cnLT = 3, cnGT = -3 };

inline enum ClCnRelation
ReverseInequality(enum ClCnRelation c)
{
  if (c != cnNEQ)
    c = (enum ClCnRelation) (- int(c));
  return c;
}

inline string
StrCnRelation(enum ClCnRelation rel) {
  switch (rel) {
  case cnEQ: return "=";
  case cnNEQ: return "=/=";
  case cnLEQ: return "<=";
  case cnGEQ: return ">=";
  case cnLT: return "<";
  case cnGT: return ">";
  default: assert(false);
  }
}



class ClConstraint {
public:

  ClConstraint(const ClStrength &strength = ClsRequired(), double weight = 1.0 ) :
    _strength(strength),
    _readOnlyVars(),
    _weight(weight),
    _pv(0),
    _times_added(0)
    { 
      CtrTracer(__FUNCTION__,this);
    }

  virtual ~ClConstraint()
    { 
      DtrTracer(__FUNCTION__,this);
    }

  // Return my linear Expression.  (For linear equations, this
  // constraint represents Expression=0; for linear inequalities it
  // represents Expression>=0.)
  virtual ClLinearExpression Expression() const
    { assert(false); }

  // Returns true if this is an edit constraint
  virtual bool IsEditConstraint() const
    { return false; }

  // Return true if this is an inequality constraint and
  // false if it is an equality constraint.  The default is
  // that it is not.
  virtual bool IsInequality() const
    { return false; }

  virtual bool IsStrictInequality() const
    { return false; }

  virtual bool IsRequired() const
    { return _strength.IsRequired(); }

  virtual bool isStayConstraint() const
    { return false; }

  virtual const ClStrength &strength() const
    { return _strength; }

  virtual double weight() const
    { return _weight; }

#ifndef CL_NO_IO
  virtual ostream &PrintOn(ostream &xo) const = 0;

  friend ostream& operator<<(ostream &xos, const ClConstraint &constraint)
    { constraint.PrintOn(xos); return xos; }

#endif


  void SetPv(void *pv)
    { _pv = pv; }

  void *Pv() const
    { return _pv; }

  virtual bool FIsSatisfied() const { return false; }

  virtual bool FIsInSolver() const { return _times_added != 0; }

  virtual bool FIsOkayForSimplexSolver() const { return true; }

  void ChangeStrength( const ClStrength &strength) 
    { 
      if (_times_added == 0) {
        setStrength(strength);
      } else {
        throw ExCLTooDifficult();
      }
    }

  void ChangeWeight( double weight )
    { 
      if (_times_added == 0) {
        setWeight(weight);
      } else {
        throw ExCLTooDifficult();
      }
    }

  bool FIsReadOnlyVar(ClVariable v) const { 
    return !(_readOnlyVars.find(v) == _readOnlyVars.end());
  }

  const ClVarSet &ReadOnlyVars() const {
    return _readOnlyVars;
  }

  ClConstraint &AddROVars(const ClVarSet &setClv) {
    for ( ClVarSet::const_iterator it = setClv.begin(); 
          it != setClv.end(); ++it) {
      _readOnlyVars.insert(*it);
    }
    return *this;
  }

  friend class ClSimplexSolver;
  friend class ClFDSolver;
  friend class ClBlueSolver;
private:

  ClSymbolicWeight symbolicWeight() const {
    return _strength.symbolicWeight();
  }

  void addedTo(const ClSimplexSolver &)
    { ++_times_added; }

  void removedFrom(const ClSimplexSolver &)
    { --_times_added; }

  void setStrength( const ClStrength &strength )
    { _strength = strength; }

  void setWeight( double weight )
    { _weight = weight; }

  /// instance variables
  ClStrength _strength;

  ClVarSet _readOnlyVars;

  double _weight;

  void *_pv;

  int _times_added;
};

typedef ClConstraint* PClConstraint;

#endif