6cb399346a9a14765ba7e318811c722dbedecf0d
[satune.git] / src / Encoders / elementencoding.cc
1 #include "elementencoding.h"
2 #include "common.h"
3 #include "naiveencoder.h"
4 #include "element.h"
5 #include "satencoder.h"
6 #include "set.h"
7
8 const char *elemEncTypeNames[] = {"UNASSIGNED", "ONEHOT", "UNARY", "BINARYINDEX", "BINARYVAL"};
9
10 ElementEncoding::ElementEncoding(Element *_element) :
11         anyValue(false),
12         type(ELEM_UNASSIGNED),
13         element(_element),
14         variables(NULL),
15         encodingArray(NULL),
16         inUseArray(NULL),
17         edgeArray(NULL),
18         polarityArray(NULL),
19         encArraySize(0),
20         encoding(EENC_UNKNOWN),
21         numVars(0) {
22 }
23
24 ElementEncoding::~ElementEncoding() {
25         if (variables != NULL)
26                 ourfree(variables);
27         if (encodingArray != NULL)
28                 ourfree(encodingArray);
29         if (inUseArray != NULL)
30                 ourfree(inUseArray);
31         if (edgeArray != NULL)
32                 ourfree(edgeArray);
33         if (polarityArray != NULL)
34                 ourfree(polarityArray);
35 }
36
37 void ElementEncoding::allocEncodingArrayElement(uint size) {
38         encodingArray = (uint64_t *) ourcalloc(1, sizeof(uint64_t) * size);
39         encArraySize = size;
40 }
41
42 void ElementEncoding::allocInUseArrayElement(uint size) {
43         uint bytes = ((size + 63) >> 3) & ~7;   //Depends on size of inUseArray
44         inUseArray = (uint64_t *) ourcalloc(1, bytes);
45 }
46
47 void ElementEncoding::setElementEncodingType(ElementEncodingType _type) {
48         type = _type;
49 }
50
51 void ElementEncoding::encodingArrayInitialization() {
52         Set *set = element->getRange();
53         uint size = set->getSize();
54         uint encSize = getSizeEncodingArray(size);
55         allocEncodingArrayElement(encSize);
56         allocInUseArrayElement(encSize);
57         for (uint i = 0; i < size; i++) {
58                 encodingArray[i] = set->getElement(i);
59                 setInUseElement(i);
60         }
61 }
62
63 void ElementEncoding::print() {
64         model_print("%s ", elemEncTypeNames[type]);
65         if (type == BINARYINDEX) {
66                 for (uint i = 0; i < encArraySize; i++) {
67                         if (i != 0)
68                                 model_print(", ");
69                         if (isinUseElement(i))
70                                 model_print("%" PRIu64 "", encodingArray[i]);
71                         else
72                                 model_print("_");
73                 }
74         }
75 }