1
0

khash.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /* The MIT License
  2. Copyright (c) 2008, 2009, 2011 by Attractive Chaos <[email protected]>
  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. if (!ret) kh_del(32, h, k);
  31. kh_value(h, k) = 10;
  32. k = kh_get(32, h, 10);
  33. is_missing = (k == kh_end(h));
  34. k = kh_get(32, h, 5);
  35. kh_del(32, h, k);
  36. for (k = kh_begin(h); k != kh_end(h); ++k)
  37. if (kh_exist(h, k)) kh_value(h, k) = 1;
  38. kh_destroy(32, h);
  39. return 0;
  40. }
  41. */
  42. /*
  43. 2011-02-14 (0.2.5):
  44. * Allow to declare global functions.
  45. 2009-09-26 (0.2.4):
  46. * Improve portability
  47. 2008-09-19 (0.2.3):
  48. * Corrected the example
  49. * Improved interfaces
  50. 2008-09-11 (0.2.2):
  51. * Improved speed a little in kh_put()
  52. 2008-09-10 (0.2.1):
  53. * Added kh_clear()
  54. * Fixed a compiling error
  55. 2008-09-02 (0.2.0):
  56. * Changed to token concatenation which increases flexibility.
  57. 2008-08-31 (0.1.2):
  58. * Fixed a bug in kh_get(), which has not been tested previously.
  59. 2008-08-31 (0.1.1):
  60. * Added destructor
  61. */
  62. #ifndef __AC_KHASH_H
  63. #define __AC_KHASH_H
  64. /*!
  65. @header
  66. Generic hash table library.
  67. @copyright Heng Li
  68. */
  69. #define AC_VERSION_KHASH_H "0.2.5"
  70. #include <stdlib.h>
  71. #include <string.h>
  72. #include <limits.h>
  73. /* compipler specific configuration */
  74. #if UINT_MAX == 0xffffffffu
  75. typedef unsigned int khint32_t;
  76. #elif ULONG_MAX == 0xffffffffu
  77. typedef unsigned long khint32_t;
  78. #endif
  79. #if ULONG_MAX == ULLONG_MAX
  80. typedef unsigned long khint64_t;
  81. #else
  82. typedef unsigned long long khint64_t;
  83. #endif
  84. //#ifdef _MSC_VER
  85. //#define inline __inline
  86. //#endif
  87. typedef khint32_t khint_t;
  88. typedef khint_t khiter_t;
  89. #define __ac_HASH_PRIME_SIZE 32
  90. static const khint32_t __ac_prime_list[__ac_HASH_PRIME_SIZE] =
  91. {
  92. 0ul, 3ul, 11ul, 23ul, 53ul,
  93. 97ul, 193ul, 389ul, 769ul, 1543ul,
  94. 3079ul, 6151ul, 12289ul, 24593ul, 49157ul,
  95. 98317ul, 196613ul, 393241ul, 786433ul, 1572869ul,
  96. 3145739ul, 6291469ul, 12582917ul, 25165843ul, 50331653ul,
  97. 100663319ul, 201326611ul, 402653189ul, 805306457ul, 1610612741ul,
  98. 3221225473ul, 4294967291ul
  99. };
  100. #define __ac_isempty(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2)
  101. #define __ac_isdel(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1)
  102. #define __ac_iseither(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&3)
  103. #define __ac_set_isdel_false(flag, i) (flag[i>>4]&=~(1ul<<((i&0xfU)<<1)))
  104. #define __ac_set_isempty_false(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1)))
  105. #define __ac_set_isboth_false(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1)))
  106. #define __ac_set_isdel_true(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1))
  107. static const double __ac_HASH_UPPER = 0.77;
  108. #define KHASH_DECLARE(name, khkey_t, khval_t) \
  109. typedef struct { \
  110. khint_t n_buckets, size, n_occupied, upper_bound; \
  111. khint32_t *flags; \
  112. khkey_t *keys; \
  113. khval_t *vals; \
  114. } kh_##name##_t; \
  115. extern kh_##name##_t *kh_init_##name(); \
  116. extern void kh_destroy_##name(kh_##name##_t *h); \
  117. extern void kh_clear_##name(kh_##name##_t *h); \
  118. extern khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); \
  119. extern void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \
  120. extern khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret); \
  121. extern void kh_del_##name(kh_##name##_t *h, khint_t x);
  122. #define KHASH_INIT2(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
  123. typedef struct { \
  124. khint_t n_buckets, size, n_occupied, upper_bound; \
  125. khint32_t *flags; \
  126. khkey_t *keys; \
  127. khval_t *vals; \
  128. } kh_##name##_t; \
  129. SCOPE kh_##name##_t *kh_init_##name() { \
  130. return (kh_##name##_t*)calloc(1, sizeof(kh_##name##_t)); \
  131. } \
  132. SCOPE void kh_destroy_##name(kh_##name##_t *h) \
  133. { \
  134. if (h) { \
  135. free(h->keys); free(h->flags); \
  136. free(h->vals); \
  137. free(h); \
  138. } \
  139. } \
  140. SCOPE void kh_clear_##name(kh_##name##_t *h) \
  141. { \
  142. if (h && h->flags) { \
  143. memset(h->flags, 0xaa, ((h->n_buckets>>4) + 1) * sizeof(khint32_t)); \
  144. h->size = h->n_occupied = 0; \
  145. } \
  146. } \
  147. SCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \
  148. { \
  149. if (h->n_buckets) { \
  150. khint_t inc, k, i, last; \
  151. k = __hash_func(key); i = k % h->n_buckets; \
  152. inc = 1 + k % (h->n_buckets - 1); last = i; \
  153. while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
  154. if (i + inc >= h->n_buckets) i = i + inc - h->n_buckets; \
  155. else i += inc; \
  156. if (i == last) return h->n_buckets; \
  157. } \
  158. return __ac_iseither(h->flags, i)? h->n_buckets : i; \
  159. } else return 0; \
  160. } \
  161. SCOPE void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
  162. { \
  163. khint32_t *new_flags = 0; \
  164. khint_t j = 1; \
  165. { \
  166. khint_t t = __ac_HASH_PRIME_SIZE - 1; \
  167. while (__ac_prime_list[t] > new_n_buckets) --t; \
  168. new_n_buckets = __ac_prime_list[t+1]; \
  169. if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; \
  170. else { \
  171. new_flags = (khint32_t*)malloc(((new_n_buckets>>4) + 1) * sizeof(khint32_t)); \
  172. memset(new_flags, 0xaa, ((new_n_buckets>>4) + 1) * sizeof(khint32_t)); \
  173. if (h->n_buckets < new_n_buckets) { \
  174. h->keys = (khkey_t*)realloc(h->keys, new_n_buckets * sizeof(khkey_t)); \
  175. if (kh_is_map) \
  176. h->vals = (khval_t*)realloc(h->vals, new_n_buckets * sizeof(khval_t)); \
  177. } \
  178. } \
  179. } \
  180. if (j) { \
  181. for (j = 0; j != h->n_buckets; ++j) { \
  182. if (__ac_iseither(h->flags, j) == 0) { \
  183. khkey_t key = h->keys[j]; \
  184. khval_t val; \
  185. if (kh_is_map) val = h->vals[j]; \
  186. __ac_set_isdel_true(h->flags, j); \
  187. while (1) { \
  188. khint_t inc, k, i; \
  189. k = __hash_func(key); \
  190. i = k % new_n_buckets; \
  191. inc = 1 + k % (new_n_buckets - 1); \
  192. while (!__ac_isempty(new_flags, i)) { \
  193. if (i + inc >= new_n_buckets) i = i + inc - new_n_buckets; \
  194. else i += inc; \
  195. } \
  196. __ac_set_isempty_false(new_flags, i); \
  197. if (i < h->n_buckets && __ac_iseither(h->flags, i) == 0) { \
  198. { khkey_t tmp = h->keys[i]; h->keys[i] = key; key = tmp; } \
  199. if (kh_is_map) { khval_t tmp = h->vals[i]; h->vals[i] = val; val = tmp; } \
  200. __ac_set_isdel_true(h->flags, i); \
  201. } else { \
  202. h->keys[i] = key; \
  203. if (kh_is_map) h->vals[i] = val; \
  204. break; \
  205. } \
  206. } \
  207. } \
  208. } \
  209. if (h->n_buckets > new_n_buckets) { \
  210. h->keys = (khkey_t*)realloc(h->keys, new_n_buckets * sizeof(khkey_t)); \
  211. if (kh_is_map) \
  212. h->vals = (khval_t*)realloc(h->vals, new_n_buckets * sizeof(khval_t)); \
  213. } \
  214. free(h->flags); \
  215. h->flags = new_flags; \
  216. h->n_buckets = new_n_buckets; \
  217. h->n_occupied = h->size; \
  218. h->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \
  219. } \
  220. } \
  221. SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \
  222. { \
  223. khint_t x; \
  224. if (h->n_occupied >= h->upper_bound) { \
  225. if (h->n_buckets > (h->size<<1)) kh_resize_##name(h, h->n_buckets - 1); \
  226. else kh_resize_##name(h, h->n_buckets + 1); \
  227. } \
  228. { \
  229. khint_t inc, k, i, site, last; \
  230. x = site = h->n_buckets; k = __hash_func(key); i = k % h->n_buckets; \
  231. if (__ac_isempty(h->flags, i)) x = i; \
  232. else { \
  233. inc = 1 + k % (h->n_buckets - 1); last = i; \
  234. while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
  235. if (__ac_isdel(h->flags, i)) site = i; \
  236. if (i + inc >= h->n_buckets) i = i + inc - h->n_buckets; \
  237. else i += inc; \
  238. if (i == last) { x = site; break; } \
  239. } \
  240. if (x == h->n_buckets) { \
  241. if (__ac_isempty(h->flags, i) && site != h->n_buckets) x = site; \
  242. else x = i; \
  243. } \
  244. } \
  245. } \
  246. if (__ac_isempty(h->flags, x)) { \
  247. h->keys[x] = key; \
  248. __ac_set_isboth_false(h->flags, x); \
  249. ++h->size; ++h->n_occupied; \
  250. *ret = 1; \
  251. } else if (__ac_isdel(h->flags, x)) { \
  252. h->keys[x] = key; \
  253. __ac_set_isboth_false(h->flags, x); \
  254. ++h->size; \
  255. *ret = 2; \
  256. } else *ret = 0; \
  257. return x; \
  258. } \
  259. SCOPE void kh_del_##name(kh_##name##_t *h, khint_t x) \
  260. { \
  261. if (x != h->n_buckets && !__ac_iseither(h->flags, x)) { \
  262. __ac_set_isdel_true(h->flags, x); \
  263. --h->size; \
  264. } \
  265. }
  266. #define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
  267. KHASH_INIT2(name, static inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
  268. /* --- BEGIN OF HASH FUNCTIONS --- */
  269. /*! @function
  270. @abstract Integer hash function
  271. @param key The integer [khint32_t]
  272. @return The hash value [khint_t]
  273. */
  274. #define kh_int_hash_func(key) (khint32_t)(key)
  275. /*! @function
  276. @abstract Integer comparison function
  277. */
  278. #define kh_int_hash_equal(a, b) ((a) == (b))
  279. /*! @function
  280. @abstract 64-bit integer hash function
  281. @param key The integer [khint64_t]
  282. @return The hash value [khint_t]
  283. */
  284. #define kh_int64_hash_func(key) (khint32_t)((key)>>33^(key)^(key)<<11)
  285. /*! @function
  286. @abstract 64-bit integer comparison function
  287. */
  288. #define kh_int64_hash_equal(a, b) ((a) == (b))
  289. /*! @function
  290. @abstract const char* hash function
  291. @param s Pointer to a null terminated string
  292. @return The hash value
  293. */
  294. static inline khint_t __ac_X31_hash_string(const char *s)
  295. {
  296. khint_t h = *s;
  297. if (h) for (++s ; *s; ++s) h = (h << 5) - h + *s;
  298. return h;
  299. }
  300. /*! @function
  301. @abstract Another interface to const char* hash function
  302. @param key Pointer to a null terminated string [const char*]
  303. @return The hash value [khint_t]
  304. */
  305. #define kh_str_hash_func(key) __ac_X31_hash_string(key)
  306. /*! @function
  307. @abstract Const char* comparison function
  308. */
  309. #define kh_str_hash_equal(a, b) (strcmp(a, b) == 0)
  310. /* --- END OF HASH FUNCTIONS --- */
  311. /* Other necessary macros... */
  312. /*!
  313. @abstract Type of the hash table.
  314. @param name Name of the hash table [symbol]
  315. */
  316. #define khash_t(name) kh_##name##_t
  317. /*! @function
  318. @abstract Initiate a hash table.
  319. @param name Name of the hash table [symbol]
  320. @return Pointer to the hash table [khash_t(name)*]
  321. */
  322. #define kh_init(name) kh_init_##name()
  323. /*! @function
  324. @abstract Destroy a hash table.
  325. @param name Name of the hash table [symbol]
  326. @param h Pointer to the hash table [khash_t(name)*]
  327. */
  328. #define kh_destroy(name, h) kh_destroy_##name(h)
  329. /*! @function
  330. @abstract Reset a hash table without deallocating memory.
  331. @param name Name of the hash table [symbol]
  332. @param h Pointer to the hash table [khash_t(name)*]
  333. */
  334. #define kh_clear(name, h) kh_clear_##name(h)
  335. /*! @function
  336. @abstract Resize a hash table.
  337. @param name Name of the hash table [symbol]
  338. @param h Pointer to the hash table [khash_t(name)*]
  339. @param s New size [khint_t]
  340. */
  341. #define kh_resize(name, h, s) kh_resize_##name(h, s)
  342. /*! @function
  343. @abstract Insert a key to the hash table.
  344. @param name Name of the hash table [symbol]
  345. @param h Pointer to the hash table [khash_t(name)*]
  346. @param k Key [type of keys]
  347. @param r Extra return code: 0 if the key is present in the hash table;
  348. 1 if the bucket is empty (never used); 2 if the element in
  349. the bucket has been deleted [int*]
  350. @return Iterator to the inserted element [khint_t]
  351. */
  352. #define kh_put(name, h, k, r) kh_put_##name(h, k, r)
  353. /*! @function
  354. @abstract Retrieve a key from the hash table.
  355. @param name Name of the hash table [symbol]
  356. @param h Pointer to the hash table [khash_t(name)*]
  357. @param k Key [type of keys]
  358. @return Iterator to the found element, or kh_end(h) is the element is absent [khint_t]
  359. */
  360. #define kh_get(name, h, k) kh_get_##name(h, k)
  361. /*! @function
  362. @abstract Remove a key from the hash table.
  363. @param name Name of the hash table [symbol]
  364. @param h Pointer to the hash table [khash_t(name)*]
  365. @param k Iterator to the element to be deleted [khint_t]
  366. */
  367. #define kh_del(name, h, k) kh_del_##name(h, k)
  368. /*! @function
  369. @abstract Test whether a bucket contains data.
  370. @param h Pointer to the hash table [khash_t(name)*]
  371. @param x Iterator to the bucket [khint_t]
  372. @return 1 if containing data; 0 otherwise [int]
  373. */
  374. #define kh_exist(h, x) (!__ac_iseither((h)->flags, (x)))
  375. /*! @function
  376. @abstract Get key given an iterator
  377. @param h Pointer to the hash table [khash_t(name)*]
  378. @param x Iterator to the bucket [khint_t]
  379. @return Key [type of keys]
  380. */
  381. #define kh_key(h, x) ((h)->keys[x])
  382. /*! @function
  383. @abstract Get value given an iterator
  384. @param h Pointer to the hash table [khash_t(name)*]
  385. @param x Iterator to the bucket [khint_t]
  386. @return Value [type of values]
  387. @discussion For hash sets, calling this results in segfault.
  388. */
  389. #define kh_val(h, x) ((h)->vals[x])
  390. /*! @function
  391. @abstract Alias of kh_val()
  392. */
  393. #define kh_value(h, x) ((h)->vals[x])
  394. /*! @function
  395. @abstract Get the start iterator
  396. @param h Pointer to the hash table [khash_t(name)*]
  397. @return The start iterator [khint_t]
  398. */
  399. #define kh_begin(h) (khint_t)(0)
  400. /*! @function
  401. @abstract Get the end iterator
  402. @param h Pointer to the hash table [khash_t(name)*]
  403. @return The end iterator [khint_t]
  404. */
  405. #define kh_end(h) ((h)->n_buckets)
  406. /*! @function
  407. @abstract Get the number of elements in the hash table
  408. @param h Pointer to the hash table [khash_t(name)*]
  409. @return Number of elements in the hash table [khint_t]
  410. */
  411. #define kh_size(h) ((h)->size)
  412. /*! @function
  413. @abstract Get the number of buckets in the hash table
  414. @param h Pointer to the hash table [khash_t(name)*]
  415. @return Number of buckets in the hash table [khint_t]
  416. */
  417. #define kh_n_buckets(h) ((h)->n_buckets)
  418. /* More conenient interfaces */
  419. /*! @function
  420. @abstract Instantiate a hash set containing integer keys
  421. @param name Name of the hash table [symbol]
  422. */
  423. #define KHASH_SET_INIT_INT(name) \
  424. KHASH_INIT(name, khint32_t, char, 0, kh_int_hash_func, kh_int_hash_equal)
  425. /*! @function
  426. @abstract Instantiate a hash map containing integer keys
  427. @param name Name of the hash table [symbol]
  428. @param khval_t Type of values [type]
  429. */
  430. #define KHASH_MAP_INIT_INT(name, khval_t) \
  431. KHASH_INIT(name, khint32_t, khval_t, 1, kh_int_hash_func, kh_int_hash_equal)
  432. /*! @function
  433. @abstract Instantiate a hash map containing 64-bit integer keys
  434. @param name Name of the hash table [symbol]
  435. */
  436. #define KHASH_SET_INIT_INT64(name) \
  437. KHASH_INIT(name, khint64_t, char, 0, kh_int64_hash_func, kh_int64_hash_equal)
  438. /*! @function
  439. @abstract Instantiate a hash map containing 64-bit integer keys
  440. @param name Name of the hash table [symbol]
  441. @param khval_t Type of values [type]
  442. */
  443. #define KHASH_MAP_INIT_INT64(name, khval_t) \
  444. KHASH_INIT(name, khint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal)
  445. typedef const char *kh_cstr_t;
  446. /*! @function
  447. @abstract Instantiate a hash map containing const char* keys
  448. @param name Name of the hash table [symbol]
  449. */
  450. #define KHASH_SET_INIT_STR(name) \
  451. KHASH_INIT(name, kh_cstr_t, char, 0, kh_str_hash_func, kh_str_hash_equal)
  452. /*! @function
  453. @abstract Instantiate a hash map containing const char* keys
  454. @param name Name of the hash table [symbol]
  455. @param khval_t Type of values [type]
  456. */
  457. #define KHASH_MAP_INIT_STR(name, khval_t) \
  458. KHASH_INIT(name, kh_cstr_t, khval_t, 1, kh_str_hash_func, kh_str_hash_equal)
  459. #endif /* __AC_KHASH_H */