Bug fix for removing must edges...They also need to update constraints
[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         encArraySize(0),
17         numVars(0) {
18 }
19
20 ElementEncoding::~ElementEncoding() {
21         if (variables != NULL)
22                 ourfree(variables);
23         if (encodingArray != NULL)
24                 ourfree(encodingArray);
25         if (inUseArray != NULL)
26                 ourfree(inUseArray);
27 }
28
29 void ElementEncoding::allocEncodingArrayElement(uint size) {
30         encodingArray = (uint64_t *) ourcalloc(1, sizeof(uint64_t) * size);
31         encArraySize = size;
32 }
33
34 void ElementEncoding::allocInUseArrayElement(uint size) {
35         uint bytes = ((size + ((1 << 9) - 1)) >> 6) & ~7;//Depends on size of inUseArray
36         inUseArray = (uint64_t *) ourcalloc(1, bytes);
37 }
38
39 void ElementEncoding::setElementEncodingType(ElementEncodingType _type) {
40         type = _type;
41 }
42
43 void ElementEncoding::encodingArrayInitialization() {
44         Set *set = element->getRange();
45         uint size = set->getSize();
46         uint encSize = getSizeEncodingArray(size);
47         allocEncodingArrayElement(encSize);
48         allocInUseArrayElement(encSize);
49         for (uint i = 0; i < size; i++) {
50                 encodingArray[i] = set->getMemberAt(i);
51                 setInUseElement(i);
52         }
53 }
54
55 void ElementEncoding::print() {
56         model_print("%s ", elemEncTypeNames[type]);
57         if (type == BINARYINDEX) {
58                 for (uint i = 0; i < encArraySize; i++) {
59                         if (i != 0)
60                                 model_print(" ,");
61                         if (isinUseElement(i))
62                                 model_print("%" PRIu64 "", encodingArray[i]);
63                         else
64                                 model_print("_");
65                 }
66         }
67 }