#include <string.h>
#include <stdlib.h>
+VectorImpl(Edge, Edge, 16)
+
CNF * createCNF() {
CNF * cnf=ourmalloc(sizeof(CNF));
cnf->varcount=1;
cnf->node_array=ourcalloc(1, sizeof(Node *)*cnf->capacity);
cnf->size=0;
cnf->maxsize=(uint)(((double)cnf->capacity)*LOAD_FACTOR);
- return cnf;
+ allocInlineDefVectorEdge(& cnf->constraints);
+ return cnf;
}
void deleteCNF(CNF * cnf) {
if (n!=NULL)
ourfree(n);
}
+ deleteVectorArrayEdge(& cnf->constraints);
ourfree(cnf->node_array);
ourfree(cnf);
}
Node * allocNode(NodeType type, uint numEdges, Edge * edges, uint hashcode) {
Node *n=(Node *)ourmalloc(sizeof(Node)+sizeof(Edge)*numEdges);
memcpy(n->edges, edges, sizeof(Edge)*numEdges);
- n->numEdges=numEdges;
n->flags.type=type;
+ n->flags.wasExpanded=0;
+ n->numEdges=numEdges;
n->hashCode=hashcode;
+ n->intAnnot[0]=0;n->intAnnot[1]=0;
+ n->ptrAnnot[0]=NULL;n->ptrAnnot[1]=NULL;
return n;
}
return result;
}
+void addConstraint(CNF *cnf, Edge constraint) {
+ pushVectorEdge(&cnf->constraints, constraint);
+}
+
Edge constraintNewVar(CNF *cnf) {
uint varnum=cnf->varcount++;
Edge e={(Node *) ((((uintptr_t)varnum) << VAR_SHIFT) | EDGE_IS_VAR_CONSTANT) };
return e;
}
+
+void countPass(CNF *cnf) {
+ uint numConstraints=getSizeVectorEdge(&cnf->constraints);
+ VectorEdge *ve=allocDefVectorEdge();
+ for(uint i=0; i<numConstraints;i++) {
+ countConstraint(cnf, ve, getVectorEdge(&cnf->constraints, i));
+ }
+ deleteVectorEdge(ve);
+}
+
+void countConstraint(CNF *cnf, VectorEdge *stack, Edge e) {
+ //Skip constants and variables...
+ if (edgeIsVarConst(e))
+ return;
+
+ clearVectorEdge(stack);pushVectorEdge(stack, e);
+
+ while(getSizeVectorEdge(stack) != 0) {
+ Edge e=lastVectorEdge(stack); popVectorEdge(stack);
+ bool polarity=isNegEdge(e);
+ Node *n=getNodePtrFromEdge(e);
+ if (getExpanded(n, polarity)) {
+ if (n->flags.type == NodeType_IFF ||
+ n->flags.type == NodeType_ITE) {
+ Edge pExp={n->ptrAnnot[polarity]};
+ getNodePtrFromEdge(pExp)->intAnnot[0]++;
+ } else {
+ n->intAnnot[polarity]++;
+ }
+ } else {
+ setExpanded(n, polarity); n->intAnnot[polarity]=1;
+
+ if (n->flags.type == NodeType_ITE||
+ n->flags.type == NodeType_IFF) {
+ n->intAnnot[polarity]=0;
+ Edge tst=n->edges[0];
+ Edge thenedge=n->edges[1];
+ Edge elseedge=n->flags.type == NodeType_IFF? constraintNegate(thenedge): n->edges[2];
+
+ }
+ }
+
+ }
+
+}
#ifndef NODEEDGE_H
#define NODEEDGE_H
#include "classlist.h"
+#include "vector.h"
#define NEGATE_EDGE 1
#define EDGE_IS_VAR_CONSTANT 2
Node * node_ptr;
};
+VectorDef(Edge, Edge)
+
enum NodeType {
NodeType_AND,
NodeType_ITE,
struct NodeFlags {
NodeType type:2;
+ int wasExpanded:2;
};
typedef struct NodeFlags NodeFlags;
NodeFlags flags;
uint numEdges;
uint hashCode;
+ uint intAnnot[2];
+ void * ptrAnnot[2];
Edge edges[];
};
uint mask;
uint maxsize;
Node ** node_array;
+ VectorEdge constraints;
};
typedef struct CNF CNF;
+static inline bool getExpanded(Node *n, int isNegated) {
+ return n->flags.wasExpanded & (1<<isNegated);
+}
+
+static inline void setExpanded(Node *n, int isNegated) {
+ n->flags.wasExpanded |= (1<<isNegated);
+}
+
static inline Edge constraintNegate(Edge e) {
Edge enew = { (Node *) (((uintptr_t) e.node_ptr) ^ NEGATE_EDGE)};
return enew;
return (((uintptr_t) e.node_ptr) & ~((uintptr_t)NEGATE_EDGE)) == EDGE_IS_VAR_CONSTANT;
}
+static inline bool edgeIsVarConst(Edge e) {
+ return ((uintptr_t)e.node_ptr) & EDGE_IS_VAR_CONSTANT;
+}
+
uint hashNode(NodeType type, uint numEdges, Edge * edges);
Node * allocNode(NodeType type, uint numEdges, Edge * edges, uint hashCode);
bool compareNodes(Node * node, NodeType type, uint numEdges, Edge *edges);
Edge constraintIFF(CNF * cnf, Edge left, Edge right);
Edge constraintITE(CNF * cnf, Edge cond, Edge thenedge, Edge elseedge);
Edge constraintNewVar(CNF *cnf);
+void countPass(CNF *cnf);
+void countConstraint(CNF *cnf, VectorEdge * stack, Edge e);
+
Edge E_True={(Node *)(uintptr_t) EDGE_IS_VAR_CONSTANT};
Edge E_False={(Node *)(uintptr_t) (EDGE_IS_VAR_CONSTANT | NEGATE_EDGE)};
ArrayDef(Boolean, Boolean *);
ArrayDef(Set, Set *);
-
-VectorDef(Table, Table *, 4);
-VectorDef(Set, Set *, 4);
-VectorDef(Boolean, Boolean *, 4);
-VectorDef(Constraint, Constraint *, 4);
-VectorDef(Function, Function *, 4);
-VectorDef(Predicate, Predicate *, 4);
-VectorDef(Element, Element *, 4);
-VectorDef(Order, Order *, 4);
-VectorDef(TableEntry, TableEntry *, 4);
-VectorDef(ASTNode, ASTNode *, 4);
-VectorDef(Int, uint64_t, 4);
-
-
+VectorDef(Table, Table *);
+VectorDef(Set, Set *);
+VectorDef(Boolean, Boolean *);
+VectorDef(Constraint, Constraint *);
+VectorDef(Function, Function *);
+VectorDef(Predicate, Predicate *);
+VectorDef(Element, Element *);
+VectorDef(Order, Order *);
+VectorDef(TableEntry, TableEntry *);
+VectorDef(ASTNode, ASTNode *);
+VectorDef(Int, uint64_t);
HashTableDef(Void, void *, void *);
HashTableDef(BoolConst, BooleanOrder *, Constraint *);
#define VECTOR_H
#include <string.h>
-#define VectorDef(name, type, defcap) \
+#define VectorDef(name, type) \
struct Vector ## name { \
uint size; \
uint capacity; \
Vector ## name * allocDefVector ## name(); \
Vector ## name * allocVectorArray ## name(uint capacity, type * array); \
void pushVector ## name(Vector ## name *vector, type item); \
- type getVector ## name(Vector ## name *vector, uint index); \
+ type lastVector ## name(Vector ## name *vector); \
+ void popVector ## name(Vector ## name *vector); \
+ type getVector ## name(Vector ## name *vector, uint index); \
void setVector ## name(Vector ## name *vector, uint index, type item); \
uint getSizeVector ## name(Vector ## name *vector); \
void deleteVector ## name(Vector ## name *vector); \
memcpy(tmp->array, array, capacity * sizeof(type)); \
return tmp; \
} \
- void pushVector ## name(Vector ## name *vector, type item) { \
+ void popVector ## name(Vector ## name *vector) { \
+ vector->size--; \
+ } \
+ type lastVector ## name(Vector ## name *vector) { \
+ return vector->array[vector->size]; \
+ } \
+ void pushVector ## name(Vector ## name *vector, type item) { \
if (vector->size >= vector->capacity) { \
uint newcap=vector->capacity * 2; \
vector->array=(type *)ourrealloc(vector->array, newcap); \