towards cloning
authorbdemsky <bdemsky@uci.edu>
Sun, 27 Aug 2017 07:08:54 +0000 (00:08 -0700)
committerbdemsky <bdemsky@uci.edu>
Sun, 27 Aug 2017 07:08:54 +0000 (00:08 -0700)
14 files changed:
src/AST/boolean.cc
src/AST/boolean.h
src/AST/element.cc
src/AST/element.h
src/AST/function.h
src/AST/mutableset.cc
src/AST/mutableset.h
src/AST/order.h
src/AST/predicate.h
src/AST/set.cc
src/AST/set.h
src/Collections/structs.h
src/csolver.cc
src/csolver.h

index 6ec11935080b208c4202238ed614109266a6c2cc..1eb2f001a50ca8649c25d4364864406b3e7e66a4 100644 (file)
@@ -3,6 +3,7 @@
 #include "csolver.h"
 #include "element.h"
 #include "order.h"
 #include "csolver.h"
 #include "element.h"
 #include "order.h"
+#include "predicate.h"
 
 Boolean::Boolean(ASTNodeType _type) :
        ASTNode(_type),
 
 Boolean::Boolean(ASTNodeType _type) :
        ASTNode(_type),
@@ -42,5 +43,36 @@ BooleanLogic::BooleanLogic(CSolver *solver, LogicOp _op, Boolean **array, uint a
        inputs(array, asize) {
 }
 
        inputs(array, asize) {
 }
 
-BooleanPredicate::~BooleanPredicate() {
+Boolean * BooleanVar::clone(CSolver *solver, CloneMap *map) {
+       if (map->boolean.contains(this)) {
+               return map->boolean.get(this);
+       } else {
+               Boolean * bvar=solver->getBooleanVar(type);
+               map->boolean.put(this, bvar);
+               return bvar;
+       }
+}
+
+Boolean * BooleanOrder::clone(CSolver * solver, CloneMap *map) {
+       Order * ordercopy=order->clone(map);
+       return solver->orderConstraint(ordercopy, first, second);
+}
+
+Boolean * BooleanLogic::clone(CSolver * solver, CloneMap *map) {
+       Boolean * array[inputs.getSize()];
+       for(uint i=0;i<inputs.getSize();i++) {
+               array[i]=inputs.get(i)->clone(solver, map);
+       }
+       return solver->applyLogicalOperation(op, array, inputs.getSize());
+}
+
+Boolean * BooleanPredicate::clone(CSolver * solver, CloneMap *map) {
+       Element * array[inputs.getSize()];
+       for(uint i=0;i<inputs.getSize();i++) {
+               array[i]=inputs.get(i)->clone(solver, map);
+       }
+       Predicate * pred=predicate->clone(map);
+       Boolean * defstatus=(undefStatus != NULL) ? undefStatus->clone(solver, map) : NULL;
+       
+       return solver->applyPredicateTable(pred, array, inputs.getSize(), defstatus);
 }
 }
index c9722a91ed8c0210fc05cb543bd3a85c8701944d..2341fcfdf4edbc873515d2ffc105214cba3d9324 100644 (file)
@@ -21,6 +21,7 @@ class Boolean : public ASTNode {
 public:
        Boolean(ASTNodeType _type);
        virtual ~Boolean() {}
 public:
        Boolean(ASTNodeType _type);
        virtual ~Boolean() {}
+       virtual Boolean * clone(CSolver * solver, CloneMap *map);
        Polarity polarity;
        BooleanValue boolVal;
        Vector<Boolean *> parents;
        Polarity polarity;
        BooleanValue boolVal;
        Vector<Boolean *> parents;
@@ -30,6 +31,8 @@ public:
 class BooleanVar : public Boolean {
 public:
        BooleanVar(VarType t);
 class BooleanVar : public Boolean {
 public:
        BooleanVar(VarType t);
+       Boolean * clone(CSolver * solver, CloneMap *map);
+       
        VarType vtype;
        Edge var;
        MEMALLOC;
        VarType vtype;
        Edge var;
        MEMALLOC;
@@ -38,6 +41,8 @@ public:
 class BooleanOrder : public Boolean {
 public:
        BooleanOrder(Order *_order, uint64_t _first, uint64_t _second);
 class BooleanOrder : public Boolean {
 public:
        BooleanOrder(Order *_order, uint64_t _first, uint64_t _second);
+       Boolean * clone(CSolver * solver, CloneMap *map);
+       
        Order *order;
        uint64_t first;
        uint64_t second;
        Order *order;
        uint64_t first;
        uint64_t second;
@@ -47,7 +52,8 @@ public:
 class BooleanPredicate : public Boolean {
 public:
        BooleanPredicate(Predicate *_predicate, Element **_inputs, uint _numInputs, Boolean *_undefinedStatus);
 class BooleanPredicate : public Boolean {
 public:
        BooleanPredicate(Predicate *_predicate, Element **_inputs, uint _numInputs, Boolean *_undefinedStatus);
-       ~BooleanPredicate();
+       Boolean * clone(CSolver * solver, CloneMap *map);
+       
        Predicate *predicate;
        FunctionEncoding encoding;
        Array<Element *> inputs;
        Predicate *predicate;
        FunctionEncoding encoding;
        Array<Element *> inputs;
@@ -59,6 +65,8 @@ public:
 class BooleanLogic : public Boolean {
 public:
        BooleanLogic(CSolver *solver, LogicOp _op, Boolean **array, uint asize);
 class BooleanLogic : public Boolean {
 public:
        BooleanLogic(CSolver *solver, LogicOp _op, Boolean **array, uint asize);
+       Boolean * clone(CSolver * solver, CloneMap *map);
+
        LogicOp op;
        Array<Boolean *> inputs;
        MEMALLOC;
        LogicOp op;
        Array<Boolean *> inputs;
        MEMALLOC;
index 32b372a0ba3d279aca029c9428e2e61515258cff..d6832f94d9d8fd2b8d0d5b28c5e42dbdd2dd8547 100644 (file)
@@ -4,6 +4,7 @@
 #include "constraint.h"
 #include "function.h"
 #include "table.h"
 #include "constraint.h"
 #include "function.h"
 #include "table.h"
+#include "csolver.h"
 
 Element::Element(ASTNodeType _type) :
        ASTNode(_type),
 
 Element::Element(ASTNodeType _type) :
        ASTNode(_type),
@@ -25,9 +26,10 @@ ElementFunction::ElementFunction(Function *_function, Element **array, uint numA
                GETELEMENTPARENTS(array[i])->push(this);
 }
 
                GETELEMENTPARENTS(array[i])->push(this);
 }
 
-ElementConst::ElementConst(uint64_t _value, VarType _type) : Element(ELEMCONST), value(_value) {
-       uint64_t array[] = {value};
-       set = new Set(_type, array, 1);
+ElementConst::ElementConst(uint64_t _value, VarType _type, Set * _set) :
+       Element(ELEMCONST),
+       set(_set),
+       value(_value) {
 }
 
 Set *getElementSet(Element *This) {
 }
 
 Set *getElementSet(Element *This) {
@@ -54,12 +56,24 @@ Set *getElementSet(Element *This) {
        return NULL;
 }
 
        return NULL;
 }
 
-ElementFunction::~ElementFunction() {
+Element * ElementConst::clone(CSolver *solver, CloneMap * map) {
+       return solver->getElementConst(type, value);
 }
 }
-
-ElementConst::~ElementConst() {
-       delete set;
+               
+Element * ElementSet::clone(CSolver *solver, CloneMap * map) {
+       Element * e = map->element.get(this);
+       if (e != NULL)
+               return e;
+       e = solver->getElementVar(set->clone(solver, map));
+       map->element.put(e, e);
+       return e;
 }
 
 }
 
-Element::~Element() {
+Element * ElementFunction::clone(CSolver *solver, CloneMap * map) {
+       Element * array[inputs.getSize()];
+       for(uint i=0; i<inputs.getSize(); i++) {
+               array[i]=inputs.get(i)->clone(solver, map);
+       }
+       Element * e = solver->applyFunction(function->clone(solver, map), array, inputs.getSize(), overflowstatus->clone(solver, map));
+       return e;
 }
 }
index f563586e77b2b5034ddcc18132cadc072aba5675..a230055fe70285712de99ec0212f869f0235d2ba 100644 (file)
 class Element : public ASTNode {
 public:
        Element(ASTNodeType type);
 class Element : public ASTNode {
 public:
        Element(ASTNodeType type);
-       virtual ~Element();
+       virtual ~Element() {}
        Vector<ASTNode *> parents;
        ElementEncoding encoding;
        Vector<ASTNode *> parents;
        ElementEncoding encoding;
+       virtual Element * clone(CSolver * solver, CloneMap * map);
        MEMALLOC;
 };
 
 class ElementConst : public Element {
 public:
        MEMALLOC;
 };
 
 class ElementConst : public Element {
 public:
-       ElementConst(uint64_t value, VarType type);
-       ~ElementConst();
+       ElementConst(uint64_t value, VarType type, Set *_set);
        Set *set;
        uint64_t value;
        Set *set;
        uint64_t value;
+       Element * clone(CSolver * solver, CloneMap * map);
        MEMALLOC;
 };
 
        MEMALLOC;
 };
 
@@ -32,17 +33,18 @@ class ElementSet : public Element {
 public:
        ElementSet(Set *s);
        Set *set;
 public:
        ElementSet(Set *s);
        Set *set;
+       Element * clone(CSolver * solver, CloneMap * map);
        MEMALLOC;
 };
 
 class ElementFunction : public Element {
 public:
        ElementFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus);
        MEMALLOC;
 };
 
 class ElementFunction : public Element {
 public:
        ElementFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus);
-       ~ElementFunction();
        Function *function;
        Array<Element *> inputs;
        Boolean *overflowstatus;
        FunctionEncoding functionencoding;
        Function *function;
        Array<Element *> inputs;
        Boolean *overflowstatus;
        FunctionEncoding functionencoding;
+       Element * clone(CSolver * solver, CloneMap * map);
        MEMALLOC;
 };
 
        MEMALLOC;
 };
 
index 2cd5ccae7f7ef699cfa2b5c3ea080e11ac912069..e00afd30b44b692c8dbb387f660c5dac0e77fb7e 100644 (file)
@@ -11,8 +11,9 @@ class Function {
 public:
        Function(FunctionType _type) : type(_type) {}
        FunctionType type;
 public:
        Function(FunctionType _type) : type(_type) {}
        FunctionType type;
-       MEMALLOC;
        virtual ~Function() {}
        virtual ~Function() {}
+       virtual Function * clone(CSolver * solver, CloneMap *map);
+       MEMALLOC;
 };
 
 class FunctionOperator : public Function {
 };
 
 class FunctionOperator : public Function {
@@ -24,6 +25,7 @@ public:
        FunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range, OverFlowBehavior overflowbehavior);
        uint64_t applyFunctionOperator(uint numVals, uint64_t *values);
        bool isInRangeFunction(uint64_t val);
        FunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range, OverFlowBehavior overflowbehavior);
        uint64_t applyFunctionOperator(uint numVals, uint64_t *values);
        bool isInRangeFunction(uint64_t val);
+       Function * clone(CSolver * solver, CloneMap *map);
        MEMALLOC;
 };
 
        MEMALLOC;
 };
 
@@ -32,6 +34,7 @@ public:
        Table *table;
        UndefinedBehavior undefBehavior;
        FunctionTable (Table *table, UndefinedBehavior behavior);
        Table *table;
        UndefinedBehavior undefBehavior;
        FunctionTable (Table *table, UndefinedBehavior behavior);
+       Function * clone(CSolver * solver, CloneMap *map);
        MEMALLOC;
 };
 
        MEMALLOC;
 };
 
index 19f6dec8b3878a02126245be10f5a246ad4ec55b..d029f82bbdcdf428ebf5fe1c5f9c1eb342fc8d46 100644 (file)
@@ -1,4 +1,5 @@
 #include "mutableset.h"
 #include "mutableset.h"
+#include "csolver.h"
 
 MutableSet::MutableSet(VarType t) : Set(t) {
 }
 
 MutableSet::MutableSet(VarType t) : Set(t) {
 }
@@ -6,3 +7,15 @@ MutableSet::MutableSet(VarType t) : Set(t) {
 void MutableSet::addElementMSet(uint64_t element) {
        members->push(element);
 }
 void MutableSet::addElementMSet(uint64_t element) {
        members->push(element);
 }
+
+Set * MutableSet::clone(CSolver * solver, CloneMap *map) {
+       Set * s=map->set.get(this);
+       if (s != NULL)
+               return s;
+       s=solver->createMutableSet(type);
+       for(uint i=0; i<members->getSize();i++) {
+               solver->addItem((MutableSet *) s, members->get(i));
+       }
+       map->set.put(this, s);
+       return s;
+}
index 3c2f937d2d54e14117a5ac461697f9c27ef13a5d..36ce011f26875f2e8b7b6a91b56aded7cc5ae02d 100644 (file)
@@ -6,6 +6,7 @@ class MutableSet : public Set {
 public:
        MutableSet(VarType t);
        void addElementMSet(uint64_t element);
 public:
        MutableSet(VarType t);
        void addElementMSet(uint64_t element);
+       Set * clone(CSolver * solver, CloneMap *map);
        MEMALLOC;
 };
 #endif
        MEMALLOC;
 };
 #endif
index 8d2124073968ce7a8c4cea3c09df018671b1fd2f..b4abd07e20b88e2b222284ca8288ba51450d9b86 100644 (file)
@@ -17,6 +17,7 @@ public:
        HashTableOrderPair *orderPairTable;
        HashSetOrderElement *elementTable;
        OrderGraph *graph;
        HashTableOrderPair *orderPairTable;
        HashSetOrderElement *elementTable;
        OrderGraph *graph;
+       Order * clone(CloneMap *map);
        Vector<BooleanOrder *> constraints;
        OrderEncoding order;
        void initializeOrderHashTable();
        Vector<BooleanOrder *> constraints;
        OrderEncoding order;
        void initializeOrderHashTable();
index 46bc5af83ff9b9e91449037d7f4ea6ca49851f71..c86c6c2a0c26b26e32a2693dbf3cd27c5864ed47 100644 (file)
@@ -11,6 +11,7 @@ class Predicate {
 public:
        Predicate(PredicateType _type) : type(_type) {}
        virtual ~Predicate() {}
 public:
        Predicate(PredicateType _type) : type(_type) {}
        virtual ~Predicate() {}
+       virtual Predicate * clone(CloneMap *map);
        PredicateType type;
        MEMALLOC;
 };
        PredicateType type;
        MEMALLOC;
 };
@@ -19,6 +20,7 @@ class PredicateOperator : public Predicate {
 public:
        PredicateOperator(CompOp op, Set **domain, uint numDomain);
        bool evalPredicateOperator(uint64_t *inputs);
 public:
        PredicateOperator(CompOp op, Set **domain, uint numDomain);
        bool evalPredicateOperator(uint64_t *inputs);
+       Predicate * clone(CloneMap *map);
        CompOp op;
        Array<Set *> domains;
        MEMALLOC;
        CompOp op;
        Array<Set *> domains;
        MEMALLOC;
@@ -27,6 +29,7 @@ public:
 class PredicateTable : public Predicate {
 public:
        PredicateTable(Table *table, UndefinedBehavior undefBehavior);
 class PredicateTable : public Predicate {
 public:
        PredicateTable(Table *table, UndefinedBehavior undefBehavior);
+       Predicate * clone(CloneMap *map);
        Table *table;
        UndefinedBehavior undefinedbehavior;
        MEMALLOC;
        Table *table;
        UndefinedBehavior undefinedbehavior;
        MEMALLOC;
index 4b66dfe956d499d5a6a653ed25ab767c40fe1eae..d5b9c34f360924270cf383252d88b2d88620bf8c 100644 (file)
@@ -1,5 +1,6 @@
 #include "set.h"
 #include <stddef.h>
 #include "set.h"
 #include <stddef.h>
+#include "csolver.h"
 
 Set::Set(VarType t) : type(t), isRange(false), low(0), high(0) {
        members = new Vector<uint64_t>();
 
 Set::Set(VarType t) : type(t), isRange(false), low(0), high(0) {
        members = new Vector<uint64_t>();
@@ -44,3 +45,16 @@ Set::~Set() {
        if (!isRange)
                delete members;
 }
        if (!isRange)
                delete members;
 }
+
+Set * Set::clone(CSolver * solver, CloneMap *map) {
+       Set * s=map->set.get(this);
+       if (s != NULL)
+               return s;
+       if (isRange) {
+               s=solver->createRangeSet(type, low, high);
+       } else {
+               s=solver->createSet(type, members->expose(), members->getSize());
+       }
+       map->set.put(this, s);
+       return s;
+}
index bb246b6f690d7e1c0542bb869f6f2b2e7780a441..3ff404a5be1c4b858c0da3ac66dd79501599ec1c 100644 (file)
@@ -17,11 +17,12 @@ public:
        Set(VarType t);
        Set(VarType t, uint64_t *elements, uint num);
        Set(VarType t, uint64_t lowrange, uint64_t highrange);
        Set(VarType t);
        Set(VarType t, uint64_t *elements, uint num);
        Set(VarType t, uint64_t lowrange, uint64_t highrange);
-       ~Set();
+       virtual ~Set();
        bool exists(uint64_t element);
        uint getSize();
        uint64_t getElement(uint index);
        bool exists(uint64_t element);
        uint getSize();
        uint64_t getElement(uint index);
-
+       virtual Set * clone(CSolver * solver, CloneMap *map);
+       
        VarType type;
        bool isRange;
        uint64_t low;//also used to count unique items
        VarType type;
        bool isRange;
        uint64_t low;//also used to count unique items
index 28d290f0303b9b460ec58fa9a94090ba490c632b..73f5bf59b089b8e9c9b8f0902916daef5bec2598 100644 (file)
@@ -24,10 +24,22 @@ typedef HashSet<OrderEdge *, uintptr_t, 4, order_edge_hash_function, order_edge_
 typedef HashSet<OrderElement *, uintptr_t, 4, order_element_hash_function, order_element_equals> HashSetOrderElement;
 typedef HashTable<OrderNode *, HashSetOrderNode *, uintptr_t, 4> HashTableNodeToNodeSet;
 typedef HashTable<OrderPair *, OrderPair *, uintptr_t, 4, order_pair_hash_function, order_pair_equals> HashTableOrderPair;
 typedef HashSet<OrderElement *, uintptr_t, 4, order_element_hash_function, order_element_equals> HashSetOrderElement;
 typedef HashTable<OrderNode *, HashSetOrderNode *, uintptr_t, 4> HashTableNodeToNodeSet;
 typedef HashTable<OrderPair *, OrderPair *, uintptr_t, 4, order_pair_hash_function, order_pair_equals> HashTableOrderPair;
+typedef HashTable<Order *, Order *, uintptr_t, 4> OrderMap;
+typedef HashTable<Boolean *, Boolean *, uintptr_t, 4> BooleanMap;
+typedef HashTable<Element *, Element *, uintptr_t, 4> ElementMap;
+typedef HashTable<Set *, Set *, uintptr_t, 4> SetMap;
+
+typedef struct CloneMap {
+       OrderMap order;
+       BooleanMap boolean;
+       ElementMap element;
+       SetMap set;
+} CloneMap;
 
 typedef HSIterator<TableEntry *, uintptr_t, 4, table_entry_hash_function, table_entry_equals> HSIteratorTableEntry;
 typedef HSIterator<Boolean *, uintptr_t, 4> HSIteratorBoolean;
 typedef HSIterator<OrderEdge *, uintptr_t, 4, order_edge_hash_function, order_edge_equals> HSIteratorOrderEdge;
 typedef HSIterator<OrderNode *, uintptr_t, 4, order_node_hash_function, order_node_equals> HSIteratorOrderNode;
 
 
 typedef HSIterator<TableEntry *, uintptr_t, 4, table_entry_hash_function, table_entry_equals> HSIteratorTableEntry;
 typedef HSIterator<Boolean *, uintptr_t, 4> HSIteratorBoolean;
 typedef HSIterator<OrderEdge *, uintptr_t, 4, order_edge_hash_function, order_edge_equals> HSIteratorOrderEdge;
 typedef HSIterator<OrderNode *, uintptr_t, 4, order_node_hash_function, order_node_equals> HSIteratorOrderNode;
 
+
 #endif
 #endif
index d825df5e4c2a8d12192ec1fb5da68fe60eac06cc..dec85e081ac1a8f7d04a14f1ff8765848ea58e8f 100644 (file)
@@ -60,6 +60,18 @@ CSolver::~CSolver() {
        delete tuner;
 }
 
        delete tuner;
 }
 
+CSolver * CSolver::clone() {
+       CSolver * copy=new CSolver();
+       CloneMap map;
+       HSIteratorBoolean * it=getConstraints();
+       while(it->hasNext()) {
+               Boolean *b=it->next();
+               b->clone(copy, &map);
+       }
+       delete it;
+       return copy;
+}
+
 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
        Set *set = new Set(type, elements, numelements);
        allSets.push(set);
 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
        Set *set = new Set(type, elements, numelements);
        allSets.push(set);
@@ -95,7 +107,10 @@ Element *CSolver::getElementVar(Set *set) {
 }
 
 Element *CSolver::getElementConst(VarType type, uint64_t value) {
 }
 
 Element *CSolver::getElementConst(VarType type, uint64_t value) {
-       Element *element = new ElementConst(value, type);
+       uint64_t array[] = {value};
+       Set * set = new Set(type, array, 1);
+       allSets.push(set);
+       Element *element = new ElementConst(value, type, set);
        allElements.push(element);
        return element;
 }
        allElements.push(element);
        return element;
 }
index c1fd90b5ebd125b9f429527069149247e84857cb..115006f82faaad6403574304d6d4170be42ea42a 100644 (file)
@@ -116,7 +116,7 @@ public:
        void replaceBooleanWithTrue(Boolean *bexpr);
        void replaceBooleanWithFalse(Boolean *bexpr);
        void replaceBooleanWithBoolean(Boolean *oldb, Boolean *newb);
        void replaceBooleanWithTrue(Boolean *bexpr);
        void replaceBooleanWithFalse(Boolean *bexpr);
        void replaceBooleanWithBoolean(Boolean *oldb, Boolean *newb);
-
+       CSolver * clone();
 
        MEMALLOC;
 
 
        MEMALLOC;