00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 #ifndef __AC_KHASH_H
00087 #define __AC_KHASH_H
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 #define AC_VERSION_KHASH_H "0.2.5"
00098
00099 #include <stdlib.h>
00100 #include <string.h>
00101 #include <limits.h>
00102
00103
00104
00105 #if UINT_MAX == 0xffffffffu
00106 typedef unsigned int khint32_t;
00107 #elif ULONG_MAX == 0xffffffffu
00108 typedef unsigned long khint32_t;
00109 #endif
00110
00111 #if ULONG_MAX == ULLONG_MAX
00112 typedef unsigned long khint64_t;
00113 #else
00114 typedef unsigned long long khint64_t;
00115 #endif
00116
00117 #ifdef _MSC_VER
00118 #define inline __inline
00119 #endif
00120
00121 typedef khint32_t khint_t;
00122 typedef khint_t khiter_t;
00123
00124 #define __ac_HASH_PRIME_SIZE 32
00125 static const khint32_t __ac_prime_list[__ac_HASH_PRIME_SIZE] =
00126 {
00127 0ul, 3ul, 11ul, 23ul, 53ul,
00128 97ul, 193ul, 389ul, 769ul, 1543ul,
00129 3079ul, 6151ul, 12289ul, 24593ul, 49157ul,
00130 98317ul, 196613ul, 393241ul, 786433ul, 1572869ul,
00131 3145739ul, 6291469ul, 12582917ul, 25165843ul, 50331653ul,
00132 100663319ul, 201326611ul, 402653189ul, 805306457ul, 1610612741ul,
00133 3221225473ul, 4294967291ul
00134 };
00135
00136 #define __ac_isempty(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2)
00137 #define __ac_isdel(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1)
00138 #define __ac_iseither(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&3)
00139 #define __ac_set_isdel_false(flag, i) (flag[i>>4]&=~(1ul<<((i&0xfU)<<1)))
00140 #define __ac_set_isempty_false(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1)))
00141 #define __ac_set_isboth_false(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1)))
00142 #define __ac_set_isdel_true(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1))
00143
00144 static const double __ac_HASH_UPPER = 0.77;
00145
00146 #define KHASH_DECLARE(name, khkey_t, khval_t) \
00147 typedef struct { \
00148 khint_t n_buckets, size, n_occupied, upper_bound; \
00149 khint32_t *flags; \
00150 khkey_t *keys; \
00151 khval_t *vals; \
00152 } kh_##name##_t; \
00153 extern kh_##name##_t *kh_init_##name(); \
00154 extern void kh_destroy_##name(kh_##name##_t *h); \
00155 extern void kh_clear_##name(kh_##name##_t *h); \
00156 extern khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); \
00157 extern void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \
00158 extern khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret); \
00159 extern void kh_del_##name(kh_##name##_t *h, khint_t x);
00160
00161 #define KHASH_INIT2(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
00162 typedef struct { \
00163 khint_t n_buckets, size, n_occupied, upper_bound; \
00164 khint32_t *flags; \
00165 khkey_t *keys; \
00166 khval_t *vals; \
00167 } kh_##name##_t; \
00168 SCOPE kh_##name##_t *kh_init_##name() { \
00169 return (kh_##name##_t*)calloc(1, sizeof(kh_##name##_t)); \
00170 } \
00171 SCOPE void kh_destroy_##name(kh_##name##_t *h) \
00172 { \
00173 if (h) { \
00174 free(h->keys); free(h->flags); \
00175 free(h->vals); \
00176 free(h); \
00177 } \
00178 } \
00179 SCOPE void kh_clear_##name(kh_##name##_t *h) \
00180 { \
00181 if (h && h->flags) { \
00182 memset(h->flags, 0xaa, ((h->n_buckets>>4) + 1) * sizeof(khint32_t)); \
00183 h->size = h->n_occupied = 0; \
00184 } \
00185 } \
00186 SCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \
00187 { \
00188 if (h->n_buckets) { \
00189 khint_t inc, k, i, last; \
00190 k = __hash_func(key); i = k % h->n_buckets; \
00191 inc = 1 + k % (h->n_buckets - 1); last = i; \
00192 while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
00193 if (i + inc >= h->n_buckets) i = i + inc - h->n_buckets; \
00194 else i += inc; \
00195 if (i == last) return h->n_buckets; \
00196 } \
00197 return __ac_iseither(h->flags, i)? h->n_buckets : i; \
00198 } else return 0; \
00199 } \
00200 SCOPE void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
00201 { \
00202 khint32_t *new_flags = 0; \
00203 khint_t j = 1; \
00204 { \
00205 khint_t t = __ac_HASH_PRIME_SIZE - 1; \
00206 while (__ac_prime_list[t] > new_n_buckets) --t; \
00207 new_n_buckets = __ac_prime_list[t+1]; \
00208 if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; \
00209 else { \
00210 new_flags = (khint32_t*)malloc(((new_n_buckets>>4) + 1) * sizeof(khint32_t)); \
00211 memset(new_flags, 0xaa, ((new_n_buckets>>4) + 1) * sizeof(khint32_t)); \
00212 if (h->n_buckets < new_n_buckets) { \
00213 h->keys = (khkey_t*)realloc(h->keys, new_n_buckets * sizeof(khkey_t)); \
00214 if (kh_is_map) \
00215 h->vals = (khval_t*)realloc(h->vals, new_n_buckets * sizeof(khval_t)); \
00216 } \
00217 } \
00218 } \
00219 if (j) { \
00220 for (j = 0; j != h->n_buckets; ++j) { \
00221 if (__ac_iseither(h->flags, j) == 0) { \
00222 khkey_t key = h->keys[j]; \
00223 khval_t val; \
00224 if (kh_is_map) val = h->vals[j]; \
00225 __ac_set_isdel_true(h->flags, j); \
00226 while (1) { \
00227 khint_t inc, k, i; \
00228 k = __hash_func(key); \
00229 i = k % new_n_buckets; \
00230 inc = 1 + k % (new_n_buckets - 1); \
00231 while (!__ac_isempty(new_flags, i)) { \
00232 if (i + inc >= new_n_buckets) i = i + inc - new_n_buckets; \
00233 else i += inc; \
00234 } \
00235 __ac_set_isempty_false(new_flags, i); \
00236 if (i < h->n_buckets && __ac_iseither(h->flags, i) == 0) { \
00237 { khkey_t tmp = h->keys[i]; h->keys[i] = key; key = tmp; } \
00238 if (kh_is_map) { khval_t tmp = h->vals[i]; h->vals[i] = val; val = tmp; } \
00239 __ac_set_isdel_true(h->flags, i); \
00240 } else { \
00241 h->keys[i] = key; \
00242 if (kh_is_map) h->vals[i] = val; \
00243 break; \
00244 } \
00245 } \
00246 } \
00247 } \
00248 if (h->n_buckets > new_n_buckets) { \
00249 h->keys = (khkey_t*)realloc(h->keys, new_n_buckets * sizeof(khkey_t)); \
00250 if (kh_is_map) \
00251 h->vals = (khval_t*)realloc(h->vals, new_n_buckets * sizeof(khval_t)); \
00252 } \
00253 free(h->flags); \
00254 h->flags = new_flags; \
00255 h->n_buckets = new_n_buckets; \
00256 h->n_occupied = h->size; \
00257 h->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \
00258 } \
00259 } \
00260 SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \
00261 { \
00262 khint_t x; \
00263 if (h->n_occupied >= h->upper_bound) { \
00264 if (h->n_buckets > (h->size<<1)) kh_resize_##name(h, h->n_buckets - 1); \
00265 else kh_resize_##name(h, h->n_buckets + 1); \
00266 } \
00267 { \
00268 khint_t inc, k, i, site, last; \
00269 x = site = h->n_buckets; k = __hash_func(key); i = k % h->n_buckets; \
00270 if (__ac_isempty(h->flags, i)) x = i; \
00271 else { \
00272 inc = 1 + k % (h->n_buckets - 1); last = i; \
00273 while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
00274 if (__ac_isdel(h->flags, i)) site = i; \
00275 if (i + inc >= h->n_buckets) i = i + inc - h->n_buckets; \
00276 else i += inc; \
00277 if (i == last) { x = site; break; } \
00278 } \
00279 if (x == h->n_buckets) { \
00280 if (__ac_isempty(h->flags, i) && site != h->n_buckets) x = site; \
00281 else x = i; \
00282 } \
00283 } \
00284 } \
00285 if (__ac_isempty(h->flags, x)) { \
00286 h->keys[x] = key; \
00287 __ac_set_isboth_false(h->flags, x); \
00288 ++h->size; ++h->n_occupied; \
00289 *ret = 1; \
00290 } else if (__ac_isdel(h->flags, x)) { \
00291 h->keys[x] = key; \
00292 __ac_set_isboth_false(h->flags, x); \
00293 ++h->size; \
00294 *ret = 2; \
00295 } else *ret = 0; \
00296 return x; \
00297 } \
00298 SCOPE void kh_del_##name(kh_##name##_t *h, khint_t x) \
00299 { \
00300 if (x != h->n_buckets && !__ac_iseither(h->flags, x)) { \
00301 __ac_set_isdel_true(h->flags, x); \
00302 --h->size; \
00303 } \
00304 }
00305
00306 #define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
00307 KHASH_INIT2(name, static inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
00308
00309
00310
00311
00312
00313
00314
00315
00316 #define kh_int_hash_func(key) (khint32_t)(key)
00317
00318
00319
00320 #define kh_int_hash_equal(a, b) ((a) == (b))
00321
00322
00323
00324
00325
00326 #define kh_int64_hash_func(key) (khint32_t)((key)>>33^(key)^(key)<<11)
00327
00328
00329
00330 #define kh_int64_hash_equal(a, b) ((a) == (b))
00331
00332
00333
00334
00335
00336 static inline khint_t __ac_X31_hash_string(const char *s)
00337 {
00338 khint_t h = *s;
00339 if (h) for (++s ; *s; ++s) h = (h << 5) - h + *s;
00340 return h;
00341 }
00342
00343
00344
00345
00346
00347 #define kh_str_hash_func(key) __ac_X31_hash_string(key)
00348
00349
00350
00351 #define kh_str_hash_equal(a, b) (strcmp(a, b) == 0)
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361 #define khash_t(name) kh_##name##_t
00362
00363
00364
00365
00366
00367
00368 #define kh_init(name) kh_init_##name()
00369
00370
00371
00372
00373
00374
00375 #define kh_destroy(name, h) kh_destroy_##name(h)
00376
00377
00378
00379
00380
00381
00382 #define kh_clear(name, h) kh_clear_##name(h)
00383
00384
00385
00386
00387
00388
00389
00390 #define kh_resize(name, h, s) kh_resize_##name(h, s)
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402 #define kh_put(name, h, k, r) kh_put_##name(h, k, r)
00403
00404
00405
00406
00407
00408
00409
00410
00411 #define kh_get(name, h, k) kh_get_##name(h, k)
00412
00413
00414
00415
00416
00417
00418
00419 #define kh_del(name, h, k) kh_del_##name(h, k)
00420
00421
00422
00423
00424
00425
00426
00427
00428 #define kh_exist(h, x) (!__ac_iseither((h)->flags, (x)))
00429
00430
00431
00432
00433
00434
00435
00436 #define kh_key(h, x) ((h)->keys[x])
00437
00438
00439
00440
00441
00442
00443
00444
00445 #define kh_val(h, x) ((h)->vals[x])
00446
00447
00448
00449
00450 #define kh_value(h, x) ((h)->vals[x])
00451
00452
00453
00454
00455
00456
00457 #define kh_begin(h) (khint_t)(0)
00458
00459
00460
00461
00462
00463
00464 #define kh_end(h) ((h)->n_buckets)
00465
00466
00467
00468
00469
00470
00471 #define kh_size(h) ((h)->size)
00472
00473
00474
00475
00476
00477
00478 #define kh_n_buckets(h) ((h)->n_buckets)
00479
00480
00481
00482
00483
00484
00485
00486 #define KHASH_SET_INIT_INT(name) \
00487 KHASH_INIT(name, khint32_t, char, 0, kh_int_hash_func, kh_int_hash_equal)
00488
00489
00490
00491
00492
00493
00494 #define KHASH_MAP_INIT_INT(name, khval_t) \
00495 KHASH_INIT(name, khint32_t, khval_t, 1, kh_int_hash_func, kh_int_hash_equal)
00496
00497
00498
00499
00500
00501 #define KHASH_SET_INIT_INT64(name) \
00502 KHASH_INIT(name, khint64_t, char, 0, kh_int64_hash_func, kh_int64_hash_equal)
00503
00504
00505
00506
00507
00508
00509 #define KHASH_MAP_INIT_INT64(name, khval_t) \
00510 KHASH_INIT(name, khint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal)
00511
00512 typedef const char *kh_cstr_t;
00513
00514
00515
00516
00517 #define KHASH_SET_INIT_STR(name) \
00518 KHASH_INIT(name, kh_cstr_t, char, 0, kh_str_hash_func, kh_str_hash_equal)
00519
00520
00521
00522
00523
00524
00525 #define KHASH_MAP_INIT_STR(name, khval_t) \
00526 KHASH_INIT(name, kh_cstr_t, khval_t, 1, kh_str_hash_func, kh_str_hash_equal)
00527
00528 #endif