From a06aa4dde1c7cdeabce214584009c93c0eeec29d Mon Sep 17 00:00:00 2001 From: bdemsky Date: Wed, 21 Jun 2017 19:24:40 -0700 Subject: [PATCH] Add array object to standardize arrays --- src/AST/boolean.c | 12 +++------- src/AST/boolean.h | 6 ++--- src/AST/element.c | 6 ++--- src/AST/element.h | 3 +-- src/AST/function.c | 6 ++--- src/AST/function.h | 3 +-- src/AST/ops.h | 2 +- src/AST/order.c | 24 +++++++++---------- src/AST/predicate.c | 8 ++----- src/AST/predicate.h | 5 ++-- src/AST/table.c | 6 ++--- src/AST/table.h | 3 +-- src/AST/tableentry.c | 15 ++++++------ src/Backend/satencoder.c | 31 +++++++++++++++++-------- src/Backend/satencoder.h | 3 +-- src/Collections/array.h | 49 +++++++++++++++++++++++++++++++++++++++ src/Collections/structs.h | 4 ++++ src/Collections/vector.h | 6 +++-- 18 files changed, 117 insertions(+), 75 deletions(-) create mode 100644 src/Collections/array.h diff --git a/src/AST/boolean.c b/src/AST/boolean.c index bcfd1d4..8a47f72 100644 --- a/src/AST/boolean.c +++ b/src/AST/boolean.c @@ -26,9 +26,7 @@ Boolean * allocBooleanPredicate(Predicate * predicate, Element ** inputs, uint n BooleanPredicate* This = (BooleanPredicate*) ourmalloc(sizeof(BooleanPredicate)); GETBOOLEANTYPE(This)= PREDICATEOP; This->predicate=predicate; - This->inputs=ourmalloc(sizeof(Element*)*numInputs); - memcpy(This->inputs, inputs, sizeof(Element*)*numInputs); - This->numInputs=numInputs; + allocInlineArrayInitElement(&This->inputs, inputs, numInputs); allocInlineDefVectorBoolean(GETBOOLEANPARENTS(This)); for(uint i=0;iarray=ourmalloc(sizeof(Boolean *)*asize); - memcpy(This->array, array, sizeof(Boolean *)*asize); - for(uint i=0;iinputs, array, asize); pushVectorBoolean(solver->allBooleans, (Boolean *) This); return & This->base; } @@ -52,7 +46,7 @@ Boolean * allocBooleanLogicArray(CSolver *solver, LogicOp op, Boolean ** array, void deleteBoolean(Boolean * This) { switch(GETBOOLEANTYPE(This)){ case PREDICATEOP: - ourfree(((BooleanPredicate*)This)->inputs ); + deleteInlineArrayElement(& ((BooleanPredicate*)This)->inputs ); break; default: break; diff --git a/src/AST/boolean.h b/src/AST/boolean.h index 4ade14a..a9d0702 100644 --- a/src/AST/boolean.h +++ b/src/AST/boolean.h @@ -34,15 +34,13 @@ struct BooleanVar { struct BooleanLogic { Boolean base; LogicOp op; - Boolean ** array; - uint numArray; + ArrayBoolean inputs; }; struct BooleanPredicate { Boolean base; Predicate * predicate; - Element** inputs; - int numInputs; + ArrayElement inputs; }; Boolean * allocBoolean(VarType t); diff --git a/src/AST/element.c b/src/AST/element.c index 36005e6..7401173 100644 --- a/src/AST/element.c +++ b/src/AST/element.c @@ -15,9 +15,7 @@ Element* allocElementFunction(Function * function, Element ** array, uint numArr GETELEMENTTYPE(tmp)= ELEMFUNCRETURN; tmp->function=function; tmp->overflowstatus = overflowstatus; - tmp->inputs=ourmalloc(sizeof(Element *)*numArrays); - tmp->numInputs=numArrays; - memcpy(tmp->inputs, array, numArrays*sizeof(Element *)); + allocInlineArrayInitElement(&tmp->inputs, array, numArrays); allocInlineDefVectorASTNode(GETELEMENTPARENTS(tmp)); for(uint i=0;iinputs); + deleteInlineArrayElement(&((ElementFunction *)This)->inputs); break; default: ; diff --git a/src/AST/element.h b/src/AST/element.h index 6e6fbd0..7886e4c 100644 --- a/src/AST/element.h +++ b/src/AST/element.h @@ -22,8 +22,7 @@ struct ElementSet { struct ElementFunction { Element base; Function * function; - Element ** inputs; - uint numInputs; + ArrayElement inputs; Boolean * overflowstatus; }; diff --git a/src/AST/function.c b/src/AST/function.c index daa3dc3..43adc25 100644 --- a/src/AST/function.c +++ b/src/AST/function.c @@ -6,9 +6,7 @@ Function* allocFunctionOperator( ArithOp op, Set ** domain, uint numDomain, Set * range,OverFlowBehavior overflowbehavior){ FunctionOperator* This = (FunctionOperator*) ourmalloc(sizeof(FunctionOperator)); GETFUNCTIONTYPE(This)=OPERATORFUNC; - This->numDomains=numDomain; - This->domains = ourmalloc(numDomain * sizeof(Set *)); - memcpy(This->domains, domain, numDomain * sizeof(Set *)); + allocInlineArrayInitSet(&This->domains, domain, numDomain); This->op=op; This->overflowbehavior = overflowbehavior; This->range=range; @@ -27,7 +25,7 @@ void deleteFunction(Function* This){ case TABLEFUNC: break; case OPERATORFUNC: - ourfree(((FunctionOperator*) This)->domains); + deleteInlineArraySet(&((FunctionOperator*) This)->domains); break; default: ASSERT(0); diff --git a/src/AST/function.h b/src/AST/function.h index 50a59ec..2e04320 100644 --- a/src/AST/function.h +++ b/src/AST/function.h @@ -14,8 +14,7 @@ struct Function { struct FunctionOperator { Function base; ArithOp op; - uint numDomains; - Set ** domains; + ArraySet domains; Set * range; OverFlowBehavior overflowbehavior; }; diff --git a/src/AST/ops.h b/src/AST/ops.h index 427b4f2..1f94cb1 100644 --- a/src/AST/ops.h +++ b/src/AST/ops.h @@ -32,7 +32,7 @@ typedef enum FunctionType FunctionType; enum PredicateType {TABLEPRED, OPERATORPRED}; typedef enum PredicateType PredicateType; -enum ASTNodeType {ORDERCONST, BOOLEANVAR, LOGICOP, PREDICATEOP, TABLEPREDICATEOP, ELEMSET, ELEMFUNCRETURN}; +enum ASTNodeType {ORDERCONST, BOOLEANVAR, LOGICOP, PREDICATEOP, ELEMSET, ELEMFUNCRETURN}; typedef enum ASTNodeType ASTNodeType; #endif diff --git a/src/AST/order.c b/src/AST/order.c index 47cc374..c05a4a1 100644 --- a/src/AST/order.c +++ b/src/AST/order.c @@ -5,18 +5,18 @@ Order* allocOrder(OrderType type, Set * set){ - Order* order = (Order*)ourmalloc(sizeof(Order)); - order->set=set; - order->constraints = allocDefVectorBoolean(); - order->type=type; - return order; + Order* order = (Order*)ourmalloc(sizeof(Order)); + order->set=set; + order->constraints = allocDefVectorBoolean(); + order->type=type; + return order; } void deleteOrder(Order* order){ - uint size = getSizeVectorBoolean( order->constraints ); - for(uint i=0; iconstraints, i) ); - } - deleteSet( order->set); - ourfree(order); -} \ No newline at end of file + uint size = getSizeVectorBoolean( order->constraints ); + for(uint i=0; iconstraints, i) ); + } + deleteSet( order->set); + ourfree(order); +} diff --git a/src/AST/predicate.c b/src/AST/predicate.c index 904fe22..79e81df 100644 --- a/src/AST/predicate.c +++ b/src/AST/predicate.c @@ -1,13 +1,9 @@ #include "predicate.h" -#include "structs.h" - Predicate* allocPredicate(CompOp op, Set ** domain, uint numDomain){ PredicateOperator* predicate = ourmalloc(sizeof(PredicateOperator)); GETPREDICATETYPE(predicate)=OPERATORPRED; - predicate->numDomains=numDomain; - predicate->domains = ourmalloc(numDomain * sizeof(Set *)); - memcpy(predicate->domains, domain, numDomain * sizeof(Set *)); + allocInlineArrayInitSet(&predicate->domains, domain, numDomain); predicate->op=op; return &predicate->base; } @@ -16,7 +12,7 @@ void deletePredicate(Predicate* predicate){ switch(GETPREDICATETYPE(predicate)) { case OPERATORPRED: { PredicateOperator * operpred=(PredicateOperator *) predicate; - ourfree(operpred->domains); + deleteInlineArraySet(&operpred->domains); break; } case TABLEPRED: { diff --git a/src/AST/predicate.h b/src/AST/predicate.h index c6dd02b..ee432cb 100644 --- a/src/AST/predicate.h +++ b/src/AST/predicate.h @@ -3,7 +3,7 @@ #include "classlist.h" #include "mymemory.h" #include "ops.h" - +#include "structs.h" #define GETPREDICATETYPE(o) (((Predicate *)(o))->type) @@ -14,8 +14,7 @@ struct Predicate { struct PredicateOperator { Predicate base; CompOp op; - Set ** domains; - uint numDomains; + ArraySet domains; }; struct PredicateTable { diff --git a/src/AST/table.c b/src/AST/table.c index 6482d83..2705f13 100644 --- a/src/AST/table.c +++ b/src/AST/table.c @@ -7,9 +7,7 @@ Table * allocTable(Set **domains, uint numDomain, Set * range){ Table* table = (Table*) ourmalloc(sizeof(Table)); - table->numDomains=numDomain; - table->domains = ourmalloc(numDomain*sizeof(Set *)); - memcpy(table->domains, domains, numDomain * sizeof(Set *)); + allocInlineArrayInitSet(&table->domains, domains, numDomain); table->range =range; return table; } @@ -20,7 +18,7 @@ void addNewTableEntry(Table* table, uint64_t* inputs, uint inputSize, uint64_t r } void deleteTable(Table* table){ - ourfree(table->domains); + deleteInlineArraySet(&table->domains); uint size = getSizeVectorTableEntry(table->entries); for(uint i=0; ientries, i)); diff --git a/src/AST/table.h b/src/AST/table.h index cff8ec6..a7a322c 100644 --- a/src/AST/table.h +++ b/src/AST/table.h @@ -5,9 +5,8 @@ #include "structs.h" struct Table { - Set ** domains; + ArraySet domains; Set * range; - uint numDomains; VectorTableEntry* entries; }; diff --git a/src/AST/tableentry.c b/src/AST/tableentry.c index b99d183..9992af7 100644 --- a/src/AST/tableentry.c +++ b/src/AST/tableentry.c @@ -1,14 +1,13 @@ #include "tableentry.h" +#include TableEntry* allocTableEntry(uint64_t* inputs, uint inputSize, uint64_t result){ - TableEntry* te = (TableEntry*) ourmalloc(sizeof(TableEntry)+inputSize*sizeof(uint64_t)); - te->output=result; - for(int i=0; iinputs[i]=inputs[i]; - } - return te; + TableEntry* te = (TableEntry*) ourmalloc(sizeof(TableEntry)+inputSize*sizeof(uint64_t)); + te->output=result; + memcpy(te->inputs, inputs, inputSize * sizeof(uint64_t)); + return te; } void deleteTableEntry(TableEntry* tableEntry){ - ourfree(tableEntry); -} \ No newline at end of file + ourfree(tableEntry); +} diff --git a/src/Backend/satencoder.c b/src/Backend/satencoder.c index 2083d60..37d3bc7 100644 --- a/src/Backend/satencoder.c +++ b/src/Backend/satencoder.c @@ -32,16 +32,14 @@ Constraint * encodeConstraintSATEncoder(SATEncoder *This, Boolean *constraint) { return encodeVarSATEncoder(This, (BooleanVar *) constraint); case LOGICOP: return encodeLogicSATEncoder(This, (BooleanLogic *) constraint); + case PREDICATEOP: + return encodePredicateSATEncoder(This, (BooleanPredicate *) constraint); default: model_print("Unhandled case in encodeConstraintSATEncoder %u", GETBOOLEANTYPE(constraint)); exit(-1); } } -Constraint * encodeOrderSATEncoder(SATEncoder *This, BooleanOrder * constraint) { - return NULL; -} - Constraint * getNewVarSATEncoder(SATEncoder *This) { Constraint * var=allocVarConstraint(VAR, This->varcount); Constraint * varneg=allocVarConstraint(NOTVAR, This->varcount++); @@ -58,18 +56,20 @@ Constraint * encodeVarSATEncoder(SATEncoder *This, BooleanVar * constraint) { } Constraint * encodeLogicSATEncoder(SATEncoder *This, BooleanLogic * constraint) { - Constraint * array[constraint->numArray]; - for(uint i=0;inumArray;i++) - array[i]=encodeConstraintSATEncoder(This, constraint->array[i]); + Constraint * array[getSizeArrayBoolean(&constraint->inputs)]; + for(uint i=0;iinputs);i++) + array[i]=encodeConstraintSATEncoder(This, getArrayBoolean(&constraint->inputs, i)); switch(constraint->op) { case L_AND: - return allocArrayConstraint(AND, constraint->numArray, array); + return allocArrayConstraint(AND, getSizeArrayBoolean(&constraint->inputs), array); case L_OR: - return allocArrayConstraint(OR, constraint->numArray, array); + return allocArrayConstraint(OR, getSizeArrayBoolean(&constraint->inputs), array); case L_NOT: - return negateConstraint(allocConstraint(OR, array[0], NULL)); + ASSERT(constraint->numArray==1); + return negateConstraint(array[0]); case L_XOR: { + ASSERT(constraint->numArray==2); Constraint * nleft=negateConstraint(cloneConstraint(array[0])); Constraint * nright=negateConstraint(cloneConstraint(array[1])); return allocConstraint(OR, @@ -77,9 +77,20 @@ Constraint * encodeLogicSATEncoder(SATEncoder *This, BooleanLogic * constraint) allocConstraint(AND, nleft, array[1])); } case L_IMPLIES: + ASSERT(constraint->numArray==2); return allocConstraint(IMPLIES, array[0], array[1]); default: model_print("Unhandled case in encodeLogicSATEncoder %u", constraint->op); exit(-1); } } + +Constraint * encodeOrderSATEncoder(SATEncoder *This, BooleanOrder * constraint) { + //TO IMPLEMENT + return NULL; +} + +Constraint * encodePredicateSATEncoder(SATEncoder * This, BooleanPredicate * constraint) { + //TO IMPLEMENT + return NULL; +} diff --git a/src/Backend/satencoder.h b/src/Backend/satencoder.h index ea5de8b..5382bc5 100644 --- a/src/Backend/satencoder.h +++ b/src/Backend/satencoder.h @@ -7,7 +7,6 @@ struct SATEncoder { uint varcount; }; - SATEncoder * allocSATEncoder(); void deleteSATEncoder(SATEncoder *This); void encodeAllSATEncoder(SATEncoder *This, CSolver *csolver); @@ -16,5 +15,5 @@ Constraint * encodeConstraintSATEncoder(SATEncoder *This, Boolean *constraint); Constraint * encodeOrderSATEncoder(SATEncoder *This, BooleanOrder * constraint); Constraint * encodeVarSATEncoder(SATEncoder *This, BooleanVar * constraint); Constraint * encodeLogicSATEncoder(SATEncoder *This, BooleanLogic * constraint); - +Constraint * encodePredicateSATEncoder(SATEncoder * This, BooleanPredicate * constraint); #endif diff --git a/src/Collections/array.h b/src/Collections/array.h new file mode 100644 index 0000000..d084158 --- /dev/null +++ b/src/Collections/array.h @@ -0,0 +1,49 @@ +#ifndef ARRAY_H +#define ARRAY_H + +#define ArrayDef(name, type) \ + struct Array ## name { \ + type * array; \ + uint size; \ + }; \ + typedef struct Array ## name Array ## name; \ + inline Array ## name * allocArray ## name(uint size) { \ + Array ## name * tmp = (Array ## name *)ourmalloc(sizeof(type)); \ + tmp->size = size; \ + tmp->array = (type *) ourcalloc(1, sizeof(type) * size); \ + return tmp; \ + } \ + inline Array ## name * allocArrayInit ## name(type * array, uint size) { \ + Array ## name * tmp = allocArray ## name(size); \ + memcpy(tmp->array, array, size * sizeof(type)); \ + return tmp; \ + } \ + inline type getArray ## name(Array ## name * This, uint index) { \ + return This->array[index]; \ + } \ + inline void setArray ## name(Array ## name * This, uint index, type item) { \ + This->array[index]=item; \ + } \ + inline uint getSizeArray ## name(Array ## name *This) { \ + return This->size; \ + } \ + inline void deleteArray ## name(Array ## name *This) { \ + ourfree(This->array); \ + ourfree(This); \ + } \ + inline type * exposeCArray ## name(Array ## name * This) { \ + return This->array; \ + } \ + inline void deleteInlineArray ## name(Array ## name *This) { \ + ourfree(This->array); \ + } \ + inline void allocInlineArray ## name(Array ## name * This, uint size) { \ + This->size = size; \ + This->array = (type *) ourcalloc(1, sizeof(type) * size); \ + } \ + inline void allocInlineArrayInit ## name(Array ## name * This, type *array, uint size) { \ + allocInlineArray ##name(This, size); \ + memcpy(This->array, array, size * sizeof(type)); \ + } + +#endif diff --git a/src/Collections/structs.h b/src/Collections/structs.h index 33e8669..71ba4b0 100644 --- a/src/Collections/structs.h +++ b/src/Collections/structs.h @@ -4,7 +4,11 @@ #include "hashtable.h" #include "hashset.h" #include "classlist.h" +#include "array.h" +ArrayDef(Element, Element *); +ArrayDef(Boolean, Boolean *); +ArrayDef(Set, Set *); VectorDef(Int, uint64_t, 4); VectorDef(Boolean, Boolean *, 4); VectorDef(Constraint, Constraint *, 4); diff --git a/src/Collections/vector.h b/src/Collections/vector.h index fa1764e..72e504f 100644 --- a/src/Collections/vector.h +++ b/src/Collections/vector.h @@ -37,7 +37,8 @@ } \ Vector ## name * allocVectorArray ## name(uint capacity, type * array) { \ Vector ## name * tmp = allocVector ## name(capacity); \ - memcpy(tmp->array, array, capacity * sizeof(type)); \ + tmp->size=capacity; \ + memcpy(tmp->array, array, capacity * sizeof(type)); \ return tmp; \ } \ void pushVector ## name(Vector ## name *vector, type item) { \ @@ -79,6 +80,7 @@ } \ void allocInlineVectorArray ## name(Vector ## name * vector, uint capacity, type * array) { \ allocInlineVector ##name(vector, capacity); \ - memcpy(vector->array, array, capacity * sizeof(type)); \ + vector->size=capacity; \ + memcpy(vector->array, array, capacity * sizeof(type)); \ } #endif -- 2.34.1