#include "predicate.h"
+#include "boolean.h"
+#include "set.h"
Predicate* allocPredicateOperator(CompOp op, Set ** domain, uint numDomain){
PredicateOperator* predicate = ourmalloc(sizeof(PredicateOperator));
return &predicate->base;
}
+void getEqualitySetIntersection(PredicateOperator* predicate, uint* size, uint64_t* result){
+ ASSERT( predicate->op == EQUALS);
+ //make sure equality has 2 operands
+ ASSERT(getSizeArraySet( &predicate->domains) == 2);
+ *size=0;
+ VectorInt* mems1 = getArraySet(&predicate->domains, 0)->members;
+ uint size1 = getSizeVectorInt(mems1);
+ VectorInt* mems2 = getArraySet(&predicate->domains, 1)->members;
+ uint size2 = getSizeVectorInt(mems2);
+ //FIXME:This isn't efficient, if we a hashset datastructure for Set, we
+ // can reduce it to O(n), but for now .... HG
+ for(uint i=0; i<size1; i++){
+ uint64_t tmp= getVectorInt(mems1, i);
+ for(uint j=0; j<size2; j++){
+ if(tmp == getVectorInt(mems2, j)){
+ result[(*size)++]=tmp;
+ break;
+ }
+ }
+ }
+
+}
+
void deletePredicate(Predicate* predicate){
switch(GETPREDICATETYPE(predicate)) {
case OPERATORPRED: {
}
Constraint * encodePredicateSATEncoder(SATEncoder * This, BooleanPredicate * constraint) {
- switch(GETPREDICATETYPE(constraint) ){
+ switch(GETPREDICATETYPE(constraint->predicate) ){
case TABLEPRED:
return encodeTablePredicateSATEncoder(This, constraint);
case OPERATORPRED:
VectorTableEntry* entries = &(((PredicateTable*)constraint->predicate)->table->entries);
FunctionEncodingType encType = constraint->encoding.type;
uint size = getSizeVectorTableEntry(entries);
- Constraint* constraints[size]; //FIXME: should add a space for the case that didn't match any entries
+ Constraint* constraints[size];
for(uint i=0; i<size; i++){
TableEntry* entry = getVectorTableEntry(entries, i);
if(encType==ENUMERATEIMPLICATIONS && entry->output!= true)
Constraint* carray[inputNum];
Element* el = getArrayElement(inputs, i);
for(uint j=0; j<inputNum; j++){
- carray[inputNum] = getElementValueConstraint(el, entry->inputs[j]);
+ carray[j] = getElementValueConstraint(el, entry->inputs[j]);
}
constraints[i]=allocArrayConstraint(AND, inputNum, carray);
}
Constraint* result= allocArrayConstraint(OR, size, constraints);
+ //FIXME: if it didn't match with any entry
return encType==ENUMERATEIMPLICATIONS? result: negateConstraint(result);
}
Constraint * encodeOperatorPredicateSATEncoder(SATEncoder * This, BooleanPredicate * constraint){
switch(constraint->encoding.type){
case ENUMERATEIMPLICATIONS:
- break;
+ return encodeEnumOperatorPredicateSATEncoder(This, constraint);
case CIRCUIT:
+ ASSERT(0);
break;
default:
ASSERT(0);
return NULL;
}
+Constraint * encodeEnumOperatorPredicateSATEncoder(SATEncoder * This, BooleanPredicate * constraint){
+ ASSERT(GETPREDICATETYPE(constraint)==OPERATORPRED);
+ PredicateOperator* predicate = (PredicateOperator*)constraint->predicate;
+ ASSERT(predicate->op == EQUALS); //For now, we just only support equals
+ //getting maximum size of in common elements between two sets!
+ uint size=getSizeVectorInt( getArraySet( &predicate->domains, 0)->members);
+ uint64_t commonElements [size];
+ getEqualitySetIntersection(predicate, &size, commonElements);
+ Constraint* carray[size];
+ Element* elem1 = getArrayElement( &constraint->inputs, 0);
+ Element* elem2 = getArrayElement( &constraint->inputs, 1);
+ for(uint i=0; i<size; i++){
+
+ carray[i] = allocConstraint(AND, getElementValueConstraint(elem1, commonElements[i]),
+ getElementValueConstraint(elem2, commonElements[i]) );
+ }
+ //FIXME: the case when there is no intersection ....
+ return allocArrayConstraint(OR, size, carray);
+}
+
Constraint* encodeFunctionElementSATEncoder(SATEncoder* encoder, ElementFunction *This){
switch(GETFUNCTIONTYPE(This->function)){
case TABLEFUNC: