Vicent Marti | fff4275 | 2013-12-21 09:00:01 -0500 | [diff] [blame] | 1 | /* The MIT License |
| 2 | |
| 3 | Copyright (c) 2008, 2009, 2011 by Attractive Chaos <attractor@live.co.uk> |
| 4 | |
| 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: |
| 12 | |
| 13 | The above copyright notice and this permission notice shall be |
| 14 | included in all copies or substantial portions of the Software. |
| 15 | |
| 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 |
| 23 | SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #ifndef __AC_KHASH_H |
| 27 | #define __AC_KHASH_H |
| 28 | |
| 29 | #define AC_VERSION_KHASH_H "0.2.8" |
| 30 | |
| 31 | typedef uint32_t khint32_t; |
| 32 | typedef uint64_t khint64_t; |
| 33 | |
| 34 | typedef khint32_t khint_t; |
| 35 | typedef khint_t khiter_t; |
| 36 | |
| 37 | #define __ac_isempty(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2) |
| 38 | #define __ac_isdel(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1) |
| 39 | #define __ac_iseither(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&3) |
| 40 | #define __ac_set_isdel_false(flag, i) (flag[i>>4]&=~(1ul<<((i&0xfU)<<1))) |
| 41 | #define __ac_set_isempty_false(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1))) |
| 42 | #define __ac_set_isboth_false(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1))) |
| 43 | #define __ac_set_isdel_true(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1)) |
| 44 | |
| 45 | #define __ac_fsize(m) ((m) < 16? 1 : (m)>>4) |
| 46 | |
| 47 | #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) |
| 48 | |
| 49 | static inline khint_t __ac_X31_hash_string(const char *s) |
| 50 | { |
| 51 | khint_t h = (khint_t)*s; |
| 52 | if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)*s; |
| 53 | return h; |
| 54 | } |
| 55 | |
| 56 | #define kh_str_hash_func(key) __ac_X31_hash_string(key) |
| 57 | #define kh_str_hash_equal(a, b) (strcmp(a, b) == 0) |
| 58 | |
| 59 | static const double __ac_HASH_UPPER = 0.77; |
| 60 | |
| 61 | #define __KHASH_TYPE(name, khkey_t, khval_t) \ |
| 62 | typedef struct { \ |
| 63 | khint_t n_buckets, size, n_occupied, upper_bound; \ |
| 64 | khint32_t *flags; \ |
| 65 | khkey_t *keys; \ |
| 66 | khval_t *vals; \ |
| 67 | } kh_##name##_t; |
| 68 | |
| 69 | #define __KHASH_PROTOTYPES(name, khkey_t, khval_t) \ |
| 70 | extern kh_##name##_t *kh_init_##name(void); \ |
| 71 | extern void kh_destroy_##name(kh_##name##_t *h); \ |
| 72 | extern void kh_clear_##name(kh_##name##_t *h); \ |
| 73 | extern khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); \ |
| 74 | extern int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \ |
| 75 | extern khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret); \ |
| 76 | extern void kh_del_##name(kh_##name##_t *h, khint_t x); |
| 77 | |
| 78 | #define __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \ |
| 79 | SCOPE kh_##name##_t *kh_init_##name(void) { \ |
| 80 | return (kh_##name##_t*)xcalloc(1, sizeof(kh_##name##_t)); \ |
| 81 | } \ |
| 82 | SCOPE void kh_destroy_##name(kh_##name##_t *h) \ |
| 83 | { \ |
| 84 | if (h) { \ |
| 85 | free((void *)h->keys); free(h->flags); \ |
| 86 | free((void *)h->vals); \ |
| 87 | free(h); \ |
| 88 | } \ |
| 89 | } \ |
| 90 | SCOPE void kh_clear_##name(kh_##name##_t *h) \ |
| 91 | { \ |
| 92 | if (h && h->flags) { \ |
| 93 | memset(h->flags, 0xaa, __ac_fsize(h->n_buckets) * sizeof(khint32_t)); \ |
| 94 | h->size = h->n_occupied = 0; \ |
| 95 | } \ |
| 96 | } \ |
| 97 | SCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \ |
| 98 | { \ |
| 99 | if (h->n_buckets) { \ |
| 100 | khint_t k, i, last, mask, step = 0; \ |
| 101 | mask = h->n_buckets - 1; \ |
| 102 | k = __hash_func(key); i = k & mask; \ |
| 103 | last = i; \ |
| 104 | while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \ |
| 105 | i = (i + (++step)) & mask; \ |
| 106 | if (i == last) return h->n_buckets; \ |
| 107 | } \ |
| 108 | return __ac_iseither(h->flags, i)? h->n_buckets : i; \ |
| 109 | } else return 0; \ |
| 110 | } \ |
| 111 | SCOPE int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \ |
| 112 | { /* This function uses 0.25*n_buckets bytes of working space instead of [sizeof(key_t+val_t)+.25]*n_buckets. */ \ |
| 113 | khint32_t *new_flags = NULL; \ |
| 114 | khint_t j = 1; \ |
| 115 | { \ |
| 116 | kroundup32(new_n_buckets); \ |
| 117 | if (new_n_buckets < 4) new_n_buckets = 4; \ |
| 118 | if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; /* requested size is too small */ \ |
| 119 | else { /* hash table size to be changed (shrink or expand); rehash */ \ |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 120 | ALLOC_ARRAY(new_flags, __ac_fsize(new_n_buckets)); \ |
Vicent Marti | fff4275 | 2013-12-21 09:00:01 -0500 | [diff] [blame] | 121 | if (!new_flags) return -1; \ |
| 122 | memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \ |
| 123 | if (h->n_buckets < new_n_buckets) { /* expand */ \ |
René Scharfe | 2756ca4 | 2014-09-16 20:56:57 +0200 | [diff] [blame] | 124 | REALLOC_ARRAY(h->keys, new_n_buckets); \ |
Vicent Marti | fff4275 | 2013-12-21 09:00:01 -0500 | [diff] [blame] | 125 | if (kh_is_map) { \ |
René Scharfe | 2756ca4 | 2014-09-16 20:56:57 +0200 | [diff] [blame] | 126 | REALLOC_ARRAY(h->vals, new_n_buckets); \ |
Vicent Marti | fff4275 | 2013-12-21 09:00:01 -0500 | [diff] [blame] | 127 | } \ |
| 128 | } /* otherwise shrink */ \ |
| 129 | } \ |
| 130 | } \ |
| 131 | if (j) { /* rehashing is needed */ \ |
| 132 | for (j = 0; j != h->n_buckets; ++j) { \ |
| 133 | if (__ac_iseither(h->flags, j) == 0) { \ |
| 134 | khkey_t key = h->keys[j]; \ |
| 135 | khval_t val; \ |
| 136 | khint_t new_mask; \ |
| 137 | new_mask = new_n_buckets - 1; \ |
| 138 | if (kh_is_map) val = h->vals[j]; \ |
| 139 | __ac_set_isdel_true(h->flags, j); \ |
| 140 | while (1) { /* kick-out process; sort of like in Cuckoo hashing */ \ |
| 141 | khint_t k, i, step = 0; \ |
| 142 | k = __hash_func(key); \ |
| 143 | i = k & new_mask; \ |
| 144 | while (!__ac_isempty(new_flags, i)) i = (i + (++step)) & new_mask; \ |
| 145 | __ac_set_isempty_false(new_flags, i); \ |
| 146 | if (i < h->n_buckets && __ac_iseither(h->flags, i) == 0) { /* kick out the existing element */ \ |
| 147 | { khkey_t tmp = h->keys[i]; h->keys[i] = key; key = tmp; } \ |
| 148 | if (kh_is_map) { khval_t tmp = h->vals[i]; h->vals[i] = val; val = tmp; } \ |
| 149 | __ac_set_isdel_true(h->flags, i); /* mark it as deleted in the old hash table */ \ |
| 150 | } else { /* write the element and jump out of the loop */ \ |
| 151 | h->keys[i] = key; \ |
| 152 | if (kh_is_map) h->vals[i] = val; \ |
| 153 | break; \ |
| 154 | } \ |
| 155 | } \ |
| 156 | } \ |
| 157 | } \ |
| 158 | if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \ |
René Scharfe | 2756ca4 | 2014-09-16 20:56:57 +0200 | [diff] [blame] | 159 | REALLOC_ARRAY(h->keys, new_n_buckets); \ |
| 160 | if (kh_is_map) REALLOC_ARRAY(h->vals, new_n_buckets); \ |
Vicent Marti | fff4275 | 2013-12-21 09:00:01 -0500 | [diff] [blame] | 161 | } \ |
| 162 | free(h->flags); /* free the working space */ \ |
| 163 | h->flags = new_flags; \ |
| 164 | h->n_buckets = new_n_buckets; \ |
| 165 | h->n_occupied = h->size; \ |
| 166 | h->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \ |
| 167 | } \ |
| 168 | return 0; \ |
| 169 | } \ |
| 170 | SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \ |
| 171 | { \ |
| 172 | khint_t x; \ |
| 173 | if (h->n_occupied >= h->upper_bound) { /* update the hash table */ \ |
| 174 | if (h->n_buckets > (h->size<<1)) { \ |
| 175 | if (kh_resize_##name(h, h->n_buckets - 1) < 0) { /* clear "deleted" elements */ \ |
| 176 | *ret = -1; return h->n_buckets; \ |
| 177 | } \ |
| 178 | } else if (kh_resize_##name(h, h->n_buckets + 1) < 0) { /* expand the hash table */ \ |
| 179 | *ret = -1; return h->n_buckets; \ |
| 180 | } \ |
| 181 | } /* TODO: to implement automatically shrinking; resize() already support shrinking */ \ |
| 182 | { \ |
| 183 | khint_t k, i, site, last, mask = h->n_buckets - 1, step = 0; \ |
| 184 | x = site = h->n_buckets; k = __hash_func(key); i = k & mask; \ |
| 185 | if (__ac_isempty(h->flags, i)) x = i; /* for speed up */ \ |
| 186 | else { \ |
| 187 | last = i; \ |
| 188 | while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \ |
| 189 | if (__ac_isdel(h->flags, i)) site = i; \ |
| 190 | i = (i + (++step)) & mask; \ |
| 191 | if (i == last) { x = site; break; } \ |
| 192 | } \ |
| 193 | if (x == h->n_buckets) { \ |
| 194 | if (__ac_isempty(h->flags, i) && site != h->n_buckets) x = site; \ |
| 195 | else x = i; \ |
| 196 | } \ |
| 197 | } \ |
| 198 | } \ |
| 199 | if (__ac_isempty(h->flags, x)) { /* not present at all */ \ |
| 200 | h->keys[x] = key; \ |
| 201 | __ac_set_isboth_false(h->flags, x); \ |
| 202 | ++h->size; ++h->n_occupied; \ |
| 203 | *ret = 1; \ |
| 204 | } else if (__ac_isdel(h->flags, x)) { /* deleted */ \ |
| 205 | h->keys[x] = key; \ |
| 206 | __ac_set_isboth_false(h->flags, x); \ |
| 207 | ++h->size; \ |
| 208 | *ret = 2; \ |
| 209 | } else *ret = 0; /* Don't touch h->keys[x] if present and not deleted */ \ |
| 210 | return x; \ |
| 211 | } \ |
| 212 | SCOPE void kh_del_##name(kh_##name##_t *h, khint_t x) \ |
| 213 | { \ |
| 214 | if (x != h->n_buckets && !__ac_iseither(h->flags, x)) { \ |
| 215 | __ac_set_isdel_true(h->flags, x); \ |
| 216 | --h->size; \ |
| 217 | } \ |
| 218 | } |
| 219 | |
| 220 | #define KHASH_DECLARE(name, khkey_t, khval_t) \ |
| 221 | __KHASH_TYPE(name, khkey_t, khval_t) \ |
| 222 | __KHASH_PROTOTYPES(name, khkey_t, khval_t) |
| 223 | |
| 224 | #define KHASH_INIT2(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \ |
| 225 | __KHASH_TYPE(name, khkey_t, khval_t) \ |
| 226 | __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) |
| 227 | |
| 228 | #define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \ |
| 229 | KHASH_INIT2(name, static inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) |
| 230 | |
| 231 | /* Other convenient macros... */ |
| 232 | |
| 233 | /*! @function |
| 234 | @abstract Test whether a bucket contains data. |
| 235 | @param h Pointer to the hash table [khash_t(name)*] |
| 236 | @param x Iterator to the bucket [khint_t] |
| 237 | @return 1 if containing data; 0 otherwise [int] |
| 238 | */ |
| 239 | #define kh_exist(h, x) (!__ac_iseither((h)->flags, (x))) |
| 240 | |
| 241 | /*! @function |
| 242 | @abstract Get key given an iterator |
| 243 | @param h Pointer to the hash table [khash_t(name)*] |
| 244 | @param x Iterator to the bucket [khint_t] |
| 245 | @return Key [type of keys] |
| 246 | */ |
| 247 | #define kh_key(h, x) ((h)->keys[x]) |
| 248 | |
| 249 | /*! @function |
| 250 | @abstract Get value given an iterator |
| 251 | @param h Pointer to the hash table [khash_t(name)*] |
| 252 | @param x Iterator to the bucket [khint_t] |
| 253 | @return Value [type of values] |
| 254 | @discussion For hash sets, calling this results in segfault. |
| 255 | */ |
| 256 | #define kh_val(h, x) ((h)->vals[x]) |
| 257 | |
| 258 | /*! @function |
| 259 | @abstract Alias of kh_val() |
| 260 | */ |
| 261 | #define kh_value(h, x) ((h)->vals[x]) |
| 262 | |
| 263 | /*! @function |
| 264 | @abstract Get the start iterator |
| 265 | @param h Pointer to the hash table [khash_t(name)*] |
| 266 | @return The start iterator [khint_t] |
| 267 | */ |
| 268 | #define kh_begin(h) (khint_t)(0) |
| 269 | |
| 270 | /*! @function |
| 271 | @abstract Get the end iterator |
| 272 | @param h Pointer to the hash table [khash_t(name)*] |
| 273 | @return The end iterator [khint_t] |
| 274 | */ |
| 275 | #define kh_end(h) ((h)->n_buckets) |
| 276 | |
| 277 | /*! @function |
| 278 | @abstract Get the number of elements in the hash table |
| 279 | @param h Pointer to the hash table [khash_t(name)*] |
| 280 | @return Number of elements in the hash table [khint_t] |
| 281 | */ |
| 282 | #define kh_size(h) ((h)->size) |
| 283 | |
| 284 | /*! @function |
| 285 | @abstract Get the number of buckets in the hash table |
| 286 | @param h Pointer to the hash table [khash_t(name)*] |
| 287 | @return Number of buckets in the hash table [khint_t] |
| 288 | */ |
| 289 | #define kh_n_buckets(h) ((h)->n_buckets) |
| 290 | |
| 291 | /*! @function |
| 292 | @abstract Iterate over the entries in the hash table |
| 293 | @param h Pointer to the hash table [khash_t(name)*] |
| 294 | @param kvar Variable to which key will be assigned |
| 295 | @param vvar Variable to which value will be assigned |
| 296 | @param code Block of code to execute |
| 297 | */ |
| 298 | #define kh_foreach(h, kvar, vvar, code) { khint_t __i; \ |
| 299 | for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \ |
| 300 | if (!kh_exist(h,__i)) continue; \ |
| 301 | (kvar) = kh_key(h,__i); \ |
| 302 | (vvar) = kh_val(h,__i); \ |
| 303 | code; \ |
| 304 | } } |
| 305 | |
| 306 | /*! @function |
| 307 | @abstract Iterate over the values in the hash table |
| 308 | @param h Pointer to the hash table [khash_t(name)*] |
| 309 | @param vvar Variable to which value will be assigned |
| 310 | @param code Block of code to execute |
| 311 | */ |
| 312 | #define kh_foreach_value(h, vvar, code) { khint_t __i; \ |
| 313 | for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \ |
| 314 | if (!kh_exist(h,__i)) continue; \ |
| 315 | (vvar) = kh_val(h,__i); \ |
| 316 | code; \ |
| 317 | } } |
| 318 | |
Vicent Marti | fff4275 | 2013-12-21 09:00:01 -0500 | [diff] [blame] | 319 | #define __kh_oid_cmp(a, b) (hashcmp(a, b) == 0) |
| 320 | |
Karsten Blees | 039dc71 | 2014-07-03 00:20:20 +0200 | [diff] [blame] | 321 | KHASH_INIT(sha1, const unsigned char *, void *, 1, sha1hash, __kh_oid_cmp) |
Vicent Marti | fff4275 | 2013-12-21 09:00:01 -0500 | [diff] [blame] | 322 | typedef kh_sha1_t khash_sha1; |
| 323 | |
Karsten Blees | 039dc71 | 2014-07-03 00:20:20 +0200 | [diff] [blame] | 324 | KHASH_INIT(sha1_pos, const unsigned char *, int, 1, sha1hash, __kh_oid_cmp) |
Vicent Marti | fff4275 | 2013-12-21 09:00:01 -0500 | [diff] [blame] | 325 | typedef kh_sha1_pos_t khash_sha1_pos; |
| 326 | |
| 327 | #endif /* __AC_KHASH_H */ |