Merge branch 'hamed' into brian
[satune.git] / src / AST / boolean.c
1 #include "boolean.h"
2 #include "structs.h"
3 #include "csolver.h"
4 #include "element.h"
5 #include "order.h"
6
7 Boolean* allocBoolean(VarType t) {
8         BooleanVar* tmp=(BooleanVar *) ourmalloc(sizeof (BooleanVar));
9         GETBOOLEANTYPE(tmp)=BOOLEANVAR;
10         tmp->vtype=t;
11         tmp->var=NULL;
12         allocInlineDefVectorBoolean(GETBOOLEANPARENTS(tmp));
13         return & tmp->base;
14 }
15
16 Boolean* allocBooleanOrder(Order* order, uint64_t first, uint64_t second) {
17         BooleanOrder* tmp=(BooleanOrder *) ourmalloc(sizeof (BooleanOrder));
18         GETBOOLEANTYPE(tmp)=ORDERCONST;
19         tmp->order=order;
20         tmp->first=first;
21         tmp->second=second;
22         //FIXME: what if client calls this function with the same arguments?
23         //Instead of vector we should keep a hashtable from PAIR->BOOLEANOrder with
24         //asymmetric hash function.  
25         pushVectorBoolean(&order->constraints, &tmp->base);
26         allocInlineDefVectorBoolean(GETBOOLEANPARENTS(tmp));
27         return & tmp -> base;
28 }
29
30 Boolean * allocBooleanPredicate(Predicate * predicate, Element ** inputs, uint numInputs){
31         BooleanPredicate* This = (BooleanPredicate*) ourmalloc(sizeof(BooleanPredicate));
32         GETBOOLEANTYPE(This)= PREDICATEOP;
33         This->predicate=predicate;
34         allocInlineArrayInitElement(&This->inputs, inputs, numInputs);
35         allocInlineDefVectorBoolean(GETBOOLEANPARENTS(This));
36
37         for(uint i=0;i<numInputs;i++) {
38                 pushVectorASTNode(GETELEMENTPARENTS(inputs[i]), (ASTNode *)This);
39         }
40         initPredicateEncoding(&This->encoding, (Boolean *) This);
41
42         return & This->base;
43 }
44
45 Boolean * allocBooleanLogicArray(CSolver *solver, LogicOp op, Boolean ** array, uint asize){
46         BooleanLogic * This = ourmalloc(sizeof(BooleanLogic));
47         allocInlineDefVectorBoolean(GETBOOLEANPARENTS(This));
48         allocInlineArrayInitBoolean(&This->inputs, array, asize);
49         pushVectorBoolean(solver->allBooleans, (Boolean *) This);
50         return & This->base;
51 }
52
53 void deleteBoolean(Boolean * This) {
54         switch(GETBOOLEANTYPE(This)){
55         case PREDICATEOP: {
56                 BooleanPredicate *bp=(BooleanPredicate *)This;
57                 deleteInlineArrayElement(& bp->inputs );
58                 deleteFunctionEncoding(& bp->encoding);
59                 break;
60         }
61         default:
62                 break;
63         }
64         deleteVectorArrayBoolean(GETBOOLEANPARENTS(This));
65         ourfree(This);
66 }