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,
65
static const size_t num_primes = sizeof(primes) / sizeof(size_t);
67
static JSON_INLINE size_t num_buckets(hashtable_t *hashtable)
69
return primes[hashtable->num_buckets];
73
static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket,
74
const void *key, size_t hash)
79
if(bucket_is_empty(hashtable, bucket))
85
pair = list_to_pair(list);
86
if(pair->hash == hash && hashtable->cmp_keys(pair->key, key))
89
if(list == bucket->last)
98
/* returns 0 on success, -1 if key was not found */
99
static int hashtable_do_del(hashtable_t *hashtable,
100
const void *key, size_t hash)
106
index = hash % num_buckets(hashtable);
107
bucket = &hashtable->buckets[index];
109
pair = hashtable_find_pair(hashtable, bucket, key, hash);
113
if(&pair->list == bucket->first && &pair->list == bucket->last)
114
bucket->first = bucket->last = &hashtable->list;
116
else if(&pair->list == bucket->first)
117
bucket->first = pair->list.next;
119
else if(&pair->list == bucket->last)
120
bucket->last = pair->list.prev;
122
list_remove(&pair->list);
124
if(hashtable->free_key)
125
hashtable->free_key(pair->key);
126
if(hashtable->free_value)
127
hashtable->free_value(pair->value);
135
static void hashtable_do_clear(hashtable_t *hashtable)
140
for(list = hashtable->list.next; list != &hashtable->list; list = next)
143
pair = list_to_pair(list);
144
if(hashtable->free_key)
145
hashtable->free_key(pair->key);
146
if(hashtable->free_value)
147
hashtable->free_value(pair->value);
152
static int hashtable_do_rehash(hashtable_t *hashtable)
156
size_t i, index, new_size;
158
jsonp_free(hashtable->buckets);
160
hashtable->num_buckets++;
161
new_size = num_buckets(hashtable);
163
hashtable->buckets = (struct hashtable_bucket *) jsonp_malloc(new_size * sizeof(bucket_t));
164
if(!hashtable->buckets)
167
for(i = 0; i < num_buckets(hashtable); i++)
169
hashtable->buckets[i].first = hashtable->buckets[i].last =
173
list = hashtable->list.next;
174
list_init(&hashtable->list);
176
for(; list != &hashtable->list; list = next) {
178
pair = list_to_pair(list);
179
index = pair->hash % new_size;
180
insert_to_bucket(hashtable, &hashtable->buckets[index], &pair->list);
187
hashtable_t *hashtable_create(key_hash_fn hash_key, key_cmp_fn cmp_keys,
188
free_fn free_key, free_fn free_value)
190
hashtable_t *hashtable = (hashtable_t *) jsonp_malloc(sizeof(hashtable_t));
194
if(hashtable_init(hashtable, hash_key, cmp_keys, free_key, free_value))
196
jsonp_free(hashtable);
203
void hashtable_destroy(hashtable_t *hashtable)
205
hashtable_close(hashtable);
206
jsonp_free(hashtable);
209
int hashtable_init(hashtable_t *hashtable,
210
key_hash_fn hash_key, key_cmp_fn cmp_keys,
211
free_fn free_key, free_fn free_value)
216
hashtable->num_buckets = 0; /* index to primes[] */
217
hashtable->buckets = (struct hashtable_bucket *) jsonp_malloc(num_buckets(hashtable) * sizeof(bucket_t));
218
if(!hashtable->buckets)
221
list_init(&hashtable->list);
223
hashtable->hash_key = hash_key;
224
hashtable->cmp_keys = cmp_keys;
225
hashtable->free_key = free_key;
226
hashtable->free_value = free_value;
228
for(i = 0; i < num_buckets(hashtable); i++)
230
hashtable->buckets[i].first = hashtable->buckets[i].last =
237
void hashtable_close(hashtable_t *hashtable)
239
hashtable_do_clear(hashtable);
240
jsonp_free(hashtable->buckets);
243
int hashtable_set(hashtable_t *hashtable, void *key, void *value)
249
/* rehash if the load ratio exceeds 1 */
250
if(hashtable->size >= num_buckets(hashtable))
251
if(hashtable_do_rehash(hashtable))
254
hash = hashtable->hash_key(key);
255
index = hash % num_buckets(hashtable);
256
bucket = &hashtable->buckets[index];
257
pair = hashtable_find_pair(hashtable, bucket, key, hash);
261
if(hashtable->free_key)
262
hashtable->free_key(key);
263
if(hashtable->free_value)
264
hashtable->free_value(pair->value);
269
pair = (pair_t *) jsonp_malloc(sizeof(pair_t));
276
list_init(&pair->list);
278
insert_to_bucket(hashtable, bucket, &pair->list);
285
void *hashtable_get(hashtable_t *hashtable, const void *key)
291
hash = hashtable->hash_key(key);
292
bucket = &hashtable->buckets[hash % num_buckets(hashtable)];
294
pair = hashtable_find_pair(hashtable, bucket, key, hash);
301
int hashtable_del(hashtable_t *hashtable, const void *key)
303
size_t hash = hashtable->hash_key(key);
304
return hashtable_do_del(hashtable, key, hash);
307
void hashtable_clear(hashtable_t *hashtable)
311
hashtable_do_clear(hashtable);
313
for(i = 0; i < num_buckets(hashtable); i++)
315
hashtable->buckets[i].first = hashtable->buckets[i].last =
319
list_init(&hashtable->list);
323
void *hashtable_iter(hashtable_t *hashtable)
325
return hashtable_iter_next(hashtable, &hashtable->list);
328
void *hashtable_iter_at(hashtable_t *hashtable, const void *key)
334
hash = hashtable->hash_key(key);
335
bucket = &hashtable->buckets[hash % num_buckets(hashtable)];
337
pair = hashtable_find_pair(hashtable, bucket, key, hash);
344
void *hashtable_iter_next(hashtable_t *hashtable, void *iter)
346
list_t *list = (list_t *)iter;
347
if(list->next == &hashtable->list)
352
void *hashtable_iter_key(void *iter)
354
pair_t *pair = list_to_pair((list_t *)iter);
358
void *hashtable_iter_value(void *iter)
360
pair_t *pair = list_to_pair((list_t *)iter);
364
void hashtable_iter_set(hashtable_t *hashtable, void *iter, void *value)
366
pair_t *pair = list_to_pair((list_t *)iter);
368
if(hashtable->free_value)
369
hashtable->free_value(pair->value);