2 * Copyright (c) 2009-2011 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.
9 #include <jansson_config.h> /* for JSON_INLINE */
10 #include "jansson_private.h" /* for container_of() */
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 list_to_pair(list_) container_of(list_, pair_t, list)
19 static JSON_INLINE void list_init(list_t *list)
25 static JSON_INLINE void list_insert(list_t *list, list_t *node)
28 node->prev = list->prev;
29 list->prev->next = node;
33 static JSON_INLINE void list_remove(list_t *list)
35 list->prev->next = list->next;
36 list->next->prev = list->prev;
39 static JSON_INLINE int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket)
41 return bucket->first == &hashtable->list && bucket->first == bucket->last;
44 static void insert_to_bucket(hashtable_t *hashtable, bucket_t *bucket,
47 if(bucket_is_empty(hashtable, bucket))
49 list_insert(&hashtable->list, list);
50 bucket->first = bucket->last = list;
54 list_insert(bucket->first, list);
59 static size_t primes[] = {
60 5, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
61 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
62 12582917, 25165843, 50331653, 100663319, 201326611, 402653189,
66 static JSON_INLINE size_t num_buckets(hashtable_t *hashtable)
68 return primes[hashtable->num_buckets];
72 static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket,
73 const void *key, size_t hash)
78 if(bucket_is_empty(hashtable, bucket))
84 pair = list_to_pair(list);
85 if(pair->hash == hash && hashtable->cmp_keys(pair->key, key))
88 if(list == bucket->last)
97 /* returns 0 on success, -1 if key was not found */
98 static int hashtable_do_del(hashtable_t *hashtable,
99 const void *key, size_t hash)
105 index = hash % num_buckets(hashtable);
106 bucket = &hashtable->buckets[index];
108 pair = hashtable_find_pair(hashtable, bucket, key, hash);
112 if(&pair->list == bucket->first && &pair->list == bucket->last)
113 bucket->first = bucket->last = &hashtable->list;
115 else if(&pair->list == bucket->first)
116 bucket->first = pair->list.next;
118 else if(&pair->list == bucket->last)
119 bucket->last = pair->list.prev;
121 list_remove(&pair->list);
123 if(hashtable->free_key)
124 hashtable->free_key(pair->key);
125 if(hashtable->free_value)
126 hashtable->free_value(pair->value);
134 static void hashtable_do_clear(hashtable_t *hashtable)
139 for(list = hashtable->list.next; list != &hashtable->list; list = next)
142 pair = list_to_pair(list);
143 if(hashtable->free_key)
144 hashtable->free_key(pair->key);
145 if(hashtable->free_value)
146 hashtable->free_value(pair->value);
151 static int hashtable_do_rehash(hashtable_t *hashtable)
155 size_t i, index, new_size;
157 jsonp_free(hashtable->buckets);
159 hashtable->num_buckets++;
160 new_size = num_buckets(hashtable);
162 hashtable->buckets = jsonp_malloc(new_size * sizeof(bucket_t));
163 if(!hashtable->buckets)
166 for(i = 0; i < num_buckets(hashtable); i++)
168 hashtable->buckets[i].first = hashtable->buckets[i].last =
172 list = hashtable->list.next;
173 list_init(&hashtable->list);
175 for(; list != &hashtable->list; list = next) {
177 pair = list_to_pair(list);
178 index = pair->hash % new_size;
179 insert_to_bucket(hashtable, &hashtable->buckets[index], &pair->list);
186 hashtable_t *hashtable_create(key_hash_fn hash_key, key_cmp_fn cmp_keys,
187 free_fn free_key, free_fn free_value)
189 hashtable_t *hashtable = jsonp_malloc(sizeof(hashtable_t));
193 if(hashtable_init(hashtable, hash_key, cmp_keys, free_key, free_value))
195 jsonp_free(hashtable);
202 void hashtable_destroy(hashtable_t *hashtable)
204 hashtable_close(hashtable);
205 jsonp_free(hashtable);
208 int hashtable_init(hashtable_t *hashtable,
209 key_hash_fn hash_key, key_cmp_fn cmp_keys,
210 free_fn free_key, free_fn free_value)
215 hashtable->num_buckets = 0; /* index to primes[] */
216 hashtable->buckets = jsonp_malloc(num_buckets(hashtable) * sizeof(bucket_t));
217 if(!hashtable->buckets)
220 list_init(&hashtable->list);
222 hashtable->hash_key = hash_key;
223 hashtable->cmp_keys = cmp_keys;
224 hashtable->free_key = free_key;
225 hashtable->free_value = free_value;
227 for(i = 0; i < num_buckets(hashtable); i++)
229 hashtable->buckets[i].first = hashtable->buckets[i].last =
236 void hashtable_close(hashtable_t *hashtable)
238 hashtable_do_clear(hashtable);
239 jsonp_free(hashtable->buckets);
242 int hashtable_set(hashtable_t *hashtable, void *key, void *value)
248 /* rehash if the load ratio exceeds 1 */
249 if(hashtable->size >= num_buckets(hashtable))
250 if(hashtable_do_rehash(hashtable))
253 hash = hashtable->hash_key(key);
254 index = hash % num_buckets(hashtable);
255 bucket = &hashtable->buckets[index];
256 pair = hashtable_find_pair(hashtable, bucket, key, hash);
260 if(hashtable->free_key)
261 hashtable->free_key(key);
262 if(hashtable->free_value)
263 hashtable->free_value(pair->value);
268 pair = jsonp_malloc(sizeof(pair_t));
275 list_init(&pair->list);
277 insert_to_bucket(hashtable, bucket, &pair->list);
284 void *hashtable_get(hashtable_t *hashtable, const void *key)
290 hash = hashtable->hash_key(key);
291 bucket = &hashtable->buckets[hash % num_buckets(hashtable)];
293 pair = hashtable_find_pair(hashtable, bucket, key, hash);
300 int hashtable_del(hashtable_t *hashtable, const void *key)
302 size_t hash = hashtable->hash_key(key);
303 return hashtable_do_del(hashtable, key, hash);
306 void hashtable_clear(hashtable_t *hashtable)
310 hashtable_do_clear(hashtable);
312 for(i = 0; i < num_buckets(hashtable); i++)
314 hashtable->buckets[i].first = hashtable->buckets[i].last =
318 list_init(&hashtable->list);
322 void *hashtable_iter(hashtable_t *hashtable)
324 return hashtable_iter_next(hashtable, &hashtable->list);
327 void *hashtable_iter_at(hashtable_t *hashtable, const void *key)
333 hash = hashtable->hash_key(key);
334 bucket = &hashtable->buckets[hash % num_buckets(hashtable)];
336 pair = hashtable_find_pair(hashtable, bucket, key, hash);
343 void *hashtable_iter_next(hashtable_t *hashtable, void *iter)
345 list_t *list = (list_t *)iter;
346 if(list->next == &hashtable->list)
351 void *hashtable_iter_key(void *iter)
353 pair_t *pair = list_to_pair((list_t *)iter);
357 void *hashtable_iter_value(void *iter)
359 pair_t *pair = list_to_pair((list_t *)iter);
363 void hashtable_iter_set(hashtable_t *hashtable, void *iter, void *value)
365 pair_t *pair = list_to_pair((list_t *)iter);
367 if(hashtable->free_value)
368 hashtable->free_value(pair->value);