Fixing bug with running the test
[satune.git] / src / Encoders / naiveencoder.c
1 #include "naiveencoder.h"
2 #include "elementencoding.h"
3 #include "element.h"
4 #include "functionencoding.h"
5 #include "function.h"
6 #include "set.h"
7 #include "common.h"
8 #include "structs.h"
9 #include "csolver.h"
10 #include "boolean.h"
11 #include "table.h"
12 #include <strings.h>
13
14 void naiveEncodingDecision(CSolver* csolver){
15         uint size = getSizeVectorElement(csolver->allElements);
16         for(uint i=0; i<size; i++){
17                 Element* element = getVectorElement(csolver->allElements, i);
18                 switch(GETELEMENTTYPE(element)){
19                         case ELEMSET:
20                                 setElementEncodingType(&((ElementSet*)element)->encoding, BINARYINDEX);
21                                 baseBinaryIndexElementAssign(&((ElementSet*)element)->encoding);
22                                 break;
23                         case ELEMFUNCRETURN: 
24                                 setFunctionEncodingType(&((ElementFunction*)element)->functionencoding, ENUMERATEIMPLICATIONS);
25                                 break;
26                         default:
27                                 ASSERT(0);
28                 }
29         }
30         
31         size = getSizeVectorBoolean(csolver->allBooleans);
32         for(uint i=0; i<size; i++){
33                 Boolean* predicate = getVectorBoolean(csolver->allBooleans, i);
34                 switch(GETBOOLEANTYPE(predicate)){
35                         case PREDICATEOP:
36                                 setFunctionEncodingType(&((BooleanPredicate*)predicate)->encoding, ENUMERATEIMPLICATIONS);
37                                 break;
38                         default:
39                                 continue;
40                 } 
41         }
42 }
43
44 void baseBinaryIndexElementAssign(ElementEncoding *This) {
45         Element * element=This->element;
46         ASSERT(element->type == ELEMSET);
47         Set * set= ((ElementSet*)element)->set;
48         ASSERT(set->isRange==false);
49         uint size=getSizeVectorInt(set->members);
50         uint encSize=NEXTPOW2(size);
51         allocEncodingArrayElement(This, encSize);
52         allocInUseArrayElement(This, encSize);
53
54         for(uint i=0;i<size;i++) {
55                 This->encodingArray[i]=getVectorInt(set->members, i);
56                 setInUseElement(This, i);
57         }
58 }
59
60
61 void encode(CSolver* csolver){
62         uint size = getSizeVectorElement(csolver->allElements);
63         for(uint i=0; i<size; i++){
64                 Element* element = getVectorElement(csolver->allElements, i);
65                 switch(GETELEMENTTYPE(element)){
66                         case ELEMFUNCRETURN: 
67                                 naiveEncodeFunctionPredicate(&((ElementFunction*)element)->functionencoding);
68                                 break;
69                         default:
70                                 continue;
71                 }
72         }
73         
74         size = getSizeVectorBoolean(csolver->allBooleans);
75         for(uint i=0; i<size; i++){
76                 Boolean* predicate = getVectorBoolean(csolver->allBooleans, i);
77                 switch(GETBOOLEANTYPE(predicate)){
78                         case PREDICATEOP:
79                                 naiveEncodeFunctionPredicate(&((BooleanPredicate*)predicate)->encoding);
80                                 break;
81                         default:
82                                 continue;
83                 } 
84         }
85 }
86
87 void naiveEncodeFunctionPredicate(FunctionEncoding *This){
88         if(This->isFunction) {
89                 ASSERT(GETELEMENTTYPE(This->op.function)==ELEMFUNCRETURN);
90                 switch(This->type){
91                         case ENUMERATEIMPLICATIONS:
92                                 naiveEncodeEnumeratedFunction(This);
93                                 break;
94                         case CIRCUIT:
95                                 naiveEncodeCircuitFunction(This);
96                                 break;
97                         default:
98                                 ASSERT(0);
99                 }
100         }else {
101                 ASSERT(GETBOOLEANTYPE(This->op.predicate) == PREDICATEOP);
102                 BooleanPredicate* predicate = (BooleanPredicate*)This->op.predicate;
103                 //FIXME
104                 
105         }
106 }
107
108
109 void naiveEncodeCircuitFunction(FunctionEncoding* This){
110         
111 }
112
113 void naiveEncodeEnumeratedFunction(FunctionEncoding* This){
114         ElementFunction* ef =(ElementFunction*)This->op.function;
115         Function * function = ef->function;
116         switch(GETFUNCTIONTYPE(function)){
117                 case TABLEFUNC:
118                         naiveEncodeEnumTableFunc(ef);
119                         break;
120                 case OPERATORFUNC:
121                         naiveEncodeEnumOperatingFunc(ef);
122                         break;
123                 default:
124                         ASSERT(0);
125         } 
126 }
127
128 void naiveEncodeEnumTableFunc(ElementFunction* This){
129         ASSERT(GETFUNCTIONTYPE(This->function)==TABLEFUNC);
130         ArrayElement* elements= &This->inputs;
131         Table* table = ((FunctionTable*) (This->function))->table;
132         uint size = getSizeVectorTableEntry(&table->entries);
133         for(uint i=0; i<size; i++){
134                 TableEntry* entry = getVectorTableEntry(&table->entries, i);
135                 //FIXME: generate Constraints
136         }
137         
138 }
139
140 void naiveEncodeEnumOperatingFunc(ElementFunction* This){
141         
142 }