6 static inline uint boundedSize(uint x) { return (x > MERGESIZE)?MERGESIZE:x; }
8 LitVector * allocLitVector() {
9 LitVector *This=ourmalloc(sizeof(LitVector));
14 void initLitVector(LitVector *This) {
16 This->capacity=LITCAPACITY;
17 This->literals=ourmalloc(This->capacity * sizeof(Literal));
20 LitVector *cloneLitVector(LitVector *orig) {
21 LitVector *This=ourmalloc(sizeof(LitVector));
22 This->size=orig->size;
23 This->capacity=orig->capacity;
24 This->literals=ourmalloc(This->capacity * sizeof(Literal));
25 memcpy(This->literals, orig->literals, sizeof(Literal) * This->size);
29 void clearLitVector(LitVector *This) {
33 void freeLitVector(LitVector *This) {
34 ourfree(This->literals);
37 void deleteLitVector(LitVector *This) {
42 Literal getLiteralLitVector(LitVector *This, uint index) {
43 return This->literals[index];
46 void addLiteralLitVector(LitVector *This, Literal l) {
47 Literal labs = abs(l);
48 uint vec_size=This->size;
49 uint searchsize=boundedSize(vec_size);
51 for (; i < searchsize; i++) {
52 Literal curr = This->literals[i];
53 Literal currabs = abs(curr);
56 if (currabs == labs) {
58 This->size = 0; //either true or false now depending on whether this is a conj or disj
61 if ((++This->size) >= This->capacity) {
63 This->literals=ourrealloc(This->literals, This->capacity * sizeof(Literal));
66 if (vec_size < MERGESIZE) {
67 memmove(&This->literals[i+1], &This->literals[i], (vec_size-i) * sizeof(Literal));
70 This->literals[vec_size]=l;
75 CNFExpr * allocCNFExprBool(bool isTrue) {
76 CNFExpr *This=ourmalloc(sizeof(CNFExpr));
79 allocInlineVectorLitVector(&This->clauses, 2);
80 initLitVector(&This->singletons);
84 CNFExpr * allocCNFExprLiteral(Literal l) {
85 CNFExpr *This=ourmalloc(sizeof(CNFExpr));
88 allocInlineVectorLitVector(&This->clauses, 2);
89 initLitVector(&This->singletons);
90 addLiteralLitVector(&This->singletons, l);
94 void clearCNFExpr(CNFExpr *This, bool isTrue) {
95 for(uint i=0;i<getSizeVectorLitVector(&This->clauses);i++) {
96 deleteLitVector(getVectorLitVector(&This->clauses, i));
98 clearVectorLitVector(&This->clauses);
99 clearLitVector(&This->singletons);
104 void deleteCNFExpr(CNFExpr *This) {
105 for(uint i=0;i<getSizeVectorLitVector(&This->clauses);i++) {
106 deleteLitVector(getVectorLitVector(&This->clauses, i));
108 deleteVectorArrayLitVector(&This->clauses);
109 freeLitVector(&This->singletons);
113 void conjoinCNFLit(CNFExpr *This, Literal l) {
114 if (This->litSize==0 && !This->isTrue) //Handle False
117 This->litSize-=getSizeLitVector(&This->singletons);
118 addLiteralLitVector(&This->singletons, l);
119 uint newsize=getSizeLitVector(&This->singletons);
121 clearCNF(This, false); //We found a conflict
123 This->litSize+=getSizeLitVector(&This->singletons);
126 void copyCNF(CNFExpr *This, CNFExpr *expr, bool destroy) {
128 ourfree(This->singletons.literals);
129 ourfree(This->clauses.array);
130 This->litSize=expr->litSize;
131 This->singletons.literals=expr->singletons.literals;
132 This->singletons.capacity=expr->singletons.capacity;
133 This->clauses.size=expr->clauses.size;
134 This->clauses.array=expr->clauses.array;
135 This->clauses.capacity=expr->clauses.capacity;
138 for(uint i=0;i<getSizeLitVector(&expr->singletons);i++) {
139 Literal l=getLiteralLitVector(&expr->singletons,i);
140 addLiteralLitVector(&This->singletons, l);
142 for(uint i=0;i<getSizeVectorLitVector(&expr->clauses);i++) {
143 LitVector *lv=getVectorLitVector(&expr->clauses,i);
144 pushVectorLitVector(&This->clauses, cloneLitVector(lv));
146 This->litSize=expr->litSize;
150 void conjoinCNFExpr(CNFExpr *This, CNFExpr *expr, bool destroy) {
151 if (expr->litSize==0) {
153 clearCNF(This, false);
160 if (This->litSize==0) {
162 copyCNF(This, expr, destroy);
163 } else if (destroy) {
168 uint litSize=This->litSize;
169 litSize-=getSizeLitVector(&expr->singletons);
170 for(uint i=0;i<getSizeLitVector(&expr->singletons);i++) {
171 Literal l=getLiteralLitVector(&expr->singletons,i);
172 addLiteralLitVector(&This->singletons, l);
173 if (getSizeLitVector(&This->singletons)==0) {
175 clearCNF(This, false);
182 litSize+=getSizeLitVector(&expr->singletons);
184 for(uint i=0;i<getSizeVectorLitVector(&expr->clauses);i++) {
185 LitVector *lv=getVectorLitVector(&expr->clauses,i);
186 litSize+=getSizeLitVector(lv);
187 pushVectorLitVector(&This->clauses, lv);
189 clearVectorLitVector(&expr->clauses);
192 for(uint i=0;i<getSizeVectorLitVector(&expr->clauses);i++) {
193 LitVector *lv=getVectorLitVector(&expr->clauses,i);
194 litSize+=getSizeLitVector(lv);
195 pushVectorLitVector(&This->clauses, cloneLitVector(lv));
198 This->litSize=litSize;
201 void disjoinCNFLit(CNFExpr *This, Literal l) {
202 if (This->litSize==0) {
205 addLiteralLitVector(&This->singletons, l);
212 for(uint i=0;i<getSizeVectorLitVector(&This->clauses);i++) {
213 LitVector * lv=getVectorLitVector(&This->clauses, i);
214 addLiteralLitVector(lv, l);
215 uint newSize=getSizeLitVector(lv);
217 setVectorLitVector(&This->clauses, newindex++, lv);
223 setSizeVectorLitVector(&This->clauses, newindex);
225 bool hasSameSingleton=false;
226 for(uint i=0;i<getSizeLitVector(&This->singletons);i++) {
227 Literal lsing=getLiteralLitVector(&This->singletons, i);
229 hasSameSingleton=true;
230 } else if (lsing != -l) {
231 //Create new LitVector with both l and lsing
232 LitVector *newlitvec=allocLitVector();
233 addLiteralLitVector(newlitvec, l);
234 addLiteralLitVector(newlitvec, lsing);
236 pushVectorLitVector(&This->clauses, newlitvec);
239 clearLitVector(&This->singletons);
240 if (hasSameSingleton) {
241 addLiteralLitVector(&This->singletons, l);
243 } else if (litSize==0) {
244 This->isTrue=true;//we are true
246 This->litSize=litSize;
249 void disjoinCNFExpr(CNFExpr *This, CNFExpr *expr, bool destroy) {