You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

khash.h 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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 kh_unused
  111. # ifdef __GNUC__
  112. # define kh_unused(x) __attribute__((__unused__)) x
  113. # else
  114. # define kh_unused(x) x
  115. # endif
  116. #endif
  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_static_init_##name(kh_##name##_t *); \
  153. extern void kh_destroy_##name(kh_##name##_t *h); \
  154. extern void kh_static_destroy_##name(kh_##name##_t *h); \
  155. extern void kh_clear_##name(kh_##name##_t *h); \
  156. extern khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); \
  157. extern int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \
  158. extern khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret); \
  159. extern void kh_del_##name(kh_##name##_t *h, khint_t x);
  160. #define __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
  161. SCOPE kh_##name##_t *kh_init_##name(void) { \
  162. return (kh_##name##_t*)kcalloc(1, sizeof(kh_##name##_t)); \
  163. } \
  164. SCOPE void kh_unused(kh_static_init_##name)(kh_##name##_t *target) {\
  165. memset(target, 0, sizeof(*target)); \
  166. } \
  167. SCOPE void kh_destroy_##name(kh_##name##_t *h) \
  168. { \
  169. if (h) { \
  170. kfree((void *)h->keys); kfree(h->flags); \
  171. kfree((void *)h->vals); \
  172. kfree(h); \
  173. } \
  174. } \
  175. SCOPE void kh_unused(kh_static_destroy_##name)(kh_##name##_t *h) { \
  176. kfree((void *)h->keys); kfree(h->flags); \
  177. kfree((void *)h->vals); \
  178. } \
  179. SCOPE void kh_unused(kh_clear_##name)(kh_##name##_t *h) \
  180. { \
  181. if (h && h->flags) { \
  182. memset(h->flags, 0xaa, __ac_fsize(h->n_buckets) * sizeof(khint32_t)); \
  183. h->size = h->n_occupied = 0; \
  184. } \
  185. } \
  186. SCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \
  187. { \
  188. if (h->n_buckets) { \
  189. khint_t k, i, last, mask, step = 0; \
  190. mask = h->n_buckets - 1; \
  191. k = __hash_func(key); i = k & mask; \
  192. last = i; \
  193. while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
  194. i = (i + (++step)) & mask; \
  195. if (i == last) return h->n_buckets; \
  196. } \
  197. return __ac_iseither(h->flags, i)? h->n_buckets : i; \
  198. } else return 0; \
  199. } \
  200. SCOPE int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
  201. { /* This function uses 0.25*n_buckets bytes of working space instead of [sizeof(key_t+val_t)+.25]*n_buckets. */ \
  202. khint32_t *new_flags = 0; \
  203. khint_t j = 1; \
  204. { \
  205. kroundup32(new_n_buckets); \
  206. if (new_n_buckets < 4) new_n_buckets = 4; \
  207. if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; /* requested size is too small */ \
  208. else { /* hash table size to be changed (shrink or expand); rehash */ \
  209. new_flags = (khint32_t*)kmalloc(__ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
  210. if (!new_flags) return -1; \
  211. memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
  212. if (h->n_buckets < new_n_buckets) { /* expand */ \
  213. khkey_t *new_keys = (khkey_t*)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
  214. if (!new_keys) { kfree(new_flags); return -1; } \
  215. h->keys = new_keys; \
  216. if (kh_is_map) { \
  217. khval_t *new_vals = (khval_t*)krealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \
  218. if (!new_vals) { kfree(new_flags); return -1; } \
  219. h->vals = new_vals; \
  220. } \
  221. } /* otherwise shrink */ \
  222. } \
  223. } \
  224. if (j) { /* rehashing is needed */ \
  225. for (j = 0; j != h->n_buckets; ++j) { \
  226. if (__ac_iseither(h->flags, j) == 0) { \
  227. khkey_t key = h->keys[j]; \
  228. khval_t val; \
  229. khint_t new_mask; \
  230. new_mask = new_n_buckets - 1; \
  231. if (kh_is_map) val = h->vals[j]; \
  232. __ac_set_isdel_true(h->flags, j); \
  233. while (1) { /* kick-out process; sort of like in Cuckoo hashing */ \
  234. khint_t k, i, step = 0; \
  235. k = __hash_func(key); \
  236. i = k & new_mask; \
  237. while (!__ac_isempty(new_flags, i)) i = (i + (++step)) & new_mask; \
  238. __ac_set_isempty_false(new_flags, i); \
  239. if (i < h->n_buckets && __ac_iseither(h->flags, i) == 0) { /* kick out the existing element */ \
  240. { khkey_t tmp = h->keys[i]; h->keys[i] = key; key = tmp; } \
  241. if (kh_is_map) { khval_t tmp = h->vals[i]; h->vals[i] = val; val = tmp; } \
  242. __ac_set_isdel_true(h->flags, i); /* mark it as deleted in the old hash table */ \
  243. } else { /* write the element and jump out of the loop */ \
  244. h->keys[i] = key; \
  245. if (kh_is_map) h->vals[i] = val; \
  246. break; \
  247. } \
  248. } \
  249. } \
  250. } \
  251. if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \
  252. h->keys = (khkey_t*)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
  253. if (kh_is_map) h->vals = (khval_t*)krealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \
  254. } \
  255. kfree(h->flags); /* free the working space */ \
  256. h->flags = new_flags; \
  257. h->n_buckets = new_n_buckets; \
  258. h->n_occupied = h->size; \
  259. h->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \
  260. } \
  261. return 0; \
  262. } \
  263. SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \
  264. { \
  265. khint_t x; \
  266. if (h->n_occupied >= h->upper_bound) { /* update the hash table */ \
  267. if (h->n_buckets > (h->size<<1)) { \
  268. if (kh_resize_##name(h, h->n_buckets - 1) < 0) { /* clear "deleted" elements */ \
  269. *ret = -1; return h->n_buckets; \
  270. } \
  271. } else if (kh_resize_##name(h, h->n_buckets + 1) < 0) { /* expand the hash table */ \
  272. *ret = -1; return h->n_buckets; \
  273. } \
  274. } /* TODO: to implement automatically shrinking; resize() already support shrinking */ \
  275. { \
  276. khint_t k, i, site, last, mask = h->n_buckets - 1, step = 0; \
  277. x = site = h->n_buckets; k = __hash_func(key); i = k & mask; \
  278. if (__ac_isempty(h->flags, i)) x = i; /* for speed up */ \
  279. else { \
  280. last = i; \
  281. while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
  282. if (__ac_isdel(h->flags, i)) site = i; \
  283. i = (i + (++step)) & mask; \
  284. if (i == last) { x = site; break; } \
  285. } \
  286. if (x == h->n_buckets) { \
  287. if (__ac_isempty(h->flags, i) && site != h->n_buckets) x = site; \
  288. else x = i; \
  289. } \
  290. } \
  291. } \
  292. if (__ac_isempty(h->flags, x)) { /* not present at all */ \
  293. h->keys[x] = key; \
  294. __ac_set_isboth_false(h->flags, x); \
  295. ++h->size; ++h->n_occupied; \
  296. *ret = 1; \
  297. } else if (__ac_isdel(h->flags, x)) { /* deleted */ \
  298. h->keys[x] = key; \
  299. __ac_set_isboth_false(h->flags, x); \
  300. ++h->size; \
  301. *ret = 2; \
  302. } else *ret = 0; /* Don't touch h->keys[x] if present and not deleted */ \
  303. return x; \
  304. } \
  305. SCOPE void kh_unused(kh_del_##name)(kh_##name##_t *h, khint_t x) \
  306. { \
  307. if (x != h->n_buckets && !__ac_iseither(h->flags, x)) { \
  308. __ac_set_isdel_true(h->flags, x); \
  309. --h->size; \
  310. } \
  311. }
  312. #define KHASH_DECLARE(name, khkey_t, khval_t) \
  313. __KHASH_TYPE(name, khkey_t, khval_t); \
  314. __KHASH_PROTOTYPES(name, khkey_t, khval_t)
  315. #define KHASH_INIT2(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
  316. __KHASH_TYPE(name, khkey_t, khval_t); \
  317. __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
  318. #define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
  319. KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
  320. /* --- BEGIN OF HASH FUNCTIONS --- */
  321. /*! @function
  322. @abstract Integer hash function
  323. @param key The integer [khint32_t]
  324. @return The hash value [khint_t]
  325. */
  326. #define kh_int_hash_func(key) (khint32_t)(key)
  327. /*! @function
  328. @abstract Integer comparison function
  329. */
  330. #define kh_int_hash_equal(a, b) ((a) == (b))
  331. /*! @function
  332. @abstract 64-bit integer hash function
  333. @param key The integer [khint64_t]
  334. @return The hash value [khint_t]
  335. */
  336. #define kh_int64_hash_func(key) (khint32_t)((key)>>33^(key)^(key)<<11)
  337. /*! @function
  338. @abstract 64-bit integer comparison function
  339. */
  340. #define kh_int64_hash_equal(a, b) ((a) == (b))
  341. /*! @function
  342. @abstract const char* hash function
  343. @param s Pointer to a null terminated string
  344. @return The hash value
  345. */
  346. static kh_inline khint_t __ac_X31_hash_string(const char *s)
  347. {
  348. khint_t h = (khint_t)*s;
  349. if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)*s;
  350. return h;
  351. }
  352. /**
  353. * Wyhash implementation from https://github.com/wangyi-fudan/wyhash
  354. */
  355. static inline unsigned _wyr32(const uint8_t *p) { unsigned v; memcpy(&v, p, 4); return v;}
  356. static inline unsigned _wyr24(const uint8_t *p, unsigned k) { return (((unsigned)p[0])<<16)|(((unsigned)p[k>>1])<<8)|p[k-1];}
  357. static inline void _wymix32(unsigned *A, unsigned *B){
  358. uint64_t c=*A^0x53c5ca59u; c*=*B^0x74743c1bu;
  359. *A=(unsigned)c;
  360. *B=(unsigned)(c>>32);
  361. }
  362. // This version is vulnerable when used with a few bad seeds, which should be skipped beforehand:
  363. // 0x429dacdd, 0xd637dbf3
  364. static inline unsigned _wyhash32(const void *key, uint64_t len, unsigned seed) {
  365. const uint8_t *p=(const uint8_t *)key; uint64_t i=len;
  366. unsigned see1=(unsigned)len; seed^=(unsigned)(len>>32); _wymix32(&seed, &see1);
  367. for(;i>8;i-=8,p+=8){ seed^=_wyr32(p); see1^=_wyr32(p+4); _wymix32(&seed, &see1); }
  368. if(i>=4){ seed^=_wyr32(p); see1^=_wyr32(p+i-4); } else if (i) seed^=_wyr24(p,(unsigned)i);
  369. _wymix32(&seed, &see1); _wymix32(&seed, &see1); return seed^see1;
  370. }
  371. /*! @function
  372. @abstract Another interface to const char* hash function
  373. @param key Pointer to a null terminated string [const char*]
  374. @return The hash value [khint_t]
  375. */
  376. #define kh_str_hash_func(key) _wyhash32(key, strlen(key), 0)
  377. /*! @function
  378. @abstract Const char* comparison function
  379. */
  380. #define kh_str_hash_equal(a, b) (strcmp(a, b) == 0)
  381. static kh_inline khint_t __ac_Wang_hash(khint_t key)
  382. {
  383. key += ~(key << 15);
  384. key ^= (key >> 10);
  385. key += (key << 3);
  386. key ^= (key >> 6);
  387. key += ~(key << 11);
  388. key ^= (key >> 16);
  389. return key;
  390. }
  391. #define kh_int_hash_func2(k) __ac_Wang_hash((khint_t)key)
  392. /* --- END OF HASH FUNCTIONS --- */
  393. /* Other convenient macros... */
  394. /*!
  395. @abstract Type of the hash table.
  396. @param name Name of the hash table [symbol]
  397. */
  398. #define khash_t(name) kh_##name##_t
  399. /*! @function
  400. @abstract Initiate a hash table.
  401. @param name Name of the hash table [symbol]
  402. @return Pointer to the hash table [khash_t(name)*]
  403. */
  404. #define kh_init(name) kh_init_##name()
  405. #define kh_static_init(name, h) kh_static_init_##name(h)
  406. /*! @function
  407. @abstract Destroy a hash table.
  408. @param name Name of the hash table [symbol]
  409. @param h Pointer to the hash table [khash_t(name)*]
  410. */
  411. #define kh_destroy(name, h) kh_destroy_##name(h)
  412. #define kh_static_destroy(name, h) kh_static_destroy_##name(h)
  413. /*! @function
  414. @abstract Reset a hash table without deallocating memory.
  415. @param name Name of the hash table [symbol]
  416. @param h Pointer to the hash table [khash_t(name)*]
  417. */
  418. #define kh_clear(name, h) kh_clear_##name(h)
  419. /*! @function
  420. @abstract Resize a hash table.
  421. @param name Name of the hash table [symbol]
  422. @param h Pointer to the hash table [khash_t(name)*]
  423. @param s New size [khint_t]
  424. */
  425. #define kh_resize(name, h, s) kh_resize_##name(h, s)
  426. /*! @function
  427. @abstract Insert a key to the hash table.
  428. @param name Name of the hash table [symbol]
  429. @param h Pointer to the hash table [khash_t(name)*]
  430. @param k Key [type of keys]
  431. @param r Extra return code: -1 if the operation failed;
  432. 0 if the key is present in the hash table;
  433. 1 if the bucket is empty (never used); 2 if the element in
  434. the bucket has been deleted [int*]
  435. @return Iterator to the inserted element [khint_t]
  436. */
  437. #define kh_put(name, h, k, r) kh_put_##name(h, k, r)
  438. /*! @function
  439. @abstract Retrieve a key from the hash table.
  440. @param name Name of the hash table [symbol]
  441. @param h Pointer to the hash table [khash_t(name)*]
  442. @param k Key [type of keys]
  443. @return Iterator to the found element, or kh_end(h) if the element is absent [khint_t]
  444. */
  445. #define kh_get(name, h, k) kh_get_##name(h, k)
  446. /*! @function
  447. @abstract Remove a key from the hash table.
  448. @param name Name of the hash table [symbol]
  449. @param h Pointer to the hash table [khash_t(name)*]
  450. @param k Iterator to the element to be deleted [khint_t]
  451. */
  452. #define kh_del(name, h, k) kh_del_##name(h, k)
  453. /*! @function
  454. @abstract Test whether a bucket contains data.
  455. @param h Pointer to the hash table [khash_t(name)*]
  456. @param x Iterator to the bucket [khint_t]
  457. @return 1 if containing data; 0 otherwise [int]
  458. */
  459. #define kh_exist(h, x) (!__ac_iseither((h)->flags, (x)))
  460. /*! @function
  461. @abstract Get key given an iterator
  462. @param h Pointer to the hash table [khash_t(name)*]
  463. @param x Iterator to the bucket [khint_t]
  464. @return Key [type of keys]
  465. */
  466. #define kh_key(h, x) ((h)->keys[x])
  467. /*! @function
  468. @abstract Get value given an iterator
  469. @param h Pointer to the hash table [khash_t(name)*]
  470. @param x Iterator to the bucket [khint_t]
  471. @return Value [type of values]
  472. @discussion For hash sets, calling this results in segfault.
  473. */
  474. #define kh_val(h, x) ((h)->vals[x])
  475. /*! @function
  476. @abstract Alias of kh_val()
  477. */
  478. #define kh_value(h, x) ((h)->vals[x])
  479. /*! @function
  480. @abstract Get the start iterator
  481. @param h Pointer to the hash table [khash_t(name)*]
  482. @return The start iterator [khint_t]
  483. */
  484. #define kh_begin(h) (khint_t)(0)
  485. /*! @function
  486. @abstract Get the end iterator
  487. @param h Pointer to the hash table [khash_t(name)*]
  488. @return The end iterator [khint_t]
  489. */
  490. #define kh_end(h) ((h)->n_buckets)
  491. /*! @function
  492. @abstract Get the number of elements in the hash table
  493. @param h Pointer to the hash table [khash_t(name)*]
  494. @return Number of elements in the hash table [khint_t]
  495. */
  496. #define kh_size(h) ((h)->size)
  497. /*! @function
  498. @abstract Get the number of buckets in the hash table
  499. @param h Pointer to the hash table [khash_t(name)*]
  500. @return Number of buckets in the hash table [khint_t]
  501. */
  502. #define kh_n_buckets(h) ((h)->n_buckets)
  503. /*! @function
  504. @abstract Iterate over the entries in the hash table
  505. @param h Pointer to the hash table [khash_t(name)*]
  506. @param kvar Variable to which key will be assigned
  507. @param vvar Variable to which value will be assigned
  508. @param code Block of code to execute
  509. */
  510. #define kh_foreach(h, kvar, vvar, code) { khint_t __i; \
  511. for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
  512. if (!kh_exist(h,__i)) continue; \
  513. (kvar) = kh_key(h,__i); \
  514. (vvar) = kh_val(h,__i); \
  515. code; \
  516. } }
  517. /*! @function
  518. @abstract Iterate over the values in the hash table
  519. @param h Pointer to the hash table [khash_t(name)*]
  520. @param vvar Variable to which value will be assigned
  521. @param code Block of code to execute
  522. */
  523. #define kh_foreach_value(h, vvar, code) { khint_t __i; \
  524. for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
  525. if (!kh_exist(h,__i)) continue; \
  526. (vvar) = kh_val(h,__i); \
  527. code; \
  528. } }
  529. #define kh_foreach_value_ptr(h, pvvar, code) { khint_t __i; \
  530. for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
  531. if (!kh_exist(h,__i)) continue; \
  532. (pvvar) = &kh_val(h,__i); \
  533. code; \
  534. } }
  535. #define kh_foreach_key_value_ptr(h, kvar, pvvar, code) { khint_t __i; \
  536. for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
  537. if (!kh_exist(h,__i)) continue; \
  538. (kvar) = kh_key(h,__i); \
  539. (pvvar) = &kh_val(h,__i); \
  540. code; \
  541. } }
  542. #define kh_foreach_key(h, kvar, code) { \
  543. khint_t __i; \
  544. for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
  545. if (!kh_exist(h,__i)) continue; \
  546. (kvar) = kh_key(h,__i); \
  547. code; \
  548. } }
  549. /* More conenient interfaces */
  550. /*! @function
  551. @abstract Instantiate a hash set containing integer keys
  552. @param name Name of the hash table [symbol]
  553. */
  554. #define KHASH_SET_INIT_INT(name) \
  555. KHASH_INIT(name, khint32_t, char, 0, kh_int_hash_func, kh_int_hash_equal)
  556. /*! @function
  557. @abstract Instantiate a hash map containing integer keys
  558. @param name Name of the hash table [symbol]
  559. @param khval_t Type of values [type]
  560. */
  561. #define KHASH_MAP_INIT_INT(name, khval_t) \
  562. KHASH_INIT(name, khint32_t, khval_t, 1, kh_int_hash_func, kh_int_hash_equal)
  563. /*! @function
  564. @abstract Instantiate a hash map containing 64-bit integer keys
  565. @param name Name of the hash table [symbol]
  566. */
  567. #define KHASH_SET_INIT_INT64(name) \
  568. KHASH_INIT(name, khint64_t, char, 0, kh_int64_hash_func, kh_int64_hash_equal)
  569. /*! @function
  570. @abstract Instantiate a hash map containing 64-bit integer keys
  571. @param name Name of the hash table [symbol]
  572. @param khval_t Type of values [type]
  573. */
  574. #define KHASH_MAP_INIT_INT64(name, khval_t) \
  575. KHASH_INIT(name, khint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal)
  576. typedef const char *kh_cstr_t;
  577. /*! @function
  578. @abstract Instantiate a hash map containing const char* keys
  579. @param name Name of the hash table [symbol]
  580. */
  581. #define KHASH_SET_INIT_STR(name) \
  582. KHASH_INIT(name, kh_cstr_t, char, 0, kh_str_hash_func, kh_str_hash_equal)
  583. /*! @function
  584. @abstract Instantiate a hash map containing const char* keys
  585. @param name Name of the hash table [symbol]
  586. @param khval_t Type of values [type]
  587. */
  588. #define KHASH_MAP_INIT_STR(name, khval_t) \
  589. KHASH_INIT(name, kh_cstr_t, khval_t, 1, kh_str_hash_func, kh_str_hash_equal)
  590. #endif /* __AC_KHASH_H */