2 * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the MIT license. See LICENSE for details.
11 #include "hashtable.h"
13 typedef struct hashtable_list list_t;
14 typedef struct hashtable_pair pair_t;
15 typedef struct hashtable_bucket bucket_t;
17 #define container_of(ptr_, type_, member_) \
18 ((type_ *)((char *)ptr_ - (size_t)&((type_ *)0)->member_))
20 #define list_to_pair(list_) container_of(list_, pair_t, list)
22 static inline void list_init(list_t *list)
28 static inline void list_insert(list_t *list, list_t *node)
31 node->prev = list->prev;
32 list->prev->next = node;
36 static inline void list_remove(list_t *list)
38 list->prev->next = list->next;
39 list->next->prev = list->prev;
42 static inline int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket)
44 return bucket->first == &hashtable->list && bucket->first == bucket->last;
47 static void insert_to_bucket(hashtable_t *hashtable, bucket_t *bucket,
50 if(bucket_is_empty(hashtable, bucket))
52 list_insert(&hashtable->list, list);
53 bucket->first = bucket->last = list;
57 list_insert(bucket->first, list);
62 static unsigned int primes[] = {
63 5, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
64 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
65 12582917, 25165843, 50331653, 100663319, 201326611, 402653189,
68 static const unsigned int num_primes = sizeof(primes) / sizeof(unsigned int);
70 static inline unsigned int num_buckets(hashtable_t *hashtable)
72 return primes[hashtable->num_buckets];
76 static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket,
77 const void *key, unsigned int hash)
82 if(bucket_is_empty(hashtable, bucket))
88 pair = list_to_pair(list);
89 if(pair->hash == hash && hashtable->cmp_keys(pair->key, key))
92 if(list == bucket->last)
101 /* returns 0 on success, -1 if key was not found */
102 static int hashtable_do_del(hashtable_t *hashtable,
103 const void *key, unsigned int hash)
109 index = hash % num_buckets(hashtable);
110 bucket = &hashtable->buckets[index];
112 pair = hashtable_find_pair(hashtable, bucket, key, hash);
116 if(&pair->list == bucket->first && &pair->list == bucket->last)
117 bucket->first = bucket->last = &hashtable->list;
119 else if(&pair->list == bucket->first)
120 bucket->first = pair->list.next;
122 else if(&pair->list == bucket->last)
123 bucket->last = pair->list.prev;
125 list_remove(&pair->list);
127 if(hashtable->free_key)
128 hashtable->free_key(pair->key);
129 if(hashtable->free_value)
130 hashtable->free_value(pair->value);
138 static void hashtable_do_clear(hashtable_t *hashtable)
143 for(list = hashtable->list.next; list != &hashtable->list; list = next)
146 pair = list_to_pair(list);
147 if(hashtable->free_key)
148 hashtable->free_key(pair->key);
149 if(hashtable->free_value)
150 hashtable->free_value(pair->value);
155 static int hashtable_do_rehash(hashtable_t *hashtable)
159 unsigned int i, index, new_size;
161 free(hashtable->buckets);
163 hashtable->num_buckets++;
164 new_size = num_buckets(hashtable);
166 hashtable->buckets = malloc(new_size * sizeof(bucket_t));
167 if(!hashtable->buckets)
170 for(i = 0; i < num_buckets(hashtable); i++)
172 hashtable->buckets[i].first = hashtable->buckets[i].last =
176 list = hashtable->list.next;
177 list_init(&hashtable->list);
179 for(; list != &hashtable->list; list = next) {
181 pair = list_to_pair(list);
182 index = pair->hash % new_size;
183 insert_to_bucket(hashtable, &hashtable->buckets[index], &pair->list);
190 hashtable_t *hashtable_create(key_hash_fn hash_key, key_cmp_fn cmp_keys,
191 free_fn free_key, free_fn free_value)
193 hashtable_t *hashtable = malloc(sizeof(hashtable_t));
197 if(hashtable_init(hashtable, hash_key, cmp_keys, free_key, free_value))
206 void hashtable_destroy(hashtable_t *hashtable)
208 hashtable_close(hashtable);
212 int hashtable_init(hashtable_t *hashtable,
213 key_hash_fn hash_key, key_cmp_fn cmp_keys,
214 free_fn free_key, free_fn free_value)
219 hashtable->num_buckets = 0; /* index to primes[] */
220 hashtable->buckets = malloc(num_buckets(hashtable) * sizeof(bucket_t));
221 if(!hashtable->buckets)
224 list_init(&hashtable->list);
226 hashtable->hash_key = hash_key;
227 hashtable->cmp_keys = cmp_keys;
228 hashtable->free_key = free_key;
229 hashtable->free_value = free_value;
231 for(i = 0; i < num_buckets(hashtable); i++)
233 hashtable->buckets[i].first = hashtable->buckets[i].last =
240 void hashtable_close(hashtable_t *hashtable)
242 hashtable_do_clear(hashtable);
243 free(hashtable->buckets);
246 int hashtable_set(hashtable_t *hashtable, void *key, void *value)
250 unsigned int hash, index;
252 /* rehash if the load ratio exceeds 1 */
253 if(hashtable->size >= num_buckets(hashtable))
254 if(hashtable_do_rehash(hashtable))
257 hash = hashtable->hash_key(key);
258 index = hash % num_buckets(hashtable);
259 bucket = &hashtable->buckets[index];
260 pair = hashtable_find_pair(hashtable, bucket, key, hash);
264 if(hashtable->free_key)
265 hashtable->free_key(key);
266 if(hashtable->free_value)
267 hashtable->free_value(pair->value);
272 pair = malloc(sizeof(pair_t));
279 list_init(&pair->list);
281 insert_to_bucket(hashtable, bucket, &pair->list);
288 void *hashtable_get(hashtable_t *hashtable, const void *key)
294 hash = hashtable->hash_key(key);
295 bucket = &hashtable->buckets[hash % num_buckets(hashtable)];
297 pair = hashtable_find_pair(hashtable, bucket, key, hash);
304 int hashtable_del(hashtable_t *hashtable, const void *key)
306 unsigned int hash = hashtable->hash_key(key);
307 return hashtable_do_del(hashtable, key, hash);
310 void hashtable_clear(hashtable_t *hashtable)
314 hashtable_do_clear(hashtable);
316 for(i = 0; i < num_buckets(hashtable); i++)
318 hashtable->buckets[i].first = hashtable->buckets[i].last =
322 list_init(&hashtable->list);
326 void *hashtable_iter(hashtable_t *hashtable)
328 return hashtable_iter_next(hashtable, &hashtable->list);
331 void *hashtable_iter_at(hashtable_t *hashtable, const void *key)
337 hash = hashtable->hash_key(key);
338 bucket = &hashtable->buckets[hash % num_buckets(hashtable)];
340 pair = hashtable_find_pair(hashtable, bucket, key, hash);
347 void *hashtable_iter_next(hashtable_t *hashtable, void *iter)
349 list_t *list = (list_t *)iter;
350 if(list->next == &hashtable->list)
355 void *hashtable_iter_key(void *iter)
357 pair_t *pair = list_to_pair((list_t *)iter);
361 void *hashtable_iter_value(void *iter)
363 pair_t *pair = list_to_pair((list_t *)iter);
367 void hashtable_iter_set(hashtable_t *hashtable, void *iter, void *value)
369 pair_t *pair = list_to_pair((list_t *)iter);
371 if(hashtable->free_value)
372 hashtable->free_value(pair->value);