hashtable.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
  3. *
  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.
  6. */
  7. #include <stdlib.h>
  8. #include <jansson_config.h> /* for JSON_INLINE */
  9. #include "jansson_private.h" /* for container_of() */
  10. #include "hashtable.h"
  11. typedef struct hashtable_list list_t;
  12. typedef struct hashtable_pair pair_t;
  13. typedef struct hashtable_bucket bucket_t;
  14. #define list_to_pair(list_) container_of(list_, pair_t, list)
  15. static JSON_INLINE void list_init(list_t *list)
  16. {
  17. list->next = list;
  18. list->prev = list;
  19. }
  20. static JSON_INLINE void list_insert(list_t *list, list_t *node)
  21. {
  22. node->next = list;
  23. node->prev = list->prev;
  24. list->prev->next = node;
  25. list->prev = node;
  26. }
  27. static JSON_INLINE void list_remove(list_t *list)
  28. {
  29. list->prev->next = list->next;
  30. list->next->prev = list->prev;
  31. }
  32. static JSON_INLINE int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket)
  33. {
  34. return bucket->first == &hashtable->list && bucket->first == bucket->last;
  35. }
  36. static void insert_to_bucket(hashtable_t *hashtable, bucket_t *bucket,
  37. list_t *list)
  38. {
  39. if(bucket_is_empty(hashtable, bucket))
  40. {
  41. list_insert(&hashtable->list, list);
  42. bucket->first = bucket->last = list;
  43. }
  44. else
  45. {
  46. list_insert(bucket->first, list);
  47. bucket->first = list;
  48. }
  49. }
  50. static size_t primes[] = {
  51. 5, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
  52. 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
  53. 12582917, 25165843, 50331653, 100663319, 201326611, 402653189,
  54. 805306457, 1610612741
  55. };
  56. static const size_t num_primes = sizeof(primes) / sizeof(size_t);
  57. static JSON_INLINE size_t num_buckets(hashtable_t *hashtable)
  58. {
  59. return primes[hashtable->num_buckets];
  60. }
  61. static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket,
  62. const void *key, size_t hash)
  63. {
  64. list_t *list;
  65. pair_t *pair;
  66. if(bucket_is_empty(hashtable, bucket))
  67. return NULL;
  68. list = bucket->first;
  69. while(1)
  70. {
  71. pair = list_to_pair(list);
  72. if(pair->hash == hash && hashtable->cmp_keys(pair->key, key))
  73. return pair;
  74. if(list == bucket->last)
  75. break;
  76. list = list->next;
  77. }
  78. return NULL;
  79. }
  80. /* returns 0 on success, -1 if key was not found */
  81. static int hashtable_do_del(hashtable_t *hashtable,
  82. const void *key, size_t hash)
  83. {
  84. pair_t *pair;
  85. bucket_t *bucket;
  86. size_t index;
  87. index = hash % num_buckets(hashtable);
  88. bucket = &hashtable->buckets[index];
  89. pair = hashtable_find_pair(hashtable, bucket, key, hash);
  90. if(!pair)
  91. return -1;
  92. if(&pair->list == bucket->first && &pair->list == bucket->last)
  93. bucket->first = bucket->last = &hashtable->list;
  94. else if(&pair->list == bucket->first)
  95. bucket->first = pair->list.next;
  96. else if(&pair->list == bucket->last)
  97. bucket->last = pair->list.prev;
  98. list_remove(&pair->list);
  99. if(hashtable->free_key)
  100. hashtable->free_key(pair->key);
  101. if(hashtable->free_value)
  102. hashtable->free_value(pair->value);
  103. jsonp_free(pair);
  104. hashtable->size--;
  105. return 0;
  106. }
  107. static void hashtable_do_clear(hashtable_t *hashtable)
  108. {
  109. list_t *list, *next;
  110. pair_t *pair;
  111. for(list = hashtable->list.next; list != &hashtable->list; list = next)
  112. {
  113. next = list->next;
  114. pair = list_to_pair(list);
  115. if(hashtable->free_key)
  116. hashtable->free_key(pair->key);
  117. if(hashtable->free_value)
  118. hashtable->free_value(pair->value);
  119. jsonp_free(pair);
  120. }
  121. }
  122. static int hashtable_do_rehash(hashtable_t *hashtable)
  123. {
  124. list_t *list, *next;
  125. pair_t *pair;
  126. size_t i, index, new_size;
  127. jsonp_free(hashtable->buckets);
  128. hashtable->num_buckets++;
  129. new_size = num_buckets(hashtable);
  130. hashtable->buckets = jsonp_malloc(new_size * sizeof(bucket_t));
  131. if(!hashtable->buckets)
  132. return -1;
  133. for(i = 0; i < num_buckets(hashtable); i++)
  134. {
  135. hashtable->buckets[i].first = hashtable->buckets[i].last =
  136. &hashtable->list;
  137. }
  138. list = hashtable->list.next;
  139. list_init(&hashtable->list);
  140. for(; list != &hashtable->list; list = next) {
  141. next = list->next;
  142. pair = list_to_pair(list);
  143. index = pair->hash % new_size;
  144. insert_to_bucket(hashtable, &hashtable->buckets[index], &pair->list);
  145. }
  146. return 0;
  147. }
  148. hashtable_t *hashtable_create(key_hash_fn hash_key, key_cmp_fn cmp_keys,
  149. free_fn free_key, free_fn free_value)
  150. {
  151. hashtable_t *hashtable = jsonp_malloc(sizeof(hashtable_t));
  152. if(!hashtable)
  153. return NULL;
  154. if(hashtable_init(hashtable, hash_key, cmp_keys, free_key, free_value))
  155. {
  156. jsonp_free(hashtable);
  157. return NULL;
  158. }
  159. return hashtable;
  160. }
  161. void hashtable_destroy(hashtable_t *hashtable)
  162. {
  163. hashtable_close(hashtable);
  164. jsonp_free(hashtable);
  165. }
  166. int hashtable_init(hashtable_t *hashtable,
  167. key_hash_fn hash_key, key_cmp_fn cmp_keys,
  168. free_fn free_key, free_fn free_value)
  169. {
  170. size_t i;
  171. hashtable->size = 0;
  172. hashtable->num_buckets = 0; /* index to primes[] */
  173. hashtable->buckets = jsonp_malloc(num_buckets(hashtable) * sizeof(bucket_t));
  174. if(!hashtable->buckets)
  175. return -1;
  176. list_init(&hashtable->list);
  177. hashtable->hash_key = hash_key;
  178. hashtable->cmp_keys = cmp_keys;
  179. hashtable->free_key = free_key;
  180. hashtable->free_value = free_value;
  181. for(i = 0; i < num_buckets(hashtable); i++)
  182. {
  183. hashtable->buckets[i].first = hashtable->buckets[i].last =
  184. &hashtable->list;
  185. }
  186. return 0;
  187. }
  188. void hashtable_close(hashtable_t *hashtable)
  189. {
  190. hashtable_do_clear(hashtable);
  191. jsonp_free(hashtable->buckets);
  192. }
  193. int hashtable_set(hashtable_t *hashtable, void *key, void *value)
  194. {
  195. pair_t *pair;
  196. bucket_t *bucket;
  197. size_t hash, index;
  198. /* rehash if the load ratio exceeds 1 */
  199. if(hashtable->size >= num_buckets(hashtable))
  200. if(hashtable_do_rehash(hashtable))
  201. return -1;
  202. hash = hashtable->hash_key(key);
  203. index = hash % num_buckets(hashtable);
  204. bucket = &hashtable->buckets[index];
  205. pair = hashtable_find_pair(hashtable, bucket, key, hash);
  206. if(pair)
  207. {
  208. if(hashtable->free_key)
  209. hashtable->free_key(key);
  210. if(hashtable->free_value)
  211. hashtable->free_value(pair->value);
  212. pair->value = value;
  213. }
  214. else
  215. {
  216. pair = jsonp_malloc(sizeof(pair_t));
  217. if(!pair)
  218. return -1;
  219. pair->key = key;
  220. pair->value = value;
  221. pair->hash = hash;
  222. list_init(&pair->list);
  223. insert_to_bucket(hashtable, bucket, &pair->list);
  224. hashtable->size++;
  225. }
  226. return 0;
  227. }
  228. void *hashtable_get(hashtable_t *hashtable, const void *key)
  229. {
  230. pair_t *pair;
  231. size_t hash;
  232. bucket_t *bucket;
  233. hash = hashtable->hash_key(key);
  234. bucket = &hashtable->buckets[hash % num_buckets(hashtable)];
  235. pair = hashtable_find_pair(hashtable, bucket, key, hash);
  236. if(!pair)
  237. return NULL;
  238. return pair->value;
  239. }
  240. int hashtable_del(hashtable_t *hashtable, const void *key)
  241. {
  242. size_t hash = hashtable->hash_key(key);
  243. return hashtable_do_del(hashtable, key, hash);
  244. }
  245. void hashtable_clear(hashtable_t *hashtable)
  246. {
  247. size_t i;
  248. hashtable_do_clear(hashtable);
  249. for(i = 0; i < num_buckets(hashtable); i++)
  250. {
  251. hashtable->buckets[i].first = hashtable->buckets[i].last =
  252. &hashtable->list;
  253. }
  254. list_init(&hashtable->list);
  255. hashtable->size = 0;
  256. }
  257. void *hashtable_iter(hashtable_t *hashtable)
  258. {
  259. return hashtable_iter_next(hashtable, &hashtable->list);
  260. }
  261. void *hashtable_iter_at(hashtable_t *hashtable, const void *key)
  262. {
  263. pair_t *pair;
  264. size_t hash;
  265. bucket_t *bucket;
  266. hash = hashtable->hash_key(key);
  267. bucket = &hashtable->buckets[hash % num_buckets(hashtable)];
  268. pair = hashtable_find_pair(hashtable, bucket, key, hash);
  269. if(!pair)
  270. return NULL;
  271. return &pair->list;
  272. }
  273. void *hashtable_iter_next(hashtable_t *hashtable, void *iter)
  274. {
  275. list_t *list = (list_t *)iter;
  276. if(list->next == &hashtable->list)
  277. return NULL;
  278. return list->next;
  279. }
  280. void *hashtable_iter_key(void *iter)
  281. {
  282. pair_t *pair = list_to_pair((list_t *)iter);
  283. return pair->key;
  284. }
  285. void *hashtable_iter_value(void *iter)
  286. {
  287. pair_t *pair = list_to_pair((list_t *)iter);
  288. return pair->value;
  289. }
  290. void hashtable_iter_set(hashtable_t *hashtable, void *iter, void *value)
  291. {
  292. pair_t *pair = list_to_pair((list_t *)iter);
  293. if(hashtable->free_value)
  294. hashtable->free_value(pair->value);
  295. pair->value = value;
  296. }