2 * Copyright (c) 2009 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 #define container_of(ptr_, type_, member_) \
12 ((type_ *)((char *)ptr_ - (size_t)&((type_ *)0)->member_))
14 typedef struct list_t {
26 #define list_to_pair(list_) container_of(list_, pair_t, list)
36 unsigned int num_buckets; /* index to primes[] */
40 key_cmp_fn cmp_keys; /* returns non-zero for equal keys */
45 static inline void list_init(list_t *list)
51 static inline void list_insert(list_t *list, list_t *node)
54 node->prev = list->prev;
55 list->prev->next = node;
59 static inline void list_remove(list_t *list)
61 list->prev->next = list->next;
62 list->next->prev = list->prev;
65 static inline int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket)
67 return bucket->first == &hashtable->list && bucket->first == bucket->last;
70 static void insert_to_bucket(hashtable_t *hashtable, bucket_t *bucket,
73 if(bucket_is_empty(hashtable, bucket))
75 list_insert(&hashtable->list, list);
76 bucket->first = bucket->last = list;
80 list_insert(bucket->first, list);
85 static unsigned int primes[] = {
86 5, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
87 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
88 12582917, 25165843, 50331653, 100663319, 201326611, 402653189,
91 static const unsigned int num_primes = sizeof(primes) / sizeof(unsigned int);
93 static inline unsigned int num_buckets(hashtable_t *hashtable)
95 return primes[hashtable->num_buckets];
99 static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket,
100 const void *key, unsigned int hash)
105 if(bucket_is_empty(hashtable, bucket))
108 list = bucket->first;
111 pair = list_to_pair(list);
112 if(pair->hash == hash && hashtable->cmp_keys(pair->key, key))
115 if(list == bucket->last)
124 /* returns 0 on success, -1 if key was not found */
125 static int hashtable_do_del(hashtable_t *hashtable,
126 const void *key, unsigned int hash)
132 index = hash % num_buckets(hashtable);
133 bucket = &hashtable->buckets[index];
135 pair = hashtable_find_pair(hashtable, bucket, key, hash);
139 if(&pair->list == bucket->first && &pair->list == bucket->last)
140 bucket->first = bucket->last = &hashtable->list;
142 else if(&pair->list == bucket->first)
143 bucket->first = pair->list.next;
145 else if(&pair->list == bucket->last)
146 bucket->last = pair->list.prev;
148 list_remove(&pair->list);
150 if(hashtable->free_key)
151 hashtable->free_key(pair->key);
152 if(hashtable->free_value)
153 hashtable->free_value(pair->value);
161 static int hashtable_do_rehash(hashtable_t *hashtable)
165 unsigned int i, index, new_size;
167 free(hashtable->buckets);
169 hashtable->num_buckets++;
170 new_size = num_buckets(hashtable);
172 hashtable->buckets = malloc(new_size * sizeof(bucket_t));
173 if(!hashtable->buckets)
176 for(i = 0; i < num_buckets(hashtable); i++)
178 hashtable->buckets[i].first = hashtable->buckets[i].last =
182 list = hashtable->list.next;
183 list_init(&hashtable->list);
185 for(; list != &hashtable->list; list = next) {
187 pair = list_to_pair(list);
188 index = pair->hash % new_size;
189 insert_to_bucket(hashtable, &hashtable->buckets[index], &pair->list);
196 hashtable_t *hashtable_new(key_hash_fn hash_key, key_cmp_fn cmp_keys,
197 free_fn free_key, free_fn free_value)
200 hashtable_t *hashtable = malloc(sizeof(hashtable_t));
205 hashtable->num_buckets = 0; /* index to primes[] */
206 hashtable->buckets = malloc(num_buckets(hashtable) * sizeof(bucket_t));
207 if(!hashtable->buckets)
212 list_init(&hashtable->list);
214 hashtable->hash_key = hash_key;
215 hashtable->cmp_keys = cmp_keys;
216 hashtable->free_key = free_key;
217 hashtable->free_value = free_value;
219 for(i = 0; i < num_buckets(hashtable); i++)
221 hashtable->buckets[i].first = hashtable->buckets[i].last =
228 void hashtable_free(hashtable_t *hashtable)
232 for(list = hashtable->list.next; list != &hashtable->list; list = next)
235 pair = list_to_pair(list);
236 if(hashtable->free_key)
237 hashtable->free_key(pair->key);
238 if(hashtable->free_value)
239 hashtable->free_value(pair->value);
243 free(hashtable->buckets);
247 int hashtable_set(hashtable_t *hashtable, void *key, void *value)
251 unsigned int hash, index;
253 hash = hashtable->hash_key(key);
255 /* if the key already exists, delete it */
256 hashtable_do_del(hashtable, key, hash);
258 /* rehash if the load ratio exceeds 1 */
259 if(hashtable->size >= num_buckets(hashtable))
260 if(hashtable_do_rehash(hashtable))
263 pair = malloc(sizeof(pair_t));
271 index = hash % num_buckets(hashtable);
272 bucket = &hashtable->buckets[index];
274 list_init(&pair->list);
275 insert_to_bucket(hashtable, bucket, &pair->list);
281 void *hashtable_get(hashtable_t *hashtable, const void *key)
287 hash = hashtable->hash_key(key);
288 bucket = &hashtable->buckets[hash % num_buckets(hashtable)];
290 pair = hashtable_find_pair(hashtable, bucket, key, hash);
297 int hashtable_del(hashtable_t *hashtable, const void *key)
299 unsigned int hash = hashtable->hash_key(key);
300 return hashtable_do_del(hashtable, key, hash);
303 void *hashtable_iter(hashtable_t *hashtable)
305 return hashtable_iter_next(hashtable, &hashtable->list);
308 void *hashtable_iter_next(hashtable_t *hashtable, void *iter)
310 list_t *list = (list_t *)iter;
311 if(list->next == &hashtable->list)
316 void *hashtable_iter_key(void *iter)
318 pair_t *pair = list_to_pair((list_t *)iter);
322 void *hashtable_iter_value(void *iter)
324 pair_t *pair = list_to_pair((list_t *)iter);