diff options
Diffstat (limited to 'libhashtable')
-rw-r--r-- | libhashtable/example.c | 2 | ||||
-rwxr-xr-x | libhashtable/genheader.sh | 3 | ||||
-rw-r--r-- | libhashtable/hashtable.h | 16 | ||||
-rw-r--r-- | libhashtable/libhashtable.c | 24 |
4 files changed, 25 insertions, 20 deletions
diff --git a/libhashtable/example.c b/libhashtable/example.c index bc42a67..919d75c 100644 --- a/libhashtable/example.c +++ b/libhashtable/example.c @@ -19,5 +19,7 @@ int main(int argc,char *argv[]) { free(name); } printf("PATH='%s'\n",ht_getvalue(&ht,"PATH")); + //if you want to get a whole entry struct use ht_getnode(); + //getentry() just returns the first struct in the linked list in that bucket. return 0; } diff --git a/libhashtable/genheader.sh b/libhashtable/genheader.sh new file mode 100755 index 0000000..3995579 --- /dev/null +++ b/libhashtable/genheader.sh @@ -0,0 +1,3 @@ +#!/bin/sh +cat libhashtable.c | head -n 22 | tail -n 16 > hashtable.h +cat libhashtable.c | grep '(.*) *{' | egrep -v 'if|for|while' | sed 's/ {/;/' >> hashtable.h diff --git a/libhashtable/hashtable.h b/libhashtable/hashtable.h index 820d230..f0e2a15 100644 --- a/libhashtable/hashtable.h +++ b/libhashtable/hashtable.h @@ -1,23 +1,23 @@ -struct entry {//linked list node. +struct entry {/*linked list node.*/ char *original; - char *target; - struct entry *prev;// doubly linked list. why? + void *target; + struct entry *prev;/* doubly linked list. why? */ struct entry *next; }; -struct hitem {//dunno why I don't just have this as a linked list. +struct hitem {/*dunno why I don't just have this as a linked list. */ struct entry *ll; }; struct hashtable { - int kl;//number of keys in the table + int kl;/*number of keys in the table*/ struct hitem **bucket; int *keys; }; -unsigned short hash(char *v);//maybe use a seeded rand()? :) Thanks FreeArtMan +unsigned short hash(char *key);/*maybe use a seeded rand()? :) Thanks FreeArtMan*/ void inittable(struct hashtable *ht,int tsize); -int ht_setkey(struct hashtable *ht,char *key,char *value); +int ht_setkey(struct hashtable *ht,char *key,void *value); struct entry *ll_getentry(struct entry *start,char *msg); struct entry *ht_getentry(struct hashtable *ht,char *key); struct entry *ht_getnode(struct hashtable *ht,char *msg); -char *ht_getvalue(struct hashtable *ht,char *msg); +void *ht_getvalue(struct hashtable *ht,char *msg); diff --git a/libhashtable/libhashtable.c b/libhashtable/libhashtable.c index c4ac2d3..e89d0cd 100644 --- a/libhashtable/libhashtable.c +++ b/libhashtable/libhashtable.c @@ -6,7 +6,7 @@ /* struct entry {//linked list node. char *original; - char *target; + void *target; struct entry *prev;// doubly linked list. why? struct entry *next; }; @@ -22,11 +22,8 @@ struct hashtable { }; */ -unsigned short hash(char *v) {//maybe use a seeded rand()? :) Thanks FreeArtMan - unsigned short h=0; - h=((*v)<<8)+(v?*(v+1):0); - srand(h); - return rand(); +unsigned short hash(char *key) {//maybe use a seeded rand()? :) Thanks FreeArtMan + return (strlen(key)<<8)+(key[0]<<4)+key[1]; } void inittable(struct hashtable *ht,int tsize) { @@ -44,7 +41,7 @@ void inittable(struct hashtable *ht,int tsize) { } //this seems too complicated. -int ht_setkey(struct hashtable *ht,char *key,char *value) { +int ht_setkey(struct hashtable *ht,char *key,void *value) { unsigned short h=hash(key); struct entry *tmp; int i; @@ -63,8 +60,9 @@ int ht_setkey(struct hashtable *ht,char *key,char *value) { } if((tmp=ll_getentry(ht->bucket[h]->ll,key)) != NULL) { //we found this alias in the ll. now to replace the value - free(tmp->target); - if(!(tmp->target=strdup(value))) return 2; + //free(tmp->target);//WHY? no. + //if(!(tmp->target=strdup(value))) return 2; + tmp->target=value;//can't strdup for non-strings. do it yourself. return 0; } if(ht->bucket[h]->ll == NULL) { @@ -72,7 +70,8 @@ int ht_setkey(struct hashtable *ht,char *key,char *value) { ht->bucket[h]->ll->next=0; ht->bucket[h]->ll->prev=0; if(!(ht->bucket[h]->ll->original=strdup(key))) return 4; - if(!(ht->bucket[h]->ll->target=strdup(value))) return 5; + //if(!(ht->bucket[h]->ll->target=strdup(value))) return 5; + ht->bucket[h]->ll->target=value; } else { //go to the end and add another entry to the ll. for(tmp=ht->bucket[h]->ll;tmp->next;tmp=tmp->next); @@ -80,7 +79,8 @@ int ht_setkey(struct hashtable *ht,char *key,char *value) { tmp->next->prev=tmp; tmp=tmp->next; if(!(tmp->original=strdup(key))) return 7; - if(!(tmp->target=strdup(value))) return 8; + //if(!(tmp->target=strdup(value))) return 8; + tmp->target=value; tmp->next=0; } return 0; @@ -109,7 +109,7 @@ struct entry *ht_getnode(struct hashtable *ht,char *msg) { } //you'll probably want to use me. -char *ht_getvalue(struct hashtable *ht,char *msg) { +void *ht_getvalue(struct hashtable *ht,char *msg) { struct entry *tmp=ll_getentry(ht_getentry(ht,msg),msg); return tmp?tmp->target:0; } |