diff --git a/packages/cplint/cplint.h b/packages/cplint/cplint.h index 56e5491e8..07ff93c40 100644 --- a/packages/cplint/cplint.h +++ b/packages/cplint/cplint.h @@ -10,9 +10,7 @@ for the relative license. #include "util.h" #include "cuddInt.h" -#include "array.h" #include "mtr.h" -#include "avl.h" #include "YapInterface.h" #include @@ -21,16 +19,35 @@ typedef struct int var,value; } factor; +typedef struct + { + int nFact; + factor * factors; + } term; + +typedef struct + { + int nTerms; + term * terms; + } expr; + typedef struct { int nVal,nBit; - array_t * probabilities; - array_t * booleanVars; + double * probabilities; + DdNode * * booleanVars; } variable; +typedef struct + { + int nVar; + int nBVar; + variable * varar; + int * bVar2mVar; + } variables; -void createVars(array_t * vars, YAP_Term t,DdManager * mgr, array_t * bVar2mVar,int create_dot, char inames[1000][20]); -void createExpression(array_t * expression, YAP_Term t); +variables createVars(YAP_Term t,DdManager * mgr, int create_dot, char inames[1000][20]); +expr createExpression(YAP_Term t); void init_my_predicates(void); int compare(char *a, char *b); gint my_equal(gconstpointer v,gconstpointer v2); @@ -39,11 +56,11 @@ void dealloc(gpointer key,gpointer value,gpointer user_data); -DdNode * retFunction(DdManager * mgr, array_t * expression,array_t * v); -DdNode * retTerm(DdManager * mgr,array_t *factors,array_t * v); -DdNode * retFactor(DdManager * mgr, factor f, array_t * v); +DdNode * retFunction(DdManager * mgr, expr expression,variables v); +DdNode * retTerm(DdManager * mgr,term t,variables v); +DdNode * retFactor(DdManager * mgr, factor f, variables v); -double Prob(DdNode *node, array_t * vars,array_t * bVar2mVar, GHashTable * nodes); +double Prob(DdNode *node, variables vars,GHashTable * nodes); double ProbBool(DdNode *node, int bits, int nBit,int posBVar,variable v, -array_t * vars,array_t * bVar2mVar, GHashTable * nodes); +variables vars,GHashTable * nodes); diff --git a/packages/cplint/cplint_Prob.c b/packages/cplint/cplint_Prob.c index 9af2d2f32..5ea44a285 100644 --- a/packages/cplint/cplint_Prob.c +++ b/packages/cplint/cplint_Prob.c @@ -19,20 +19,20 @@ int correctPosition(int index,variable v, DdNode * node,int posBVar); -DdNode * retFunction(DdManager * mgr,array_t *expression, array_t *v) +DdNode * retFunction(DdManager * mgr,expr expression, variables v) /* given an expression term1+term2+...+termn, returns the BDD that implements that function */ { - array_t * term; + term term1; DdNode * tNode, * tmp, *tmp1; int i; i=0; tNode=Cudd_ReadLogicZero(mgr); Cudd_Ref(tNode); - while(itype.value; - return value; + return value; } else { @@ -141,10 +141,10 @@ so that it is not recomputed } else { - mVarIndex=array_fetch(int,bVar2mVar,index); - v=array_fetch(variable,vars,mVarIndex); + mVarIndex=vars.bVar2mVar[index]; + v=vars.varar[mVarIndex]; nBit=v.nBit; - res=ProbBool(node,0,nBit,0,v,vars,bVar2mVar,nodes); + res=ProbBool(node,0,nBit,0,v,vars,nodes); key=(DdNode **)malloc(sizeof(DdNode *)); *key=node; rp=(double *)malloc(sizeof(double)); @@ -156,22 +156,22 @@ so that it is not recomputed } double ProbBool(DdNode *node, int bits, int nBit,int posBVar,variable v, -array_t * vars,array_t * bVar2mVar, GHashTable * nodes) +variables vars, GHashTable * nodes) /* explores a group of binary variables making up the multivalued variable v */ { DdNode *T,*F; double p,res; - array_t * probs; - + double * probs; + probs=v.probabilities; if (nBit==0) { - if (bits>=array_n(probs)) + if (bits>=v.nVal) return 0; else { - p=array_fetch(double,probs,bits); - res=p*Prob(node,vars,bVar2mVar,nodes); + p=probs[bits]; + res=p*Prob(node,vars,nodes); return res; } } @@ -183,16 +183,16 @@ array_t * vars,array_t * bVar2mVar, GHashTable * nodes) F = node->type.kids.E; bits=bits<<1; - res=ProbBool(T,bits+1,nBit-1,posBVar+1,v,vars,bVar2mVar,nodes)+ - ProbBool(F,bits,nBit-1,posBVar+1,v,vars,bVar2mVar,nodes); + res=ProbBool(T,bits+1,nBit-1,posBVar+1,v,vars,nodes)+ + ProbBool(F,bits,nBit-1,posBVar+1,v,vars,nodes); return res; } else { bits=bits<<1; - res=ProbBool(node,bits+1,nBit-1,posBVar+1,v,vars,bVar2mVar,nodes)+ - ProbBool(node,bits,nBit-1,posBVar+1,v,vars,bVar2mVar,nodes); + res=ProbBool(node,bits+1,nBit-1,posBVar+1,v,vars,nodes)+ + ProbBool(node,bits,nBit-1,posBVar+1,v,vars,nodes); return res; } } @@ -204,6 +204,6 @@ currently explored by ProbBool */ { DdNode * bvar; - bvar=array_fetch(DdNode *,v.booleanVars,posBVar); + bvar=v.booleanVars[posBVar]; return bvar->index==index; } diff --git a/packages/cplint/cplint_yap.c b/packages/cplint/cplint_yap.c index 350ad5491..5021f8019 100644 --- a/packages/cplint/cplint_yap.c +++ b/packages/cplint/cplint_yap.c @@ -24,9 +24,9 @@ FILE *open_file (char *filename, const char *mode); void reverse(char s[]); static int compute_prob(void); -void createVars(array_t * vars, YAP_Term t,DdManager * mgr, array_t * bVar2mVar,int create_dot, char inames[1000][20]) +variables createVars(YAP_Term t,DdManager * mgr, int create_dot, char inames[1000][20]) /* adds the boolean variables to the BDD and returns -an array_t containing them (array_t is defined in the util library of glu) +the array vars containing them returns also the names of the variables to be used to save the ADD in dot format */ { @@ -35,10 +35,16 @@ returns also the names of the variables to be used to save the ADD in dot format variable v; char numberVar[10],numberBit[10]; double p; - b=0; + variables vars; + + vars.varar= (variable *) malloc(0* sizeof(variable)); + vars.bVar2mVar=(int *) malloc(0* sizeof(int)); + b=0; + vars.nVar=0; while(YAP_IsPairTerm(t)) { + varTerm=YAP_HeadOfTerm(t); varIndex=YAP_IntOfTerm(YAP_HeadOfTerm(varTerm)); @@ -48,10 +54,18 @@ returns also the names of the variables to be used to save the ADD in dot format probTerm=YAP_HeadOfTerm(varTerm); v.nVal=nVal; v.nBit=(int)ceil(log(nVal)/log(2)); - v.probabilities=array_alloc(double,0); - v.booleanVars=array_alloc(DdNode *,0); + v.probabilities=(double *) malloc(nVal* sizeof(double)); + v.booleanVars=(DdNode * *) malloc(v.nBit* sizeof(DdNode *)); for (i=0;i