Add array object to standardize arrays
authorbdemsky <bdemsky@uci.edu>
Thu, 22 Jun 2017 02:24:40 +0000 (19:24 -0700)
committerbdemsky <bdemsky@uci.edu>
Thu, 22 Jun 2017 02:24:40 +0000 (19:24 -0700)
18 files changed:
src/AST/boolean.c
src/AST/boolean.h
src/AST/element.c
src/AST/element.h
src/AST/function.c
src/AST/function.h
src/AST/ops.h
src/AST/order.c
src/AST/predicate.c
src/AST/predicate.h
src/AST/table.c
src/AST/table.h
src/AST/tableentry.c
src/Backend/satencoder.c
src/Backend/satencoder.h
src/Collections/array.h [new file with mode: 0644]
src/Collections/structs.h
src/Collections/vector.h

index bcfd1d41bd4e406f39cb93663044d3144ebaa09d..8a47f72ee36f8e33557674331c57c2730fdbc8a7 100644 (file)
@@ -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;i<numInputs;i++) {
@@ -40,11 +38,7 @@ Boolean * allocBooleanPredicate(Predicate * predicate, Element ** inputs, uint n
 Boolean * allocBooleanLogicArray(CSolver *solver, LogicOp op, Boolean ** array, uint asize){
        BooleanLogic * This = ourmalloc(sizeof(BooleanLogic));
        allocInlineDefVectorBoolean(GETBOOLEANPARENTS(This));
-       This->array=ourmalloc(sizeof(Boolean *)*asize);
-       memcpy(This->array, array, sizeof(Boolean *)*asize);
-       for(uint i=0;i<asize;i++) {
-               pushVectorBoolean(GETBOOLEANPARENTS(array[i]), (Boolean *)This);
-       }
+       allocInlineArrayInitBoolean(&This->inputs, 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;
index 4ade14a0eed9707a68e10a6aef5e6bf6ee959b79..a9d07026820c867ae32c69cd5a00fcc08552d411 100644 (file)
@@ -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);
index 36005e630a35366dc3cf380c99db6b5036437991..7401173ba806df7f9648ae8a81a957883764502c 100644 (file)
@@ -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;i<numArrays;i++)
                pushVectorASTNode(GETELEMENTPARENTS(array[i]), (ASTNode *) tmp);
@@ -27,7 +25,7 @@ Element* allocElementFunction(Function * function, Element ** array, uint numArr
 void deleteElement(Element *This) {
        switch(GETELEMENTTYPE(This)) {
        case ELEMFUNCRETURN:
-               ourfree(((ElementFunction *)This)->inputs);
+               deleteInlineArrayElement(&((ElementFunction *)This)->inputs);
                break;
        default:
                ;
index 6e6fbd06f3afbbf16eeb19c06903f445d337791e..7886e4cc8d3073eb307851b24be86a0a5eff9204 100644 (file)
@@ -22,8 +22,7 @@ struct ElementSet {
 struct ElementFunction {
        Element base;
        Function * function;
-       Element ** inputs;
-       uint numInputs;
+       ArrayElement inputs;
        Boolean * overflowstatus;
 };
 
index daa3dc38b3868af935a4b639869feb548424249e..43adc256e699690af4c0088800ba89a86a741cc9 100644 (file)
@@ -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);
index 50a59ec081c504b849fa1ce0349c0e6e9f35c36b..2e043203ec6ab9a6b93ce972731483cb8700fca0 100644 (file)
@@ -14,8 +14,7 @@ struct Function {
 struct FunctionOperator {
        Function base;
        ArithOp op;
-       uint numDomains;
-       Set ** domains;
+       ArraySet domains;
        Set * range;
        OverFlowBehavior overflowbehavior;
 };
index 427b4f2539d5954f659e6f3c22faa674598a5593..1f94cb1a037c86cddb33d903a5d422efaf5b26fa 100644 (file)
@@ -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
index 47cc374a1c4fa431c92fa54bd944b15885a95089..c05a4a19891101487ff07924f0336b89085c3162 100644 (file)
@@ -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; i<size; i++){
-       deleteBoolean( getVectorBoolean(order->constraints, i) );
-    }
-    deleteSet( order->set);
-    ourfree(order);
-}
\ No newline at end of file
+       uint size = getSizeVectorBoolean( order->constraints );
+       for(uint i=0; i<size; i++){
+               deleteBoolean( getVectorBoolean(order->constraints, i) );
+       }
+       deleteSet( order->set);
+       ourfree(order);
+}
index 904fe224740e511d1fbe84f2275c484482e124fb..79e81df66443e8a0e8d68ed311da81979272b783 100644 (file)
@@ -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: {
index c6dd02b6fcb1d27c68ff4be9d65b703432f3aba7..ee432cb70a6a09b8f1767e20ee14c9788c8f23ed 100644 (file)
@@ -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 {
index 6482d83558bc5114db1a98ae208d9c3c781b1d49..2705f13a666d388e9f857f11af350300b48ff5d0 100644 (file)
@@ -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; i<size; i++){
                deleteTableEntry(getVectorTableEntry(table->entries, i));
index cff8ec6c80ddd056c2707f1666f98682cb7c41ba..a7a322ccfc8b7b087329764da36a6023b9310c9d 100644 (file)
@@ -5,9 +5,8 @@
 #include "structs.h"
 
 struct Table {
-       Set ** domains;
+       ArraySet domains;
        Set * range;
-       uint numDomains;
        VectorTableEntry* entries;
 };
 
index b99d183c9878b3b8184e34f467f3d758300e9543..9992af7238a4dba9a8c614c158ed1e7b63753707 100644 (file)
@@ -1,14 +1,13 @@
 #include "tableentry.h"
+#include <string.h>
 
 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; i<inputSize; i++){
-       te->inputs[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);
+}
index 2083d6074de106b87165056f92f9f839daf3386e..37d3bc7f4847b35c1fb2432a7dbc82464e344a16 100644 (file)
@@ -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;i<constraint->numArray;i++)
-               array[i]=encodeConstraintSATEncoder(This, constraint->array[i]);
+       Constraint * array[getSizeArrayBoolean(&constraint->inputs)];
+       for(uint i=0;i<getSizeArrayBoolean(&constraint->inputs);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;
+}
index ea5de8b5dd96689cb1e9ed0126f83504e3b88c79..5382bc505acb2830887938b75576ce7465a3a0b9 100644 (file)
@@ -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 (file)
index 0000000..d084158
--- /dev/null
@@ -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
index 33e86696007502f0665e76bb9e1617ce9ef35f7b..71ba4b0f94161a515cbe26ffa88084ff3e97b33f 100644 (file)
@@ -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);
index fa1764e07a0405409c29359eb56310bf22754992..72e504fa20c537676939fdcf8be295de93468556 100644 (file)
@@ -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