annotate PsiCLASS-1.0.2/samtools-0.1.19/khash.h @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 /* The MIT License
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 Copyright (c) 2008, 2009, 2011 by Attractive Chaos <attractor@live.co.uk>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 Permission is hereby granted, free of charge, to any person obtaining
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 a copy of this software and associated documentation files (the
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 "Software"), to deal in the Software without restriction, including
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 without limitation the rights to use, copy, modify, merge, publish,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 distribute, sublicense, and/or sell copies of the Software, and to
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 permit persons to whom the Software is furnished to do so, subject to
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 the following conditions:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 The above copyright notice and this permission notice shall be
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 included in all copies or substantial portions of the Software.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 SOFTWARE.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 /*
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 An example:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 #include "khash.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 KHASH_MAP_INIT_INT(32, char)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 int main() {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 int ret, is_missing;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 khiter_t k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 khash_t(32) *h = kh_init(32);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 k = kh_put(32, h, 5, &ret);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 if (!ret) kh_del(32, h, k);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 kh_value(h, k) = 10;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 k = kh_get(32, h, 10);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 is_missing = (k == kh_end(h));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 k = kh_get(32, h, 5);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 kh_del(32, h, k);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 for (k = kh_begin(h); k != kh_end(h); ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 if (kh_exist(h, k)) kh_value(h, k) = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 kh_destroy(32, h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 /*
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 2011-02-14 (0.2.5):
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 * Allow to declare global functions.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 2009-09-26 (0.2.4):
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 * Improve portability
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 2008-09-19 (0.2.3):
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 * Corrected the example
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 * Improved interfaces
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 2008-09-11 (0.2.2):
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 * Improved speed a little in kh_put()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 2008-09-10 (0.2.1):
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 * Added kh_clear()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 * Fixed a compiling error
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 2008-09-02 (0.2.0):
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 * Changed to token concatenation which increases flexibility.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 2008-08-31 (0.1.2):
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 * Fixed a bug in kh_get(), which has not been tested previously.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 2008-08-31 (0.1.1):
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 * Added destructor
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 #ifndef __AC_KHASH_H
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 #define __AC_KHASH_H
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 /*!
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 @header
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 Generic hash table library.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 @copyright Heng Li
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 #define AC_VERSION_KHASH_H "0.2.5"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 #include <stdlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 #include <string.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 #include <limits.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 /* compipler specific configuration */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 #if UINT_MAX == 0xffffffffu
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 typedef unsigned int khint32_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 #elif ULONG_MAX == 0xffffffffu
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 typedef unsigned long khint32_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 #if ULONG_MAX == ULLONG_MAX
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 typedef unsigned long khint64_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 typedef unsigned long long khint64_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 #ifdef _MSC_VER
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 #define inline __inline
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 typedef khint32_t khint_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 typedef khint_t khiter_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 #define __ac_HASH_PRIME_SIZE 32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 static const khint32_t __ac_prime_list[__ac_HASH_PRIME_SIZE] =
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 0ul, 3ul, 11ul, 23ul, 53ul,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 97ul, 193ul, 389ul, 769ul, 1543ul,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 3079ul, 6151ul, 12289ul, 24593ul, 49157ul,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 98317ul, 196613ul, 393241ul, 786433ul, 1572869ul,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 3145739ul, 6291469ul, 12582917ul, 25165843ul, 50331653ul,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 100663319ul, 201326611ul, 402653189ul, 805306457ul, 1610612741ul,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 3221225473ul, 4294967291ul
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 #define __ac_isempty(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 #define __ac_isdel(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 #define __ac_iseither(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&3)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 #define __ac_set_isdel_false(flag, i) (flag[i>>4]&=~(1ul<<((i&0xfU)<<1)))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 #define __ac_set_isempty_false(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1)))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 #define __ac_set_isboth_false(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1)))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 #define __ac_set_isdel_true(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 static const double __ac_HASH_UPPER = 0.77;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 #define KHASH_DECLARE(name, khkey_t, khval_t) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 typedef struct { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 khint_t n_buckets, size, n_occupied, upper_bound; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 khint32_t *flags; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 khkey_t *keys; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 khval_t *vals; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 } kh_##name##_t; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 extern kh_##name##_t *kh_init_##name(); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 extern void kh_destroy_##name(kh_##name##_t *h); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 extern void kh_clear_##name(kh_##name##_t *h); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 extern khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 extern void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 extern khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 extern void kh_del_##name(kh_##name##_t *h, khint_t x);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 #define KHASH_INIT2(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 typedef struct { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 khint_t n_buckets, size, n_occupied, upper_bound; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 khint32_t *flags; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 khkey_t *keys; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 khval_t *vals; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 } kh_##name##_t; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 SCOPE kh_##name##_t *kh_init_##name() { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 return (kh_##name##_t*)calloc(1, sizeof(kh_##name##_t)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 SCOPE void kh_destroy_##name(kh_##name##_t *h) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 if (h) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 free(h->keys); free(h->flags); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 free(h->vals); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 free(h); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 SCOPE void kh_clear_##name(kh_##name##_t *h) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 if (h && h->flags) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 memset(h->flags, 0xaa, ((h->n_buckets>>4) + 1) * sizeof(khint32_t)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 h->size = h->n_occupied = 0; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 SCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 if (h->n_buckets) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 khint_t inc, k, i, last; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 k = __hash_func(key); i = k % h->n_buckets; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 inc = 1 + k % (h->n_buckets - 1); last = i; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 if (i + inc >= h->n_buckets) i = i + inc - h->n_buckets; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 else i += inc; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 if (i == last) return h->n_buckets; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 return __ac_iseither(h->flags, i)? h->n_buckets : i; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 } else return 0; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 SCOPE void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 khint32_t *new_flags = 0; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 khint_t j = 1; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 khint_t t = __ac_HASH_PRIME_SIZE - 1; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 while (__ac_prime_list[t] > new_n_buckets) --t; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 new_n_buckets = __ac_prime_list[t+1]; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 else { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 new_flags = (khint32_t*)malloc(((new_n_buckets>>4) + 1) * sizeof(khint32_t)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 memset(new_flags, 0xaa, ((new_n_buckets>>4) + 1) * sizeof(khint32_t)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 if (h->n_buckets < new_n_buckets) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 h->keys = (khkey_t*)realloc(h->keys, new_n_buckets * sizeof(khkey_t)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 if (kh_is_map) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 h->vals = (khval_t*)realloc(h->vals, new_n_buckets * sizeof(khval_t)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 if (j) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 for (j = 0; j != h->n_buckets; ++j) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 if (__ac_iseither(h->flags, j) == 0) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 khkey_t key = h->keys[j]; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 khval_t val; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 if (kh_is_map) val = h->vals[j]; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 __ac_set_isdel_true(h->flags, j); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 while (1) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 khint_t inc, k, i; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 k = __hash_func(key); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 i = k % new_n_buckets; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 inc = 1 + k % (new_n_buckets - 1); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 while (!__ac_isempty(new_flags, i)) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 if (i + inc >= new_n_buckets) i = i + inc - new_n_buckets; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 else i += inc; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235 __ac_set_isempty_false(new_flags, i); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 if (i < h->n_buckets && __ac_iseither(h->flags, i) == 0) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 { khkey_t tmp = h->keys[i]; h->keys[i] = key; key = tmp; } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 if (kh_is_map) { khval_t tmp = h->vals[i]; h->vals[i] = val; val = tmp; } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 __ac_set_isdel_true(h->flags, i); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 } else { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 h->keys[i] = key; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 if (kh_is_map) h->vals[i] = val; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243 break; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 if (h->n_buckets > new_n_buckets) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 h->keys = (khkey_t*)realloc(h->keys, new_n_buckets * sizeof(khkey_t)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 if (kh_is_map) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 h->vals = (khval_t*)realloc(h->vals, new_n_buckets * sizeof(khval_t)); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253 free(h->flags); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 h->flags = new_flags; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255 h->n_buckets = new_n_buckets; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256 h->n_occupied = h->size; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 h->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 khint_t x; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 if (h->n_occupied >= h->upper_bound) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 if (h->n_buckets > (h->size<<1)) kh_resize_##name(h, h->n_buckets - 1); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 else kh_resize_##name(h, h->n_buckets + 1); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 khint_t inc, k, i, site, last; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 x = site = h->n_buckets; k = __hash_func(key); i = k % h->n_buckets; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 if (__ac_isempty(h->flags, i)) x = i; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 else { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 inc = 1 + k % (h->n_buckets - 1); last = i; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 if (__ac_isdel(h->flags, i)) site = i; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 if (i + inc >= h->n_buckets) i = i + inc - h->n_buckets; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276 else i += inc; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 if (i == last) { x = site; break; } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279 if (x == h->n_buckets) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 if (__ac_isempty(h->flags, i) && site != h->n_buckets) x = site; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 else x = i; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 if (__ac_isempty(h->flags, x)) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 h->keys[x] = key; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 __ac_set_isboth_false(h->flags, x); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288 ++h->size; ++h->n_occupied; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 *ret = 1; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 } else if (__ac_isdel(h->flags, x)) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 h->keys[x] = key; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 __ac_set_isboth_false(h->flags, x); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 ++h->size; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294 *ret = 2; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 } else *ret = 0; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 return x; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 SCOPE void kh_del_##name(kh_##name##_t *h, khint_t x) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 if (x != h->n_buckets && !__ac_iseither(h->flags, x)) { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301 __ac_set_isdel_true(h->flags, x); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 --h->size; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 } \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 #define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307 KHASH_INIT2(name, static inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 /* --- BEGIN OF HASH FUNCTIONS --- */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312 @abstract Integer hash function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313 @param key The integer [khint32_t]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314 @return The hash value [khint_t]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316 #define kh_int_hash_func(key) (khint32_t)(key)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318 @abstract Integer comparison function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320 #define kh_int_hash_equal(a, b) ((a) == (b))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 @abstract 64-bit integer hash function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323 @param key The integer [khint64_t]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 @return The hash value [khint_t]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 #define kh_int64_hash_func(key) (khint32_t)((key)>>33^(key)^(key)<<11)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 @abstract 64-bit integer comparison function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 #define kh_int64_hash_equal(a, b) ((a) == (b))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332 @abstract const char* hash function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 @param s Pointer to a null terminated string
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334 @return The hash value
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 static inline khint_t __ac_X31_hash_string(const char *s)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
337 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
338 khint_t h = *s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
339 if (h) for (++s ; *s; ++s) h = (h << 5) - h + *s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
340 return h;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
341 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
342 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
343 @abstract Another interface to const char* hash function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
344 @param key Pointer to a null terminated string [const char*]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
345 @return The hash value [khint_t]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
346 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
347 #define kh_str_hash_func(key) __ac_X31_hash_string(key)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
348 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
349 @abstract Const char* comparison function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
350 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
351 #define kh_str_hash_equal(a, b) (strcmp(a, b) == 0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
352
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
353 /* --- END OF HASH FUNCTIONS --- */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
354
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
355 /* Other necessary macros... */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
356
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
357 /*!
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
358 @abstract Type of the hash table.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
359 @param name Name of the hash table [symbol]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
360 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
361 #define khash_t(name) kh_##name##_t
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
362
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
363 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
364 @abstract Initiate a hash table.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
365 @param name Name of the hash table [symbol]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
366 @return Pointer to the hash table [khash_t(name)*]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
367 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
368 #define kh_init(name) kh_init_##name()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
369
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
370 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
371 @abstract Destroy a hash table.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
372 @param name Name of the hash table [symbol]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
373 @param h Pointer to the hash table [khash_t(name)*]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
374 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
375 #define kh_destroy(name, h) kh_destroy_##name(h)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
376
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
377 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
378 @abstract Reset a hash table without deallocating memory.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
379 @param name Name of the hash table [symbol]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
380 @param h Pointer to the hash table [khash_t(name)*]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
381 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
382 #define kh_clear(name, h) kh_clear_##name(h)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
383
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
384 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
385 @abstract Resize a hash table.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
386 @param name Name of the hash table [symbol]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
387 @param h Pointer to the hash table [khash_t(name)*]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
388 @param s New size [khint_t]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
389 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
390 #define kh_resize(name, h, s) kh_resize_##name(h, s)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
391
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
392 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
393 @abstract Insert a key to the hash table.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
394 @param name Name of the hash table [symbol]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
395 @param h Pointer to the hash table [khash_t(name)*]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
396 @param k Key [type of keys]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
397 @param r Extra return code: 0 if the key is present in the hash table;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
398 1 if the bucket is empty (never used); 2 if the element in
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
399 the bucket has been deleted [int*]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
400 @return Iterator to the inserted element [khint_t]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
401 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
402 #define kh_put(name, h, k, r) kh_put_##name(h, k, r)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
403
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
404 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
405 @abstract Retrieve a key from the hash table.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
406 @param name Name of the hash table [symbol]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
407 @param h Pointer to the hash table [khash_t(name)*]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
408 @param k Key [type of keys]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
409 @return Iterator to the found element, or kh_end(h) is the element is absent [khint_t]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
410 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
411 #define kh_get(name, h, k) kh_get_##name(h, k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
412
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
413 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
414 @abstract Remove a key from the hash table.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
415 @param name Name of the hash table [symbol]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
416 @param h Pointer to the hash table [khash_t(name)*]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
417 @param k Iterator to the element to be deleted [khint_t]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
418 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
419 #define kh_del(name, h, k) kh_del_##name(h, k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
420
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
421
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
422 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
423 @abstract Test whether a bucket contains data.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
424 @param h Pointer to the hash table [khash_t(name)*]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
425 @param x Iterator to the bucket [khint_t]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
426 @return 1 if containing data; 0 otherwise [int]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
427 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
428 #define kh_exist(h, x) (!__ac_iseither((h)->flags, (x)))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
429
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
430 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
431 @abstract Get key given an iterator
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
432 @param h Pointer to the hash table [khash_t(name)*]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
433 @param x Iterator to the bucket [khint_t]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
434 @return Key [type of keys]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
435 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
436 #define kh_key(h, x) ((h)->keys[x])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
437
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
438 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
439 @abstract Get value given an iterator
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
440 @param h Pointer to the hash table [khash_t(name)*]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
441 @param x Iterator to the bucket [khint_t]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
442 @return Value [type of values]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
443 @discussion For hash sets, calling this results in segfault.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
444 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
445 #define kh_val(h, x) ((h)->vals[x])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
446
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
447 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
448 @abstract Alias of kh_val()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
449 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
450 #define kh_value(h, x) ((h)->vals[x])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
451
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
452 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
453 @abstract Get the start iterator
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
454 @param h Pointer to the hash table [khash_t(name)*]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
455 @return The start iterator [khint_t]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
456 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
457 #define kh_begin(h) (khint_t)(0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
458
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
459 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
460 @abstract Get the end iterator
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
461 @param h Pointer to the hash table [khash_t(name)*]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
462 @return The end iterator [khint_t]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
463 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
464 #define kh_end(h) ((h)->n_buckets)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
465
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
466 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
467 @abstract Get the number of elements in the hash table
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
468 @param h Pointer to the hash table [khash_t(name)*]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
469 @return Number of elements in the hash table [khint_t]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
470 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
471 #define kh_size(h) ((h)->size)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
472
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
473 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
474 @abstract Get the number of buckets in the hash table
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
475 @param h Pointer to the hash table [khash_t(name)*]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
476 @return Number of buckets in the hash table [khint_t]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
477 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
478 #define kh_n_buckets(h) ((h)->n_buckets)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
479
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
480 /* More conenient interfaces */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
481
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
482 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
483 @abstract Instantiate a hash set containing integer keys
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
484 @param name Name of the hash table [symbol]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
485 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
486 #define KHASH_SET_INIT_INT(name) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
487 KHASH_INIT(name, khint32_t, char, 0, kh_int_hash_func, kh_int_hash_equal)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
488
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
489 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
490 @abstract Instantiate a hash map containing integer keys
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
491 @param name Name of the hash table [symbol]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
492 @param khval_t Type of values [type]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
493 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
494 #define KHASH_MAP_INIT_INT(name, khval_t) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
495 KHASH_INIT(name, khint32_t, khval_t, 1, kh_int_hash_func, kh_int_hash_equal)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
496
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
497 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
498 @abstract Instantiate a hash map containing 64-bit integer keys
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
499 @param name Name of the hash table [symbol]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
500 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
501 #define KHASH_SET_INIT_INT64(name) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
502 KHASH_INIT(name, khint64_t, char, 0, kh_int64_hash_func, kh_int64_hash_equal)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
503
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
504 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
505 @abstract Instantiate a hash map containing 64-bit integer keys
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
506 @param name Name of the hash table [symbol]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
507 @param khval_t Type of values [type]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
508 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
509 #define KHASH_MAP_INIT_INT64(name, khval_t) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
510 KHASH_INIT(name, khint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
511
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
512 typedef const char *kh_cstr_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
513 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
514 @abstract Instantiate a hash map containing const char* keys
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
515 @param name Name of the hash table [symbol]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
516 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
517 #define KHASH_SET_INIT_STR(name) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
518 KHASH_INIT(name, kh_cstr_t, char, 0, kh_str_hash_func, kh_str_hash_equal)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
519
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
520 /*! @function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
521 @abstract Instantiate a hash map containing const char* keys
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
522 @param name Name of the hash table [symbol]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
523 @param khval_t Type of values [type]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
524 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
525 #define KHASH_MAP_INIT_STR(name, khval_t) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
526 KHASH_INIT(name, kh_cstr_t, khval_t, 1, kh_str_hash_func, kh_str_hash_equal)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
527
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
528 #endif /* __AC_KHASH_H */