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