blob: ff8816317785862662b1eeb2fe5036b801be1773 [file] [log] [blame]
Vicent Martifff42752013-12-21 09:00:01 -05001/* 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
Elijah Newrend1cbe1e2023-04-22 20:17:20 +000029#include "hash.h"
Elijah Newrenef3ca952018-08-15 10:54:05 -070030
Vicent Martifff42752013-12-21 09:00:01 -050031#define AC_VERSION_KHASH_H "0.2.8"
32
33typedef uint32_t khint32_t;
34typedef uint64_t khint64_t;
35
36typedef khint32_t khint_t;
37typedef khint_t khiter_t;
38
39#define __ac_isempty(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2)
40#define __ac_isdel(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1)
41#define __ac_iseither(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&3)
42#define __ac_set_isdel_false(flag, i) (flag[i>>4]&=~(1ul<<((i&0xfU)<<1)))
43#define __ac_set_isempty_false(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1)))
44#define __ac_set_isboth_false(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1)))
45#define __ac_set_isdel_true(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1))
46
47#define __ac_fsize(m) ((m) < 16? 1 : (m)>>4)
48
49#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
50
51static inline khint_t __ac_X31_hash_string(const char *s)
52{
53 khint_t h = (khint_t)*s;
54 if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)*s;
55 return h;
56}
57
58#define kh_str_hash_func(key) __ac_X31_hash_string(key)
59#define kh_str_hash_equal(a, b) (strcmp(a, b) == 0)
60
61static const double __ac_HASH_UPPER = 0.77;
62
63#define __KHASH_TYPE(name, khkey_t, khval_t) \
Elijah Newren80434182023-05-16 06:34:05 +000064 typedef struct kh_##name { \
Vicent Martifff42752013-12-21 09:00:01 -050065 khint_t n_buckets, size, n_occupied, upper_bound; \
66 khint32_t *flags; \
67 khkey_t *keys; \
68 khval_t *vals; \
69 } kh_##name##_t;
70
Denton Liuad6dad02019-04-29 04:28:23 -040071#define __KHASH_PROTOTYPES(name, khkey_t, khval_t) \
72 kh_##name##_t *kh_init_##name(void); \
Denton Liub199d712019-04-29 04:28:20 -040073 void kh_destroy_##name(kh_##name##_t *h); \
74 void kh_clear_##name(kh_##name##_t *h); \
Denton Liuad6dad02019-04-29 04:28:23 -040075 khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); \
René Scharfe5632e832021-07-03 14:57:30 +020076 void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \
Denton Liub199d712019-04-29 04:28:20 -040077 khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret); \
78 void kh_del_##name(kh_##name##_t *h, khint_t x);
Vicent Martifff42752013-12-21 09:00:01 -050079
80#define __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
81 SCOPE kh_##name##_t *kh_init_##name(void) { \
82 return (kh_##name##_t*)xcalloc(1, sizeof(kh_##name##_t)); \
83 } \
René Scharfe9249ca22018-10-04 17:10:54 +020084 SCOPE void kh_release_##name(kh_##name##_t *h) \
85 { \
86 free(h->flags); \
87 free((void *)h->keys); \
88 free((void *)h->vals); \
89 } \
Vicent Martifff42752013-12-21 09:00:01 -050090 SCOPE void kh_destroy_##name(kh_##name##_t *h) \
91 { \
92 if (h) { \
René Scharfe9249ca22018-10-04 17:10:54 +020093 kh_release_##name(h); \
Vicent Martifff42752013-12-21 09:00:01 -050094 free(h); \
95 } \
96 } \
97 SCOPE void kh_clear_##name(kh_##name##_t *h) \
98 { \
99 if (h && h->flags) { \
100 memset(h->flags, 0xaa, __ac_fsize(h->n_buckets) * sizeof(khint32_t)); \
101 h->size = h->n_occupied = 0; \
102 } \
103 } \
104 SCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \
105 { \
106 if (h->n_buckets) { \
107 khint_t k, i, last, mask, step = 0; \
108 mask = h->n_buckets - 1; \
109 k = __hash_func(key); i = k & mask; \
110 last = i; \
111 while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
112 i = (i + (++step)) & mask; \
113 if (i == last) return h->n_buckets; \
114 } \
115 return __ac_iseither(h->flags, i)? h->n_buckets : i; \
116 } else return 0; \
117 } \
René Scharfe5632e832021-07-03 14:57:30 +0200118 SCOPE void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
Vicent Martifff42752013-12-21 09:00:01 -0500119 { /* This function uses 0.25*n_buckets bytes of working space instead of [sizeof(key_t+val_t)+.25]*n_buckets. */ \
120 khint32_t *new_flags = NULL; \
121 khint_t j = 1; \
122 { \
123 kroundup32(new_n_buckets); \
124 if (new_n_buckets < 4) new_n_buckets = 4; \
125 if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; /* requested size is too small */ \
126 else { /* hash table size to be changed (shrink or expand); rehash */ \
Jeff Kingb32fa952016-02-22 17:44:25 -0500127 ALLOC_ARRAY(new_flags, __ac_fsize(new_n_buckets)); \
Vicent Martifff42752013-12-21 09:00:01 -0500128 memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
129 if (h->n_buckets < new_n_buckets) { /* expand */ \
René Scharfe2756ca42014-09-16 20:56:57 +0200130 REALLOC_ARRAY(h->keys, new_n_buckets); \
Vicent Martifff42752013-12-21 09:00:01 -0500131 if (kh_is_map) { \
René Scharfe2756ca42014-09-16 20:56:57 +0200132 REALLOC_ARRAY(h->vals, new_n_buckets); \
Vicent Martifff42752013-12-21 09:00:01 -0500133 } \
134 } /* otherwise shrink */ \
135 } \
136 } \
137 if (j) { /* rehashing is needed */ \
138 for (j = 0; j != h->n_buckets; ++j) { \
139 if (__ac_iseither(h->flags, j) == 0) { \
140 khkey_t key = h->keys[j]; \
141 khval_t val; \
142 khint_t new_mask; \
143 new_mask = new_n_buckets - 1; \
144 if (kh_is_map) val = h->vals[j]; \
145 __ac_set_isdel_true(h->flags, j); \
146 while (1) { /* kick-out process; sort of like in Cuckoo hashing */ \
147 khint_t k, i, step = 0; \
148 k = __hash_func(key); \
149 i = k & new_mask; \
150 while (!__ac_isempty(new_flags, i)) i = (i + (++step)) & new_mask; \
151 __ac_set_isempty_false(new_flags, i); \
152 if (i < h->n_buckets && __ac_iseither(h->flags, i) == 0) { /* kick out the existing element */ \
153 { khkey_t tmp = h->keys[i]; h->keys[i] = key; key = tmp; } \
154 if (kh_is_map) { khval_t tmp = h->vals[i]; h->vals[i] = val; val = tmp; } \
155 __ac_set_isdel_true(h->flags, i); /* mark it as deleted in the old hash table */ \
156 } else { /* write the element and jump out of the loop */ \
157 h->keys[i] = key; \
158 if (kh_is_map) h->vals[i] = val; \
159 break; \
160 } \
161 } \
162 } \
163 } \
164 if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \
René Scharfe2756ca42014-09-16 20:56:57 +0200165 REALLOC_ARRAY(h->keys, new_n_buckets); \
166 if (kh_is_map) REALLOC_ARRAY(h->vals, new_n_buckets); \
Vicent Martifff42752013-12-21 09:00:01 -0500167 } \
168 free(h->flags); /* free the working space */ \
169 h->flags = new_flags; \
170 h->n_buckets = new_n_buckets; \
171 h->n_occupied = h->size; \
172 h->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \
173 } \
Vicent Martifff42752013-12-21 09:00:01 -0500174 } \
175 SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \
176 { \
177 khint_t x; \
178 if (h->n_occupied >= h->upper_bound) { /* update the hash table */ \
179 if (h->n_buckets > (h->size<<1)) { \
René Scharfe5632e832021-07-03 14:57:30 +0200180 kh_resize_##name(h, h->n_buckets - 1); /* clear "deleted" elements */ \
181 } else { \
182 kh_resize_##name(h, h->n_buckets + 1); /* expand the hash table */ \
Vicent Martifff42752013-12-21 09:00:01 -0500183 } \
184 } /* TODO: to implement automatically shrinking; resize() already support shrinking */ \
185 { \
186 khint_t k, i, site, last, mask = h->n_buckets - 1, step = 0; \
187 x = site = h->n_buckets; k = __hash_func(key); i = k & mask; \
188 if (__ac_isempty(h->flags, i)) x = i; /* for speed up */ \
189 else { \
190 last = i; \
191 while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
192 if (__ac_isdel(h->flags, i)) site = i; \
193 i = (i + (++step)) & mask; \
194 if (i == last) { x = site; break; } \
195 } \
196 if (x == h->n_buckets) { \
197 if (__ac_isempty(h->flags, i) && site != h->n_buckets) x = site; \
198 else x = i; \
199 } \
200 } \
201 } \
202 if (__ac_isempty(h->flags, x)) { /* not present at all */ \
203 h->keys[x] = key; \
204 __ac_set_isboth_false(h->flags, x); \
205 ++h->size; ++h->n_occupied; \
206 *ret = 1; \
207 } else if (__ac_isdel(h->flags, x)) { /* deleted */ \
208 h->keys[x] = key; \
209 __ac_set_isboth_false(h->flags, x); \
210 ++h->size; \
211 *ret = 2; \
212 } else *ret = 0; /* Don't touch h->keys[x] if present and not deleted */ \
213 return x; \
214 } \
215 SCOPE void kh_del_##name(kh_##name##_t *h, khint_t x) \
216 { \
217 if (x != h->n_buckets && !__ac_iseither(h->flags, x)) { \
218 __ac_set_isdel_true(h->flags, x); \
219 --h->size; \
220 } \
221 }
222
223#define KHASH_DECLARE(name, khkey_t, khval_t) \
224 __KHASH_TYPE(name, khkey_t, khval_t) \
225 __KHASH_PROTOTYPES(name, khkey_t, khval_t)
226
227#define KHASH_INIT2(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
228 __KHASH_TYPE(name, khkey_t, khval_t) \
229 __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
230
231#define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
Carlo Marcelo Arenas Belónd99ea5f2018-10-23 14:50:20 -0700232 KHASH_INIT2(name, MAYBE_UNUSED static inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
Vicent Martifff42752013-12-21 09:00:01 -0500233
234/* Other convenient macros... */
235
236/*! @function
237 @abstract Test whether a bucket contains data.
238 @param h Pointer to the hash table [khash_t(name)*]
239 @param x Iterator to the bucket [khint_t]
240 @return 1 if containing data; 0 otherwise [int]
241 */
242#define kh_exist(h, x) (!__ac_iseither((h)->flags, (x)))
243
244/*! @function
245 @abstract Get key given an iterator
246 @param h Pointer to the hash table [khash_t(name)*]
247 @param x Iterator to the bucket [khint_t]
248 @return Key [type of keys]
249 */
250#define kh_key(h, x) ((h)->keys[x])
251
252/*! @function
253 @abstract Get value given an iterator
254 @param h Pointer to the hash table [khash_t(name)*]
255 @param x Iterator to the bucket [khint_t]
256 @return Value [type of values]
257 @discussion For hash sets, calling this results in segfault.
258 */
259#define kh_val(h, x) ((h)->vals[x])
260
261/*! @function
262 @abstract Alias of kh_val()
263 */
264#define kh_value(h, x) ((h)->vals[x])
265
266/*! @function
267 @abstract Get the start iterator
268 @param h Pointer to the hash table [khash_t(name)*]
269 @return The start iterator [khint_t]
270 */
271#define kh_begin(h) (khint_t)(0)
272
273/*! @function
274 @abstract Get the end iterator
275 @param h Pointer to the hash table [khash_t(name)*]
276 @return The end iterator [khint_t]
277 */
278#define kh_end(h) ((h)->n_buckets)
279
280/*! @function
281 @abstract Get the number of elements in the hash table
282 @param h Pointer to the hash table [khash_t(name)*]
283 @return Number of elements in the hash table [khint_t]
284 */
285#define kh_size(h) ((h)->size)
286
287/*! @function
288 @abstract Get the number of buckets in the hash table
289 @param h Pointer to the hash table [khash_t(name)*]
290 @return Number of buckets in the hash table [khint_t]
291 */
292#define kh_n_buckets(h) ((h)->n_buckets)
293
294/*! @function
295 @abstract Iterate over the entries in the hash table
296 @param h Pointer to the hash table [khash_t(name)*]
297 @param kvar Variable to which key will be assigned
298 @param vvar Variable to which value will be assigned
299 @param code Block of code to execute
300 */
301#define kh_foreach(h, kvar, vvar, code) { khint_t __i; \
302 for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
303 if (!kh_exist(h,__i)) continue; \
304 (kvar) = kh_key(h,__i); \
305 (vvar) = kh_val(h,__i); \
306 code; \
307 } }
308
309/*! @function
310 @abstract Iterate over the values in the hash table
311 @param h Pointer to the hash table [khash_t(name)*]
312 @param vvar Variable to which value will be assigned
313 @param code Block of code to execute
314 */
315#define kh_foreach_value(h, vvar, code) { khint_t __i; \
316 for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
317 if (!kh_exist(h,__i)) continue; \
318 (vvar) = kh_val(h,__i); \
319 code; \
320 } }
321
Jeff Kinge465e7c2019-06-20 03:41:42 -0400322static inline unsigned int oidhash_by_value(struct object_id oid)
brian m. carlson5a8643e2019-02-19 00:04:53 +0000323{
Jeff Kingd40abc82019-06-20 03:41:49 -0400324 return oidhash(&oid);
brian m. carlson5a8643e2019-02-19 00:04:53 +0000325}
326
Jeff Kinge465e7c2019-06-20 03:41:42 -0400327static inline int oideq_by_value(struct object_id a, struct object_id b)
brian m. carlson5a8643e2019-02-19 00:04:53 +0000328{
329 return oideq(&a, &b);
330}
331
Jeff Kinge465e7c2019-06-20 03:41:42 -0400332KHASH_INIT(oid_set, struct object_id, int, 0, oidhash_by_value, oideq_by_value)
brian m. carlson5a8643e2019-02-19 00:04:53 +0000333
Jeff Kinge465e7c2019-06-20 03:41:42 -0400334KHASH_INIT(oid_map, struct object_id, void *, 1, oidhash_by_value, oideq_by_value)
brian m. carlson5a8643e2019-02-19 00:04:53 +0000335
Jeff Kinge465e7c2019-06-20 03:41:42 -0400336KHASH_INIT(oid_pos, struct object_id, int, 1, oidhash_by_value, oideq_by_value)
brian m. carlson5a8643e2019-02-19 00:04:53 +0000337
Vicent Martifff42752013-12-21 09:00:01 -0500338#endif /* __AC_KHASH_H */