2 Copyright (C) 2002, 2003, 2004, 2005, 2006 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 2 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, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
29 #include "bitset_stats.h"
32 const char * const bitset_type_names
[] = BITSET_TYPE_NAMES
;
35 /* Return number of bytes required to create a N_BIT bitset
36 of TYPE. The bitset may grow to require more bytes than this. */
38 bitset_bytes (enum bitset_type type
, bitset_bindex n_bits
)
42 if (bitset_stats_enabled
)
43 return bitset_stats_bytes ();
51 bytes
= abitset_bytes (n_bits
);
55 bytes
= lbitset_bytes (n_bits
);
59 bytes
= ebitset_bytes (n_bits
);
63 bytes
= vbitset_bytes (n_bits
);
71 /* Initialise bitset BSET of TYPE for N_BITS. */
73 bitset_init (bitset bset
, bitset_bindex n_bits
, enum bitset_type type
)
75 if (bitset_stats_enabled
)
76 return bitset_stats_init (bset
, n_bits
, type
);
84 return abitset_init (bset
, n_bits
);
87 return lbitset_init (bset
, n_bits
);
90 return ebitset_init (bset
, n_bits
);
93 return vbitset_init (bset
, n_bits
);
98 /* Select a bitset type for a set of N_BITS and with attribute hints
99 specified by ATTR. For variable size bitsets, N_BITS is only a
100 hint and may be zero. */
102 bitset_type_choose (bitset_bindex n_bits ATTRIBUTE_UNUSED
, unsigned int attr
)
104 /* Check attributes. */
105 if (attr
& BITSET_FIXED
&& attr
& BITSET_VARIABLE
)
107 if (attr
& BITSET_SPARSE
&& attr
& BITSET_DENSE
)
110 /* Choose the type of bitset. Note that sometimes we will be asked
111 for a zero length fixed size bitset. */
114 /* If no attributes selected, choose a good compromise. */
116 return BITSET_VARRAY
;
118 if (attr
& BITSET_SPARSE
)
121 if (attr
& BITSET_FIXED
)
124 if (attr
& BITSET_GREEDY
)
127 return BITSET_VARRAY
;
131 /* Create a bitset of N_BITS of type TYPE. */
133 bitset_alloc (bitset_bindex n_bits
, enum bitset_type type
)
138 bytes
= bitset_bytes (type
, n_bits
);
140 bset
= xcalloc (1, bytes
);
142 /* The cache is disabled until some elements are allocated. If we
143 have variable length arrays, then we may need to allocate a dummy
146 return bitset_init (bset
, n_bits
, type
);
150 /* Create a bitset of N_BITS of type TYPE. */
152 bitset_obstack_alloc (struct obstack
*bobstack
,
153 bitset_bindex n_bits
, enum bitset_type type
)
158 bytes
= bitset_bytes (type
, n_bits
);
160 bset
= obstack_alloc (bobstack
, bytes
);
161 memset (bset
, 0, bytes
);
163 return bitset_init (bset
, n_bits
, type
);
167 /* Create a bitset of N_BITS and with attribute hints specified by
170 bitset_create (bitset_bindex n_bits
, unsigned int attr
)
172 enum bitset_type type
;
174 type
= bitset_type_choose (n_bits
, attr
);
176 return bitset_alloc (n_bits
, type
);
180 /* Free bitset BSET. */
182 bitset_free (bitset bset
)
189 /* Free bitset BSET allocated on obstack. */
191 bitset_obstack_free (bitset bset
)
197 /* Return bitset type. */
199 bitset_type_get (bitset bset
)
201 enum bitset_type type
;
203 type
= BITSET_TYPE_ (bset
);
204 if (type
!= BITSET_STATS
)
207 return bitset_stats_type_get (bset
);
211 /* Return name of bitset type. */
213 bitset_type_name_get (bitset bset
)
215 enum bitset_type type
;
217 type
= bitset_type_get (bset
);
219 return bitset_type_names
[type
];
223 /* Find next bit set in SRC starting from and including BITNO.
224 Return BITSET_BINDEX_MAX if SRC empty. */
226 bitset_next (bitset src
, bitset_bindex bitno
)
229 bitset_bindex next
= bitno
;
231 if (!bitset_list (src
, &val
, 1, &next
))
232 return BITSET_BINDEX_MAX
;
237 /* Return true if both bitsets are of the same type and size. */
239 bitset_compatible_p (bitset bset1
, bitset bset2
)
241 return BITSET_COMPATIBLE_ (bset1
, bset2
);
245 /* Find previous bit set in SRC starting from and including BITNO.
246 Return BITSET_BINDEX_MAX if SRC empty. */
248 bitset_prev (bitset src
, bitset_bindex bitno
)
251 bitset_bindex next
= bitno
;
253 if (!bitset_list_reverse (src
, &val
, 1, &next
))
254 return BITSET_BINDEX_MAX
;
259 /* Find first set bit. */
261 bitset_first (bitset src
)
263 return bitset_next (src
, 0);
267 /* Find last set bit. */
269 bitset_last (bitset src
)
271 return bitset_prev (src
, 0);
275 /* Is BITNO in SRC the only set bit? */
277 bitset_only_set_p (bitset src
, bitset_bindex bitno
)
279 bitset_bindex val
[2];
280 bitset_bindex next
= 0;
282 if (bitset_list (src
, val
, 2, &next
) != 1)
284 return val
[0] == bitno
;
288 /* Print contents of bitset BSET to FILE. */
290 bitset_print (FILE *file
, bitset bset
, bool verbose
)
294 bitset_iterator iter
;
297 fprintf (file
, "n_bits = %lu, set = {",
298 (unsigned long int) bitset_size (bset
));
301 BITSET_FOR_EACH (iter
, bset
, i
, 0)
305 fprintf (file
, "\n");
309 fprintf (file
, "%lu ", (unsigned long int) i
);
310 pos
+= 1 + (i
>= 10) + (i
>= 100);
314 fprintf (file
, "}\n");
318 /* Dump bitset BSET to FILE. */
320 bitset_dump (FILE *file
, bitset bset
)
322 bitset_print (file
, bset
, false);
326 /* Release memory associated with bitsets. */
328 bitset_release_memory (void)
330 lbitset_release_memory ();
331 ebitset_release_memory ();
335 /* Toggle bit BITNO in bitset BSET and the new value of the bit. */
337 bitset_toggle_ (bitset bset
, bitset_bindex bitno
)
339 /* This routine is for completeness. It could be optimized if
341 if (bitset_test (bset
, bitno
))
343 bitset_reset (bset
, bitno
);
348 bitset_set (bset
, bitno
);
354 /* Return number of bits in bitset SRC. */
356 bitset_size_ (bitset src
)
358 return BITSET_NBITS_ (src
);
362 /* Return number of bits set in bitset SRC. */
364 bitset_count_ (bitset src
)
366 bitset_bindex list
[BITSET_LIST_SIZE
];
371 /* This could be greatly sped up by adding a count method for each
372 bitset implementation that uses a direct technique (based on
373 masks) for counting the number of bits set in a word. */
376 for (count
= 0; (num
= bitset_list (src
, list
, BITSET_LIST_SIZE
, &next
));
384 /* DST = SRC. Return true if DST != SRC.
385 This is a fallback for the case where SRC and DST are different
388 bitset_copy_ (bitset dst
, bitset src
)
391 bitset_iterator iter
;
393 /* Convert bitset types. We assume that the DST bitset
394 is large enough to hold the SRC bitset. */
396 BITSET_FOR_EACH (iter
, src
, i
, 0)
405 /* This is a fallback for implementations that do not support
406 four operand operations. */
408 bitset_op4_cmp (bitset dst
, bitset src1
, bitset src2
, bitset src3
,
411 bool changed
= false;
412 bool stats_enabled_save
;
415 /* Create temporary bitset. */
416 stats_enabled_save
= bitset_stats_enabled
;
417 bitset_stats_enabled
= false;
418 tmp
= bitset_alloc (0, bitset_type_get (dst
));
419 bitset_stats_enabled
= stats_enabled_save
;
426 case BITSET_OP_OR_AND
:
427 bitset_or (tmp
, src1
, src2
);
428 changed
= bitset_and_cmp (dst
, src3
, tmp
);
431 case BITSET_OP_AND_OR
:
432 bitset_and (tmp
, src1
, src2
);
433 changed
= bitset_or_cmp (dst
, src3
, tmp
);
436 case BITSET_OP_ANDN_OR
:
437 bitset_andn (tmp
, src1
, src2
);
438 changed
= bitset_or_cmp (dst
, src3
, tmp
);
447 /* DST = (SRC1 & SRC2) | SRC3. */
449 bitset_and_or_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
451 bitset_and_or_cmp_ (dst
, src1
, src2
, src3
);
455 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
456 DST != (SRC1 & SRC2) | SRC3. */
458 bitset_and_or_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
460 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_AND_OR
);
464 /* DST = (SRC1 & ~SRC2) | SRC3. */
466 bitset_andn_or_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
468 bitset_andn_or_cmp_ (dst
, src1
, src2
, src3
);
472 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
473 DST != (SRC1 & ~SRC2) | SRC3. */
475 bitset_andn_or_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
477 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_ANDN_OR
);
481 /* DST = (SRC1 | SRC2) & SRC3. */
483 bitset_or_and_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
485 bitset_or_and_cmp_ (dst
, src1
, src2
, src3
);
489 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
490 DST != (SRC1 | SRC2) & SRC3. */
492 bitset_or_and_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
494 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_OR_AND
);
498 /* Function to be called from debugger to print bitset. */
500 debug_bitset (bitset bset
)
503 bitset_print (stderr
, bset
, true);