3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2009, 2010 Free
4 Software Foundation, Inc.
6 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 #include "bitset_stats.h"
34 const char * const bitset_type_names
[] = BITSET_TYPE_NAMES
;
37 /* Return number of bytes required to create a N_BIT bitset
38 of TYPE. The bitset may grow to require more bytes than this. */
40 bitset_bytes (enum bitset_type type
, bitset_bindex n_bits
)
44 if (bitset_stats_enabled
)
45 return bitset_stats_bytes ();
53 bytes
= abitset_bytes (n_bits
);
57 bytes
= lbitset_bytes (n_bits
);
61 bytes
= ebitset_bytes (n_bits
);
65 bytes
= vbitset_bytes (n_bits
);
73 /* Initialise bitset BSET of TYPE for N_BITS. */
75 bitset_init (bitset bset
, bitset_bindex n_bits
, enum bitset_type type
)
77 if (bitset_stats_enabled
)
78 return bitset_stats_init (bset
, n_bits
, type
);
86 return abitset_init (bset
, n_bits
);
89 return lbitset_init (bset
, n_bits
);
92 return ebitset_init (bset
, n_bits
);
95 return vbitset_init (bset
, n_bits
);
100 /* Select a bitset type for a set of N_BITS and with attribute hints
101 specified by ATTR. For variable size bitsets, N_BITS is only a
102 hint and may be zero. */
104 bitset_type_choose (bitset_bindex n_bits ATTRIBUTE_UNUSED
, unsigned int attr
)
106 /* Check attributes. */
107 if (attr
& BITSET_FIXED
&& attr
& BITSET_VARIABLE
)
109 if (attr
& BITSET_SPARSE
&& attr
& BITSET_DENSE
)
112 /* Choose the type of bitset. Note that sometimes we will be asked
113 for a zero length fixed size bitset. */
116 /* If no attributes selected, choose a good compromise. */
118 return BITSET_VARRAY
;
120 if (attr
& BITSET_SPARSE
)
123 if (attr
& BITSET_FIXED
)
126 if (attr
& BITSET_GREEDY
)
129 return BITSET_VARRAY
;
133 /* Create a bitset of N_BITS of type TYPE. */
135 bitset_alloc (bitset_bindex n_bits
, enum bitset_type type
)
140 bytes
= bitset_bytes (type
, n_bits
);
142 bset
= xcalloc (1, bytes
);
144 /* The cache is disabled until some elements are allocated. If we
145 have variable length arrays, then we may need to allocate a dummy
148 return bitset_init (bset
, n_bits
, type
);
152 /* Create a bitset of N_BITS of type TYPE. */
154 bitset_obstack_alloc (struct obstack
*bobstack
,
155 bitset_bindex n_bits
, enum bitset_type type
)
160 bytes
= bitset_bytes (type
, n_bits
);
162 bset
= obstack_alloc (bobstack
, bytes
);
163 memset (bset
, 0, bytes
);
165 return bitset_init (bset
, n_bits
, type
);
169 /* Create a bitset of N_BITS and with attribute hints specified by
172 bitset_create (bitset_bindex n_bits
, unsigned int attr
)
174 enum bitset_type type
;
176 type
= bitset_type_choose (n_bits
, attr
);
178 return bitset_alloc (n_bits
, type
);
182 /* Free bitset BSET. */
184 bitset_free (bitset bset
)
191 /* Free bitset BSET allocated on obstack. */
193 bitset_obstack_free (bitset bset
)
199 /* Return bitset type. */
201 bitset_type_get (bitset bset
)
203 enum bitset_type type
;
205 type
= BITSET_TYPE_ (bset
);
206 if (type
!= BITSET_STATS
)
209 return bitset_stats_type_get (bset
);
213 /* Return name of bitset type. */
215 bitset_type_name_get (bitset bset
)
217 enum bitset_type type
;
219 type
= bitset_type_get (bset
);
221 return bitset_type_names
[type
];
225 /* Find next bit set in SRC starting from and including BITNO.
226 Return BITSET_BINDEX_MAX if SRC empty. */
228 bitset_next (bitset src
, bitset_bindex bitno
)
231 bitset_bindex next
= bitno
;
233 if (!bitset_list (src
, &val
, 1, &next
))
234 return BITSET_BINDEX_MAX
;
239 /* Return true if both bitsets are of the same type and size. */
241 bitset_compatible_p (bitset bset1
, bitset bset2
)
243 return BITSET_COMPATIBLE_ (bset1
, bset2
);
247 /* Find previous bit set in SRC starting from and including BITNO.
248 Return BITSET_BINDEX_MAX if SRC empty. */
250 bitset_prev (bitset src
, bitset_bindex bitno
)
253 bitset_bindex next
= bitno
;
255 if (!bitset_list_reverse (src
, &val
, 1, &next
))
256 return BITSET_BINDEX_MAX
;
261 /* Find first set bit. */
263 bitset_first (bitset src
)
265 return bitset_next (src
, 0);
269 /* Find last set bit. */
271 bitset_last (bitset src
)
273 return bitset_prev (src
, 0);
277 /* Is BITNO in SRC the only set bit? */
279 bitset_only_set_p (bitset src
, bitset_bindex bitno
)
281 bitset_bindex val
[2];
282 bitset_bindex next
= 0;
284 if (bitset_list (src
, val
, 2, &next
) != 1)
286 return val
[0] == bitno
;
290 /* Print contents of bitset BSET to FILE. */
292 bitset_print (FILE *file
, bitset bset
, bool verbose
)
296 bitset_iterator iter
;
299 fprintf (file
, "n_bits = %lu, set = {",
300 (unsigned long int) bitset_size (bset
));
303 BITSET_FOR_EACH (iter
, bset
, i
, 0)
307 fprintf (file
, "\n");
311 fprintf (file
, "%lu ", (unsigned long int) i
);
312 pos
+= 1 + (i
>= 10) + (i
>= 100);
316 fprintf (file
, "}\n");
320 /* Dump bitset BSET to FILE. */
322 bitset_dump (FILE *file
, bitset bset
)
324 bitset_print (file
, bset
, false);
328 /* Release memory associated with bitsets. */
330 bitset_release_memory (void)
332 lbitset_release_memory ();
333 ebitset_release_memory ();
337 /* Toggle bit BITNO in bitset BSET and the new value of the bit. */
339 bitset_toggle_ (bitset bset
, bitset_bindex bitno
)
341 /* This routine is for completeness. It could be optimized if
343 if (bitset_test (bset
, bitno
))
345 bitset_reset (bset
, bitno
);
350 bitset_set (bset
, bitno
);
356 /* Return number of bits in bitset SRC. */
358 bitset_size_ (bitset src
)
360 return BITSET_NBITS_ (src
);
364 /* Return number of bits set in bitset SRC. */
366 bitset_count_ (bitset src
)
368 bitset_bindex list
[BITSET_LIST_SIZE
];
373 /* This could be greatly sped up by adding a count method for each
374 bitset implementation that uses a direct technique (based on
375 masks) for counting the number of bits set in a word. */
378 for (count
= 0; (num
= bitset_list (src
, list
, BITSET_LIST_SIZE
, &next
));
386 /* DST = SRC. Return true if DST != SRC.
387 This is a fallback for the case where SRC and DST are different
390 bitset_copy_ (bitset dst
, bitset src
)
393 bitset_iterator iter
;
395 /* Convert bitset types. We assume that the DST bitset
396 is large enough to hold the SRC bitset. */
398 BITSET_FOR_EACH (iter
, src
, i
, 0)
407 /* This is a fallback for implementations that do not support
408 four operand operations. */
410 bitset_op4_cmp (bitset dst
, bitset src1
, bitset src2
, bitset src3
,
413 bool changed
= false;
414 bool stats_enabled_save
;
417 /* Create temporary bitset. */
418 stats_enabled_save
= bitset_stats_enabled
;
419 bitset_stats_enabled
= false;
420 tmp
= bitset_alloc (0, bitset_type_get (dst
));
421 bitset_stats_enabled
= stats_enabled_save
;
428 case BITSET_OP_OR_AND
:
429 bitset_or (tmp
, src1
, src2
);
430 changed
= bitset_and_cmp (dst
, src3
, tmp
);
433 case BITSET_OP_AND_OR
:
434 bitset_and (tmp
, src1
, src2
);
435 changed
= bitset_or_cmp (dst
, src3
, tmp
);
438 case BITSET_OP_ANDN_OR
:
439 bitset_andn (tmp
, src1
, src2
);
440 changed
= bitset_or_cmp (dst
, src3
, tmp
);
449 /* DST = (SRC1 & SRC2) | SRC3. */
451 bitset_and_or_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
453 bitset_and_or_cmp_ (dst
, src1
, src2
, src3
);
457 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
458 DST != (SRC1 & SRC2) | SRC3. */
460 bitset_and_or_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
462 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_AND_OR
);
466 /* DST = (SRC1 & ~SRC2) | SRC3. */
468 bitset_andn_or_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
470 bitset_andn_or_cmp_ (dst
, src1
, src2
, src3
);
474 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
475 DST != (SRC1 & ~SRC2) | SRC3. */
477 bitset_andn_or_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
479 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_ANDN_OR
);
483 /* DST = (SRC1 | SRC2) & SRC3. */
485 bitset_or_and_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
487 bitset_or_and_cmp_ (dst
, src1
, src2
, src3
);
491 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
492 DST != (SRC1 | SRC2) & SRC3. */
494 bitset_or_and_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
496 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_OR_AND
);
500 /* Function to be called from debugger to print bitset. */
502 debug_bitset (bitset bset
)
505 bitset_print (stderr
, bset
, true);