fc76a4ad14d69c6d7dae6cb42b687d2b4f505eb0
[satune.git] / src / Encoders / elementencoding.c
1 #include "elementencoding.h"
2 #include "common.h"
3 #include "naiveencoder.h"
4 #include "element.h"
5
6 void initElementEncoding(ElementEncoding * This, Element *element) {
7         This->element=element;
8         This->type=ELEM_UNASSIGNED;
9         This->variables=NULL;
10         This->encodingArray=NULL;
11         This->inUseArray=NULL;
12         This->numVars=0;
13 }
14
15 void deleteElementEncoding(ElementEncoding *This) {
16         if (This->variables!=NULL)
17                 ourfree(This->variables);
18         if (This->encodingArray!=NULL)
19                 ourfree(This->encodingArray);
20         if (This->inUseArray!=NULL)
21                 ourfree(This->inUseArray);
22 }
23
24 void allocEncodingArrayElement(ElementEncoding *This, uint size) {
25         This->encodingArray=ourcalloc(1, sizeof(uint64_t)*size);
26 }
27
28 void allocInUseArrayElement(ElementEncoding *This, uint size) {
29         This->inUseArray=ourcalloc(1, size >> 6);
30 }
31
32 void allocElementConstraintVariables(ElementEncoding* This, uint numVars){
33         This->numVars = numVars;
34         This->variables = ourmalloc(sizeof(Constraint*) * numVars);
35 }
36
37 void setElementEncodingType(ElementEncoding* This, ElementEncodingType type){
38         This->type = type;
39 }
40
41 void generateBinaryIndexEncodingVars(NaiveEncoder* encoder, ElementEncoding* This){
42         ASSERT(This->type==BINARYINDEX);
43         uint size = getElementSize(This->element);
44         allocElementConstraintVariables(This, NUMBITS(size-1));
45         getArrayNewVars(encoder, This->numVars, This->variables);
46 }
47
48 void generateElementEncodingVariables(NaiveEncoder* encoder, ElementEncoding* This){
49         ASSERT(This->type!=ELEM_UNASSIGNED);
50         ASSERT(This->variables==NULL);
51         switch(This->type){
52                 case BINARYINDEX:
53                         generateBinaryIndexEncodingVars(encoder, This);
54                         break;
55                 default:
56                         ASSERT(0);
57         }
58 }