7 #define EDGE_IS_VAR_CONSTANT 2
9 #define EDGE_MASK (NEGATE_EDGE | EDGE_IS_VAR_CONSTANT)
14 typedef struct Edge Edge;
17 typedef struct Node Node;
31 typedef enum NodeType NodeType;
42 typedef struct Node Node;
44 #define DEFAULT_CNF_ARRAY_SIZE 2048
49 IncrementalSolver *solver;
56 typedef struct CNF CNF;
58 static inline Edge constraintNegate(Edge e) {
59 Edge eneg = { (Node *) (((uintptr_t) e.node_ptr) ^ NEGATE_EDGE)};
63 static inline bool sameNodeVarEdge(Edge e1, Edge e2) {
64 return !(((uintptr_t) e1.node_ptr ^ (uintptr_t) e2.node_ptr) & (~(uintptr_t) NEGATE_EDGE));
67 static inline bool sameSignEdge(Edge e1, Edge e2) {
68 return !(((uintptr_t) e1.node_ptr ^ (uintptr_t) e2.node_ptr) & NEGATE_EDGE);
71 static inline bool sameNodeOppSign(Edge e1, Edge e2) {
72 return (((uintptr_t) e1.node_ptr) ^ ((uintptr_t)e2.node_ptr)) == NEGATE_EDGE;
75 static inline bool isNegEdge(Edge e) {
76 return ((uintptr_t)e.node_ptr) & NEGATE_EDGE;
79 static inline bool isPosEdge(Edge e) {
80 return !(((uintptr_t)e.node_ptr) & NEGATE_EDGE);
83 static inline bool isNodeEdge(Edge e) {
84 return !(((uintptr_t)e.node_ptr) & EDGE_IS_VAR_CONSTANT);
87 static inline bool isNegNodeEdge(Edge e) {
88 return (((uintptr_t) e.node_ptr) & (NEGATE_EDGE | EDGE_IS_VAR_CONSTANT)) == NEGATE_EDGE;
91 static inline bool isPosNodeEdge(Edge e) {
92 return (((uintptr_t) e.node_ptr) & (NEGATE_EDGE | EDGE_IS_VAR_CONSTANT)) == 0;
95 static inline Node *getNodePtrFromEdge(Edge e) {
96 return (Node *) (((uintptr_t) e.node_ptr) & ~((uintptr_t) EDGE_MASK));
99 static inline NodeType getNodeType(Edge e) {
100 Node *n = getNodePtrFromEdge(e);
104 static inline bool equalsEdge(Edge e1, Edge e2) {
105 return e1.node_ptr == e2.node_ptr;
108 static inline bool ltEdge(Edge e1, Edge e2) {
109 return (uintptr_t) e1.node_ptr < (uintptr_t) e2.node_ptr;
112 static inline uint getNodeSize(Edge e) {
113 Node *n = getNodePtrFromEdge(e);
117 static inline Edge *getEdgeArray(Edge e) {
118 Node *n = getNodePtrFromEdge(e);
122 static inline Edge getNonNeg(Edge e) {
123 Edge enew = {(Node *)(((uintptr_t)e.node_ptr) & (~((uintptr_t)NEGATE_EDGE)))};
127 static inline bool edgeIsConst(Edge e) {
128 return (((uintptr_t) e.node_ptr) & ~((uintptr_t)NEGATE_EDGE)) == EDGE_IS_VAR_CONSTANT;
131 static inline bool edgeIsNull(Edge e) {
132 return e.node_ptr == NULL;
135 static inline bool edgeIsVarConst(Edge e) {
136 return ((uintptr_t)e.node_ptr) & EDGE_IS_VAR_CONSTANT;
139 static inline Edge constraintNegateIf(Edge e, bool negate) {
140 Edge eret = {(Node *)(((uintptr_t)e.node_ptr) ^ negate)};
144 static inline Literal getEdgeVar(Edge e) {
145 int val = (int) (((uintptr_t) e.node_ptr) >> VAR_SHIFT);
146 return isNegEdge(e) ? -val : val;
150 void deleteCNF(CNF *cnf);
151 void resetCNF(CNF *cnf);
153 uint hashNode(NodeType type, uint numEdges, Edge *edges);
154 Node *allocNode(NodeType type, uint numEdges, Edge *edges);
155 bool compareNodes(Node *node, NodeType type, uint numEdges, Edge *edges);
156 Edge createNode(NodeType type, uint numEdges, Edge *edges);
157 Edge constraintOR(CNF *cnf, uint numEdges, Edge *edges);
158 Edge constraintAND(CNF *cnf, uint numEdges, Edge *edges);
159 Edge constraintOR2(CNF *cnf, Edge left, Edge right);
160 Edge constraintAND2(CNF *cnf, Edge left, Edge right);
161 Edge constraintIMPLIES(CNF *cnf, Edge left, Edge right);
162 Edge constraintIFF(CNF *cnf, Edge left, Edge right);
163 static inline Edge constraintXOR(CNF *cnf, Edge left, Edge right) {return constraintNegate(constraintIFF(cnf, left,right));}
164 Edge constraintITE(CNF *cnf, Edge cond, Edge thenedge, Edge elseedge);
165 Edge constraintNewVar(CNF *cnf);
166 void countPass(CNF *cnf);
167 void countConstraint(CNF *cnf, VectorEdge *stack, Edge e);
168 void addConstraintCNF(CNF *cnf, Edge constraint);
169 int solveCNF(CNF *cnf);
170 bool getValueCNF(CNF *cnf, Edge var);
171 void printCNF(Edge e);
172 Node *allocBaseNode(NodeType type, uint numEdges);
173 Node *allocResizeNode(uint capacity);
174 Edge cloneEdge(Edge e);
175 void addEdgeToResizeNode(Node ** node, Edge e);
176 void mergeFreeNodeToResizeNode(Node **node, Node * innode);
177 void mergeNodeToResizeNode(Node **node, Node * innode);
178 void freeEdgeRec(Edge e);
180 Edge generateBinaryConstraint(CNF *cnf, uint numvars, Edge *vars, uint value);
181 Edge generateLTValueConstraint(CNF *cnf, uint numvars, Edge *vars, uint value);
182 Edge generateEquivNVConstraint(CNF *cnf, uint numvars, Edge *var1, Edge *var2);
183 Edge generateLTConstraint(CNF *cnf, uint numvars, Edge *var1, Edge *var2);
184 Edge generateLTEConstraint(CNF *cnf, uint numvars, Edge *var1, Edge *var2);