hashtable.c 8.8 KB

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