This source file includes following definitions.
- bitset_ffs_
- bitset_fls_
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #ifndef _BITSET_BASE_H
22 #define _BITSET_BASE_H
23
24 #include <limits.h>
25 #include <stdbool.h>
26 #include <stddef.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "attribute.h"
31 #include "integer_length.h"
32 #include "xalloc.h"
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 enum bitset_type {BITSET_ARRAY, BITSET_LIST, BITSET_TABLE, BITSET_VECTOR,
49 BITSET_TYPE_NUM, BITSET_STATS};
50 #define BITSET_TYPE_NAMES {"abitset", "lbitset", "tbitset", "vbitset"}
51
52 extern const char * const bitset_type_names[];
53
54
55 typedef unsigned long bitset_word;
56 #define BITSET_WORD_BITS ((unsigned) (CHAR_BIT * sizeof (bitset_word)))
57
58
59
60
61
62
63
64 typedef size_t bitset_bindex;
65
66
67 typedef size_t bitset_windex;
68
69
70
71 #define BITSET_BINDEX_MAX ((bitset_bindex) -1)
72
73
74
75 #define BITSET_WINDEX_MAX (((bitset_windex) -1) >> 1)
76 #define BITSET_SIZE_MAX ((size_t) -1)
77
78 #define BITSET_MSB ((bitset_word) 1 << (BITSET_WORD_BITS - 1))
79
80 #define BITSET_LIST_SIZE 1024
81
82 enum bitset_ops {BITSET_OP_ZERO, BITSET_OP_ONES,
83 BITSET_OP_COPY, BITSET_OP_NOT,
84 BITSET_OP_EMPTY_P, BITSET_OP_EQUAL_P,
85 BITSET_OP_SUBSET_P, BITSET_OP_DISJOINT_P,
86 BITSET_OP_AND, BITSET_OP_OR, BITSET_OP_XOR, BITSET_OP_ANDN,
87 BITSET_OP_OR_AND, BITSET_OP_AND_OR, BITSET_OP_ANDN_OR};
88
89 struct bbitset_struct
90 {
91 const struct bitset_vtable *vtable;
92 bitset_windex cindex;
93 bitset_windex csize;
94 bitset_word *cdata;
95 bitset_bindex n_bits;
96
97
98
99
100
101 };
102
103
104 typedef union bitset_union *bitset;
105
106
107
108 #define BITSET_VTABLE_(SRC) (SRC)->b.vtable
109 #define BITSET_CINDEX_(SRC) (SRC)->b.cindex
110 #define BITSET_CDATA_(SRC) (SRC)->b.cdata
111 #define BITSET_CSIZE_(SRC) (SRC)->b.csize
112 #define BITSET_NBITS_(SRC) (SRC)->b.n_bits
113
114
115
116 struct bitset_vtable
117 {
118 void (*set) (bitset, bitset_bindex);
119 void (*reset) (bitset, bitset_bindex);
120 bool (*toggle) (bitset, bitset_bindex);
121 bool (*test) (bitset, bitset_bindex);
122 bitset_bindex (*resize) (bitset, bitset_bindex);
123 bitset_bindex (*size) (bitset);
124 bitset_bindex (*count) (bitset);
125
126 bool (*empty_p) (bitset);
127 void (*ones) (bitset);
128 void (*zero) (bitset);
129
130 void (*copy) (bitset, bitset);
131 bool (*disjoint_p) (bitset, bitset);
132 bool (*equal_p) (bitset, bitset);
133 void (*not_) (bitset, bitset);
134 bool (*subset_p) (bitset, bitset);
135
136 void (*and_) (bitset, bitset, bitset);
137 bool (*and_cmp) (bitset, bitset, bitset);
138 void (*andn) (bitset, bitset, bitset);
139 bool (*andn_cmp) (bitset, bitset, bitset);
140 void (*or_) (bitset, bitset, bitset);
141 bool (*or_cmp) (bitset, bitset, bitset);
142 void (*xor_) (bitset, bitset, bitset);
143 bool (*xor_cmp) (bitset, bitset, bitset);
144
145 void (*and_or) (bitset, bitset, bitset, bitset);
146 bool (*and_or_cmp) (bitset, bitset, bitset, bitset);
147 void (*andn_or) (bitset, bitset, bitset, bitset);
148 bool (*andn_or_cmp) (bitset, bitset, bitset, bitset);
149 void (*or_and) (bitset, bitset, bitset, bitset);
150 bool (*or_and_cmp) (bitset, bitset, bitset, bitset);
151
152 bitset_bindex (*list) (bitset, bitset_bindex *, bitset_bindex,
153 bitset_bindex *);
154 bitset_bindex (*list_reverse) (bitset, bitset_bindex *, bitset_bindex,
155 bitset_bindex *);
156 void (*free) (bitset);
157 enum bitset_type type;
158 };
159
160 #define BITSET_COMPATIBLE_(BSET1, BSET2) \
161 ((BSET1)->b.vtable == (BSET2)->b.vtable)
162
163 #define BITSET_CHECK2_(DST, SRC) \
164 if (!BITSET_COMPATIBLE_ (DST, SRC)) abort ();
165
166 #define BITSET_CHECK3_(DST, SRC1, SRC2) \
167 if (!BITSET_COMPATIBLE_ (DST, SRC1) \
168 || !BITSET_COMPATIBLE_ (DST, SRC2)) abort ();
169
170 #define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) \
171 if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \
172 || !BITSET_COMPATIBLE_ (DST, SRC3)) abort ();
173
174
175
176 #define BITSET_RESIZE_(DST, SIZE) (DST)->b.vtable->resize (DST, SIZE)
177
178
179 #define BITSET_SIZE_(SRC) (SRC)->b.vtable->size (SRC)
180
181
182 #define BITSET_COUNT_(SRC) (SRC)->b.vtable->count (SRC)
183
184
185 #define BITSET_TYPE_(DST) (DST)->b.vtable->type
186
187
188 #define BITSET_SET_(DST, BITNO) (DST)->b.vtable->set (DST, BITNO)
189
190
191 #define BITSET_RESET_(DST, BITNO) (DST)->b.vtable->reset (DST, BITNO)
192
193
194 #define BITSET_TOGGLE_(DST, BITNO) (DST)->b.vtable->toggle (DST, BITNO)
195
196
197 #define BITSET_TEST_(SRC, BITNO) (SRC)->b.vtable->test (SRC, BITNO)
198
199
200 #define BITSET_FREE_(SRC)\
201 ((SRC)->b.vtable->free ? (SRC)->b.vtable->free (SRC) :(void)0)
202
203
204
205 #define BITSET_EMPTY_P_(SRC) (SRC)->b.vtable->empty_p (SRC)
206
207
208 #define BITSET_ONES_(DST) (DST)->b.vtable->ones (DST)
209
210
211 #define BITSET_ZERO_(DST) (DST)->b.vtable->zero (DST)
212
213
214
215
216 #define BITSET_COPY_(DST, SRC) (SRC)->b.vtable->copy (DST, SRC)
217
218
219 #define BITSET_DISJOINT_P_(DST, SRC) (SRC)->b.vtable->disjoint_p (DST, SRC)
220
221
222 #define BITSET_EQUAL_P_(DST, SRC) (SRC)->b.vtable->equal_p (DST, SRC)
223
224
225 #define BITSET_NOT_(DST, SRC) (SRC)->b.vtable->not_ (DST, SRC)
226
227
228 #define BITSET_SUBSET_P_(DST, SRC) (SRC)->b.vtable->subset_p (DST, SRC)
229
230
231
232 #define BITSET_AND_(DST, SRC1, SRC2) (SRC1)->b.vtable->and_ (DST, SRC1, SRC2)
233 #define BITSET_AND_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->and_cmp (DST, SRC1, SRC2)
234
235
236 #define BITSET_ANDN_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn (DST, SRC1, SRC2)
237 #define BITSET_ANDN_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn_cmp (DST, SRC1, SRC2)
238
239
240 #define BITSET_OR_(DST, SRC1, SRC2) (SRC1)->b.vtable->or_ (DST, SRC1, SRC2)
241 #define BITSET_OR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->or_cmp (DST, SRC1, SRC2)
242
243
244 #define BITSET_XOR_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor_ (DST, SRC1, SRC2)
245 #define BITSET_XOR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor_cmp (DST, SRC1, SRC2)
246
247
248
249
250
251 #define BITSET_AND_OR_(DST, SRC1, SRC2, SRC3) \
252 (SRC1)->b.vtable->and_or (DST, SRC1, SRC2, SRC3)
253 #define BITSET_AND_OR_CMP_(DST, SRC1, SRC2, SRC3) \
254 (SRC1)->b.vtable->and_or_cmp (DST, SRC1, SRC2, SRC3)
255
256
257
258 #define BITSET_ANDN_OR_(DST, SRC1, SRC2, SRC3) \
259 (SRC1)->b.vtable->andn_or (DST, SRC1, SRC2, SRC3)
260 #define BITSET_ANDN_OR_CMP_(DST, SRC1, SRC2, SRC3) \
261 (SRC1)->b.vtable->andn_or_cmp (DST, SRC1, SRC2, SRC3)
262
263
264
265 #define BITSET_OR_AND_(DST, SRC1, SRC2, SRC3) \
266 (SRC1)->b.vtable->or_and (DST, SRC1, SRC2, SRC3)
267 #define BITSET_OR_AND_CMP_(DST, SRC1, SRC2, SRC3) \
268 (SRC1)->b.vtable->or_and_cmp (DST, SRC1, SRC2, SRC3)
269
270
271
272
273
274 #define BITSET_LIST_(BSET, LIST, NUM, NEXT) \
275 (BSET)->b.vtable->list (BSET, LIST, NUM, NEXT)
276
277
278
279
280 #define BITSET_LIST_REVERSE_(BSET, LIST, NUM, NEXT) \
281 (BSET)->b.vtable->list_reverse (BSET, LIST, NUM, NEXT)
282
283
284
285
286 #define BITSET_FOR_EACH_BIT(Pos, Word) \
287 for (int Pos = bitset_ffs_ (Word); \
288 0 <= Pos; \
289 Word ^= 1UL << Pos, Pos = bitset_ffs_ (Word))
290
291
292
293
294 #define BITSET_FOR_EACH_BIT_REVERSE(Pos, Word) \
295 for (int Pos = bitset_fls_ (Word); \
296 0 <= Pos; \
297 Word ^= 1UL << Pos, Pos = bitset_fls_ (Word))
298
299
300
301 bool bitset_toggle_ (bitset, bitset_bindex);
302
303 bitset_bindex bitset_count_ (bitset);
304
305 bitset_bindex bitset_size_ (bitset);
306
307 bool bitset_copy_ (bitset, bitset);
308
309 void bitset_and_or_ (bitset, bitset, bitset, bitset);
310
311 bool bitset_and_or_cmp_ (bitset, bitset, bitset, bitset);
312
313 void bitset_andn_or_ (bitset, bitset, bitset, bitset);
314
315 bool bitset_andn_or_cmp_ (bitset, bitset, bitset, bitset);
316
317 void bitset_or_and_ (bitset, bitset, bitset, bitset);
318
319 bool bitset_or_and_cmp_ (bitset, bitset, bitset, bitset);
320
321
322
323 static inline
324 int bitset_ffs_ (bitset_word word)
325 {
326 return ffsl ((long) word) - 1;
327 }
328
329
330
331 static inline
332 int bitset_fls_ (bitset_word word)
333 {
334 return integer_length_l (word) - 1;
335 }
336
337 #endif