//TODO: Should handle sharing of AST Nodes without recoding them a second time
-SATEncoder *allocSATEncoder(CSolver *solver) {
- SATEncoder *This = (SATEncoder *)ourmalloc(sizeof (SATEncoder));
- This->solver = solver;
- This->varcount = 1;
- This->cnf = createCNF();
- return This;
+SATEncoder::SATEncoder(CSolver * _solver) :
+ varcount(1),
+ cnf(createCNF()),
+ solver(_solver) {
}
-void deleteSATEncoder(SATEncoder *This) {
- deleteCNF(This->cnf);
- ourfree(This);
+SATEncoder::~SATEncoder() {
+ deleteCNF(cnf);
}
-void encodeAllSATEncoder(CSolver *csolver, SATEncoder *This) {
+void SATEncoder::encodeAllSATEncoder(CSolver *csolver) {
HSIteratorBoolean *iterator = csolver->getConstraints();
while (iterator->hasNext()) {
Boolean *constraint = iterator->next();
model_print("Encoding All ...\n\n");
- Edge c = encodeConstraintSATEncoder(This, constraint);
+ Edge c = encodeConstraintSATEncoder(constraint);
model_print("Returned Constraint in EncodingAll:\n");
ASSERT( !equalsEdge(c, E_BOGUS));
- addConstraintCNF(This->cnf, c);
+ addConstraintCNF(cnf, c);
}
delete iterator;
}
-Edge encodeConstraintSATEncoder(SATEncoder *This, Boolean *constraint) {
+Edge SATEncoder::encodeConstraintSATEncoder(Boolean *constraint) {
switch (GETBOOLEANTYPE(constraint)) {
case ORDERCONST:
- return encodeOrderSATEncoder(This, (BooleanOrder *) constraint);
+ return encodeOrderSATEncoder(this, (BooleanOrder *) constraint);
case BOOLEANVAR:
- return encodeVarSATEncoder(This, (BooleanVar *) constraint);
+ return encodeVarSATEncoder(this, (BooleanVar *) constraint);
case LOGICOP:
- return encodeLogicSATEncoder(This, (BooleanLogic *) constraint);
+ return encodeLogicSATEncoder(this, (BooleanLogic *) constraint);
case PREDICATEOP:
- return encodePredicateSATEncoder(This, (BooleanPredicate *) constraint);
+ return encodePredicateSATEncoder(this, (BooleanPredicate *) constraint);
default:
model_print("Unhandled case in encodeConstraintSATEncoder %u", GETBOOLEANTYPE(constraint));
exit(-1);
Edge encodeLogicSATEncoder(SATEncoder *This, BooleanLogic *constraint) {
Edge array[constraint->inputs.getSize()];
for (uint i = 0; i < constraint->inputs.getSize(); i++)
- array[i] = encodeConstraintSATEncoder(This, constraint->inputs.get(i));
+ array[i] = This->encodeConstraintSATEncoder(constraint->inputs.get(i));
switch (constraint->op) {
case L_AND:
#include "structs.h"
#include "inc_solver.h"
#include "constraint.h"
-
-struct SATEncoder {
- uint varcount;
- CNF *cnf;
- CSolver *solver;
-};
-
#include "satelemencoder.h"
#include "satorderencoder.h"
#include "satfunctableencoder.h"
-SATEncoder *allocSATEncoder(CSolver *solver);
-void deleteSATEncoder(SATEncoder *This);
-void encodeAllSATEncoder(CSolver *csolver, SATEncoder *This);
-Edge getNewVarSATEncoder(SATEncoder *This);
-void getArrayNewVarsSATEncoder(SATEncoder *encoder, uint num, Edge *carray);
-Edge encodeConstraintSATEncoder(SATEncoder *This, Boolean *constraint);
-Edge encodeVarSATEncoder(SATEncoder *This, BooleanVar *constraint);
-Edge encodeLogicSATEncoder(SATEncoder *This, BooleanLogic *constraint);
-Edge encodePredicateSATEncoder(SATEncoder *This, BooleanPredicate *constraint);
-Edge encodeTablePredicateSATEncoder(SATEncoder *This, BooleanPredicate *constraint);
+class SATEncoder {
+ public:
+ uint varcount;
+ CNF *cnf;
+ CSolver *solver;
+
+ SATEncoder(CSolver *solver);
+ ~SATEncoder();
+ void encodeAllSATEncoder(CSolver *csolver);
+ Edge encodeConstraintSATEncoder(Boolean *constraint);
+ MEMALLOC;
+};
-void encodeElementSATEncoder(SATEncoder *encoder, Element *This);
-void encodeElementFunctionSATEncoder(SATEncoder *encoder, ElementFunction *This);
-void encodeTableElementFunctionSATEncoder(SATEncoder *encoder, ElementFunction *This);
+ Edge getNewVarSATEncoder(SATEncoder *This);
+ void getArrayNewVarsSATEncoder(SATEncoder *encoder, uint num, Edge *carray);
+ Edge encodeVarSATEncoder(SATEncoder *This, BooleanVar *constraint);
+ Edge encodeLogicSATEncoder(SATEncoder *This, BooleanLogic *constraint);
+ Edge encodePredicateSATEncoder(SATEncoder *This, BooleanPredicate *constraint);
+ Edge encodeTablePredicateSATEncoder(SATEncoder *This, BooleanPredicate *constraint);
+ void encodeElementSATEncoder(SATEncoder *encoder, Element *This);
+ void encodeElementFunctionSATEncoder(SATEncoder *encoder, ElementFunction *This);
+ void encodeTableElementFunctionSATEncoder(SATEncoder *encoder, ElementFunction *This);
#endif
uint size = table->entries->getSize();
bool generateNegation = encType == ENUMERATEIMPLICATIONSNEGATE;
Edge constraints[size];
- Edge undefConst = encodeConstraintSATEncoder(This, constraint->undefStatus);
+ Edge undefConst = This->encodeConstraintSATEncoder(constraint->undefStatus);
printCNF(undefConst);
model_print("**\n");
HSIteratorTableEntry *iterator = table->entries->iterator();
vals[i] = set->getElement(indices[i]);
}
bool hasOverflow = false;
- Edge undefConstraint = encodeConstraintSATEncoder (This, constraint->undefStatus);
+ Edge undefConstraint = This->encodeConstraintSATEncoder(constraint->undefStatus);
printCNF(undefConstraint);
bool notfinished = true;
while (notfinished) {
break;
}
case FLAGFORCEUNDEFINED: {
- Edge undefConst = encodeConstraintSATEncoder(This, func->overflowstatus);
+ Edge undefConst = This->encodeConstraintSATEncoder(func->overflowstatus);
row = constraintIMPLIES(This->cnf,constraintAND(This->cnf, inputNum, carray), constraintAND2(This->cnf, output, constraintNegate(undefConst)));
break;
}
vals[i] = set->getElement(indices[i]);
}
- Edge undefConstraint = encodeConstraintSATEncoder(This, elemFunc->overflowstatus);
+ Edge undefConstraint = This->encodeConstraintSATEncoder(elemFunc->overflowstatus);
bool notfinished = true;
while (notfinished) {
Edge carray[numDomains + 1];
#include <inttypes.h>
class CSolver;
-struct SATEncoder;
-typedef struct SATEncoder SATEncoder;
-
+class SATEncoder;
class Boolean;
class BooleanOrder;
class BooleanVar;
struct TableEntry;
typedef struct TableEntry TableEntry;
-struct OrderEncoder;
-typedef struct OrderEncoder OrderEncoder;
-
class Tuner;
class TunableDesc;
CSolver::CSolver() : unsat(false) {
tuner = new Tuner();
- satEncoder = allocSATEncoder(this);
+ satEncoder = new SATEncoder(this);
}
/** This function tears down the solver and the entire AST */
delete allFunctions.get(i);
}
- deleteSATEncoder(satEncoder);
+ delete satEncoder;
delete tuner;
}
computePolarities(this);
orderAnalysis(this);
naiveEncodingDecision(this);
- encodeAllSATEncoder(this, satEncoder);
+ satEncoder->encodeAllSATEncoder(this);
int result = solveCNF(satEncoder->cnf);
model_print("sat_solver's result:%d\tsolutionSize=%d\n", result, satEncoder->cnf->solver->solutionsize);
for (int i = 1; i <= satEncoder->cnf->solver->solutionsize; i++) {