]>
Commit | Line | Data |
---|---|---|
7086e707 AD |
1 | /* General bitsets. |
2 | Copyright (C) 2002 Free Software Foundation, Inc. | |
3 | Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz). | |
4 | ||
ef017502 AD |
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. | |
7086e707 | 9 | |
ef017502 AD |
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. | |
7086e707 | 14 | |
ef017502 AD |
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. */ | |
7086e707 AD |
18 | |
19 | #ifdef HAVE_CONFIG_H | |
20 | #include "config.h" | |
21 | #endif | |
22 | ||
23 | #include <stdlib.h> | |
24 | #include "bitset.h" | |
613f5e1a | 25 | #include "abitset.h" |
ef017502 AD |
26 | #include "lbitset.h" |
27 | #include "ebitset.h" | |
613f5e1a | 28 | #include "bitset_stats.h" |
7086e707 AD |
29 | #include "obstack.h" |
30 | ||
6aa452a6 AD |
31 | const char * const bitset_type_names[] = BITSET_TYPE_NAMES; |
32 | ||
7086e707 AD |
33 | static void bitset_print PARAMS ((FILE *, bitset, int)); |
34 | ||
7086e707 | 35 | |
7086e707 AD |
36 | /* Return number of bytes required to create a N_BIT bitset |
37 | of TYPE. The bitset may grow to require more bytes than this. */ | |
d32fe6f6 | 38 | size_t |
7086e707 AD |
39 | bitset_bytes (type, n_bits) |
40 | enum bitset_type type; | |
41 | bitset_bindex n_bits; | |
42 | { | |
d32fe6f6 | 43 | size_t bytes; |
7086e707 | 44 | |
613f5e1a AD |
45 | if (bitset_stats_enabled) |
46 | return bitset_stats_bytes (); | |
47 | ||
7086e707 AD |
48 | switch (type) |
49 | { | |
50 | case BITSET_ARRAY: | |
613f5e1a | 51 | bytes = abitset_bytes (n_bits); |
7086e707 AD |
52 | break; |
53 | ||
54 | case BITSET_LIST: | |
55 | bytes = lbitset_bytes (n_bits); | |
56 | break; | |
57 | ||
58 | case BITSET_TABLE: | |
59 | bytes = ebitset_bytes (n_bits); | |
60 | break; | |
61 | ||
62 | default: | |
63 | abort (); | |
64 | } | |
65 | ||
66 | return bytes; | |
67 | } | |
68 | ||
69 | ||
70 | /* Initialise bitset BSET of TYPE for N_BITS. */ | |
71 | bitset | |
72 | bitset_init (bset, n_bits, type) | |
73 | bitset bset; | |
74 | bitset_bindex n_bits; | |
75 | enum bitset_type type; | |
76 | { | |
613f5e1a AD |
77 | if (bitset_stats_enabled) |
78 | return bitset_stats_init (bset, n_bits, type); | |
79 | ||
7086e707 AD |
80 | switch (type) |
81 | { | |
82 | case BITSET_ARRAY: | |
613f5e1a | 83 | return abitset_init (bset, n_bits); |
7086e707 AD |
84 | |
85 | case BITSET_LIST: | |
86 | return lbitset_init (bset, n_bits); | |
87 | ||
88 | case BITSET_TABLE: | |
89 | return ebitset_init (bset, n_bits); | |
90 | ||
91 | default: | |
92 | abort (); | |
93 | } | |
94 | } | |
95 | ||
96 | ||
97 | /* Select a bitset type for a set of N_BITS and with attribute hints | |
98 | specified by ATTR. For variable size bitsets, N_BITS is only a | |
99 | hint and may be zero. */ | |
100 | enum bitset_type | |
101 | bitset_type_choose (n_bits, attr) | |
102 | bitset_bindex n_bits ATTRIBUTE_UNUSED; | |
103 | unsigned int attr; | |
104 | { | |
105 | enum bitset_type type; | |
106 | ||
7086e707 AD |
107 | /* Check attributes. */ |
108 | if (attr & BITSET_FIXED && attr & BITSET_VARIABLE) | |
109 | abort (); | |
110 | if (attr & BITSET_SPARSE && attr & BITSET_DENSE) | |
111 | abort (); | |
112 | ||
6aa452a6 AD |
113 | /* Choose the type of bitset. Note that sometimes we will be asked |
114 | for a zero length fixed size bitset. */ | |
7086e707 AD |
115 | |
116 | type = BITSET_ARRAY; | |
117 | /* Currently, the simple bitsets do not support a variable size. */ | |
118 | if (attr & BITSET_VARIABLE || attr & BITSET_SPARSE) | |
119 | { | |
120 | type = BITSET_LIST; | |
121 | if (attr & BITSET_DENSE || attr & BITSET_GREEDY) | |
122 | type = BITSET_TABLE; | |
123 | } | |
124 | ||
125 | return type; | |
126 | } | |
127 | ||
128 | ||
129 | /* Create a bitset of N_BITS of type TYPE. */ | |
130 | bitset | |
131 | bitset_alloc (n_bits, type) | |
132 | bitset_bindex n_bits; | |
133 | enum bitset_type type; | |
134 | { | |
d32fe6f6 | 135 | size_t bytes; |
7086e707 AD |
136 | bitset bset; |
137 | ||
7086e707 AD |
138 | bytes = bitset_bytes (type, n_bits); |
139 | ||
ef017502 AD |
140 | bset = (bitset) xcalloc (1, bytes); |
141 | ||
142 | /* The cache is disabled until some elements are allocated. If we | |
613f5e1a | 143 | have variable length arrays, then we may need to allocate a dummy |
ef017502 | 144 | element. */ |
7086e707 AD |
145 | |
146 | return bitset_init (bset, n_bits, type); | |
147 | } | |
148 | ||
149 | ||
150 | /* Create a bitset of N_BITS of type TYPE. */ | |
151 | bitset | |
152 | bitset_obstack_alloc (bobstack, n_bits, type) | |
ef017502 AD |
153 | struct obstack *bobstack; |
154 | bitset_bindex n_bits; | |
155 | enum bitset_type type; | |
7086e707 | 156 | { |
d32fe6f6 | 157 | size_t bytes; |
ef017502 | 158 | bitset bset; |
7086e707 | 159 | |
7086e707 AD |
160 | bytes = bitset_bytes (type, n_bits); |
161 | ||
ef017502 AD |
162 | bset = obstack_alloc (bobstack, bytes); |
163 | memset (bset, 0, bytes); | |
164 | ||
165 | return bitset_init (bset, n_bits, type); | |
7086e707 AD |
166 | } |
167 | ||
168 | ||
169 | /* Create a bitset of N_BITS and with attribute hints specified by | |
170 | ATTR. */ | |
171 | bitset | |
172 | bitset_create (n_bits, attr) | |
173 | bitset_bindex n_bits; | |
174 | unsigned int attr; | |
175 | { | |
176 | enum bitset_type type; | |
177 | ||
178 | type = bitset_type_choose (n_bits, attr); | |
179 | ||
180 | return bitset_alloc (n_bits, type); | |
181 | } | |
182 | ||
183 | ||
184 | /* Free bitset BSET. */ | |
185 | void | |
186 | bitset_free (bset) | |
187 | bitset bset; | |
188 | { | |
ef017502 | 189 | BITSET_FREE_ (bset); |
7086e707 AD |
190 | free (bset); |
191 | } | |
192 | ||
193 | ||
194 | /* Free bitset BSET allocated on obstack. */ | |
195 | void | |
196 | bitset_obstack_free (bset) | |
197 | bitset bset; | |
198 | { | |
ef017502 | 199 | BITSET_FREE_ (bset); |
7086e707 AD |
200 | } |
201 | ||
202 | ||
613f5e1a AD |
203 | /* Return bitset type. */ |
204 | enum bitset_type | |
205 | bitset_type_get (bset) | |
206 | bitset bset; | |
207 | { | |
208 | enum bitset_type type; | |
209 | ||
210 | type = BITSET_TYPE_ (bset); | |
211 | if (type != BITSET_STATS) | |
212 | return type; | |
213 | ||
214 | return bitset_stats_type_get (bset); | |
215 | } | |
216 | ||
217 | ||
6aa452a6 AD |
218 | /* Return name of bitset type. */ |
219 | const char * | |
220 | bitset_type_name_get (bset) | |
221 | bitset bset; | |
222 | { | |
223 | enum bitset_type type; | |
224 | ||
225 | type = bitset_type_get (bset); | |
226 | ||
227 | return bitset_type_names[type]; | |
228 | } | |
229 | ||
230 | ||
7086e707 | 231 | /* Find next bit set in SRC starting from and including BITNO. |
d32fe6f6 PE |
232 | Return BITSET_BINDEX_MAX if SRC empty. */ |
233 | bitset_bindex | |
7086e707 AD |
234 | bitset_next (src, bitno) |
235 | bitset src; | |
236 | bitset_bindex bitno; | |
237 | { | |
238 | bitset_bindex val; | |
239 | bitset_bindex next = bitno; | |
240 | ||
241 | if (!bitset_list (src, &val, 1, &next)) | |
d32fe6f6 | 242 | return BITSET_BINDEX_MAX; |
7086e707 AD |
243 | return val; |
244 | } | |
245 | ||
246 | ||
247 | /* Find previous bit set in SRC starting from and including BITNO. | |
d32fe6f6 PE |
248 | Return BITSET_BINDEX_MAX if SRC empty. */ |
249 | bitset_bindex | |
7086e707 AD |
250 | bitset_prev (src, bitno) |
251 | bitset src; | |
252 | bitset_bindex bitno; | |
253 | { | |
254 | bitset_bindex val; | |
255 | bitset_bindex next = bitno; | |
256 | ||
6aa452a6 | 257 | if (!bitset_list_reverse (src, &val, 1, &next)) |
d32fe6f6 | 258 | return BITSET_BINDEX_MAX; |
7086e707 AD |
259 | return val; |
260 | } | |
261 | ||
262 | ||
263 | /* Find first set bit. */ | |
d32fe6f6 | 264 | bitset_bindex |
7086e707 AD |
265 | bitset_first (src) |
266 | bitset src; | |
267 | { | |
268 | return bitset_next (src, 0); | |
269 | } | |
270 | ||
271 | ||
272 | /* Find last set bit. */ | |
d32fe6f6 | 273 | bitset_bindex |
7086e707 AD |
274 | bitset_last (src) |
275 | bitset src; | |
276 | { | |
277 | return bitset_prev (src, 0); | |
278 | } | |
279 | ||
280 | ||
345cea78 AD |
281 | /* Return non-zero if BITNO in SRC is the only set bit. */ |
282 | int | |
283 | bitset_only_set_p (src, bitno) | |
284 | bitset src; | |
285 | bitset_bindex bitno; | |
286 | { | |
287 | bitset_bindex val[2]; | |
288 | bitset_bindex next = 0; | |
289 | ||
290 | if (bitset_list (src, val, 2, &next) != 1) | |
291 | return 0; | |
292 | return val[0] == bitno; | |
293 | } | |
294 | ||
295 | ||
ef017502 | 296 | /* Print contents of bitset BSET to FILE. */ |
7086e707 AD |
297 | static void |
298 | bitset_print (file, bset, verbose) | |
299 | FILE *file; | |
300 | bitset bset; | |
301 | int verbose; | |
302 | { | |
6aa452a6 AD |
303 | unsigned int pos; |
304 | bitset_bindex i; | |
613f5e1a | 305 | bitset_iterator iter; |
7086e707 AD |
306 | |
307 | if (verbose) | |
d32fe6f6 PE |
308 | fprintf (file, "n_bits = %lu, set = {", |
309 | (unsigned long) bitset_size (bset)); | |
7086e707 AD |
310 | |
311 | pos = 30; | |
613f5e1a | 312 | BITSET_FOR_EACH (iter, bset, i, 0) |
7086e707 AD |
313 | { |
314 | if (pos > 70) | |
315 | { | |
316 | fprintf (file, "\n"); | |
317 | pos = 0; | |
318 | } | |
319 | ||
320 | fprintf (file, "%d ", i); | |
321 | pos += 1 + (i >= 10) + (i >= 100); | |
613f5e1a | 322 | }; |
7086e707 AD |
323 | |
324 | if (verbose) | |
325 | fprintf (file, "}\n"); | |
326 | } | |
327 | ||
328 | ||
6aa452a6 AD |
329 | /* Dump bitset BSET to FILE. */ |
330 | void | |
331 | bitset_dump (file, bset) | |
332 | FILE *file; | |
333 | bitset bset; | |
7086e707 | 334 | { |
6aa452a6 AD |
335 | bitset_print (file, bset, 0); |
336 | } | |
7086e707 | 337 | |
7086e707 | 338 | |
7086e707 | 339 | |
6aa452a6 AD |
340 | /* Release memory associated with bitsets. */ |
341 | void | |
342 | bitset_release_memory () | |
343 | { | |
344 | lbitset_release_memory (); | |
345 | ebitset_release_memory (); | |
7086e707 AD |
346 | } |
347 | ||
348 | ||
6aa452a6 AD |
349 | |
350 | /* Toggle bit BITNO in bitset BSET and return non-zero if not set. */ | |
7086e707 | 351 | int |
6aa452a6 AD |
352 | bitset_toggle_ (bset, bitno) |
353 | bitset bset; | |
354 | bitset_bindex bitno; | |
ef017502 | 355 | { |
6aa452a6 AD |
356 | /* This routine is for completeness. It could be optimized if |
357 | required. */ | |
358 | if (bitset_test (bset, bitno)) | |
359 | { | |
360 | bitset_reset (bset, bitno); | |
361 | return 0; | |
362 | } | |
363 | else | |
364 | { | |
365 | bitset_set (bset, bitno); | |
366 | return 1; | |
367 | } | |
ef017502 AD |
368 | } |
369 | ||
370 | ||
371 | /* Return number of bits set in bitset SRC. */ | |
d32fe6f6 | 372 | bitset_bindex |
6aa452a6 | 373 | bitset_count_ (src) |
ef017502 | 374 | bitset src; |
7086e707 | 375 | { |
ef017502 AD |
376 | bitset_bindex list[BITSET_LIST_SIZE]; |
377 | bitset_bindex next; | |
d32fe6f6 PE |
378 | bitset_bindex num; |
379 | bitset_bindex count; | |
ef017502 | 380 | |
613f5e1a AD |
381 | /* This could be greatly sped up by adding a count method for each |
382 | bitset implementation that uses a direct technique (based on | |
383 | masks) for counting the number of bits set in a word. */ | |
384 | ||
ef017502 AD |
385 | next = 0; |
386 | for (count = 0; (num = bitset_list (src, list, BITSET_LIST_SIZE, &next)); | |
387 | count += num) | |
388 | continue; | |
389 | ||
390 | return count; | |
7086e707 AD |
391 | } |
392 | ||
393 | ||
6aa452a6 AD |
394 | /* DST = SRC. Return non-zero if DST != SRC. |
395 | This is a fallback for the case where SRC and DST are different | |
396 | bitset types. */ | |
ef017502 | 397 | int |
6aa452a6 | 398 | bitset_copy_ (dst, src) |
ef017502 AD |
399 | bitset dst; |
400 | bitset src; | |
401 | { | |
6aa452a6 AD |
402 | bitset_bindex i; |
403 | bitset_iterator iter; | |
7086e707 | 404 | |
6aa452a6 AD |
405 | /* Convert bitset types. We assume that the DST bitset |
406 | is large enough to hold the SRC bitset. */ | |
407 | bitset_zero (dst); | |
408 | BITSET_FOR_EACH (iter, src, i, 0) | |
409 | { | |
410 | bitset_set (dst, i); | |
411 | }; | |
7086e707 | 412 | |
6aa452a6 | 413 | return 1; |
7086e707 AD |
414 | } |
415 | ||
416 | ||
613f5e1a AD |
417 | /* This is a fallback for implementations that do not support |
418 | four operand operations. */ | |
6aa452a6 AD |
419 | static inline int |
420 | bitset_op4_cmp (dst, src1, src2, src3, op) | |
ef017502 AD |
421 | bitset dst; |
422 | bitset src1; | |
423 | bitset src2; | |
424 | bitset src3; | |
425 | enum bitset_ops op; | |
426 | { | |
427 | int changed = 0; | |
6aa452a6 | 428 | int stats_enabled_save; |
ef017502 AD |
429 | bitset tmp; |
430 | ||
431 | /* Create temporary bitset. */ | |
6aa452a6 AD |
432 | stats_enabled_save = bitset_stats_enabled; |
433 | bitset_stats_enabled = 0; | |
613f5e1a | 434 | tmp = bitset_alloc (0, bitset_type_get (dst)); |
6aa452a6 | 435 | bitset_stats_enabled = stats_enabled_save; |
ef017502 AD |
436 | |
437 | switch (op) | |
438 | { | |
439 | case BITSET_OP_OR_AND: | |
6aa452a6 AD |
440 | bitset_or (tmp, src1, src2); |
441 | changed = bitset_and_cmp (dst, src3, tmp); | |
ef017502 AD |
442 | break; |
443 | ||
444 | case BITSET_OP_AND_OR: | |
6aa452a6 AD |
445 | bitset_and (tmp, src1, src2); |
446 | changed = bitset_or_cmp (dst, src3, tmp); | |
ef017502 AD |
447 | break; |
448 | ||
449 | case BITSET_OP_ANDN_OR: | |
6aa452a6 AD |
450 | bitset_andn (tmp, src1, src2); |
451 | changed = bitset_or_cmp (dst, src3, tmp); | |
ef017502 AD |
452 | break; |
453 | ||
454 | default: | |
455 | abort (); | |
456 | } | |
457 | ||
458 | bitset_free (tmp); | |
459 | return changed; | |
7086e707 AD |
460 | } |
461 | ||
462 | ||
6aa452a6 AD |
463 | /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if |
464 | DST != (SRC1 & SRC2) | SRC3. */ | |
7086e707 | 465 | int |
6aa452a6 | 466 | bitset_and_or_cmp_ (dst, src1, src2, src3) |
ef017502 AD |
467 | bitset dst; |
468 | bitset src1; | |
469 | bitset src2; | |
470 | bitset src3; | |
7086e707 | 471 | { |
6aa452a6 | 472 | return bitset_op4_cmp (dst, src1, src2, src3, BITSET_OP_AND_OR); |
7086e707 AD |
473 | } |
474 | ||
475 | ||
6aa452a6 AD |
476 | /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if |
477 | DST != (SRC1 & ~SRC2) | SRC3. */ | |
7086e707 | 478 | int |
6aa452a6 | 479 | bitset_andn_or_cmp_ (dst, src1, src2, src3) |
ef017502 AD |
480 | bitset dst; |
481 | bitset src1; | |
482 | bitset src2; | |
483 | bitset src3; | |
484 | { | |
6aa452a6 | 485 | return bitset_op4_cmp (dst, src1, src2, src3, BITSET_OP_ANDN_OR); |
ef017502 AD |
486 | } |
487 | ||
488 | ||
6aa452a6 AD |
489 | /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if |
490 | DST != (SRC1 | SRC2) & SRC3. */ | |
ef017502 | 491 | int |
6aa452a6 | 492 | bitset_or_and_cmp_ (dst, src1, src2, src3) |
ef017502 AD |
493 | bitset dst; |
494 | bitset src1; | |
495 | bitset src2; | |
496 | bitset src3; | |
7086e707 | 497 | { |
6aa452a6 | 498 | return bitset_op4_cmp (dst, src1, src2, src3, BITSET_OP_OR_AND); |
7086e707 AD |
499 | } |
500 | ||
501 | ||
502 | /* Function to be called from debugger to print bitset. */ | |
503 | void | |
504 | debug_bitset (bset) | |
505 | bitset bset; | |
506 | { | |
ef017502 AD |
507 | if (bset) |
508 | bitset_print (stderr, bset, 1); | |
7086e707 | 509 | } |