2 * Copyright (c) 2009-2012 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.
10 #include <jansson_config.h> /* for JSON_INLINE */
11 #include "jansson_private.h" /* for container_of() */
12 #include "hashtable.h"
14 typedef struct hashtable_list list_t;
15 typedef struct hashtable_pair pair_t;
16 typedef struct hashtable_bucket bucket_t;
18 #define list_to_pair(list_) container_of(list_, pair_t, list)
20 /* From http://www.cse.yorku.ca/~oz/hash.html */
21 static size_t hash_str(const void *ptr)
23 const char *str = (const char *)ptr;
28 while((c = (size_t)*str))
30 hash = ((hash << 5) + hash) + c;
37 static JSON_INLINE void list_init(list_t *list)
43 static JSON_INLINE void list_insert(list_t *list, list_t *node)
46 node->prev = list->prev;
47 list->prev->next = node;
51 static JSON_INLINE void list_remove(list_t *list)
53 list->prev->next = list->next;
54 list->next->prev = list->prev;
57 static JSON_INLINE int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket)
59 return bucket->first == &hashtable->list && bucket->first == bucket->last;
62 static void insert_to_bucket(hashtable_t *hashtable, bucket_t *bucket,
65 if(bucket_is_empty(hashtable, bucket))
67 list_insert(&hashtable->list, list);
68 bucket->first = bucket->last = list;
72 list_insert(bucket->first, list);
77 static size_t primes[] = {
78 5, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
79 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
80 12582917, 25165843, 50331653, 100663319, 201326611, 402653189,
84 static JSON_INLINE size_t num_buckets(hashtable_t *hashtable)
86 return primes[hashtable->num_buckets];
90 static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket,
91 const char *key, size_t hash)
96 if(bucket_is_empty(hashtable, bucket))
102 pair = list_to_pair(list);
103 if(pair->hash == hash && strcmp(pair->key, key) == 0)
106 if(list == bucket->last)
115 /* returns 0 on success, -1 if key was not found */
116 static int hashtable_do_del(hashtable_t *hashtable,
117 const char *key, size_t hash)
123 index = hash % num_buckets(hashtable);
124 bucket = &hashtable->buckets[index];
126 pair = hashtable_find_pair(hashtable, bucket, key, hash);
130 if(&pair->list == bucket->first && &pair->list == bucket->last)
131 bucket->first = bucket->last = &hashtable->list;
133 else if(&pair->list == bucket->first)
134 bucket->first = pair->list.next;
136 else if(&pair->list == bucket->last)
137 bucket->last = pair->list.prev;
139 list_remove(&pair->list);
140 json_decref(pair->value);
148 static void hashtable_do_clear(hashtable_t *hashtable)
153 for(list = hashtable->list.next; list != &hashtable->list; list = next)
156 pair = list_to_pair(list);
157 json_decref(pair->value);
162 static int hashtable_do_rehash(hashtable_t *hashtable)
166 size_t i, index, new_size;
168 jsonp_free(hashtable->buckets);
170 hashtable->num_buckets++;
171 new_size = num_buckets(hashtable);
173 hashtable->buckets = jsonp_malloc(new_size * sizeof(bucket_t));
174 if(!hashtable->buckets)
177 for(i = 0; i < num_buckets(hashtable); i++)
179 hashtable->buckets[i].first = hashtable->buckets[i].last =
183 list = hashtable->list.next;
184 list_init(&hashtable->list);
186 for(; list != &hashtable->list; list = next) {
188 pair = list_to_pair(list);
189 index = pair->hash % new_size;
190 insert_to_bucket(hashtable, &hashtable->buckets[index], &pair->list);
197 int hashtable_init(hashtable_t *hashtable)
202 hashtable->num_buckets = 0; /* index to primes[] */
203 hashtable->buckets = jsonp_malloc(num_buckets(hashtable) * sizeof(bucket_t));
204 if(!hashtable->buckets)
207 list_init(&hashtable->list);
209 for(i = 0; i < num_buckets(hashtable); i++)
211 hashtable->buckets[i].first = hashtable->buckets[i].last =
218 void hashtable_close(hashtable_t *hashtable)
220 hashtable_do_clear(hashtable);
221 jsonp_free(hashtable->buckets);
224 int hashtable_set(hashtable_t *hashtable,
225 const char *key, size_t serial,
232 /* rehash if the load ratio exceeds 1 */
233 if(hashtable->size >= num_buckets(hashtable))
234 if(hashtable_do_rehash(hashtable))
237 hash = hash_str(key);
238 index = hash % num_buckets(hashtable);
239 bucket = &hashtable->buckets[index];
240 pair = hashtable_find_pair(hashtable, bucket, key, hash);
244 json_decref(pair->value);
249 /* offsetof(...) returns the size of pair_t without the last,
250 flexible member. This way, the correct amount is
252 pair = jsonp_malloc(offsetof(pair_t, key) + strlen(key) + 1);
257 pair->serial = serial;
258 strcpy(pair->key, key);
260 list_init(&pair->list);
262 insert_to_bucket(hashtable, bucket, &pair->list);
269 void *hashtable_get(hashtable_t *hashtable, const char *key)
275 hash = hash_str(key);
276 bucket = &hashtable->buckets[hash % num_buckets(hashtable)];
278 pair = hashtable_find_pair(hashtable, bucket, key, hash);
285 int hashtable_del(hashtable_t *hashtable, const char *key)
287 size_t hash = hash_str(key);
288 return hashtable_do_del(hashtable, key, hash);
291 void hashtable_clear(hashtable_t *hashtable)
295 hashtable_do_clear(hashtable);
297 for(i = 0; i < num_buckets(hashtable); i++)
299 hashtable->buckets[i].first = hashtable->buckets[i].last =
303 list_init(&hashtable->list);
307 void *hashtable_iter(hashtable_t *hashtable)
309 return hashtable_iter_next(hashtable, &hashtable->list);
312 void *hashtable_iter_at(hashtable_t *hashtable, const char *key)
318 hash = hash_str(key);
319 bucket = &hashtable->buckets[hash % num_buckets(hashtable)];
321 pair = hashtable_find_pair(hashtable, bucket, key, hash);
328 void *hashtable_iter_next(hashtable_t *hashtable, void *iter)
330 list_t *list = (list_t *)iter;
331 if(list->next == &hashtable->list)
336 void *hashtable_iter_key(void *iter)
338 pair_t *pair = list_to_pair((list_t *)iter);
342 size_t hashtable_iter_serial(void *iter)
344 pair_t *pair = list_to_pair((list_t *)iter);
348 void *hashtable_iter_value(void *iter)
350 pair_t *pair = list_to_pair((list_t *)iter);
354 void hashtable_iter_set(void *iter, json_t *value)
356 pair_t *pair = list_to_pair((list_t *)iter);
358 json_decref(pair->value);