simplified hash table management

This commit is contained in:
Fabrizio Riguzzi 2010-07-27 23:03:06 +02:00
parent 42bdf4f50b
commit b62c7b98f2
3 changed files with 5 additions and 67 deletions

View File

@ -49,10 +49,6 @@ typedef struct
variables createVars(YAP_Term t,DdManager * mgr, int create_dot, char inames[1000][20]); variables createVars(YAP_Term t,DdManager * mgr, int create_dot, char inames[1000][20]);
expr createExpression(YAP_Term t); expr createExpression(YAP_Term t);
void init_my_predicates(void); void init_my_predicates(void);
int compare(char *a, char *b);
gint my_equal(gconstpointer v,gconstpointer v2);
guint my_hash(gconstpointer key);
void dealloc(gpointer key,gpointer value,gpointer user_data);

View File

@ -121,7 +121,7 @@ so that it is not recomputed
variable v; variable v;
double res; double res;
double * value_p; double * value_p;
DdNode **key,*nodereg; DdNode *nodereg;
double *rp; double *rp;
index=Cudd_NodeReadIndex(node); index=Cudd_NodeReadIndex(node);
@ -136,7 +136,7 @@ so that it is not recomputed
else else
{ {
nodereg=Cudd_Regular(node); nodereg=Cudd_Regular(node);
value_p=g_hash_table_lookup(nodes,&nodereg); value_p=g_hash_table_lookup(nodes,nodereg);
if (value_p!=NULL) if (value_p!=NULL)
{ {
if (comp) if (comp)
@ -150,11 +150,9 @@ so that it is not recomputed
v=vars.varar[mVarIndex]; v=vars.varar[mVarIndex];
nBit=v.nBit; nBit=v.nBit;
res=ProbBool(node,0,nBit,0,v,mVarIndex,vars,nodes,0); res=ProbBool(node,0,nBit,0,v,mVarIndex,vars,nodes,0);
key=(DdNode **)malloc(sizeof(DdNode *));
*key=nodereg;
rp=(double *)malloc(sizeof(double)); rp=(double *)malloc(sizeof(double));
*rp=res; *rp=res;
g_hash_table_insert(nodes, key, rp); g_hash_table_insert(nodes, nodereg, rp);
if (comp) if (comp)
return 1-res; return 1-res;
else else

View File

@ -18,10 +18,8 @@ for the relative license.
#include <string.h> #include <string.h>
unsigned long dividend;
FILE *open_file (char *filename, const char *mode); FILE *open_file (char *filename, const char *mode);
void reverse(char s[]);
static int compute_prob(void); static int compute_prob(void);
variables createVars(YAP_Term t,DdManager * mgr, int create_dot, char inames[1000][20]) variables createVars(YAP_Term t,DdManager * mgr, int create_dot, char inames[1000][20])
@ -141,7 +139,7 @@ static int compute_prob(void)
expr expression; expr expression;
DdNode * function; DdNode * function;
DdManager * mgr; DdManager * mgr;
int nBVar,i,j,intBits,create_dot; int nBVar,i,intBits,create_dot;
FILE * file; FILE * file;
DdNode * array[1]; DdNode * array[1];
double prob; double prob;
@ -195,18 +193,10 @@ static int compute_prob(void)
fclose(file); fclose(file);
} }
nodes=g_hash_table_new(my_hash,my_equal); nodes=g_hash_table_new_full(NULL,NULL,NULL,free);
intBits=sizeof(unsigned int)*8; intBits=sizeof(unsigned int)*8;
/* dividend is a global variable used by my_hash
it is equal to an unsigned int with binary representation 11..1 */
dividend=1;
for(j=1;j<intBits;j++)
{
dividend=(dividend<<1)+1;
}
prob=Prob(function,vars,nodes); prob=Prob(function,vars,nodes);
out=YAP_MkFloatTerm(prob); out=YAP_MkFloatTerm(prob);
g_hash_table_foreach (nodes,dealloc,NULL);
g_hash_table_destroy(nodes); g_hash_table_destroy(nodes);
Cudd_Quit(mgr); Cudd_Quit(mgr);
for(i=0;i<vars.nVar;i++) for(i=0;i<vars.nVar;i++)
@ -223,22 +213,7 @@ static int compute_prob(void)
free(expression.terms); free(expression.terms);
return(YAP_Unify(out,arg3)); return(YAP_Unify(out,arg3));
} }
/*
int compare(char *a, char *b)
{
int aval,bval;
aval=(int) *((DdNode **)a);
aval=(int) *((DdNode **)b);
if (aval<bval)
return -1;
else
if (aval>bval)
return 1;
else
return 0;
}
*/
void init_my_predicates() void init_my_predicates()
/* function required by YAP for intitializing the predicates defined by a C function*/ /* function required by YAP for intitializing the predicates defined by a C function*/
{ {
@ -257,35 +232,4 @@ open_file(char *filename, const char *mode)
return fp; return fp;
} }
void reverse(char s[])
/* reverses a string */
{
int i,c,j;
for (i=0,j=strlen(s)-1;i<j;i++,j--)
{
c=s[i];
s[i]=s[j];
s[j]=c;
}
}
gint my_equal(gconstpointer v,gconstpointer v2)
/* function used by GHashTable to compare two keys */
{
DdNode *a,*b;
a=*(DdNode **)v;
b=*(DdNode **)v2;
return (a==b);
}
guint my_hash(gconstpointer key)
/* function used by GHashTable to hash a key */
{
unsigned int h;
h=(unsigned int)((unsigned long) *((DdNode **)key) % dividend);
return h;
}
void dealloc(gpointer key,gpointer value,gpointer user_data)
{
free(key);
free(value);
}