20 #define BITS_IN_int (sizeof(int) * CHAR_BIT)
21 #define THREE_QUARTERS ((int)((BITS_IN_int * 3) / 4))
22 #define ONE_EIGHTH ((int)(BITS_IN_int / 8))
23 #define HIGH_BITS (~((unsigned int)(~0) >> ONE_EIGHTH))
38 static unsigned int hash_pjw(
void *key)
40 char *datum = (
char*)key;
41 unsigned int hash_value, i;
46 for (hash_value = 0; *datum; ++datum) {
47 hash_value = (hash_value <<
ONE_EIGHTH) + *datum;
55 static int string_compare(
void *a,
void *b)
57 return (strcmp( (
char*)a, (
char*)b ) == 0);
72 unsigned int (*hash_function)(
void*),
73 int (*hash_key_compare)(
void*,
void*))
111 unsigned int hash_val;
117 for (curr = ht->
buckets[hash_val]; curr != NULL; curr = curr->
next)
137 unsigned int hash_val;
143 for (curr = ht->
buckets[hash_val]; curr != NULL; curr = curr->
next)
174 icl_hash_t *ht,
void* key,
void *data,
void **olddata)
177 unsigned int hash_val;
185 for (prev = NULL, curr=ht->
buckets[hash_val];
187 prev = curr, curr = curr->
next)
191 if (olddata != NULL) {
192 *olddata = curr->
data;
214 if (olddata != NULL && *olddata != NULL)
234 void (*free_key)(
void*),
235 void (*free_data)(
void*))
238 unsigned int hash_val;
246 for (curr = ht->
buckets[hash_val]; curr != NULL;) {
254 if (*free_key && curr->
key)
255 (*free_key)(curr->
key);
256 if (*free_data && curr->
data)
257 (*free_data)(curr->
data);
281 void (*free_key)(
void*),
282 void (*free_data)(
void*))
290 for (i = 0; i < ht->
nbuckets; i++) {
292 for (curr = bucket; curr!=NULL;) {
294 if (*free_key && curr->
key)
295 (*free_key)(curr->
key);
296 if (*free_data && curr->
data)
297 (*free_data)(curr->
data);
327 for (i = 0; i < ht->
nbuckets; i++) {
329 for (curr = bucket; curr!=NULL;) {
331 fprintf(stream,
"icl_hash_dump: %s: %p\n", (
char*)curr->
key, curr->
data);
int icl_hash_destroy(icl_hash_t *ht, void(*free_key)(void *), void(*free_data)(void *))
Free hash table structures. Key and data are freed using functions.
int icl_hash_dump(FILE *stream, icl_hash_t *ht)
Dump the hash table's contents to the given file pointer.
struct icl_entry_s * next
icl_entry_t * icl_hash_insert(icl_hash_t *ht, void *key, void *data)
Insert an item into the hash table.
int(* hash_key_compare)(void *, void *)
icl_entry_t * icl_hash_update_insert(icl_hash_t *ht, void *key, void *data, void **olddata)
Replace entry in hash table with the given entry.
dependency free hash table
void * icl_hash_find(icl_hash_t *ht, void *key)
Search for an entry in a hash table.
unsigned int(* hash_function)(void *)
icl_hash_t * icl_hash_create(int nbuckets, unsigned int(*hash_function)(void *), int(*hash_key_compare)(void *, void *))
Create a new hash table.
int icl_hash_delete(icl_hash_t *ht, void *key, void(*free_key)(void *), void(*free_data)(void *))
Free one hash table entry located by key. Key and data are freed using functions. ...