Off by 1 bug
[satune.git] / src / Encoders / elementencoding.cc
index dde2692d6894c207e6fc541db5852e0848f9845f..19439dc426e88933e13a4561d878398316611cc8 100644 (file)
@@ -3,38 +3,65 @@
 #include "naiveencoder.h"
 #include "element.h"
 #include "satencoder.h"
+#include "set.h"
 
-void initElementEncoding(ElementEncoding *This, Element *element) {
-       This->element = element;
-       This->type = ELEM_UNASSIGNED;
-       This->variables = NULL;
-       This->encodingArray = NULL;
-       This->inUseArray = NULL;
-       This->numVars = 0;
-       This->encArraySize = 0;
+const char *elemEncTypeNames[] = {"UNASSIGNED", "ONEHOT", "UNARY", "BINARYINDEX", "BINARYVAL"};
+
+ElementEncoding::ElementEncoding(Element *_element) :
+       type(ELEM_UNASSIGNED),
+       element(_element),
+       variables(NULL),
+       encodingArray(NULL),
+       inUseArray(NULL),
+       encArraySize(0),
+       numVars(0) {
 }
 
-void deleteElementEncoding(ElementEncoding *This) {
-       if (This->variables != NULL)
-               ourfree(This->variables);
-       if (This->encodingArray != NULL)
-               ourfree(This->encodingArray);
-       if (This->inUseArray != NULL)
-               ourfree(This->inUseArray);
+ElementEncoding::~ElementEncoding() {
+       if (variables != NULL)
+               ourfree(variables);
+       if (encodingArray != NULL)
+               ourfree(encodingArray);
+       if (inUseArray != NULL)
+               ourfree(inUseArray);
 }
 
-void allocEncodingArrayElement(ElementEncoding *This, uint size) {
-       This->encodingArray = (uint64_t *) ourcalloc(1, sizeof(uint64_t) * size);
-       This->encArraySize = size;
+void ElementEncoding::allocEncodingArrayElement(uint size) {
+       encodingArray = (uint64_t *) ourcalloc(1, sizeof(uint64_t) * size);
+       encArraySize = size;
 }
 
-void allocInUseArrayElement(ElementEncoding *This, uint size) {
+void ElementEncoding::allocInUseArrayElement(uint size) {
        uint bytes = ((size + ((1 << 9) - 1)) >> 6) & ~7;//Depends on size of inUseArray
-       This->inUseArray = (uint64_t *) ourcalloc(1, bytes);
+       inUseArray = (uint64_t *) ourcalloc(1, bytes);
 }
 
-void setElementEncodingType(ElementEncoding *This, ElementEncodingType type) {
-       This->type = type;
+void ElementEncoding::setElementEncodingType(ElementEncodingType _type) {
+       type = _type;
 }
 
+void ElementEncoding::encodingArrayInitialization() {
+       Set *set = element->getRange();
+       uint size = set->getSize();
+       uint encSize = getSizeEncodingArray(size);
+       allocEncodingArrayElement(encSize);
+       allocInUseArrayElement(encSize);
+       for (uint i = 0; i < size; i++) {
+               encodingArray[i] = set->getMemberAt(i);
+               setInUseElement(i);
+       }
+}
 
+void ElementEncoding::print() {
+       model_print("%s ", elemEncTypeNames[type]);
+       if (type == BINARYINDEX) {
+               for (uint i = 0; i < encArraySize; i++) {
+                       if (i != 0)
+                               model_print(", ");
+                       if (isinUseElement(i))
+                               model_print("%" PRIu64 "", encodingArray[i]);
+                       else
+                               model_print("_");
+               }
+       }
+}