/* insert data into hashtable */ void htable_insert(node *hashtable, char *str) { int index = 0; /* // determine hash function */ index = htable_hash(str); if(hashtable[index].data != NULL) { /* // collision occurs - resolve by chaining */ htable_resolve(hashtable, index, str); } else { hashtable[index].data = calloc(strlen(str) + 1, sizeof(char)); strcpy(hashtable[index].data, str); } }
/* delete an entry from hashtable */ int htable_delete(node *hashtable, char *str) { node *bla; node *blb; char *tmp = NULL; int index = 0; index = htable_hash(str); /* no item at this location */ if(hashtable[index].data == NULL) return 1; /* only one item at this location */ if(hashtable[index].next == NULL) { if(strcmp(hashtable[index].data, str) == 0) { /* item found */ tmp = hashtable[index].data; hashtable[index].data = NULL; free(tmp); } } else { /* there is a chaining case */ bla = hashtable + index; /* linked list similar */ while(bla->next != NULL) { if(strcmp(bla->next->data, str) == 0) { blb = bla->next; if(bla->next->next) bla->next = bla->next->next; else bla->next = NULL; free(blb); } /* if */ } /* while */ } /* else */ return 0; }
int htable_ibucket(htable* htable, void* key, size_t keylen, int* hval) { // {{{ *hval = htable_hash(key, keylen); return *hval % htable->bucketc; } // }}}