]> git.saurik.com Git - bison.git/blob - lib/bbitset.h
04bc487d5d5abe5b66b99b168f6ccd6795dfa4e1
[bison.git] / lib / bbitset.h
1 /* Base bitset stuff.
2 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18
19 #ifndef _BBITSET_H
20 #define _BBITSET_H
21
22 #include "config.h"
23 #include "libiberty.h"
24
25 #include <stdbool.h>
26 #include <limits.h>
27 #include <sys/types.h>
28
29 /* Currently we support five flavours of bitsets:
30 BITSET_ARRAY: Array of bits (fixed size, fast for dense bitsets).
31 Memory for bit array and bitset structure allocated
32 contiguously.
33 BITSET_LIST: Linked list of arrays of bits (variable size, least storage
34 for large very sparse sets).
35 BITSET_TABLE: Expandable table of pointers to arrays of bits
36 (variable size, less storage for large sparse sets).
37 Faster than BITSET_LIST for random access.
38 BITSET_VARRAY: Variable array of bits (variable size, fast for
39 dense bitsets).
40 BITSET_STATS: Wrapper bitset for internal use only. Used for gathering
41 statistics and/or better run-time checking.
42 */
43 enum bitset_type {BITSET_ARRAY, BITSET_LIST, BITSET_TABLE, BITSET_VARRAY,
44 BITSET_TYPE_NUM, BITSET_STATS};
45 #define BITSET_TYPE_NAMES {"abitset", "lbitset", "ebitset", "vbitset"}
46
47 extern const char * const bitset_type_names[];
48
49 enum bitset_alloc_type {BITSET_MALLOC, BITSET_OBALLOC};
50
51 /* Data type used to store a word of bits. */
52 typedef unsigned long bitset_word;
53 #define BITSET_WORD_BITS ((unsigned) (CHAR_BIT * sizeof (bitset_word)))
54
55 /* Bit index. In theory we might need a type wider than size_t, but
56 in practice we lose at most a factor of CHAR_BIT by going with
57 size_t, and that is good enough. If this type is changed to be
58 wider than size_t, the code needs to be modified to check for
59 overflow when converting bit counts to byte or word counts.
60 The bit and word index types must be unsigned. */
61 typedef size_t bitset_bindex;
62
63 /* Word index. */
64 typedef size_t bitset_windex;
65
66 /* Maximum values for commonly-used unsigned types. BITSET_SIZE_MAX
67 always equals SIZE_MAX, but some older systems lack SIZE_MAX. */
68 #define BITSET_BINDEX_MAX ((bitset_bindex) -1)
69
70 /* Limit max word index to the maximum value of a signed integer
71 to simplify cache disabling. */
72 #define BITSET_WINDEX_MAX (((bitset_windex) -1) >> 1)
73 #define BITSET_SIZE_MAX ((size_t) -1)
74
75 #define BITSET_MSB ((bitset_word) 1 << (BITSET_WORD_BITS - 1))
76
77 #define BITSET_LIST_SIZE 1024
78
79 enum bitset_ops {BITSET_OP_ZERO, BITSET_OP_ONES,
80 BITSET_OP_COPY, BITSET_OP_NOT,
81 BITSET_OP_EMPTY_P, BITSET_OP_EQUAL_P,
82 BITSET_OP_SUBSET_P, BITSET_OP_DISJOINT_P,
83 BITSET_OP_AND, BITSET_OP_OR, BITSET_OP_XOR, BITSET_OP_ANDN,
84 BITSET_OP_OR_AND, BITSET_OP_AND_OR, BITSET_OP_ANDN_OR};
85
86 struct bbitset_struct
87 {
88 const struct bitset_vtable *vtable;
89 bitset_windex cindex; /* Cache word index. */
90 bitset_windex csize; /* Cache size in words. */
91 bitset_word *cdata; /* Cache data pointer. */
92 bitset_bindex n_bits; /* Number of bits. */
93 /* Perhaps we could sacrifice another word to indicate
94 that the bitset is known to be zero, that a bit has been set
95 in the cache, and that a bit has been cleared in the cache.
96 This would speed up some of the searches but slightly slow down
97 bit set/reset operations of cached bits. */
98 };
99
100
101 typedef union bitset_union *bitset;
102
103
104 /* Private accessor macros to bitset structure. */
105 #define BITSET_VTABLE_(SRC) (SRC)->b.vtable
106 #define BITSET_CINDEX_(SRC) (SRC)->b.cindex
107 #define BITSET_CDATA_(SRC) (SRC)->b.cdata
108 #define BITSET_CSIZE_(SRC) (SRC)->b.csize
109 #define BITSET_NBITS_(SRC) (SRC)->b.n_bits
110
111
112 /* The contents of this structure should be considered private. */
113 struct bitset_vtable
114 {
115 void (*set) PARAMS ((bitset, bitset_bindex));
116 void (*reset) PARAMS ((bitset, bitset_bindex));
117 bool (*toggle) PARAMS ((bitset, bitset_bindex));
118 bool (*test) PARAMS ((bitset, bitset_bindex));
119 bitset_bindex (*resize) PARAMS ((bitset, bitset_bindex));
120 bitset_bindex (*size) PARAMS ((bitset));
121 bitset_bindex (*count) PARAMS ((bitset));
122
123 bool (*empty_p) PARAMS ((bitset));
124 void (*ones) PARAMS ((bitset));
125 void (*zero) PARAMS ((bitset));
126
127 void (*copy) PARAMS ((bitset, bitset));
128 bool (*disjoint_p) PARAMS ((bitset, bitset));
129 bool (*equal_p) PARAMS ((bitset, bitset));
130 void (*not) PARAMS ((bitset, bitset));
131 bool (*subset_p) PARAMS ((bitset, bitset));
132
133 void (*and) PARAMS ((bitset, bitset, bitset));
134 bool (*and_cmp) PARAMS ((bitset, bitset, bitset));
135 void (*andn) PARAMS ((bitset, bitset, bitset));
136 bool (*andn_cmp) PARAMS ((bitset, bitset, bitset));
137 void (*or) PARAMS ((bitset, bitset, bitset));
138 bool (*or_cmp) PARAMS ((bitset, bitset, bitset));
139 void (*xor) PARAMS ((bitset, bitset, bitset));
140 bool (*xor_cmp) PARAMS ((bitset, bitset, bitset));
141
142 void (*and_or) PARAMS ((bitset, bitset, bitset, bitset));
143 bool (*and_or_cmp) PARAMS ((bitset, bitset, bitset, bitset));
144 void (*andn_or) PARAMS ((bitset, bitset, bitset, bitset));
145 bool (*andn_or_cmp) PARAMS ((bitset, bitset, bitset, bitset));
146 void (*or_and) PARAMS ((bitset, bitset, bitset, bitset));
147 bool (*or_and_cmp) PARAMS ((bitset, bitset, bitset, bitset));
148
149 bitset_bindex (*list) PARAMS ((bitset, bitset_bindex *,
150 bitset_bindex, bitset_bindex *));
151 bitset_bindex (*list_reverse) PARAMS ((bitset,
152 bitset_bindex *, bitset_bindex,
153 bitset_bindex *));
154 void (*free) PARAMS ((bitset));
155 enum bitset_type type;
156 };
157
158 #define BITSET_COMPATIBLE_(BSET1, BSET2) \
159 ((BSET1)->b.vtable == (BSET2)->b.vtable)
160
161 #define BITSET_CHECK2_(DST, SRC) \
162 if (!BITSET_COMPATIBLE_ (DST, SRC)) abort ();
163
164 #define BITSET_CHECK3_(DST, SRC1, SRC2) \
165 if (!BITSET_COMPATIBLE_ (DST, SRC1) \
166 || !BITSET_COMPATIBLE_ (DST, SRC2)) abort ();
167
168 #define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) \
169 if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \
170 || !BITSET_COMPATIBLE_ (DST, SRC3)) abort ();
171
172
173 /* Redefine number of bits in bitset DST. */
174 #define BITSET_RESIZE_(DST, SIZE) (DST)->b.vtable->resize (DST, SIZE)
175
176 /* Return size in bits of bitset SRC. */
177 #define BITSET_SIZE_(SRC) (SRC)->b.vtable->size (SRC)
178
179 /* Return number of bits set in bitset SRC. */
180 #define BITSET_COUNT_(SRC) (SRC)->b.vtable->count (SRC)
181
182 /* Return type of bitset SRC. */
183 #define BITSET_TYPE_(DST) (DST)->b.vtable->type
184
185 /* Set bit BITNO in bitset DST. */
186 #define BITSET_SET_(DST, BITNO) (DST)->b.vtable->set (DST, BITNO)
187
188 /* Reset bit BITNO in bitset DST. */
189 #define BITSET_RESET_(DST, BITNO) (DST)->b.vtable->reset (DST, BITNO)
190
191 /* Toggle bit BITNO in bitset DST. */
192 #define BITSET_TOGGLE_(DST, BITNO) (DST)->b.vtable->toggle (DST, BITNO)
193
194 /* Return non-zero if bit BITNO in bitset SRC is set. */
195 #define BITSET_TEST_(SRC, BITNO) (SRC)->b.vtable->test (SRC, BITNO)
196
197 /* Free bitset SRC. */
198 #define BITSET_FREE_(SRC)\
199 ((SRC)->b.vtable->free ? (SRC)->b.vtable->free (SRC) :(void)0)
200
201
202 /* Return SRC == 0. */
203 #define BITSET_EMPTY_P_(SRC) (SRC)->b.vtable->empty_p (SRC)
204
205 /* DST = ~0. */
206 #define BITSET_ONES_(DST) (DST)->b.vtable->ones (DST)
207
208 /* DST = 0. */
209 #define BITSET_ZERO_(DST) (DST)->b.vtable->zero (DST)
210
211
212
213 /* DST = SRC. */
214 #define BITSET_COPY_(DST, SRC) (SRC)->b.vtable->copy (DST, SRC)
215
216 /* Return DST & SRC == 0. */
217 #define BITSET_DISJOINT_P_(DST, SRC) (SRC)->b.vtable->disjoint_p (DST, SRC)
218
219 /* Return DST == SRC. */
220 #define BITSET_EQUAL_P_(DST, SRC) (SRC)->b.vtable->equal_p (DST, SRC)
221
222 /* DST = ~SRC. */
223 #define BITSET_NOT_(DST, SRC) (SRC)->b.vtable->not (DST, SRC)
224
225 /* Return DST == DST | SRC. */
226 #define BITSET_SUBSET_P_(DST, SRC) (SRC)->b.vtable->subset_p (DST, SRC)
227
228
229 /* DST = SRC1 & SRC2. */
230 #define BITSET_AND_(DST, SRC1, SRC2) (SRC1)->b.vtable->and (DST, SRC1, SRC2)
231 #define BITSET_AND_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->and_cmp (DST, SRC1, SRC2)
232
233 /* DST = SRC1 & ~SRC2. */
234 #define BITSET_ANDN_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn (DST, SRC1, SRC2)
235 #define BITSET_ANDN_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn_cmp (DST, SRC1, SRC2)
236
237 /* DST = SRC1 | SRC2. */
238 #define BITSET_OR_(DST, SRC1, SRC2) (SRC1)->b.vtable->or (DST, SRC1, SRC2)
239 #define BITSET_OR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->or_cmp (DST, SRC1, SRC2)
240
241 /* DST = SRC1 ^ SRC2. */
242 #define BITSET_XOR_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor (DST, SRC1, SRC2)
243 #define BITSET_XOR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor_cmp (DST, SRC1, SRC2)
244
245
246
247 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
248 DST != (SRC1 & SRC2) | SRC3. */
249 #define BITSET_AND_OR_(DST, SRC1, SRC2, SRC3) \
250 (SRC1)->b.vtable->and_or (DST, SRC1, SRC2, SRC3)
251 #define BITSET_AND_OR_CMP_(DST, SRC1, SRC2, SRC3) \
252 (SRC1)->b.vtable->and_or_cmp (DST, SRC1, SRC2, SRC3)
253
254 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
255 DST != (SRC1 & ~SRC2) | SRC3. */
256 #define BITSET_ANDN_OR_(DST, SRC1, SRC2, SRC3) \
257 (SRC1)->b.vtable->andn_or (DST, SRC1, SRC2, SRC3)
258 #define BITSET_ANDN_OR_CMP_(DST, SRC1, SRC2, SRC3) \
259 (SRC1)->b.vtable->andn_or_cmp (DST, SRC1, SRC2, SRC3)
260
261 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
262 DST != (SRC1 | SRC2) & SRC3. */
263 #define BITSET_OR_AND_(DST, SRC1, SRC2, SRC3) \
264 (SRC1)->b.vtable->or_and (DST, SRC1, SRC2, SRC3)
265 #define BITSET_OR_AND_CMP_(DST, SRC1, SRC2, SRC3) \
266 (SRC1)->b.vtable->or_and_cmp (DST, SRC1, SRC2, SRC3)
267
268
269 /* Find list of up to NUM bits set in BSET starting from and including
270 *NEXT. Return with actual number of bits found and with *NEXT
271 indicating where search stopped. */
272 #define BITSET_LIST_(BSET, LIST, NUM, NEXT) \
273 (BSET)->b.vtable->list (BSET, LIST, NUM, NEXT)
274
275 /* Find reverse list of up to NUM bits set in BSET starting from and
276 including NEXT. Return with actual number of bits found and with
277 *NEXT indicating where search stopped. */
278 #define BITSET_LIST_REVERSE_(BSET, LIST, NUM, NEXT) \
279 (BSET)->b.vtable->list_reverse (BSET, LIST, NUM, NEXT)
280
281
282 /* Private functions for bitset implementations. */
283
284 extern bool bitset_toggle_ PARAMS ((bitset, bitset_bindex));
285
286 extern bitset_bindex bitset_count_ PARAMS ((bitset));
287
288 extern bitset_bindex bitset_size_ PARAMS ((bitset));
289
290 extern bool bitset_copy_ PARAMS ((bitset, bitset));
291
292 extern void bitset_and_or_ PARAMS ((bitset, bitset, bitset, bitset));
293
294 extern bool bitset_and_or_cmp_ PARAMS ((bitset, bitset, bitset, bitset));
295
296 extern void bitset_andn_or_ PARAMS ((bitset, bitset, bitset, bitset));
297
298 extern bool bitset_andn_or_cmp_ PARAMS ((bitset, bitset, bitset, bitset));
299
300 extern void bitset_or_and_ PARAMS ((bitset, bitset, bitset, bitset));
301
302 extern bool bitset_or_and_cmp_ PARAMS ((bitset, bitset, bitset, bitset));
303
304 #endif /* _BBITSET_H */