1 #include "naiveencoder.h"
2 #include "elementencoding.h"
4 #include "functionencoding.h"
12 #include "tableentry.h"
13 //THIS FILE SHOULD HAVE NOTHING TO DO WITH CONSTRAINTS...
14 //#include "constraint.h"
17 NaiveEncoder* allocNaiveEncoder(){
18 NaiveEncoder* encoder = (NaiveEncoder*) ourmalloc(sizeof(NaiveEncoder));
19 allocInlineDefVectorConstraint(GETNAIVEENCODERALLCONSTRAINTS(encoder));
20 allocInlineDefVectorConstraint(GETNAIVEENCODERVARS(encoder));
25 void naiveEncodingDecision(CSolver* csolver, NaiveEncoder* encoder){
26 uint size = getSizeVectorElement(csolver->allElements);
27 for(uint i=0; i<size; i++){
28 Element* element = getVectorElement(csolver->allElements, i);
29 switch(GETELEMENTTYPE(element)){
31 setElementEncodingType(GETELEMENTENCODING(element), BINARYINDEX);
32 baseBinaryIndexElementAssign(GETELEMENTENCODING(element));
33 generateElementEncodingVariables(encoder,GETELEMENTENCODING(element));
36 setFunctionEncodingType(GETFUNCTIONENCODING(element), ENUMERATEIMPLICATIONS);
43 size = getSizeVectorBoolean(csolver->allBooleans);
44 for(uint i=0; i<size; i++){
45 Boolean* predicate = getVectorBoolean(csolver->allBooleans, i);
46 switch(GETBOOLEANTYPE(predicate)){
48 setFunctionEncodingType(GETFUNCTIONENCODING(predicate), ENUMERATEIMPLICATIONS);
57 // THIS SHOULD NOT BE HERE
59 void getArrayNewVars(NaiveEncoder* encoder, uint num, Constraint **carray) {
60 for(uint i=0;i<num;i++)
61 carray[i]=getNewVar(encoder);
65 // THIS SHOULD NOT BE HERE
67 Constraint * getNewVar(NaiveEncoder* encoder) {
68 Constraint* var = allocVarConstraint(VAR, encoder->varindex);
69 Constraint* notVar = allocVarConstraint(NOTVAR, encoder->varindex);
70 setNegConstraint(var, notVar);
71 setNegConstraint(notVar, var);
72 pushVectorConstraint(GETNAIVEENCODERVARS(encoder), var);
78 void baseBinaryIndexElementAssign(ElementEncoding *This) {
79 Element * element=This->element;
80 ASSERT(element->type == ELEMSET);
81 Set * set= ((ElementSet*)element)->set;
82 ASSERT(set->isRange==false);
83 uint size=getSizeVectorInt(set->members);
84 uint encSize=NEXTPOW2(size);
85 allocEncodingArrayElement(This, encSize);
86 allocInUseArrayElement(This, encSize);
88 for(uint i=0;i<size;i++) {
89 This->encodingArray[i]=getVectorInt(set->members, i);
90 setInUseElement(This, i);
92 This->numVars = NUMBITS(size-1);
93 This->variables = ourmalloc(sizeof(Constraint*)* This->numVars);
99 void encode(CSolver* csolver){
100 NaiveEncoder* encoder = allocNaiveEncoder();
101 naiveEncodingDecision( csolver, encoder);
102 uint size = getSizeVectorElement(csolver->allElements);
103 for(uint i=0; i<size; i++){
104 Element* element = getVectorElement(csolver->allElements, i);
105 switch(GETELEMENTTYPE(element)){
107 naiveEncodeFunctionPredicate(encoder, GETFUNCTIONENCODING(element));
114 size = getSizeVectorBoolean(csolver->allBooleans);
115 for(uint i=0; i<size; i++){
116 Boolean* predicate = getVectorBoolean(csolver->allBooleans, i);
117 switch(GETBOOLEANTYPE(predicate)){
119 naiveEncodeFunctionPredicate(encoder, GETFUNCTIONENCODING(predicate));
127 void naiveEncodeFunctionPredicate(NaiveEncoder* encoder, FunctionEncoding *This){
128 if(This->isFunction) {
129 ASSERT(GETELEMENTTYPE(This->op.function)==ELEMFUNCRETURN);
131 case ENUMERATEIMPLICATIONS:
132 naiveEncodeEnumeratedFunction(encoder, This);
135 naiveEncodeCircuitFunction(encoder, This);
141 ASSERT(GETBOOLEANTYPE(This->op.predicate) == PREDICATEOP);
142 BooleanPredicate* predicate = (BooleanPredicate*)This->op.predicate;
148 void naiveEncodeEnumeratedFunction(NaiveEncoder* encoder, FunctionEncoding* This){
149 ElementFunction* ef =(ElementFunction*)This->op.function;
150 switch(GETFUNCTIONTYPE(ef->function)){
152 naiveEncodeEnumTableFunc(encoder, ef);
155 naiveEncodeEnumOperatingFunc(encoder, ef);
162 void naiveEncodeEnumTableFunc(NaiveEncoder* encoder, ElementFunction* This){
163 ASSERT(GETFUNCTIONTYPE(This->function)==TABLEFUNC);
164 ArrayElement* elements= &This->inputs;
165 Table* table = ((FunctionTable*) (This->function))->table;
166 uint size = getSizeVectorTableEntry(&table->entries);
167 for(uint i=0; i<size; i++){
168 TableEntry* entry = getVectorTableEntry(&table->entries, i);
169 uint inputNum =getSizeArrayElement(elements);
170 Element* el= getArrayElement(elements, i);
171 Constraint* carray[inputNum];
172 for(uint j=0; j<inputNum; j++){
173 carray[inputNum] = getElementValueConstraint(el, entry->inputs[j]);
175 Constraint* row= allocConstraint(IMPLIES, allocArrayConstraint(AND, inputNum, carray),
176 getElementValueConstraint(table->range, entry->output));
177 pushVectorConstraint( GETNAIVEENCODERALLCONSTRAINTS(encoder), row);
182 void naiveEncodeEnumOperatingFunc(NaiveEncoder* encoder, ElementFunction* This){
187 void naiveEncodeCircuitFunction(NaiveEncoder* encoder, FunctionEncoding* This){
191 void deleteNaiveEncoder(NaiveEncoder* encoder){
192 deleteVectorArrayConstraint(&encoder->vars);