From 1d1fce5e2b9baf78074f2d7f6364c734b3cb02da Mon Sep 17 00:00:00 2001 From: Hamed Date: Thu, 22 Jun 2017 21:19:27 -0700 Subject: [PATCH] Adding naive encoding pass. functionEncoding partially is added --- src/Collections/hashtable.h | 3 ++ src/Collections/structs.h | 6 +++ src/Encoders/encodings.c | 57 ++++++++++++++++++++++++++++ src/Encoders/encodings.h | 29 ++++++++++++++ src/Encoders/functionencoding.h | 1 + src/Encoders/naiveelementencoder.c | 2 + src/Encoders/naivefunctionencoder.c | 59 +++++++++++++++++++++++++++++ src/Encoders/naivefunctionencoder.h | 14 +++++++ src/classlist.h | 3 ++ 9 files changed, 174 insertions(+) create mode 100644 src/Encoders/encodings.c create mode 100644 src/Encoders/encodings.h create mode 100644 src/Encoders/naivefunctionencoder.c create mode 100644 src/Encoders/naivefunctionencoder.h diff --git a/src/Collections/hashtable.h b/src/Collections/hashtable.h index 8bb5cc2..f4610d0 100644 --- a/src/Collections/hashtable.h +++ b/src/Collections/hashtable.h @@ -20,6 +20,9 @@ #include "mymemory.h" #include "common.h" +#define HT_DEFAULT_FACTOR 0.75 +#define HT_INITIAL_CAPACITY 16 + /** * @brief A simple, custom hash table * diff --git a/src/Collections/structs.h b/src/Collections/structs.h index 33e8669..112fc08 100644 --- a/src/Collections/structs.h +++ b/src/Collections/structs.h @@ -16,6 +16,8 @@ VectorDef(Table, Table *, 4); VectorDef(Order, Order *, 4); VectorDef(Function, Function *, 4); VectorDef(ASTNode, ASTNode *, 4); +VectorDef(FunctionEncoding, FunctionEncoding *, 4); +VectorDef(ElementEncoding, ElementEncoding *, 4); inline unsigned int Ptr_hash_function(void * hash) { return (unsigned int)((uint64_t)hash >> 4); @@ -26,5 +28,9 @@ inline bool Ptr_equals(void * key1, void * key2) { } HashTableDef(Void, void *, void *, Ptr_hash_function, Ptr_equals); +HashTableDef(ElemToEncod, Element *, ElementEncoding *, Ptr_hash_function, Ptr_equals); +HashTableDef(VoidToFuncEncod, void *, FunctionEncoding *, Ptr_hash_function, Ptr_equals); + HashSetDef(Void, void *, Ptr_hash_function, Ptr_equals); + #endif diff --git a/src/Encoders/encodings.c b/src/Encoders/encodings.c new file mode 100644 index 0000000..f86389d --- /dev/null +++ b/src/Encoders/encodings.c @@ -0,0 +1,57 @@ +#include "encodings.h" +#include "elementencoding.h" +#include "functionencoding.h" +#include "element.h" +#include "common.h" +#include "boolean.h" +#include "naiveelementencoder.h" + +Encodings* allocEncodings(){ + Encodings* This = (Encodings*) ourmalloc(sizeof(Encodings)); + allocInlineDefVectorElementEncoding(GETVECTORELEMENTENCODING(This)); + allocInlineDefVectorFunctionEncoding(GETVECTORFUNCTIONENCODING(This)); + This->elemToEncode= allocHashTableElemToEncod(HT_INITIAL_CAPACITY, HT_DEFAULT_FACTOR); + This->voidToFuncEncode= allocHashTableVoidToFuncEncod(HT_INITIAL_CAPACITY, HT_DEFAULT_FACTOR); + return This; +} + +void deleteEncodings(Encodings* This){ + deleteVectorArrayFunctionEncoding(GETVECTORFUNCTIONENCODING(This)); + deleteVectorArrayElementEncoding(GETVECTORELEMENTENCODING(This)); + deleteHashTableElemToEncod(This->elemToEncode); + deleteHashTableVoidToFuncEncod(This->voidToFuncEncode); + ourfree(This); +} + +void assignEncoding(CSolver* csolver, Encodings* This){ + uint size = getSizeVectorElement(csolver->allElements); + for(uint i=0; iallElements, i); + if(GETELEMENTTYPE(element)==ELEMSET){ + ElementEncoding* eencoding = allocElementEncoding( BINARYINDEX, element); + baseBinaryIndexElementAssign(eencoding); + pushVectorElementEncoding(GETVECTORELEMENTENCODING(This) , eencoding); + putElemToEncod(This->elemToEncode, element, eencoding); + }else if (GETELEMENTTYPE(element)==ELEMFUNCRETURN){ + FunctionEncoding* fencoding = allocFunctionEncoding( ENUMERATEIMPLICATIONS, element); + pushVectorFunctionEncoding(GETVECTORFUNCTIONENCODING(This) , fencoding); + putVoidToFuncEncod(This->voidToFuncEncode,element, fencoding); + }else + ASSERT(0); + } + + size = getSizeVectorBoolean(csolver->allBooleans); + for(uint i=0; iallBooleans, i); + if(GETBOOLEANTYPE(predicate)==PREDICATEOP){ + FunctionEncoding* fencoding = allocPredicateEncoding(ENUMERATEIMPLICATIONS, predicate); + pushVectorFunctionEncoding(GETVECTORFUNCTIONENCODING(This), fencoding); + putVoidToFuncEncod(This->voidToFuncEncode, predicate,fencoding); + }else + ASSERT(0); + } +} + +void encodeFunctionsElements(Encodings* This){ + //call encoding for each element/predicate +} \ No newline at end of file diff --git a/src/Encoders/encodings.h b/src/Encoders/encodings.h new file mode 100644 index 0000000..30c611c --- /dev/null +++ b/src/Encoders/encodings.h @@ -0,0 +1,29 @@ + +#ifndef NAIVEENCODINGASSIGNMENT_H +#define NAIVEENCODINGASSIGNMENT_H +#include "classlist.h" +#include "structs.h" +#include "csolver.h" +#include "mymemory.h" + + +#define GETVECTORFUNCTIONENCODING(o) (&((Encodings*)o)->funcEncoding) +#define GETVECTORELEMENTENCODING(o) (&((Encodings*)o)->elemEncoding) + +struct Encodings{ + HashTableVoidToFuncEncod* voidToFuncEncode; + HashTableElemToEncod* elemToEncode; + VectorFunctionEncoding funcEncoding; + VectorElementEncoding elemEncoding; +}; + +Encodings* allocEncodings(); + +//For now, This function just simply goes through elements/functions and +//assigns a predefined Encoding to each of them +void assignEncoding(CSolver* csolver, Encodings* This); +void encodeFunctionsElements(Encodings* This); +void deleteEncodings(Encodings* This); + +#endif /* NAIVEENCODINGASSIGNMENT_H */ + diff --git a/src/Encoders/functionencoding.h b/src/Encoders/functionencoding.h index 63583b6..dc52aa7 100644 --- a/src/Encoders/functionencoding.h +++ b/src/Encoders/functionencoding.h @@ -24,4 +24,5 @@ struct FunctionEncoding { FunctionEncoding * allocFunctionEncoding(FunctionEncodingType type, Element *function); FunctionEncoding * allocPredicateEncoding(FunctionEncodingType type, Boolean *predicate); void deleteFunctionEncoding(FunctionEncoding *This); + #endif diff --git a/src/Encoders/naiveelementencoder.c b/src/Encoders/naiveelementencoder.c index 242a4e7..348c54c 100644 --- a/src/Encoders/naiveelementencoder.c +++ b/src/Encoders/naiveelementencoder.c @@ -22,3 +22,5 @@ void baseBinaryIndexElementAssign(ElementEncoding *This) { } } + + diff --git a/src/Encoders/naivefunctionencoder.c b/src/Encoders/naivefunctionencoder.c new file mode 100644 index 0000000..03037fd --- /dev/null +++ b/src/Encoders/naivefunctionencoder.c @@ -0,0 +1,59 @@ + +#include "naivefunctionencoder.h" +#include "functionencoding.h" +#include "common.h" +#include "element.h" +#include "boolean.h" +#include "function.h" +#include "table.h" +#include "tableentry.h" + +void naiveEncodeFunctionPredicate(Encodings* encodings, FunctionEncoding *This){ + if(This->isFunction) { + ASSERT(GETELEMENTTYPE(This->op.function)==ELEMFUNCRETURN); + if(This->type==CIRCUIT){ + naiveEncodeCircuitFunction(encodings, This); + } else if( This->type == ENUMERATEIMPLICATIONS){ + naiveEncodeEnumeratedFunction(encodings, This); + } else + ASSERT(0); + + }else { + ASSERT(GETBOOLEANTYPE(This->op.predicate) == PREDICATEOP); + BooleanPredicate* predicate = (BooleanPredicate*)This->op.predicate; + //FIXME + + } +} + + +void naiveEncodeCircuitFunction(Encodings* encodings, FunctionEncoding* This){ + +} + +void naiveEncodeEnumeratedFunction(Encodings* encodings, FunctionEncoding* This){ + ElementFunction* ef =(ElementFunction*)This->op.function; + Function * function = ef->function; + if(GETFUNCTIONTYPE(function)==TABLEFUNC){ + naiveEncodeEnumTableFunc(encodings, ef); + }else if (GETFUNCTIONTYPE(function)== OPERATORFUNC){ + naiveEncodeEnumOperatingFunc(encodings, ef); + }else + ASSERT(0); +} + +void naiveEncodeEnumTableFunc(Encodings* encodings, ElementFunction* This){ + ASSERT(GETFUNCTIONTYPE(This->function)==TABLEFUNC); + VectorElement* elements= This->Elements; + Table* table = ((FunctionTable*) This->function)->table; + uint size = getSizeVectorTableEntry(table->entries); + for(uint i=0; ientries, i); + //FIXME: generate Constraints + } + +} + +void naiveEncodeEnumOperatingFunc(Encodings* encodings, ElementFunction* This){ + +} \ No newline at end of file diff --git a/src/Encoders/naivefunctionencoder.h b/src/Encoders/naivefunctionencoder.h new file mode 100644 index 0000000..b0935ac --- /dev/null +++ b/src/Encoders/naivefunctionencoder.h @@ -0,0 +1,14 @@ + + +#ifndef NAIVEFUNCTIONENCODER_H +#define NAIVEFUNCTIONENCODER_H +#include "encodings.h" +#include "functionencoding.h" + +void naiveEncodeFunctionPredicate(Encodings* encodings, FunctionEncoding *This); +void naiveEncodeCircuitFunction(Encodings* encodings,FunctionEncoding* This); +void naiveEncodeEnumeratedFunction(Encodings* encodings, FunctionEncoding* This); +void naiveEncodeEnumTableFunc(Encodings* encodings, ElementFunction* This); +void naiveEncodeEnumOperatingFunc(Encodings* encodings, ElementFunction* This); +#endif /* NAIVEFUNCTIONENCODER_H */ + diff --git a/src/classlist.h b/src/classlist.h index 3c7d071..f9aa831 100644 --- a/src/classlist.h +++ b/src/classlist.h @@ -82,6 +82,9 @@ typedef struct OrderEncoding OrderEncoding; struct TableEntry; typedef struct TableEntry TableEntry; +struct Encodings; +typedef struct Encodings Encodings; + typedef unsigned int uint; typedef uint64_t VarType; #endif -- 2.34.1