Finish hash/comparison functions
[satune.git] / src / AST / element.cc
1 #include "element.h"
2 #include "structs.h"
3 #include "set.h"
4 #include "constraint.h"
5 #include "function.h"
6 #include "table.h"
7 #include "csolver.h"
8
9 Element::Element(ASTNodeType _type) :
10         ASTNode(_type),
11         encoding(this) {
12 }
13
14 ElementSet::ElementSet(Set *s) :
15         Element(ELEMSET),
16         set(s) {
17 }
18
19 ElementFunction::ElementFunction(Function *_function, Element **array, uint numArrays, Boolean *_overflowstatus) :
20         Element(ELEMFUNCRETURN),
21         function(_function),
22         inputs(array, numArrays),
23         overflowstatus(_overflowstatus),
24         functionencoding(this) {
25         for (uint i = 0; i < numArrays; i++)
26                 array[i]->parents.push(this);
27 }
28
29 ElementConst::ElementConst(uint64_t _value, VarType _type, Set *_set) :
30         Element(ELEMCONST),
31         set(_set),
32         value(_value) {
33 }
34
35 Set *getElementSet(Element *This) {
36         switch (This->type) {
37         case ELEMSET:
38                 return ((ElementSet *)This)->set;
39         case ELEMCONST:
40                 return ((ElementConst *)This)->set;
41         case ELEMFUNCRETURN: {
42                 Function *func = ((ElementFunction *)This)->function;
43                 switch (func->type) {
44                 case TABLEFUNC:
45                         return ((FunctionTable *)func)->table->range;
46                 case OPERATORFUNC:
47                         return ((FunctionOperator *)func)->range;
48                 default:
49                         ASSERT(0);
50                 }
51         }
52         default:
53                 ASSERT(0);
54         }
55         ASSERT(0);
56         return NULL;
57 }
58
59 Element *ElementConst::clone(CSolver *solver, CloneMap *map) {
60         return solver->getElementConst(type, value);
61 }
62
63 Element *ElementSet::clone(CSolver *solver, CloneMap *map) {
64         Element *e = (Element *) map->get(this);
65         if (e != NULL)
66                 return e;
67         e = solver->getElementVar(set->clone(solver, map));
68         map->put(e, e);
69         return e;
70 }
71
72 Element *ElementFunction::clone(CSolver *solver, CloneMap *map) {
73         Element *array[inputs.getSize()];
74         for (uint i = 0; i < inputs.getSize(); i++) {
75                 array[i] = inputs.get(i)->clone(solver, map);
76         }
77         Element *e = solver->applyFunction(function->clone(solver, map), array, inputs.getSize(), overflowstatus->clone(solver, map));
78         return e;
79 }