annotate pyPRADA_1.2/tools/bwa-0.5.7-mh/khash.h @ 3:f17965495ec9 draft default tip

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