3 Copyright (C) 2002-2006, 2009-2015, 2018-2024 Free Software Foundation, Inc.
5 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <https://www.gnu.org/licenses/>. */
29 #include "bitset/array.h"
30 #include "bitset/list.h"
31 #include "bitset/stats.h"
32 #include "bitset/table.h"
33 #include "bitset/vector.h"
35 const char * const bitset_type_names
[] = BITSET_TYPE_NAMES
;
38 /* Return number of bytes required to create a N_BIT bitset
39 of TYPE. The bitset may grow to require more bytes than this. */
41 bitset_bytes (enum bitset_type type
, bitset_bindex n_bits
)
43 if (bitset_stats_enabled
)
44 return bitset_stats_bytes ();
52 return abitset_bytes (n_bits
);
55 return lbitset_bytes (n_bits
);
58 return tbitset_bytes (n_bits
);
61 return vbitset_bytes (n_bits
);
66 /* Initialise bitset BSET of TYPE for N_BITS. */
68 bitset_init (bitset bset
, bitset_bindex n_bits
, enum bitset_type type
)
70 if (bitset_stats_enabled
)
71 return bitset_stats_init (bset
, n_bits
, type
);
79 return abitset_init (bset
, n_bits
);
82 return lbitset_init (bset
, n_bits
);
85 return tbitset_init (bset
, n_bits
);
88 return vbitset_init (bset
, n_bits
);
93 /* Select a bitset type for a set of N_BITS and with attribute hints
94 specified by ATTR. For variable size bitsets, N_BITS is only a
95 hint and may be zero. */
97 bitset_type_choose (MAYBE_UNUSED bitset_bindex n_bits
, unsigned attr
)
99 /* Check attributes. */
100 if (attr
& BITSET_FIXED
&& attr
& BITSET_VARIABLE
)
102 if (attr
& BITSET_SPARSE
&& attr
& BITSET_DENSE
)
105 /* Choose the type of bitset. Note that sometimes we will be asked
106 for a zero length fixed size bitset. */
109 /* If no attributes selected, choose a good compromise. */
111 return BITSET_VECTOR
;
113 if (attr
& BITSET_SPARSE
)
116 if (attr
& BITSET_FIXED
)
119 if (attr
& BITSET_GREEDY
)
122 return BITSET_VECTOR
;
126 /* Create a bitset of N_BITS of type TYPE. */
128 bitset_alloc (bitset_bindex n_bits
, enum bitset_type type
)
130 size_t bytes
= bitset_bytes (type
, n_bits
);
132 bitset bset
= xzalloc (bytes
);
134 /* The cache is disabled until some elements are allocated. If we
135 have variable length arrays, then we may need to allocate a dummy
138 return bitset_init (bset
, n_bits
, type
);
142 /* Create a bitset of N_BITS of type TYPE. */
144 bitset_obstack_alloc (struct obstack
*bobstack
,
145 bitset_bindex n_bits
, enum bitset_type type
)
147 size_t bytes
= bitset_bytes (type
, n_bits
);
149 bitset bset
= obstack_alloc (bobstack
, bytes
);
150 memset (bset
, 0, bytes
);
152 return bitset_init (bset
, n_bits
, type
);
156 /* Create a bitset of N_BITS and with attribute hints specified by
159 bitset_create (bitset_bindex n_bits
, unsigned attr
)
161 enum bitset_type type
= bitset_type_choose (n_bits
, attr
);
163 return bitset_alloc (n_bits
, type
);
167 /* Free bitset BSET. */
169 bitset_free (bitset bset
)
179 /* Free bitset BSET allocated on obstack. */
181 bitset_obstack_free (bitset bset
)
188 /* Return bitset type. */
190 bitset_type_get (bitset bset
)
192 enum bitset_type type
= BITSET_TYPE_ (bset
);
193 if (type
!= BITSET_STATS
)
196 return bitset_stats_type_get (bset
);
200 /* Return name of bitset type. */
202 bitset_type_name_get (bitset bset
)
204 enum bitset_type type
= bitset_type_get (bset
);
206 return bitset_type_names
[type
];
211 bitset_next (bitset src
, bitset_bindex bitno
)
213 bitset_bindex next
= bitno
;
215 if (!bitset_list (src
, &val
, 1, &next
))
216 return BITSET_BINDEX_MAX
;
221 /* Return true if both bitsets are of the same type and size. */
223 bitset_compatible_p (bitset bset1
, bitset bset2
)
225 return BITSET_COMPATIBLE_ (bset1
, bset2
);
230 bitset_prev (bitset src
, bitset_bindex bitno
)
232 bitset_bindex next
= bitno
;
234 if (!bitset_list_reverse (src
, &val
, 1, &next
))
235 return BITSET_BINDEX_MAX
;
240 /* Find first set bit. */
242 bitset_first (bitset src
)
244 return bitset_next (src
, 0);
248 /* Find last set bit. */
250 bitset_last (bitset src
)
252 return bitset_prev (src
, 0);
256 /* Is BITNO in SRC the only set bit? */
258 bitset_only_set_p (bitset src
, bitset_bindex bitno
)
260 bitset_bindex val
[2];
261 bitset_bindex next
= 0;
263 if (bitset_list (src
, val
, 2, &next
) != 1)
265 return val
[0] == bitno
;
269 /* Print contents of bitset BSET to FILE. */
271 bitset_print (FILE *file
, bitset bset
, bool verbose
)
274 fprintf (file
, "%s{n_bits = %lu, set = {",
275 bitset_type_name_get (bset
),
276 (unsigned long) bitset_size (bset
));
280 bitset_iterator iter
;
281 BITSET_FOR_EACH (iter
, bset
, i
, 0)
285 fprintf (file
, "\n");
289 fprintf (file
, "%lu ", (unsigned long) i
);
290 pos
+= 1 + (i
>= 10) + (i
>= 100);
294 fprintf (file
, "}}\n");
298 /* Dump bitset BSET to FILE. */
300 bitset_dump (FILE *file
, bitset bset
)
302 bitset_print (file
, bset
, false);
306 /* Release memory associated with bitsets. */
308 bitset_release_memory (void)
310 lbitset_release_memory ();
311 tbitset_release_memory ();
315 /* Toggle bit BITNO in bitset BSET and the new value of the bit. */
317 bitset_toggle_ (bitset bset
, bitset_bindex bitno
)
319 /* This routine is for completeness. It could be optimized if
321 if (bitset_test (bset
, bitno
))
323 bitset_reset (bset
, bitno
);
328 bitset_set (bset
, bitno
);
334 /* Return number of bits in bitset SRC. */
336 bitset_size_ (bitset src
)
338 return BITSET_NBITS_ (src
);
342 /* Return number of bits set in bitset SRC. */
344 bitset_count_ (bitset src
)
346 bitset_bindex list
[BITSET_LIST_SIZE
];
347 bitset_bindex count
= 0;
349 /* This could be greatly sped up by adding a count method for each
350 bitset implementation that uses a direct technique (based on
351 masks) for counting the number of bits set in a word. */
354 bitset_bindex next
= 0;
356 while ((num
= bitset_list (src
, list
, BITSET_LIST_SIZE
, &next
)))
364 /* DST = SRC. Return true if DST != SRC.
365 This is a fallback for the case where SRC and DST are different
368 bitset_copy_ (bitset dst
, bitset src
)
371 bitset_iterator iter
;
373 /* Convert bitset types. We assume that the DST bitset
374 is large enough to hold the SRC bitset. */
376 BITSET_FOR_EACH (iter
, src
, i
, 0)
383 /* This is a fallback for implementations that do not support
384 four operand operations. */
386 bitset_op4_cmp (bitset dst
, bitset src1
, bitset src2
, bitset src3
,
389 bool changed
= false;
391 /* Create temporary bitset. */
392 bool stats_enabled_save
= bitset_stats_enabled
;
393 bitset_stats_enabled
= false;
394 bitset tmp
= bitset_alloc (0, bitset_type_get (dst
));
395 bitset_stats_enabled
= stats_enabled_save
;
402 case BITSET_OP_OR_AND
:
403 bitset_or (tmp
, src1
, src2
);
404 changed
= bitset_and_cmp (dst
, src3
, tmp
);
407 case BITSET_OP_AND_OR
:
408 bitset_and (tmp
, src1
, src2
);
409 changed
= bitset_or_cmp (dst
, src3
, tmp
);
412 case BITSET_OP_ANDN_OR
:
413 bitset_andn (tmp
, src1
, src2
);
414 changed
= bitset_or_cmp (dst
, src3
, tmp
);
423 /* DST = (SRC1 & SRC2) | SRC3. */
425 bitset_and_or_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
427 bitset_and_or_cmp_ (dst
, src1
, src2
, src3
);
431 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
432 DST != (SRC1 & SRC2) | SRC3. */
434 bitset_and_or_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
436 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_AND_OR
);
440 /* DST = (SRC1 & ~SRC2) | SRC3. */
442 bitset_andn_or_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
444 bitset_andn_or_cmp_ (dst
, src1
, src2
, src3
);
448 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
449 DST != (SRC1 & ~SRC2) | SRC3. */
451 bitset_andn_or_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
453 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_ANDN_OR
);
457 /* DST = (SRC1 | SRC2) & SRC3. */
459 bitset_or_and_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
461 bitset_or_and_cmp_ (dst
, src1
, src2
, src3
);
465 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
466 DST != (SRC1 | SRC2) & SRC3. */
468 bitset_or_and_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
470 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_OR_AND
);
474 /* Function to be called from debugger to print bitset. */
476 debug_bitset (bitset bset
)
479 bitset_print (stderr
, bset
, true);