2 Copyright (C) 2002 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
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #include "bitset_stats.h"
31 const char * const bitset_type_names
[] = BITSET_TYPE_NAMES
;
33 static void bitset_print
PARAMS ((FILE *, bitset
, int));
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. */
39 bitset_bytes (type
, n_bits
)
40 enum bitset_type type
;
45 if (bitset_stats_enabled
)
46 return bitset_stats_bytes ();
51 bytes
= abitset_bytes (n_bits
);
55 bytes
= lbitset_bytes (n_bits
);
59 bytes
= ebitset_bytes (n_bits
);
70 /* Initialise bitset BSET of TYPE for N_BITS. */
72 bitset_init (bset
, n_bits
, type
)
75 enum bitset_type type
;
77 if (bitset_stats_enabled
)
78 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
);
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 (n_bits
, attr
)
102 bitset_bindex n_bits ATTRIBUTE_UNUSED
;
105 enum bitset_type type
;
107 /* Check attributes. */
108 if (attr
& BITSET_FIXED
&& attr
& BITSET_VARIABLE
)
110 if (attr
& BITSET_SPARSE
&& attr
& BITSET_DENSE
)
113 /* Choose the type of bitset. Note that sometimes we will be asked
114 for a zero length fixed size bitset. */
117 /* Currently, the simple bitsets do not support a variable size. */
118 if (attr
& BITSET_VARIABLE
|| attr
& BITSET_SPARSE
)
121 if (attr
& BITSET_DENSE
|| attr
& BITSET_GREEDY
)
129 /* Create a bitset of N_BITS of type TYPE. */
131 bitset_alloc (n_bits
, type
)
132 bitset_bindex n_bits
;
133 enum bitset_type type
;
138 bytes
= bitset_bytes (type
, n_bits
);
140 bset
= (bitset
) 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 (bobstack
, n_bits
, type
)
153 struct obstack
*bobstack
;
154 bitset_bindex n_bits
;
155 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 (n_bits
, attr
)
173 bitset_bindex n_bits
;
176 enum bitset_type type
;
178 type
= bitset_type_choose (n_bits
, attr
);
180 return bitset_alloc (n_bits
, type
);
184 /* Free bitset BSET. */
194 /* Free bitset BSET allocated on obstack. */
196 bitset_obstack_free (bset
)
203 /* Return bitset type. */
205 bitset_type_get (bset
)
208 enum bitset_type type
;
210 type
= BITSET_TYPE_ (bset
);
211 if (type
!= BITSET_STATS
)
214 return bitset_stats_type_get (bset
);
218 /* Return name of bitset type. */
220 bitset_type_name_get (bset
)
223 enum bitset_type type
;
225 type
= bitset_type_get (bset
);
227 return bitset_type_names
[type
];
231 /* Find next bit set in SRC starting from and including BITNO.
232 Return BITSET_BINDEX_MAX if SRC empty. */
234 bitset_next (src
, bitno
)
239 bitset_bindex next
= bitno
;
241 if (!bitset_list (src
, &val
, 1, &next
))
242 return BITSET_BINDEX_MAX
;
247 /* Find previous bit set in SRC starting from and including BITNO.
248 Return BITSET_BINDEX_MAX if SRC empty. */
250 bitset_prev (src
, bitno
)
255 bitset_bindex next
= bitno
;
257 if (!bitset_list_reverse (src
, &val
, 1, &next
))
258 return BITSET_BINDEX_MAX
;
263 /* Find first set bit. */
268 return bitset_next (src
, 0);
272 /* Find last set bit. */
277 return bitset_prev (src
, 0);
281 /* Return non-zero if BITNO in SRC is the only set bit. */
283 bitset_only_set_p (src
, bitno
)
287 bitset_bindex val
[2];
288 bitset_bindex next
= 0;
290 if (bitset_list (src
, val
, 2, &next
) != 1)
292 return val
[0] == bitno
;
296 /* Print contents of bitset BSET to FILE. */
298 bitset_print (file
, bset
, verbose
)
305 bitset_iterator iter
;
308 fprintf (file
, "n_bits = %lu, set = {",
309 (unsigned long) bitset_size (bset
));
312 BITSET_FOR_EACH (iter
, bset
, i
, 0)
316 fprintf (file
, "\n");
320 fprintf (file
, "%d ", i
);
321 pos
+= 1 + (i
>= 10) + (i
>= 100);
325 fprintf (file
, "}\n");
329 /* Dump bitset BSET to FILE. */
331 bitset_dump (file
, bset
)
335 bitset_print (file
, bset
, 0);
340 /* Release memory associated with bitsets. */
342 bitset_release_memory ()
344 lbitset_release_memory ();
345 ebitset_release_memory ();
350 /* Toggle bit BITNO in bitset BSET and return non-zero if not set. */
352 bitset_toggle_ (bset
, bitno
)
356 /* This routine is for completeness. It could be optimized if
358 if (bitset_test (bset
, bitno
))
360 bitset_reset (bset
, bitno
);
365 bitset_set (bset
, bitno
);
371 /* Return number of bits set in bitset SRC. */
376 bitset_bindex list
[BITSET_LIST_SIZE
];
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. */
386 for (count
= 0; (num
= bitset_list (src
, list
, BITSET_LIST_SIZE
, &next
));
394 /* DST = SRC. Return non-zero if DST != SRC.
395 This is a fallback for the case where SRC and DST are different
398 bitset_copy_ (dst
, src
)
403 bitset_iterator iter
;
405 /* Convert bitset types. We assume that the DST bitset
406 is large enough to hold the SRC bitset. */
408 BITSET_FOR_EACH (iter
, src
, i
, 0)
417 /* This is a fallback for implementations that do not support
418 four operand operations. */
420 bitset_op4_cmp (dst
, src1
, src2
, src3
, op
)
428 int stats_enabled_save
;
431 /* Create temporary bitset. */
432 stats_enabled_save
= bitset_stats_enabled
;
433 bitset_stats_enabled
= 0;
434 tmp
= bitset_alloc (0, bitset_type_get (dst
));
435 bitset_stats_enabled
= stats_enabled_save
;
439 case BITSET_OP_OR_AND
:
440 bitset_or (tmp
, src1
, src2
);
441 changed
= bitset_and_cmp (dst
, src3
, tmp
);
444 case BITSET_OP_AND_OR
:
445 bitset_and (tmp
, src1
, src2
);
446 changed
= bitset_or_cmp (dst
, src3
, tmp
);
449 case BITSET_OP_ANDN_OR
:
450 bitset_andn (tmp
, src1
, src2
);
451 changed
= bitset_or_cmp (dst
, src3
, tmp
);
463 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
464 DST != (SRC1 & SRC2) | SRC3. */
466 bitset_and_or_cmp_ (dst
, src1
, src2
, src3
)
472 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_AND_OR
);
476 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
477 DST != (SRC1 & ~SRC2) | SRC3. */
479 bitset_andn_or_cmp_ (dst
, src1
, src2
, src3
)
485 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_ANDN_OR
);
489 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
490 DST != (SRC1 | SRC2) & SRC3. */
492 bitset_or_and_cmp_ (dst
, src1
, src2
, src3
)
498 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_OR_AND
);
502 /* Function to be called from debugger to print bitset. */
508 bitset_print (stderr
, bset
, 1);