]>
Commit | Line | Data |
---|---|---|
7086e707 AD |
1 | /* Generic bitsets. |
2 | Copyright (C) 2002 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 _BITSET_H | |
613f5e1a | 20 | #define _BITSET_H |
7086e707 | 21 | |
ef017502 AD |
22 | /* This file is the public interface to the bitset abstract data type. |
23 | Only use the functions and macros defined in this file. */ | |
7086e707 | 24 | |
ef017502 | 25 | #include "bbitset.h" |
7086e707 AD |
26 | #include "obstack.h" |
27 | #include <stdio.h> | |
7086e707 | 28 | |
ef017502 | 29 | /* Attributes used to select a bitset implementation. */ |
7086e707 AD |
30 | enum bitset_attr {BITSET_FIXED = 1, /* Bitset size fixed. */ |
31 | BITSET_VARIABLE = 2, /* Bitset size variable. */ | |
32 | BITSET_DENSE = 4, /* Bitset dense. */ | |
33 | BITSET_SPARSE = 8, /* Bitset sparse. */ | |
34 | BITSET_FRUGAL = 16, /* Prefer most compact. */ | |
35 | BITSET_GREEDY = 32}; /* Prefer fastest. */ | |
36 | ||
37 | typedef unsigned int bitset_attrs; | |
38 | ||
ef017502 AD |
39 | /* The contents of the structure should be considered to be private. |
40 | While I would like to make this structure opaque, it needs to be | |
41 | visible for the inline bit set/test functions. */ | |
42 | struct bitset_struct | |
7086e707 | 43 | { |
ef017502 | 44 | struct bbitset_struct b; |
7086e707 AD |
45 | }; |
46 | ||
613f5e1a AD |
47 | |
48 | /* The contents of this structure should be considered private. | |
49 | It is used for iterating over set bits. */ | |
50 | typedef struct | |
51 | { | |
52 | bitset_bindex list[BITSET_LIST_SIZE]; | |
53 | bitset_bindex next; | |
54 | int num; | |
55 | int i; | |
56 | } bitset_iterator; | |
57 | ||
58 | ||
7086e707 AD |
59 | /* Return bytes required for bitset of desired type and size. */ |
60 | extern int bitset_bytes PARAMS ((enum bitset_type, bitset_bindex)); | |
61 | ||
62 | /* Initialise a bitset with desired type and size. */ | |
63 | extern bitset bitset_init PARAMS ((bitset, bitset_bindex, enum bitset_type)); | |
64 | ||
ef017502 AD |
65 | /* Select an implementation type based on the desired bitset size |
66 | and attributes. */ | |
7086e707 AD |
67 | extern enum bitset_type bitset_type_choose PARAMS ((bitset_bindex, |
68 | bitset_attrs)); | |
69 | ||
ef017502 | 70 | /* Create a bitset of desired type and size. The bitset is zeroed. */ |
7086e707 AD |
71 | extern bitset bitset_alloc PARAMS ((bitset_bindex, enum bitset_type)); |
72 | ||
73 | /* Free bitset. */ | |
74 | extern void bitset_free PARAMS ((bitset)); | |
75 | ||
ef017502 AD |
76 | /* Create a bitset of desired type and size using an obstack. The |
77 | bitset is zeroed. */ | |
7086e707 AD |
78 | extern bitset bitset_obstack_alloc PARAMS ((struct obstack *bobstack, |
79 | bitset_bindex, enum bitset_type)); | |
80 | ||
81 | /* Free bitset allocated on obstack. */ | |
82 | extern void bitset_obstack_free PARAMS ((bitset)); | |
83 | ||
ef017502 | 84 | /* Create a bitset of desired size and attributes. The bitset is zeroed. */ |
7086e707 AD |
85 | extern bitset bitset_create PARAMS ((bitset_bindex, bitset_attrs)); |
86 | ||
87 | /* Return size in bits of bitset SRC. */ | |
88 | extern int bitset_size PARAMS ((bitset)); | |
89 | ||
ef017502 AD |
90 | /* Return number of bits set in bitset SRC. */ |
91 | extern int bitset_count PARAMS ((bitset)); | |
92 | ||
613f5e1a AD |
93 | /* Return bitset type. */ |
94 | extern enum bitset_type bitset_type_get PARAMS ((bitset)); | |
95 | ||
96 | #if BITSET_INLINE | |
7086e707 AD |
97 | static inline void bitset_set PARAMS ((bitset, bitset_bindex)); |
98 | static inline void bitset_reset PARAMS ((bitset, bitset_bindex)); | |
99 | static inline int bitset_test PARAMS ((bitset, bitset_bindex)); | |
100 | ||
101 | /* Set bit BITNO in bitset BSET. */ | |
102 | static inline void bitset_set (bset, bitno) | |
103 | bitset bset; | |
104 | bitset_bindex bitno; | |
105 | { | |
106 | bitset_windex index = bitno / BITSET_WORD_BITS; | |
ef017502 | 107 | bitset_windex offset = index - bset->b.cindex; |
613f5e1a | 108 | |
ef017502 AD |
109 | if (offset < bset->b.csize) |
110 | bset->b.cdata[offset] |= (1 << (bitno % BITSET_WORD_BITS)); | |
7086e707 | 111 | else |
ef017502 | 112 | BITSET_SET_ (bset, bitno); |
7086e707 AD |
113 | } |
114 | ||
115 | ||
116 | /* Reset bit BITNO in bitset BSET. */ | |
117 | static inline void bitset_reset (bset, bitno) | |
118 | bitset bset; | |
119 | bitset_bindex bitno; | |
120 | { | |
121 | bitset_windex index = bitno / BITSET_WORD_BITS; | |
ef017502 | 122 | bitset_windex offset = index - bset->b.cindex; |
613f5e1a | 123 | |
ef017502 AD |
124 | if (offset < bset->b.csize) |
125 | bset->b.cdata[offset] &= ~(1 << (bitno % BITSET_WORD_BITS)); | |
7086e707 | 126 | else |
ef017502 | 127 | BITSET_RESET_ (bset, bitno); |
7086e707 AD |
128 | } |
129 | ||
130 | ||
131 | /* Test bit BITNO in bitset BSET. */ | |
132 | static inline int bitset_test (bset, bitno) | |
133 | bitset bset; | |
134 | bitset_bindex bitno; | |
135 | { | |
136 | bitset_windex index = bitno / BITSET_WORD_BITS; | |
ef017502 | 137 | bitset_windex offset = index - bset->b.cindex; |
613f5e1a | 138 | |
ef017502 AD |
139 | if (offset < bset->b.csize) |
140 | return (bset->b.cdata[offset] >> (bitno % BITSET_WORD_BITS)) & 1; | |
7086e707 | 141 | else |
ef017502 | 142 | return BITSET_TEST_ (bset, bitno); |
7086e707 AD |
143 | } |
144 | #endif | |
145 | ||
613f5e1a | 146 | #if ! BITSET_INLINE |
7086e707 AD |
147 | |
148 | /* Set bit BITNO in bitset BSET. */ | |
149 | #define bitset_set(bset, bitno) \ | |
150 | do \ | |
151 | { \ | |
152 | bitset_bindex _bitno = (bitno); \ | |
153 | bitset_windex _index = _bitno / BITSET_WORD_BITS; \ | |
ef017502 | 154 | bitset_windex _offset = _index - (bset)->b.cindex; \ |
7086e707 | 155 | \ |
ef017502 AD |
156 | if (_offset < (bset)->b.csize) \ |
157 | (bset)->b.cdata[_offset] |= (1 << (_bitno % BITSET_WORD_BITS)); \ | |
7086e707 | 158 | else \ |
ef017502 | 159 | BITSET_SET_ ((bset), _bitno); \ |
7086e707 AD |
160 | } while (0) |
161 | ||
162 | ||
163 | /* Reset bit BITNO in bitset BSET. */ | |
164 | #define bitset_reset(bset, bitno) \ | |
165 | do \ | |
166 | { \ | |
167 | bitset_bindex _bitno = (bitno); \ | |
168 | bitset_windex _index = _bitno / BITSET_WORD_BITS; \ | |
ef017502 | 169 | bitset_windex _offset = _index - (bset)->b.cindex; \ |
7086e707 | 170 | \ |
ef017502 AD |
171 | if (_offset < (bset)->b.csize) \ |
172 | (bset)->b.cdata[_offset] &= ~(1 << (_bitno % BITSET_WORD_BITS)); \ | |
7086e707 | 173 | else \ |
ef017502 | 174 | BITSET_RESET_ ((bset), _bitno); \ |
7086e707 AD |
175 | } while (0) |
176 | ||
177 | ||
178 | /* Test bit BITNO in bitset BSET. */ | |
179 | #define bitset_test(bset, bitno) \ | |
ef017502 AD |
180 | (((((bitno) / BITSET_WORD_BITS) - (bset)->b.cindex) < (bset)->b.csize) \ |
181 | ? ((bset)->b.cdata[(((bitno) / BITSET_WORD_BITS) - (bset)->b.cindex)] \ | |
7086e707 | 182 | >> ((bitno) % BITSET_WORD_BITS)) & 1 \ |
ef017502 | 183 | : (unsigned int) BITSET_TEST_ ((bset), (bitno))) |
7086e707 AD |
184 | #endif |
185 | ||
7086e707 | 186 | |
345cea78 AD |
187 | /* Toggle bit BITNO in bitset BSET and return non-zero if now set. */ |
188 | extern int bitset_toggle PARAMS ((bitset, bitset_bindex)); | |
189 | ||
7086e707 AD |
190 | /* DST = 0. */ |
191 | extern int bitset_zero PARAMS ((bitset)); | |
192 | ||
193 | /* DST = ~0. */ | |
194 | extern int bitset_ones PARAMS ((bitset)); | |
195 | ||
196 | /* Return non-zero if all bits in bitset SRC are reset. */ | |
197 | extern int bitset_empty_p PARAMS ((bitset)); | |
198 | ||
199 | /* Return DST == DST | SRC. */ | |
200 | extern int bitset_subset_p PARAMS ((bitset, bitset)); | |
201 | ||
202 | /* Return DST == SRC. */ | |
203 | extern int bitset_equal_p PARAMS ((bitset, bitset)); | |
204 | ||
ef017502 AD |
205 | /* Return DST & SRC == 0. */ |
206 | extern int bitset_disjoint_p PARAMS ((bitset, bitset)); | |
207 | ||
7086e707 AD |
208 | /* DST == SRC. */ |
209 | extern int bitset_copy PARAMS ((bitset, bitset)); | |
210 | ||
211 | /* DST = ~SRC. */ | |
212 | extern int bitset_not PARAMS ((bitset, bitset)); | |
213 | ||
214 | /* DST = SRC1 | SRC2. Return non-zero if DST != SRC1 | SRC2. */ | |
215 | extern int bitset_or PARAMS ((bitset, bitset, bitset)); | |
216 | ||
217 | /* DST = SRC1 & SRC2. Return non-zero if DST != SRC1 & SRC2. */ | |
218 | extern int bitset_and PARAMS ((bitset, bitset, bitset)); | |
219 | ||
220 | /* DST = SRC1 ^ SRC2. Return non-zero if DST != SRC1 ^ SRC2. */ | |
221 | extern int bitset_xor PARAMS ((bitset, bitset, bitset)); | |
222 | ||
223 | /* DST = SRC1 & ~SRC2. Return non-zero if DST != SRC1 & ~SRC2. */ | |
224 | extern int bitset_andn PARAMS ((bitset, bitset, bitset)); | |
225 | ||
7086e707 AD |
226 | /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if |
227 | DST != (SRC1 | SRC2) & SRC3. */ | |
228 | extern int bitset_or_and PARAMS ((bitset, bitset, bitset, bitset)); | |
229 | ||
230 | /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if | |
231 | DST != (SRC1 & SRC2) | SRC3. */ | |
232 | extern int bitset_and_or PARAMS ((bitset, bitset, bitset, bitset)); | |
233 | ||
234 | /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if | |
235 | DST != (SRC1 & ~SRC2) | SRC3. */ | |
ef017502 | 236 | extern int bitset_andn_or PARAMS ((bitset, bitset, bitset, bitset)); |
7086e707 AD |
237 | |
238 | /* Find next bit set in BSET starting from and including BITNO. */ | |
239 | extern int bitset_next PARAMS ((bitset, bitset_bindex)); | |
240 | ||
241 | /* Find previous bit set in BSET starting from and including BITNO. */ | |
242 | extern int bitset_prev PARAMS ((bitset, bitset_bindex)); | |
243 | ||
345cea78 AD |
244 | /* Return non-zero if BITNO in SRC is the only set bit. */ |
245 | extern int bitset_only_set_p PARAMS ((bitset, bitset_bindex)); | |
246 | ||
613f5e1a | 247 | /* Find list of up to NUM bits set in BSET starting from and including |
7086e707 AD |
248 | *NEXT. Return with actual number of bits found and with *NEXT |
249 | indicating where search stopped. */ | |
7086e707 | 250 | #define bitset_list(BSET, LIST, NUM, NEXT) \ |
613f5e1a | 251 | BITSET_LIST_ (BSET, LIST, NUM, NEXT) |
7086e707 AD |
252 | |
253 | /* Find reverse list of up to NUM bits set in BSET starting from and | |
254 | including NEXT. Return with actual number of bits found and with | |
255 | *NEXT indicating where search stopped. */ | |
256 | #define bitset_reverse_list(BSET, LIST, NUM, NEXT) \ | |
613f5e1a | 257 | BITSET_REVERSE_LIST_ (BSET, LIST, NUM, NEXT) |
7086e707 AD |
258 | |
259 | /* Find first set bit. */ | |
260 | extern int bitset_first PARAMS ((bitset)); | |
261 | ||
262 | /* Find last set bit. */ | |
263 | extern int bitset_last PARAMS ((bitset)); | |
264 | ||
265 | /* Dump bitset. */ | |
266 | extern void bitset_dump PARAMS ((FILE *, bitset)); | |
267 | ||
613f5e1a AD |
268 | /* Loop over all elements of BSET, starting with MIN, setting BIT |
269 | to the index of each set bit. */ | |
270 | #define BITSET_FOR_EACH(ITER, BSET, BIT, MIN) \ | |
271 | for (ITER.next = (MIN), ITER.num = BITSET_LIST_SIZE; \ | |
272 | (ITER.num == BITSET_LIST_SIZE) \ | |
273 | && (ITER.num = bitset_list (BSET, ITER.list, \ | |
274 | BITSET_LIST_SIZE, &ITER.next));) \ | |
275 | for (ITER.i = 0; (BIT) = ITER.list[ITER.i], ITER.i < ITER.num; ITER.i++) | |
7086e707 AD |
276 | |
277 | ||
278 | /* Loop over all elements of BSET, in reverse order starting with | |
613f5e1a AD |
279 | MIN, setting BIT to the index of each set bit. */ |
280 | #define BITSET_FOR_EACH_REVERSE(ITER, BSET, BIT, MIN) \ | |
281 | for (ITER.next = (MIN), ITER.num = BITSET_LIST_SIZE; \ | |
282 | (ITER.num == BITSET_LIST_SIZE) \ | |
283 | && (ITER.num = bitset_list_reverse (BSET, ITER.list, \ | |
284 | BITSET_LIST_SIZE, &ITER.next));) \ | |
285 | for (ITER.i = 0; (BIT) = ITER.list[ITER.i], ITER.i < ITER.num; ITER.i++) | |
7086e707 AD |
286 | |
287 | ||
ef017502 | 288 | /* Define set operations in terms of logical operations. */ |
7086e707 | 289 | |
613f5e1a | 290 | #define bitset_diff(DST, SRC1, SRC2) bitset_andn (DST, SRC1, SRC2) |
7086e707 | 291 | |
613f5e1a | 292 | #define bitset_intersection(DST, SRC1, SRC2) bitset_and (DST, SRC1, SRC2) |
7086e707 | 293 | |
613f5e1a | 294 | #define bitset_union(DST, SRC1, SRC2) bitset_or (DST, SRC1, SRC2) |
7086e707 AD |
295 | |
296 | #define bitset_diff_union(DST, SRC1, SRC2, SRC3) \ | |
613f5e1a | 297 | bitset_andn_or (DST, SRC1, SRC2, SRC3) |
ef017502 | 298 | |
7086e707 AD |
299 | |
300 | /* Release any memory tied up with bitsets. */ | |
301 | extern void bitset_release_memory PARAMS ((void)); | |
302 | ||
613f5e1a AD |
303 | /* Enable bitset stats gathering. */ |
304 | extern void bitset_stats_enable PARAMS ((void)); | |
305 | ||
306 | /* Disable bitset stats gathering. */ | |
307 | extern void bitset_stats_disable PARAMS ((void)); | |
308 | ||
309 | /* Read bitset stats file of accummulated stats. */ | |
310 | void bitset_stats_read PARAMS ((const char *filename)); | |
311 | ||
312 | /* Write bitset stats file of accummulated stats. */ | |
313 | void bitset_stats_write PARAMS ((const char *filename)); | |
7086e707 AD |
314 | |
315 | /* Dump bitset stats. */ | |
316 | extern void bitset_stats_dump PARAMS ((FILE *)); | |
317 | ||
318 | /* Function to debug bitset from debugger. */ | |
319 | extern void debug_bitset PARAMS ((bitset)); | |
320 | ||
321 | /* Function to debug bitset stats from debugger. */ | |
322 | extern void debug_bitset_stats PARAMS ((void)); | |
323 | ||
7086e707 | 324 | #endif /* _BITSET_H */ |
613f5e1a | 325 |