bc64cc44f3a25777492142befd7570e44aec49fb
[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 "tableentry.h"
13 #include "order.h"
14 #include <strings.h>
15
16 void naiveEncodingDecision(CSolver* This) {
17         for (uint i=0; i < getSizeVectorBoolean(This->constraints); i++) {
18                 Boolean* boolean = getVectorBoolean(This->constraints, i);
19                 naiveEncodingConstraint(boolean);
20         }
21 }
22
23 void naiveEncodingConstraint(Boolean * This) {
24         switch(GETBOOLEANTYPE(This)) {
25         case BOOLEANVAR: {
26                 return;
27         }
28         case ORDERCONST: {
29                 setOrderEncodingType( ((BooleanOrder*)This)->order, PAIRWISE );
30                 return;
31         }
32         case LOGICOP: {
33                 naiveEncodingLogicOp((BooleanLogic *) This);
34                 return;
35         }
36         case PREDICATEOP: {
37                 naiveEncodingPredicate((BooleanPredicate *) This);
38                 return;
39         }
40         default:
41                 ASSERT(0);
42         }
43 }
44
45 void naiveEncodingLogicOp(BooleanLogic * This) {
46         for(uint i=0; i < getSizeArrayBoolean(&This->inputs); i++) {
47                 naiveEncodingConstraint(getArrayBoolean(&This->inputs, i));
48         }
49 }
50
51 void naiveEncodingPredicate(BooleanPredicate * This) {
52         FunctionEncoding *encoding = getPredicateFunctionEncoding(This);
53         if (getFunctionEncodingType(encoding) == FUNC_UNASSIGNED)
54                 setFunctionEncodingType(getPredicateFunctionEncoding(This), ENUMERATEIMPLICATIONS);
55
56         for(uint i=0; i < getSizeArrayElement(&This->inputs); i++) {
57                 Element *element=getArrayElement(&This->inputs, i);
58                 naiveEncodingElement(element);
59         }
60 }
61
62 void naiveEncodingElement(Element * This) {
63         ElementEncoding * encoding = getElementEncoding(This);
64         if (getElementEncodingType(encoding) == ELEM_UNASSIGNED) {
65                 setElementEncodingType(encoding, BINARYINDEX);
66                 encodingArrayInitialization(encoding);
67         }
68         
69         if(GETELEMENTTYPE(This) == ELEMFUNCRETURN) {
70                 ElementFunction *function=(ElementFunction *) This;
71                 for(uint i=0; i < getSizeArrayElement(&function->inputs); i++) {
72                         Element * element=getArrayElement(&function->inputs, i);
73                         naiveEncodingElement(element);
74                 }
75                 FunctionEncoding *encoding = getElementFunctionEncoding(function);
76                 if (getFunctionEncodingType(encoding) == FUNC_UNASSIGNED)
77                         setFunctionEncodingType(getElementFunctionEncoding(function), ENUMERATEIMPLICATIONS);
78         }
79 }
80
81 uint getSizeEncodingArray(ElementEncoding *This, uint setSize){
82         switch(This->type){
83                 case BINARYINDEX:
84                         return NEXTPOW2(setSize);
85                 case ONEHOT:
86                 case UNARY:
87                         return setSize;
88                 default:
89                         ASSERT(0);
90         }
91         return -1;
92 }
93
94 void encodingArrayInitialization(ElementEncoding *This) {
95         Element * element=This->element;
96         Set * set= getElementSet(element);
97         ASSERT(set->isRange==false);
98         uint size=getSizeVectorInt(set->members);
99         uint encSize=getSizeEncodingArray(This, size);
100         allocEncodingArrayElement(This, encSize);
101         allocInUseArrayElement(This, encSize);
102         for(uint i=0;i<size;i++) {
103                 This->encodingArray[i]=getVectorInt(set->members, i);
104                 setInUseElement(This, i);
105         }
106 }