khash.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /* The MIT License
  2. Copyright (c) 2008, 2009, 2011 by Attractive Chaos <attractor@live.co.uk>
  3. Permission is hereby granted, free of charge, to any person obtaining
  4. a copy of this software and associated documentation files (the
  5. "Software"), to deal in the Software without restriction, including
  6. without limitation the rights to use, copy, modify, merge, publish,
  7. distribute, sublicense, and/or sell copies of the Software, and to
  8. permit persons to whom the Software is furnished to do so, subject to
  9. the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  16. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  17. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  18. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21. /*
  22. An example:
  23. #include "khash.h"
  24. KHASH_MAP_INIT_INT(32, char)
  25. int main() {
  26. int ret, is_missing;
  27. khiter_t k;
  28. khash_t(32) *h = kh_init(32);
  29. k = kh_put(32, h, 5, &ret);
  30. kh_value(h, k) = 10;
  31. k = kh_get(32, h, 10);
  32. is_missing = (k == kh_end(h));
  33. k = kh_get(32, h, 5);
  34. kh_del(32, h, k);
  35. for (k = kh_begin(h); k != kh_end(h); ++k)
  36. if (kh_exist(h, k)) kh_value(h, k) = 1;
  37. kh_destroy(32, h);
  38. return 0;
  39. }
  40. */
  41. /*
  42. 2013-05-02 (0.2.8):
  43. * Use quadratic probing. When the capacity is power of 2, stepping function
  44. i*(i+1)/2 guarantees to traverse each bucket. It is better than double
  45. hashing on cache performance and is more robust than linear probing.
  46. In theory, double hashing should be more robust than quadratic probing.
  47. However, my implementation is probably not for large hash tables, because
  48. the second hash function is closely tied to the first hash function,
  49. which reduce the effectiveness of double hashing.
  50. Reference: http://research.cs.vt.edu/AVresearch/hashing/quadratic.php
  51. 2011-12-29 (0.2.7):
  52. * Minor code clean up; no actual effect.
  53. 2011-09-16 (0.2.6):
  54. * The capacity is a power of 2. This seems to dramatically improve the
  55. speed for simple keys. Thank Zilong Tan for the suggestion. Reference:
  56. - http://code.google.com/p/ulib/
  57. - http://nothings.org/computer/judy/
  58. * Allow to optionally use linear probing which usually has better
  59. performance for random input. Double hashing is still the default as it
  60. is more robust to certain non-random input.
  61. * Added Wang's integer hash function (not used by default). This hash
  62. function is more robust to certain non-random input.
  63. 2011-02-14 (0.2.5):
  64. * Allow to declare global functions.
  65. 2009-09-26 (0.2.4):
  66. * Improve portability
  67. 2008-09-19 (0.2.3):
  68. * Corrected the example
  69. * Improved interfaces
  70. 2008-09-11 (0.2.2):
  71. * Improved speed a little in kh_put()
  72. 2008-09-10 (0.2.1):
  73. * Added kh_clear()
  74. * Fixed a compiling error
  75. 2008-09-02 (0.2.0):
  76. * Changed to token concatenation which increases flexibility.
  77. 2008-08-31 (0.1.2):
  78. * Fixed a bug in kh_get(), which has not been tested previously.
  79. 2008-08-31 (0.1.1):
  80. * Added destructor
  81. */
  82. #ifndef __AC_KHASH_H
  83. #define __AC_KHASH_H
  84. /*!
  85. @header
  86. Generic hash table library.
  87. */
  88. #define AC_VERSION_KHASH_H "0.2.8"
  89. #include <stdlib.h>
  90. #include <string.h>
  91. #include <limits.h>
  92. /* compiler specific configuration */
  93. #if UINT_MAX == 0xffffffffu
  94. typedef unsigned int khint32_t;
  95. #elif ULONG_MAX == 0xffffffffu
  96. typedef unsigned long khint32_t;
  97. #endif
  98. #if ULONG_MAX == ULLONG_MAX
  99. typedef unsigned long khint64_t;
  100. #else
  101. typedef unsigned long long khint64_t;
  102. #endif
  103. #ifndef kh_inline
  104. #ifdef _MSC_VER
  105. #define kh_inline __inline
  106. #else
  107. #define kh_inline inline
  108. #endif
  109. #endif /* kh_inline */
  110. #ifndef klib_unused
  111. #if (defined __clang__ && __clang_major__ >= 3) || (defined __GNUC__ && __GNUC__ >= 3)
  112. #define klib_unused __attribute__ ((__unused__))
  113. #else
  114. #define klib_unused
  115. #endif
  116. #endif /* klib_unused */
  117. typedef khint32_t khint_t;
  118. typedef khint_t khiter_t;
  119. #define __ac_isempty(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2)
  120. #define __ac_isdel(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1)
  121. #define __ac_iseither(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&3)
  122. #define __ac_set_isdel_false(flag, i) (flag[i>>4]&=~(1ul<<((i&0xfU)<<1)))
  123. #define __ac_set_isempty_false(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1)))
  124. #define __ac_set_isboth_false(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1)))
  125. #define __ac_set_isdel_true(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1))
  126. #define __ac_fsize(m) ((m) < 16? 1 : (m)>>4)
  127. #ifndef kroundup32
  128. #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
  129. #endif
  130. #ifndef kcalloc
  131. #define kcalloc(N,Z) calloc(N,Z)
  132. #endif
  133. #ifndef kmalloc
  134. #define kmalloc(Z) malloc(Z)
  135. #endif
  136. #ifndef krealloc
  137. #define krealloc(P,Z) realloc(P,Z)
  138. #endif
  139. #ifndef kfree
  140. #define kfree(P) free(P)
  141. #endif
  142. static const double __ac_HASH_UPPER = 0.77;
  143. #define __KHASH_TYPE(name, khkey_t, khval_t) \
  144. typedef struct kh_##name##_s { \
  145. khint_t n_buckets, size, n_occupied, upper_bound; \
  146. khint32_t *flags; \
  147. khkey_t *keys; \
  148. khval_t *vals; \
  149. } kh_##name##_t;
  150. #define __KHASH_PROTOTYPES(name, khkey_t, khval_t) \
  151. extern kh_##name##_t *kh_init_##name(void); \
  152. extern void kh_destroy_##name(kh_##name##_t *h); \
  153. extern void kh_clear_##name(kh_##name##_t *h); \
  154. extern khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); \
  155. extern int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \
  156. extern khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret); \
  157. extern void kh_del_##name(kh_##name##_t *h, khint_t x);
  158. #define __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
  159. SCOPE kh_##name##_t *kh_init_##name(void) { \
  160. return (kh_##name##_t*)kcalloc(1, sizeof(kh_##name##_t)); \
  161. } \
  162. SCOPE void kh_destroy_##name(kh_##name##_t *h) \
  163. { \
  164. if (h) { \
  165. kfree((void *)h->keys); kfree(h->flags); \
  166. kfree((void *)h->vals); \
  167. kfree(h); \
  168. } \
  169. } \
  170. SCOPE void kh_clear_##name(kh_##name##_t *h) \
  171. { \
  172. if (h && h->flags) { \
  173. memset(h->flags, 0xaa, __ac_fsize(h->n_buckets) * sizeof(khint32_t)); \
  174. h->size = h->n_occupied = 0; \
  175. } \
  176. } \
  177. SCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \
  178. { \
  179. if (h->n_buckets) { \
  180. khint_t k, i, last, mask, step = 0; \
  181. mask = h->n_buckets - 1; \
  182. k = __hash_func(key); i = k & mask; \
  183. last = i; \
  184. while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
  185. i = (i + (++step)) & mask; \
  186. if (i == last) return h->n_buckets; \
  187. } \
  188. return __ac_iseither(h->flags, i)? h->n_buckets : i; \
  189. } else return 0; \
  190. } \
  191. SCOPE int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
  192. { /* This function uses 0.25*n_buckets bytes of working space instead of [sizeof(key_t+val_t)+.25]*n_buckets. */ \
  193. khint32_t *new_flags = 0; \
  194. khint_t j = 1; \
  195. { \
  196. kroundup32(new_n_buckets); \
  197. if (new_n_buckets < 4) new_n_buckets = 4; \
  198. if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; /* requested size is too small */ \
  199. else { /* hash table size to be changed (shrink or expand); rehash */ \
  200. new_flags = (khint32_t*)kmalloc(__ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
  201. if (!new_flags) return -1; \
  202. memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
  203. if (h->n_buckets < new_n_buckets) { /* expand */ \
  204. khkey_t *new_keys = (khkey_t*)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
  205. if (!new_keys) { kfree(new_flags); return -1; } \
  206. h->keys = new_keys; \
  207. if (kh_is_map) { \
  208. khval_t *new_vals = (khval_t*)krealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \
  209. if (!new_vals) { kfree(new_flags); return -1; } \
  210. h->vals = new_vals; \
  211. } \
  212. } /* otherwise shrink */ \
  213. } \
  214. } \
  215. if (j) { /* rehashing is needed */ \
  216. for (j = 0; j != h->n_buckets; ++j) { \
  217. if (__ac_iseither(h->flags, j) == 0) { \
  218. khkey_t key = h->keys[j]; \
  219. khval_t val; \
  220. khint_t new_mask; \
  221. new_mask = new_n_buckets - 1; \
  222. if (kh_is_map) val = h->vals[j]; \
  223. __ac_set_isdel_true(h->flags, j); \
  224. while (1) { /* kick-out process; sort of like in Cuckoo hashing */ \
  225. khint_t k, i, step = 0; \
  226. k = __hash_func(key); \
  227. i = k & new_mask; \
  228. while (!__ac_isempty(new_flags, i)) i = (i + (++step)) & new_mask; \
  229. __ac_set_isempty_false(new_flags, i); \
  230. if (i < h->n_buckets && __ac_iseither(h->flags, i) == 0) { /* kick out the existing element */ \
  231. { khkey_t tmp = h->keys[i]; h->keys[i] = key; key = tmp; } \
  232. if (kh_is_map) { khval_t tmp = h->vals[i]; h->vals[i] = val; val = tmp; } \
  233. __ac_set_isdel_true(h->flags, i); /* mark it as deleted in the old hash table */ \
  234. } else { /* write the element and jump out of the loop */ \
  235. h->keys[i] = key; \
  236. if (kh_is_map) h->vals[i] = val; \
  237. break; \
  238. } \
  239. } \
  240. } \
  241. } \
  242. if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \
  243. h->keys = (khkey_t*)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
  244. if (kh_is_map) h->vals = (khval_t*)krealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \
  245. } \
  246. kfree(h->flags); /* free the working space */ \
  247. h->flags = new_flags; \
  248. h->n_buckets = new_n_buckets; \
  249. h->n_occupied = h->size; \
  250. h->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \
  251. } \
  252. return 0; \
  253. } \
  254. SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \
  255. { \
  256. khint_t x; \
  257. if (h->n_occupied >= h->upper_bound) { /* update the hash table */ \
  258. if (h->n_buckets > (h->size<<1)) { \
  259. if (kh_resize_##name(h, h->n_buckets - 1) < 0) { /* clear "deleted" elements */ \
  260. *ret = -1; return h->n_buckets; \
  261. } \
  262. } else if (kh_resize_##name(h, h->n_buckets + 1) < 0) { /* expand the hash table */ \
  263. *ret = -1; return h->n_buckets; \
  264. } \
  265. } /* TODO: to implement automatically shrinking; resize() already support shrinking */ \
  266. { \
  267. khint_t k, i, site, last, mask = h->n_buckets - 1, step = 0; \
  268. x = site = h->n_buckets; k = __hash_func(key); i = k & mask; \
  269. if (__ac_isempty(h->flags, i)) x = i; /* for speed up */ \
  270. else { \
  271. last = i; \
  272. while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
  273. if (__ac_isdel(h->flags, i)) site = i; \
  274. i = (i + (++step)) & mask; \
  275. if (i == last) { x = site; break; } \
  276. } \
  277. if (x == h->n_buckets) { \
  278. if (__ac_isempty(h->flags, i) && site != h->n_buckets) x = site; \
  279. else x = i; \
  280. } \
  281. } \
  282. } \
  283. if (__ac_isempty(h->flags, x)) { /* not present at all */ \
  284. h->keys[x] = key; \
  285. __ac_set_isboth_false(h->flags, x); \
  286. ++h->size; ++h->n_occupied; \
  287. *ret = 1; \
  288. } else if (__ac_isdel(h->flags, x)) { /* deleted */ \
  289. h->keys[x] = key; \
  290. __ac_set_isboth_false(h->flags, x); \
  291. ++h->size; \
  292. *ret = 2; \
  293. } else *ret = 0; /* Don't touch h->keys[x] if present and not deleted */ \
  294. return x; \
  295. } \
  296. SCOPE void kh_del_##name(kh_##name##_t *h, khint_t x) \
  297. { \
  298. if (x != h->n_buckets && !__ac_iseither(h->flags, x)) { \
  299. __ac_set_isdel_true(h->flags, x); \
  300. --h->size; \
  301. } \
  302. }
  303. #define KHASH_DECLARE(name, khkey_t, khval_t) \
  304. __KHASH_TYPE(name, khkey_t, khval_t) \
  305. __KHASH_PROTOTYPES(name, khkey_t, khval_t)
  306. #define KHASH_INIT2(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
  307. __KHASH_TYPE(name, khkey_t, khval_t) \
  308. __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
  309. #define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
  310. KHASH_INIT2(name, static kh_inline klib_unused, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
  311. /* --- BEGIN OF HASH FUNCTIONS --- */
  312. /*! @function
  313. @abstract Integer hash function
  314. @param key The integer [khint32_t]
  315. @return The hash value [khint_t]
  316. */
  317. #define kh_int_hash_func(key) (khint32_t)(key)
  318. /*! @function
  319. @abstract Integer comparison function
  320. */
  321. #define kh_int_hash_equal(a, b) ((a) == (b))
  322. /*! @function
  323. @abstract 64-bit integer hash function
  324. @param key The integer [khint64_t]
  325. @return The hash value [khint_t]
  326. */
  327. #define kh_int64_hash_func(key) (khint32_t)((key)>>33^(key)^(key)<<11)
  328. /*! @function
  329. @abstract 64-bit integer comparison function
  330. */
  331. #define kh_int64_hash_equal(a, b) ((a) == (b))
  332. /*! @function
  333. @abstract const char* hash function
  334. @param s Pointer to a null terminated string
  335. @return The hash value
  336. */
  337. static kh_inline khint_t __ac_X31_hash_string(const char *s)
  338. {
  339. khint_t h = (khint_t)*s;
  340. if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)*s;
  341. return h;
  342. }
  343. /*! @function
  344. @abstract Another interface to const char* hash function
  345. @param key Pointer to a null terminated string [const char*]
  346. @return The hash value [khint_t]
  347. */
  348. #define kh_str_hash_func(key) __ac_X31_hash_string(key)
  349. /*! @function
  350. @abstract Const char* comparison function
  351. */
  352. #define kh_str_hash_equal(a, b) (strcmp(a, b) == 0)
  353. static kh_inline khint_t __ac_Wang_hash(khint_t key)
  354. {
  355. key += ~(key << 15);
  356. key ^= (key >> 10);
  357. key += (key << 3);
  358. key ^= (key >> 6);
  359. key += ~(key << 11);
  360. key ^= (key >> 16);
  361. return key;
  362. }
  363. #define kh_int_hash_func2(key) __ac_Wang_hash((khint_t)key)
  364. /* --- END OF HASH FUNCTIONS --- */
  365. /* Other convenient macros... */
  366. /*!
  367. @abstract Type of the hash table.
  368. @param name Name of the hash table [symbol]
  369. */
  370. #define khash_t(name) kh_##name##_t
  371. /*! @function
  372. @abstract Initiate a hash table.
  373. @param name Name of the hash table [symbol]
  374. @return Pointer to the hash table [khash_t(name)*]
  375. */
  376. #define kh_init(name) kh_init_##name()
  377. /*! @function
  378. @abstract Destroy a hash table.
  379. @param name Name of the hash table [symbol]
  380. @param h Pointer to the hash table [khash_t(name)*]
  381. */
  382. #define kh_destroy(name, h) kh_destroy_##name(h)
  383. /*! @function
  384. @abstract Reset a hash table without deallocating memory.
  385. @param name Name of the hash table [symbol]
  386. @param h Pointer to the hash table [khash_t(name)*]
  387. */
  388. #define kh_clear(name, h) kh_clear_##name(h)
  389. /*! @function
  390. @abstract Resize a hash table.
  391. @param name Name of the hash table [symbol]
  392. @param h Pointer to the hash table [khash_t(name)*]
  393. @param s New size [khint_t]
  394. */
  395. #define kh_resize(name, h, s) kh_resize_##name(h, s)
  396. /*! @function
  397. @abstract Insert a key to the hash table.
  398. @param name Name of the hash table [symbol]
  399. @param h Pointer to the hash table [khash_t(name)*]
  400. @param k Key [type of keys]
  401. @param r Extra return code: -1 if the operation failed;
  402. 0 if the key is present in the hash table;
  403. 1 if the bucket is empty (never used); 2 if the element in
  404. the bucket has been deleted [int*]
  405. @return Iterator to the inserted element [khint_t]
  406. */
  407. #define kh_put(name, h, k, r) kh_put_##name(h, k, r)
  408. /*! @function
  409. @abstract Retrieve a key from the hash table.
  410. @param name Name of the hash table [symbol]
  411. @param h Pointer to the hash table [khash_t(name)*]
  412. @param k Key [type of keys]
  413. @return Iterator to the found element, or kh_end(h) if the element is absent [khint_t]
  414. */
  415. #define kh_get(name, h, k) kh_get_##name(h, k)
  416. /*! @function
  417. @abstract Remove a key from the hash table.
  418. @param name Name of the hash table [symbol]
  419. @param h Pointer to the hash table [khash_t(name)*]
  420. @param k Iterator to the element to be deleted [khint_t]
  421. */
  422. #define kh_del(name, h, k) kh_del_##name(h, k)
  423. /*! @function
  424. @abstract Test whether a bucket contains data.
  425. @param h Pointer to the hash table [khash_t(name)*]
  426. @param x Iterator to the bucket [khint_t]
  427. @return 1 if containing data; 0 otherwise [int]
  428. */
  429. #define kh_exist(h, x) (!__ac_iseither((h)->flags, (x)))
  430. /*! @function
  431. @abstract Get key given an iterator
  432. @param h Pointer to the hash table [khash_t(name)*]
  433. @param x Iterator to the bucket [khint_t]
  434. @return Key [type of keys]
  435. */
  436. #define kh_key(h, x) ((h)->keys[x])
  437. /*! @function
  438. @abstract Get value given an iterator
  439. @param h Pointer to the hash table [khash_t(name)*]
  440. @param x Iterator to the bucket [khint_t]
  441. @return Value [type of values]
  442. @discussion For hash sets, calling this results in segfault.
  443. */
  444. #define kh_val(h, x) ((h)->vals[x])
  445. /*! @function
  446. @abstract Alias of kh_val()
  447. */
  448. #define kh_value(h, x) ((h)->vals[x])
  449. /*! @function
  450. @abstract Get the start iterator
  451. @param h Pointer to the hash table [khash_t(name)*]
  452. @return The start iterator [khint_t]
  453. */
  454. #define kh_begin(h) (khint_t)(0)
  455. /*! @function
  456. @abstract Get the end iterator
  457. @param h Pointer to the hash table [khash_t(name)*]
  458. @return The end iterator [khint_t]
  459. */
  460. #define kh_end(h) ((h)->n_buckets)
  461. /*! @function
  462. @abstract Get the number of elements in the hash table
  463. @param h Pointer to the hash table [khash_t(name)*]
  464. @return Number of elements in the hash table [khint_t]
  465. */
  466. #define kh_size(h) ((h)->size)
  467. /*! @function
  468. @abstract Get the number of buckets in the hash table
  469. @param h Pointer to the hash table [khash_t(name)*]
  470. @return Number of buckets in the hash table [khint_t]
  471. */
  472. #define kh_n_buckets(h) ((h)->n_buckets)
  473. /*! @function
  474. @abstract Iterate over the entries in the hash table
  475. @param h Pointer to the hash table [khash_t(name)*]
  476. @param kvar Variable to which key will be assigned
  477. @param vvar Variable to which value will be assigned
  478. @param code Block of code to execute
  479. */
  480. #define kh_foreach(h, kvar, vvar, code) { khint_t __i; \
  481. for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
  482. if (!kh_exist(h,__i)) continue; \
  483. (kvar) = kh_key(h,__i); \
  484. (vvar) = kh_val(h,__i); \
  485. code; \
  486. } }
  487. /*! @function
  488. @abstract Iterate over the values in the hash table
  489. @param h Pointer to the hash table [khash_t(name)*]
  490. @param vvar Variable to which value will be assigned
  491. @param code Block of code to execute
  492. */
  493. #define kh_foreach_value(h, vvar, code) { khint_t __i; \
  494. for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
  495. if (!kh_exist(h,__i)) continue; \
  496. (vvar) = kh_val(h,__i); \
  497. code; \
  498. } }
  499. /* More conenient interfaces */
  500. /*! @function
  501. @abstract Instantiate a hash set containing integer keys
  502. @param name Name of the hash table [symbol]
  503. */
  504. #define KHASH_SET_INIT_INT(name) \
  505. KHASH_INIT(name, khint32_t, char, 0, kh_int_hash_func, kh_int_hash_equal)
  506. /*! @function
  507. @abstract Instantiate a hash map containing integer keys
  508. @param name Name of the hash table [symbol]
  509. @param khval_t Type of values [type]
  510. */
  511. #define KHASH_MAP_INIT_INT(name, khval_t) \
  512. KHASH_INIT(name, khint32_t, khval_t, 1, kh_int_hash_func, kh_int_hash_equal)
  513. /*! @function
  514. @abstract Instantiate a hash map containing 64-bit integer keys
  515. @param name Name of the hash table [symbol]
  516. */
  517. #define KHASH_SET_INIT_INT64(name) \
  518. KHASH_INIT(name, khint64_t, char, 0, kh_int64_hash_func, kh_int64_hash_equal)
  519. /*! @function
  520. @abstract Instantiate a hash map containing 64-bit integer keys
  521. @param name Name of the hash table [symbol]
  522. @param khval_t Type of values [type]
  523. */
  524. #define KHASH_MAP_INIT_INT64(name, khval_t) \
  525. KHASH_INIT(name, khint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal)
  526. typedef const char *kh_cstr_t;
  527. /*! @function
  528. @abstract Instantiate a hash map containing const char* keys
  529. @param name Name of the hash table [symbol]
  530. */
  531. #define KHASH_SET_INIT_STR(name) \
  532. KHASH_INIT(name, kh_cstr_t, char, 0, kh_str_hash_func, kh_str_hash_equal)
  533. /*! @function
  534. @abstract Instantiate a hash map containing const char* keys
  535. @param name Name of the hash table [symbol]
  536. @param khval_t Type of values [type]
  537. */
  538. #define KHASH_MAP_INIT_STR(name, khval_t) \
  539. KHASH_INIT(name, kh_cstr_t, khval_t, 1, kh_str_hash_func, kh_str_hash_equal)
  540. #endif /* __AC_KHASH_H */