3 // Cassowary Incremental Constraint Solver
4 // Original Smalltalk Implementation by Alan Borning
5 // This C++ Implementation by Greg J. Badros, <gjb@cs.washington.edu>
6 // http://www.cs.washington.edu/homes/gjb
7 // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 // See ../LICENSE for legal details regarding this software
15 #if defined(HAVE_CONFIG_H) && !defined(CONFIG_H_INCLUDED) && !defined(CONFIG_INLINE_H_INCLUDED)
16 #include <cassowary/config-inline.h>
17 #define CONFIG_INLINE_H_INCLUDED
20 #include "Cassowary.h"
22 #include "ClVariable.h"
24 #include "ClTypedefs.h"
25 #include "ClSymbolicWeight.h"
26 #include <GTL/graph.h>
32 class ClFDBinaryOneWayConstraint;
34 // ClFDSolver is a concrete class
35 // implementing a very restricted (for now --04/23/99 gjb)
36 // finite-domain constraint solving algorithm
37 class ClFDSolver: public ClSolver {
40 : _setCns(), _mapClvToCns(), G(), nodeToVar(G)
41 { G.make_directed(); }
46 virtual ClFDSolver &AddConstraint(ClConstraint *const pcn);
48 virtual ClFDSolver &RemoveConstraint(ClConstraint *const pcn);
50 virtual ClFDSolver &Solve();
52 virtual ClFDSolver &ShowSolve();
54 void ChangeClv(ClVariable clv, FDNumber n) {
56 if (_pfnChangeClvCallback) {
57 _pfnChangeClvCallback(&clv,this);
63 ostream &PrintOn(ostream &xo) const;
65 ostream &PrintInternalInfo(ostream &xo) const;
67 ostream &PrintOnVerbose(ostream &xo) const
68 { PrintOn(xo); PrintInternalInfo(xo); xo << endl; return xo; }
70 friend ostream &operator<<(ostream &xo, const ClFDSolver &solver);
76 virtual ClFDSolver &AddConstraintInternal(ClConstraint *const pcn);
78 virtual ClFDSolver &RemoveConstraintInternal(ClConstraint *const pcn);
80 /* Create node for v in G, if necessary,
81 otherwise return the node we already created. */
82 node GetVarNode(ClVariable v);
84 /* return the best (lowest) incremental error and the value
85 at which that error occurs */
86 pair<ClSymbolicWeight,FDNumber> ComputeBest(ClFDVariable *pcldv);
88 ClSymbolicWeight ErrorForClvAtValSubjectToCn(ClFDVariable *pcldv,
89 FDNumber value,const ClConstraint &cn);
91 /* Turn all FDVariable FIsSet() flags to false */
92 void ResetSetFlagsOnVariables();
94 /* all the constraints in the solver */
95 ClConstraintSet _setCns;
97 /* _mapClvToCns maps variable to the constraints in which
98 it is rw (it omits where it is ro) */
99 ClVarToConstraintSetMap _mapClvToCns;
101 /* track what edges correspond to which constraints
102 so we can update the constraint graph when
103 removing a constraint */
104 map<ClConstraint *, edge> _mapCnToEdge;
106 /* track what nodes correspond to which variables */
107 map<ClVariable, node> _mapVarToNode;
109 /* directed graph that mirrors the structure of
110 the relations of the added constraints */
113 node_map<ClVariable> nodeToVar;
116 #define FDN_EOL LONG_MIN
118 void ListPushOnto(list<FDNumber> *pl, ...);