From 99d1bc0007e1ec7899cc6e60c98bbfb337c94731 Mon Sep 17 00:00:00 2001 From: bdemsky Date: Thu, 7 Sep 2017 20:54:20 -0700 Subject: [PATCH] More refactoring --- src/AST/element.cc | 31 +++++++++++-------------------- src/AST/element.h | 24 +++++++++++++----------- src/Backend/satfuncopencoder.cc | 4 ++-- src/Encoders/elementencoding.cc | 2 +- src/Translator/sattranslator.cc | 2 +- src/csolver.cc | 2 +- 6 files changed, 29 insertions(+), 36 deletions(-) diff --git a/src/AST/element.cc b/src/AST/element.cc index 589e3f5..05f6cef 100644 --- a/src/AST/element.cc +++ b/src/AST/element.cc @@ -16,6 +16,11 @@ ElementSet::ElementSet(Set *s) : set(s) { } +ElementSet::ElementSet(ASTNodeType _type, Set *s) : + Element(_type), + set(s) { +} + ElementFunction::ElementFunction(Function *_function, Element **array, uint numArrays, BooleanEdge _overflowstatus) : Element(ELEMFUNCRETURN), function(_function), @@ -24,29 +29,11 @@ ElementFunction::ElementFunction(Function *_function, Element **array, uint numA functionencoding(this) { } -ElementConst::ElementConst(uint64_t _value, VarType _type, Set *_set) : - Element(ELEMCONST), - set(_set), +ElementConst::ElementConst(uint64_t _value, Set *_set) : + ElementSet(ELEMCONST, _set), value(_value) { } -Set *getElementSet(Element *This) { - switch (This->type) { - case ELEMSET: - return ((ElementSet *)This)->set; - case ELEMCONST: - return ((ElementConst *)This)->set; - case ELEMFUNCRETURN: { - Function *func = ((ElementFunction *)This)->function; - return func->getRange(); - } - default: - ASSERT(0); - } - ASSERT(0); - return NULL; -} - Element *ElementConst::clone(CSolver *solver, CloneMap *map) { return solver->getElementConst(type, value); } @@ -72,3 +59,7 @@ Element *ElementFunction::clone(CSolver *solver, CloneMap *map) { void ElementFunction::updateParents() { for(uint i=0;i < inputs.getSize(); i++) inputs.get(i)->parents.push(this); } + +Set * ElementFunction::getRange() { + return function->getRange(); +} diff --git a/src/AST/element.h b/src/AST/element.h index 5447256..076e831 100644 --- a/src/AST/element.h +++ b/src/AST/element.h @@ -16,23 +16,26 @@ public: ElementEncoding encoding; virtual Element *clone(CSolver *solver, CloneMap *map) {ASSERT(0); return NULL;}; virtual void updateParents() {} - + virtual Set * getRange() = 0; CMEMALLOC; }; -class ElementConst : public Element { +class ElementSet : public Element { public: - ElementConst(uint64_t value, VarType type, Set *_set); - Set *set; - uint64_t value; - Element *clone(CSolver *solver, CloneMap *map); + ElementSet(ASTNodeType type, Set *s); + ElementSet(Set *s); + virtual Element *clone(CSolver *solver, CloneMap *map); CMEMALLOC; + Set *getRange() {return set;} + private: + Set *set; + }; -class ElementSet : public Element { +class ElementConst : public ElementSet { public: - ElementSet(Set *s); - Set *set; + ElementConst(uint64_t value, Set *_set); + uint64_t value; Element *clone(CSolver *solver, CloneMap *map); CMEMALLOC; }; @@ -45,12 +48,11 @@ public: BooleanEdge overflowstatus; FunctionEncoding functionencoding; Element *clone(CSolver *solver, CloneMap *map); + Set * getRange(); void updateParents(); CMEMALLOC; }; -Set *getElementSet(Element *This); - static inline ElementEncoding *getElementEncoding(Element *e) { return &e->encoding; } diff --git a/src/Backend/satfuncopencoder.cc b/src/Backend/satfuncopencoder.cc index ba8b32b..bfd4ea6 100644 --- a/src/Backend/satfuncopencoder.cc +++ b/src/Backend/satfuncopencoder.cc @@ -104,7 +104,7 @@ void SATEncoder::encodeOperatorElementFunctionSATEncoder(ElementFunction *func) uint64_t vals[numDomains];//setup value array for (uint i = 0; i < numDomains; i++) { - Set *set = getElementSet(func->inputs.get(i)); + Set *set = func->inputs.get(i)->getRange(); vals[i] = set->getElement(indices[i]); } @@ -173,7 +173,7 @@ void SATEncoder::encodeOperatorElementFunctionSATEncoder(ElementFunction *func) notfinished = false; for (uint i = 0; i < numDomains; i++) { uint index = ++indices[i]; - Set *set = getElementSet(func->inputs.get(i)); + Set *set = func->inputs.get(i)->getRange(); if (index < set->getSize()) { vals[i] = set->getElement(index); diff --git a/src/Encoders/elementencoding.cc b/src/Encoders/elementencoding.cc index 89d3c32..66a97d0 100644 --- a/src/Encoders/elementencoding.cc +++ b/src/Encoders/elementencoding.cc @@ -39,7 +39,7 @@ void ElementEncoding::setElementEncodingType(ElementEncodingType _type) { } void ElementEncoding::encodingArrayInitialization() { - Set *set = getElementSet(element); + Set *set = element->getRange(); uint size = set->getSize(); uint encSize = getSizeEncodingArray(size); allocEncodingArrayElement(encSize); diff --git a/src/Translator/sattranslator.cc b/src/Translator/sattranslator.cc index d670a94..bdcd196 100644 --- a/src/Translator/sattranslator.cc +++ b/src/Translator/sattranslator.cc @@ -59,7 +59,7 @@ uint64_t getElementValueUnarySATTranslator(CSolver *This, ElementEncoding *elemE uint64_t getElementValueSATTranslator(CSolver *This, Element *element) { ElementEncoding *elemEnc = getElementEncoding(element); if (elemEnc->numVars == 0)//case when the set has only one item - return getElementSet(element)->getElement(0); + return element->getRange()->getElement(0); switch (elemEnc->type) { case ONEHOT: return getElementValueOneHotSATTranslator(This, elemEnc); diff --git a/src/csolver.cc b/src/csolver.cc index 6454b37..3c6767f 100644 --- a/src/csolver.cc +++ b/src/csolver.cc @@ -126,7 +126,7 @@ Element *CSolver::getElementVar(Set *set) { Element *CSolver::getElementConst(VarType type, uint64_t value) { uint64_t array[] = {value}; Set *set = new Set(type, array, 1); - Element *element = new ElementConst(value, type, set); + Element *element = new ElementConst(value, set); Element *e = elemMap.get(element); if (e == NULL) { allSets.push(set); -- 2.34.1