3 Copyright (c) 2008 Genome Research Ltd (GRL).
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 /* Contact: Heng Li <lh3@sanger.ac.uk> */
32 KHASH_MAP_INIT_INT(32, char)
36 khash_t(32) *h = kh_init(32);
37 k = kh_put(32, h, 5, &ret);
38 if (!ret) kh_del(32, h, k);
40 k = kh_get(32, h, 10);
41 is_missing = (k == kh_end(h));
44 for (k = kh_begin(h); k != kh_end(h); ++k)
45 if (kh_exist(h, k)) kh_value(h, k) = 1;
54 * Corrected the example
59 * Improved speed a little in kh_put()
64 * Fixed a compiling error
68 * Changed to token concatenation which increases flexibility.
72 * Fixed a bug in kh_get(), which has not been tested previously.
86 Generic hash table library.
91 #define AC_VERSION_KHASH_H "0.2.2"
97 typedef uint32_t khint_t;
98 typedef khint_t khiter_t;
100 #define __ac_HASH_PRIME_SIZE 32
101 static const uint32_t __ac_prime_list[__ac_HASH_PRIME_SIZE] =
103 0ul, 3ul, 11ul, 23ul, 53ul,
104 97ul, 193ul, 389ul, 769ul, 1543ul,
105 3079ul, 6151ul, 12289ul, 24593ul, 49157ul,
106 98317ul, 196613ul, 393241ul, 786433ul, 1572869ul,
107 3145739ul, 6291469ul, 12582917ul, 25165843ul, 50331653ul,
108 100663319ul, 201326611ul, 402653189ul, 805306457ul, 1610612741ul,
109 3221225473ul, 4294967291ul
112 #define __ac_isempty(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2)
113 #define __ac_isdel(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1)
114 #define __ac_iseither(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&3)
115 #define __ac_set_isdel_false(flag, i) (flag[i>>4]&=~(1ul<<((i&0xfU)<<1)))
116 #define __ac_set_isempty_false(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1)))
117 #define __ac_set_isboth_false(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1)))
118 #define __ac_set_isdel_true(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1))
120 static const double __ac_HASH_UPPER = 0.77;
122 #define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
124 khint_t n_buckets, size, n_occupied, upper_bound; \
129 static inline kh_##name##_t *kh_init_##name() { \
130 return (kh_##name##_t*)calloc(1, sizeof(kh_##name##_t)); \
132 static inline void kh_destroy_##name(kh_##name##_t *h) \
135 free(h->keys); free(h->flags); \
140 static inline void kh_clear_##name(kh_##name##_t *h) \
142 if (h && h->flags) { \
143 memset(h->flags, 0xaa, ((h->n_buckets>>4) + 1) * sizeof(uint32_t)); \
144 h->size = h->n_occupied = 0; \
147 static inline khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \
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; \
156 if (i == last) return h->n_buckets; \
158 return __ac_iseither(h->flags, i)? h->n_buckets : i; \
161 static inline void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
163 uint32_t *new_flags = 0; \
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; \
171 new_flags = (uint32_t*)malloc(((new_n_buckets>>4) + 1) * sizeof(uint32_t)); \
172 memset(new_flags, 0xaa, ((new_n_buckets>>4) + 1) * sizeof(uint32_t)); \
173 if (h->n_buckets < new_n_buckets) { \
174 h->keys = (khkey_t*)realloc(h->keys, new_n_buckets * sizeof(khkey_t)); \
176 h->vals = (khval_t*)realloc(h->vals, new_n_buckets * sizeof(khval_t)); \
181 for (j = 0; j != h->n_buckets; ++j) { \
182 if (__ac_iseither(h->flags, j) == 0) { \
183 khkey_t key = h->keys[j]; \
185 if (kh_is_map) val = h->vals[j]; \
186 __ac_set_isdel_true(h->flags, j); \
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; \
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); \
203 if (kh_is_map) h->vals[i] = val; \
209 if (h->n_buckets > new_n_buckets) { \
210 h->keys = (khkey_t*)realloc(h->keys, new_n_buckets * sizeof(khkey_t)); \
212 h->vals = (khval_t*)realloc(h->vals, new_n_buckets * sizeof(khval_t)); \
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); \
221 static inline khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \
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); \
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; \
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; \
238 if (i == last) { x = site; break; } \
240 if (x == h->n_buckets) { \
241 if (__ac_isempty(h->flags, i) && site != h->n_buckets) x = site; \
246 if (__ac_isempty(h->flags, x)) { \
248 __ac_set_isboth_false(h->flags, x); \
249 ++h->size; ++h->n_occupied; \
251 } else if (__ac_isdel(h->flags, x)) { \
253 __ac_set_isboth_false(h->flags, x); \
259 static inline void kh_del_##name(kh_##name##_t *h, khint_t x) \
261 if (x != h->n_buckets && !__ac_iseither(h->flags, x)) { \
262 __ac_set_isdel_true(h->flags, x); \
267 /* --- BEGIN OF HASH FUNCTIONS --- */
270 @abstract Integer hash function
271 @param key The integer [uint32_t]
272 @return The hash value [khint_t]
274 #define kh_int_hash_func(key) (uint32_t)(key)
276 @abstract Integer comparison function
278 #define kh_int_hash_equal(a, b) ((a) == (b))
280 @abstract 64-bit integer hash function
281 @param key The integer [uint64_t]
282 @return The hash value [khint_t]
284 #define kh_int64_hash_func(key) (uint32_t)((key)>>33^(key)^(key)<<11)
286 @abstract 64-bit integer comparison function
288 #define kh_int64_hash_equal(a, b) ((a) == (b))
290 @abstract const char* hash function
291 @param s Pointer to a null terminated string
292 @return The hash value
294 static inline khint_t __ac_X31_hash_string(const char *s)
297 if (h) for (++s ; *s; ++s) h = (h << 5) - h + *s;
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]
305 #define kh_str_hash_func(key) __ac_X31_hash_string(key)
307 @abstract Const char* comparison function
309 #define kh_str_hash_equal(a, b) (strcmp(a, b) == 0)
311 /* --- END OF HASH FUNCTIONS --- */
313 /* Other necessary macros... */
316 @abstract Type of the hash table.
317 @param name Name of the hash table [symbol]
319 #define khash_t(name) kh_##name##_t
322 @abstract Initiate a hash table.
323 @param name Name of the hash table [symbol]
324 @return Pointer to the hash table [khash_t(name)*]
326 #define kh_init(name) kh_init_##name()
329 @abstract Destroy a hash table.
330 @param name Name of the hash table [symbol]
331 @param h Pointer to the hash table [khash_t(name)*]
333 #define kh_destroy(name, h) kh_destroy_##name(h)
336 @abstract Reset a hash table without deallocating memory.
337 @param name Name of the hash table [symbol]
338 @param h Pointer to the hash table [khash_t(name)*]
340 #define kh_clear(name, h) kh_clear_##name(h)
343 @abstract Resize a hash table.
344 @param name Name of the hash table [symbol]
345 @param h Pointer to the hash table [khash_t(name)*]
346 @param s New size [khint_t]
348 #define kh_resize(name, h, s) kh_resize_##name(h, s)
351 @abstract Insert a key to the hash table.
352 @param name Name of the hash table [symbol]
353 @param h Pointer to the hash table [khash_t(name)*]
354 @param k Key [type of keys]
355 @param r Extra return code: 0 if the key is present in the hash table;
356 1 if the bucket is empty (never used); 2 if the element in
357 the bucket has been deleted [int*]
358 @return Iterator to the inserted element [khint_t]
360 #define kh_put(name, h, k, r) kh_put_##name(h, k, r)
363 @abstract Retrieve a key from the hash table.
364 @param name Name of the hash table [symbol]
365 @param h Pointer to the hash table [khash_t(name)*]
366 @param k Key [type of keys]
367 @return Iterator to the found element, or kh_end(h) is the element is absent [khint_t]
369 #define kh_get(name, h, k) kh_get_##name(h, k)
372 @abstract Remove a key from the hash table.
373 @param name Name of the hash table [symbol]
374 @param h Pointer to the hash table [khash_t(name)*]
375 @param k Iterator to the element to be deleted [khint_t]
377 #define kh_del(name, h, k) kh_del_##name(h, k)
381 @abstract Test whether a bucket contains data.
382 @param h Pointer to the hash table [khash_t(name)*]
383 @param x Iterator to the bucket [khint_t]
384 @return 1 if containing data; 0 otherwise [int]
386 #define kh_exist(h, x) (!__ac_iseither((h)->flags, (x)))
389 @abstract Get key given an iterator
390 @param h Pointer to the hash table [khash_t(name)*]
391 @param x Iterator to the bucket [khint_t]
392 @return Key [type of keys]
394 #define kh_key(h, x) ((h)->keys[x])
397 @abstract Get value given an iterator
398 @param h Pointer to the hash table [khash_t(name)*]
399 @param x Iterator to the bucket [khint_t]
400 @return Value [type of values]
401 @discussion For hash sets, calling this results in segfault.
403 #define kh_val(h, x) ((h)->vals[x])
406 @abstract Alias of kh_val()
408 #define kh_value(h, x) ((h)->vals[x])
411 @abstract Get the start iterator
412 @param h Pointer to the hash table [khash_t(name)*]
413 @return The start iterator [khint_t]
415 #define kh_begin(h) (khint_t)(0)
418 @abstract Get the end iterator
419 @param h Pointer to the hash table [khash_t(name)*]
420 @return The end iterator [khint_t]
422 #define kh_end(h) ((h)->n_buckets)
425 @abstract Get the number of elements in the hash table
426 @param h Pointer to the hash table [khash_t(name)*]
427 @return Number of elements in the hash table [khint_t]
429 #define kh_size(h) ((h)->size)
432 @abstract Get the number of buckets in the hash table
433 @param h Pointer to the hash table [khash_t(name)*]
434 @return Number of buckets in the hash table [khint_t]
436 #define kh_n_buckets(h) ((h)->n_buckets)
438 /* More conenient interfaces */
441 @abstract Instantiate a hash set containing integer keys
442 @param name Name of the hash table [symbol]
444 #define KHASH_SET_INIT_INT(name) \
445 KHASH_INIT(name, uint32_t, char, 0, kh_int_hash_func, kh_int_hash_equal)
448 @abstract Instantiate a hash map containing integer keys
449 @param name Name of the hash table [symbol]
450 @param khval_t Type of values [type]
452 #define KHASH_MAP_INIT_INT(name, khval_t) \
453 KHASH_INIT(name, uint32_t, khval_t, 1, kh_int_hash_func, kh_int_hash_equal)
456 @abstract Instantiate a hash map containing 64-bit integer keys
457 @param name Name of the hash table [symbol]
459 #define KHASH_SET_INIT_INT64(name) \
460 KHASH_INIT(name, uint64_t, char, 0, kh_int64_hash_func, kh_int64_hash_equal)
463 @abstract Instantiate a hash map containing 64-bit integer keys
464 @param name Name of the hash table [symbol]
465 @param khval_t Type of values [type]
467 #define KHASH_MAP_INIT_INT64(name, khval_t) \
468 KHASH_INIT(name, uint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal)
470 typedef const char *kh_cstr_t;
472 @abstract Instantiate a hash map containing const char* keys
473 @param name Name of the hash table [symbol]
475 #define KHASH_SET_INIT_STR(name) \
476 KHASH_INIT(name, kh_cstr_t, char, 0, kh_str_hash_func, kh_str_hash_equal)
479 @abstract Instantiate a hash map containing const char* keys
480 @param name Name of the hash table [symbol]
481 @param khval_t Type of values [type]
483 #define KHASH_MAP_INIT_STR(name, khval_t) \
484 KHASH_INIT(name, kh_cstr_t, khval_t, 1, kh_str_hash_func, kh_str_hash_equal)
486 #endif /* __AC_KHASH_H */