2 Copyright (C) 2002-2006, 2009-2010 Free Software Foundation, Inc.
3 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
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 3 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "bitset_stats.h"
31 const char * const bitset_type_names
[] = BITSET_TYPE_NAMES
;
34 /* Return number of bytes required to create a N_BIT bitset
35 of TYPE. The bitset may grow to require more bytes than this. */
37 bitset_bytes (enum bitset_type type
, bitset_bindex n_bits
)
41 if (bitset_stats_enabled
)
42 return bitset_stats_bytes ();
50 bytes
= abitset_bytes (n_bits
);
54 bytes
= lbitset_bytes (n_bits
);
58 bytes
= ebitset_bytes (n_bits
);
62 bytes
= vbitset_bytes (n_bits
);
70 /* Initialise bitset BSET of TYPE for N_BITS. */
72 bitset_init (bitset bset
, bitset_bindex n_bits
, enum bitset_type type
)
74 if (bitset_stats_enabled
)
75 return bitset_stats_init (bset
, n_bits
, type
);
83 return abitset_init (bset
, n_bits
);
86 return lbitset_init (bset
, n_bits
);
89 return ebitset_init (bset
, n_bits
);
92 return vbitset_init (bset
, n_bits
);
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. */
101 bitset_type_choose (bitset_bindex n_bits ATTRIBUTE_UNUSED
, unsigned int attr
)
103 /* Check attributes. */
104 if (attr
& BITSET_FIXED
&& attr
& BITSET_VARIABLE
)
106 if (attr
& BITSET_SPARSE
&& attr
& BITSET_DENSE
)
109 /* Choose the type of bitset. Note that sometimes we will be asked
110 for a zero length fixed size bitset. */
113 /* If no attributes selected, choose a good compromise. */
115 return BITSET_VARRAY
;
117 if (attr
& BITSET_SPARSE
)
120 if (attr
& BITSET_FIXED
)
123 if (attr
& BITSET_GREEDY
)
126 return BITSET_VARRAY
;
130 /* Create a bitset of N_BITS of type TYPE. */
132 bitset_alloc (bitset_bindex n_bits
, enum bitset_type type
)
137 bytes
= bitset_bytes (type
, n_bits
);
139 bset
= xcalloc (1, bytes
);
141 /* The cache is disabled until some elements are allocated. If we
142 have variable length arrays, then we may need to allocate a dummy
145 return bitset_init (bset
, n_bits
, type
);
149 /* Create a bitset of N_BITS of type TYPE. */
151 bitset_obstack_alloc (struct obstack
*bobstack
,
152 bitset_bindex n_bits
, enum bitset_type type
)
157 bytes
= bitset_bytes (type
, n_bits
);
159 bset
= obstack_alloc (bobstack
, bytes
);
160 memset (bset
, 0, bytes
);
162 return bitset_init (bset
, n_bits
, type
);
166 /* Create a bitset of N_BITS and with attribute hints specified by
169 bitset_create (bitset_bindex n_bits
, unsigned int attr
)
171 enum bitset_type type
;
173 type
= bitset_type_choose (n_bits
, attr
);
175 return bitset_alloc (n_bits
, type
);
179 /* Free bitset BSET. */
181 bitset_free (bitset bset
)
188 /* Free bitset BSET allocated on obstack. */
190 bitset_obstack_free (bitset bset
)
196 /* Return bitset type. */
198 bitset_type_get (bitset bset
)
200 enum bitset_type type
;
202 type
= BITSET_TYPE_ (bset
);
203 if (type
!= BITSET_STATS
)
206 return bitset_stats_type_get (bset
);
210 /* Return name of bitset type. */
212 bitset_type_name_get (bitset bset
)
214 enum bitset_type type
;
216 type
= bitset_type_get (bset
);
218 return bitset_type_names
[type
];
222 /* Find next bit set in SRC starting from and including BITNO.
223 Return BITSET_BINDEX_MAX if SRC empty. */
225 bitset_next (bitset src
, bitset_bindex bitno
)
228 bitset_bindex next
= bitno
;
230 if (!bitset_list (src
, &val
, 1, &next
))
231 return BITSET_BINDEX_MAX
;
236 /* Return true if both bitsets are of the same type and size. */
238 bitset_compatible_p (bitset bset1
, bitset bset2
)
240 return BITSET_COMPATIBLE_ (bset1
, bset2
);
244 /* Find previous bit set in SRC starting from and including BITNO.
245 Return BITSET_BINDEX_MAX if SRC empty. */
247 bitset_prev (bitset src
, bitset_bindex bitno
)
250 bitset_bindex next
= bitno
;
252 if (!bitset_list_reverse (src
, &val
, 1, &next
))
253 return BITSET_BINDEX_MAX
;
258 /* Find first set bit. */
260 bitset_first (bitset src
)
262 return bitset_next (src
, 0);
266 /* Find last set bit. */
268 bitset_last (bitset src
)
270 return bitset_prev (src
, 0);
274 /* Is BITNO in SRC the only set bit? */
276 bitset_only_set_p (bitset src
, bitset_bindex bitno
)
278 bitset_bindex val
[2];
279 bitset_bindex next
= 0;
281 if (bitset_list (src
, val
, 2, &next
) != 1)
283 return val
[0] == bitno
;
287 /* Print contents of bitset BSET to FILE. */
289 bitset_print (FILE *file
, bitset bset
, bool verbose
)
293 bitset_iterator iter
;
296 fprintf (file
, "n_bits = %lu, set = {",
297 (unsigned long int) bitset_size (bset
));
300 BITSET_FOR_EACH (iter
, bset
, i
, 0)
304 fprintf (file
, "\n");
308 fprintf (file
, "%lu ", (unsigned long int) i
);
309 pos
+= 1 + (i
>= 10) + (i
>= 100);
313 fprintf (file
, "}\n");
317 /* Dump bitset BSET to FILE. */
319 bitset_dump (FILE *file
, bitset bset
)
321 bitset_print (file
, bset
, false);
325 /* Release memory associated with bitsets. */
327 bitset_release_memory (void)
329 lbitset_release_memory ();
330 ebitset_release_memory ();
334 /* Toggle bit BITNO in bitset BSET and the new value of the bit. */
336 bitset_toggle_ (bitset bset
, bitset_bindex bitno
)
338 /* This routine is for completeness. It could be optimized if
340 if (bitset_test (bset
, bitno
))
342 bitset_reset (bset
, bitno
);
347 bitset_set (bset
, bitno
);
353 /* Return number of bits in bitset SRC. */
355 bitset_size_ (bitset src
)
357 return BITSET_NBITS_ (src
);
361 /* Return number of bits set in bitset SRC. */
363 bitset_count_ (bitset src
)
365 bitset_bindex list
[BITSET_LIST_SIZE
];
370 /* This could be greatly sped up by adding a count method for each
371 bitset implementation that uses a direct technique (based on
372 masks) for counting the number of bits set in a word. */
375 for (count
= 0; (num
= bitset_list (src
, list
, BITSET_LIST_SIZE
, &next
));
383 /* DST = SRC. Return true if DST != SRC.
384 This is a fallback for the case where SRC and DST are different
387 bitset_copy_ (bitset dst
, bitset src
)
390 bitset_iterator iter
;
392 /* Convert bitset types. We assume that the DST bitset
393 is large enough to hold the SRC bitset. */
395 BITSET_FOR_EACH (iter
, src
, i
, 0)
404 /* This is a fallback for implementations that do not support
405 four operand operations. */
407 bitset_op4_cmp (bitset dst
, bitset src1
, bitset src2
, bitset src3
,
410 bool changed
= false;
411 bool stats_enabled_save
;
414 /* Create temporary bitset. */
415 stats_enabled_save
= bitset_stats_enabled
;
416 bitset_stats_enabled
= false;
417 tmp
= bitset_alloc (0, bitset_type_get (dst
));
418 bitset_stats_enabled
= stats_enabled_save
;
425 case BITSET_OP_OR_AND
:
426 bitset_or (tmp
, src1
, src2
);
427 changed
= bitset_and_cmp (dst
, src3
, tmp
);
430 case BITSET_OP_AND_OR
:
431 bitset_and (tmp
, src1
, src2
);
432 changed
= bitset_or_cmp (dst
, src3
, tmp
);
435 case BITSET_OP_ANDN_OR
:
436 bitset_andn (tmp
, src1
, src2
);
437 changed
= bitset_or_cmp (dst
, src3
, tmp
);
446 /* DST = (SRC1 & SRC2) | SRC3. */
448 bitset_and_or_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
450 bitset_and_or_cmp_ (dst
, src1
, src2
, src3
);
454 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
455 DST != (SRC1 & SRC2) | SRC3. */
457 bitset_and_or_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
459 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_AND_OR
);
463 /* DST = (SRC1 & ~SRC2) | SRC3. */
465 bitset_andn_or_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
467 bitset_andn_or_cmp_ (dst
, src1
, src2
, src3
);
471 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
472 DST != (SRC1 & ~SRC2) | SRC3. */
474 bitset_andn_or_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
476 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_ANDN_OR
);
480 /* DST = (SRC1 | SRC2) & SRC3. */
482 bitset_or_and_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
484 bitset_or_and_cmp_ (dst
, src1
, src2
, src3
);
488 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
489 DST != (SRC1 | SRC2) & SRC3. */
491 bitset_or_and_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
493 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_OR_AND
);
497 /* Function to be called from debugger to print bitset. */
499 debug_bitset (bitset bset
)
502 bitset_print (stderr
, bset
, true);