fixing elementSet and TablePredicate
[satune.git] / src / csolver.c
1 #include "csolver.h"
2 #include "set.h"
3 #include "mutableset.h"
4 #include "element.h"
5 #include "boolean.h"
6 #include "predicate.h"
7 #include "order.h"
8 #include "table.h"
9 #include "function.h"
10
11 CSolver * allocCSolver() {
12         CSolver * tmp=(CSolver *) ourmalloc(sizeof(CSolver));
13         tmp->constraints=allocDefVectorBoolean();
14         tmp->allBooleans=allocDefVectorBoolean();
15         tmp->allSets=allocDefVectorSet();
16         tmp->allElements=allocDefVectorElement();
17         tmp->allPredicates = allocDefVectorPredicate();
18         tmp->allTables = allocDefVectorTable();
19         tmp->allOrders = allocDefVectorOrder();
20         tmp->allFunctions = allocDefVectorFunction();
21         return tmp;
22 }
23
24 /** This function tears down the solver and the entire AST */
25
26 void deleteSolver(CSolver *This) {
27         deleteVectorBoolean(This->constraints);
28
29         uint size=getSizeVectorBoolean(This->allBooleans);
30         for(uint i=0;i<size;i++) {
31                 deleteBoolean(getVectorBoolean(This->allBooleans, i));
32         }
33         deleteVectorBoolean(This->allBooleans);
34
35         size=getSizeVectorSet(This->allSets);
36         for(uint i=0;i<size;i++) {
37                 deleteSet(getVectorSet(This->allSets, i));
38         }
39         deleteVectorSet(This->allSets);
40
41         size=getSizeVectorElement(This->allElements);
42         for(uint i=0;i<size;i++) {
43                 deleteElement(getVectorElement(This->allElements, i));
44         }
45         deleteVectorElement(This->allElements);
46
47         size=getSizeVectorTable(This->allTables);
48         for(uint i=0;i<size;i++) {
49                 deleteTable(getVectorTable(This->allTables, i));
50         }
51         deleteVectorTable(This->allTables);
52         
53         size=getSizeVectorPredicate(This->allPredicates);
54         for(uint i=0;i<size;i++) {
55                 deletePredicate(getVectorPredicate(This->allPredicates, i));
56         }
57         deleteVectorPredicate(This->allPredicates);
58
59         size=getSizeVectorOrder(This->allOrders);
60         for(uint i=0;i<size;i++) {
61                 deleteOrder(getVectorOrder(This->allOrders, i));
62         }
63         deleteVectorOrder(This->allOrders);
64         
65         size=getSizeVectorFunction(This->allFunctions);
66         for(uint i=0;i<size;i++) {
67                 deleteFunction(getVectorFunction(This->allFunctions, i));
68         }
69         deleteVectorFunction(This->allFunctions);
70         ourfree(This);
71 }
72
73 Set * createSet(CSolver * This, VarType type, uint64_t * elements, uint numelements) {
74         Set * set=allocSet(type, elements, numelements);
75         pushVectorSet(This->allSets, set);
76         return set;
77 }
78
79 Set * createRangeSet(CSolver * This, VarType type, uint64_t lowrange, uint64_t highrange) {
80         Set * set=allocSetRange(type, lowrange, highrange);
81         pushVectorSet(This->allSets, set);
82         return set;
83 }
84
85 MutableSet * createMutableSet(CSolver * This, VarType type) {
86         MutableSet * set=allocMutableSet(type);
87         pushVectorSet(This->allSets, set);
88         return set;
89 }
90
91 void addItem(CSolver *solver, MutableSet * set, uint64_t element) {
92         addElementMSet(set, element);
93 }
94
95 uint64_t createUniqueItem(CSolver *solver, MutableSet * set) {
96         uint64_t element=set->low++;
97         addElementMSet(set, element);
98         return element;
99 }
100
101 Element * getElementVar(CSolver *This, Set * set) {
102         Element * element=allocElementSet(set);
103         pushVectorElement(This->allElements, element);
104         return element;
105 }
106
107 Boolean * getBooleanVar(CSolver *solver, VarType type) {
108         Boolean* boolean= allocBoolean(type);
109         pushVectorBoolean(solver->allBooleans, boolean);
110         return boolean;
111 }
112
113 Function * createFunctionOperator(CSolver *solver, ArithOp op, Set ** domain, uint numDomain, Set * range,OverFlowBehavior overflowbehavior) {
114     Function* function = allocFunctionOperator(op, domain, numDomain, range, overflowbehavior);
115     pushVectorFunction(solver->allFunctions, function);
116     return function;
117 }
118
119 Predicate * createPredicateOperator(CSolver *solver, CompOp op, Set ** domain, uint numDomain) {
120         Predicate* predicate= allocPredicate(op, domain,numDomain);
121         pushVectorPredicate(solver->allPredicates, predicate);
122         return predicate;
123 }
124
125 Table * createTable(CSolver *solver, Set **domains, uint numDomain, Set * range) {
126         Table* table= allocTable(domains,numDomain,range);
127         pushVectorTable(solver->allTables, table);
128         return table;
129 }
130
131 void addTableEntry(CSolver *solver, Table* table, uint64_t* inputs, uint inputSize, uint64_t result) {
132     addNewTableEntry(table,inputs, inputSize,result);
133 }
134
135 Function * completeTable(CSolver *solver, Table * table) {
136     Function* function = allocFunctionTable(table);
137     pushVectorFunction(solver->allFunctions,function);
138     return function;
139 }
140
141 Element * applyFunction(CSolver *solver, Function * function, Element ** array, uint numArrays, Boolean * overflowstatus) {
142     Element* element= allocElementFunction(function,array,numArrays,overflowstatus);
143     pushVectorElement(solver->allElements, element);
144     return element;
145 }
146
147 Boolean * applyPredicate(CSolver *solver, Predicate * predicate, Element ** inputs, uint numInputs) {
148     Boolean* boolean= allocBooleanPredicate(predicate, inputs, numInputs);
149     pushVectorBoolean(solver->allBooleans, boolean);
150     return boolean;
151 }
152
153 Boolean * applyLogicalOperation(CSolver *solver, LogicOp op, Boolean ** array, uint asize) {
154     return allocBooleanLogicArray(solver, op, array, asize);
155 }
156
157 void addBoolean(CSolver *This, Boolean * constraint) {
158         pushVectorBoolean(This->constraints, constraint);
159 }
160
161 Order * createOrder(CSolver *solver, OrderType type, Set * set) {
162         Order* order = allocOrder(type, set);
163         pushVectorOrder(solver->allOrders, order);
164         return order;
165 }
166
167 Boolean * orderConstraint(CSolver *solver, Order * order, uint64_t first, uint64_t second) {
168         Boolean* constraint = allocBooleanOrder(order, first, second);
169         pushVectorBoolean(solver->allBooleans,constraint);
170         return constraint;
171 }