Bug fix: typos
[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         type(ELEM_UNASSIGNED),
12         element(_element),
13         variables(NULL),
14         encodingArray(NULL),
15         inUseArray(NULL),
16         edgeArray(NULL),
17         polarityArray(NULL),
18         encArraySize(0),
19         encoding(EENC_UNKNOWN),
20         numVars(0) {
21 }
22
23 ElementEncoding::~ElementEncoding() {
24         if (variables != NULL)
25                 ourfree(variables);
26         if (encodingArray != NULL)
27                 ourfree(encodingArray);
28         if (inUseArray != NULL)
29                 ourfree(inUseArray);
30         if (edgeArray != NULL)
31                 ourfree(edgeArray);
32         if (polarityArray != NULL)
33                 ourfree(polarityArray);
34 }
35
36 void ElementEncoding::allocEncodingArrayElement(uint size) {
37         encodingArray = (uint64_t *) ourcalloc(1, sizeof(uint64_t) * size);
38         encArraySize = size;
39 }
40
41 void ElementEncoding::allocInUseArrayElement(uint size) {
42         uint bytes = ((size + 63) >> 3) & ~7;   //Depends on size of inUseArray
43         inUseArray = (uint64_t *) ourcalloc(1, bytes);
44 }
45
46 void ElementEncoding::setElementEncodingType(ElementEncodingType _type) {
47         type = _type;
48 }
49
50 void ElementEncoding::encodingArrayInitialization() {
51         Set *set = element->getRange();
52         uint size = set->getSize();
53         uint encSize = getSizeEncodingArray(size);
54         allocEncodingArrayElement(encSize);
55         allocInUseArrayElement(encSize);
56         for (uint i = 0; i < size; i++) {
57                 encodingArray[i] = set->getElement(i);
58                 setInUseElement(i);
59         }
60 }
61
62 void ElementEncoding::print() {
63         model_print("%s ", elemEncTypeNames[type]);
64         if (type == BINARYINDEX) {
65                 for (uint i = 0; i < encArraySize; i++) {
66                         if (i != 0)
67                                 model_print(", ");
68                         if (isinUseElement(i))
69                                 model_print("%" PRIu64 "", encodingArray[i]);
70                         else
71                                 model_print("_");
72                 }
73         }
74 }