+BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
+ BooleanEdge newarray[asize];
+ switch (op) {
+ case SATC_NOT: {
+ return array[0].negate();
+ }
+ case SATC_IFF: {
+ for (uint i = 0; i < 2; i++) {
+ if (array[i]->type == BOOLCONST) {
+ if (array[i]->isTrue()) {
+ return array[1 - i];
+ } else {
+ newarray[0] = array[1 - i];
+ return applyLogicalOperation(SATC_NOT, newarray, 1);
+ }
+ } else if (array[i]->type == LOGICOP) {
+ BooleanLogic *b =(BooleanLogic *)array[i].getBoolean();
+ if (b->replaced) {
+ return rewriteLogicalOperation(op, array, asize);
+ }
+ }
+ }
+ break;
+ }
+ case SATC_OR: {
+ for (uint i =0; i <asize; i++) {
+ newarray[i] = applyLogicalOperation(SATC_NOT, array[i]);
+ }
+ return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_AND, newarray, asize));
+ }
+ case SATC_AND: {
+ uint newindex = 0;
+ for (uint i = 0; i < asize; i++) {
+ BooleanEdge b = array[i];
+ if (b->type == LOGICOP) {
+ if (((BooleanLogic *)b.getBoolean())->replaced)
+ return rewriteLogicalOperation(op, array, asize);
+ }
+ if (b->type == BOOLCONST) {
+ if (b->isTrue())
+ continue;
+ else
+ return boolFalse;
+ } else
+ newarray[newindex++] = b;
+ }
+ if (newindex == 0) {
+ return boolTrue;
+ } else if (newindex == 1) {
+ return newarray[0];
+ } else {
+ bsdqsort(newarray, newindex, sizeof(BooleanEdge), ptrcompares);
+ array = newarray;
+ asize = newindex;
+ }
+ break;
+ }
+ case SATC_XOR: {
+ //handle by translation
+ return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_IFF, array, asize));
+ }
+ case SATC_IMPLIES: {
+ //handle by translation
+ return applyLogicalOperation(SATC_OR, applyLogicalOperation(SATC_NOT, array[0]), array[1]);
+ }
+ }
+
+ ASSERT(asize != 0);