I have read this article: Quick Way to Implement Dictionary in C and implemented it. Need to say that I change the install and lookup to use hashtab as a argument instead of a global variable, It worked like charm:
struct nlist *install(struct nlist **hashtab,char *name, symbol *dfn);
struct nlist *lookup(struct nlist **hashtab, char *s);
The problem is, I need to copy the hash into a new one. I first try:
struct nlist **copy_context(struct nlist **hashtab){
nlist **copy = (nlist**)malloc(sizeof(nlist*)*HASHSIZE);
struct nlist *np;
int i = 0;
for (np = hashtab[i]; i<HASHSIZE ;i++ ){
debug_log("i vale %i",i);
if( np != NULL ){
install(copy,np->name,np->dfn);
}
}
return copy;
}
but is not clear to me where the values are stored neither where to start the copy. I was thinking something like:
struct nlist **copy_context(struct nlist **hashtab){
nlist **copy = (nlist**)malloc(sizeof(nlist*)*HASHSIZE);
i = 0;//this is not correct
or (np = hashtab[i]; np != NULL; np = np->next){
install(copy,np->name,np->dfn);
}
return copy;
}
but is notingh is working.