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 static void bitset_print
PARAMS ((FILE *, bitset
, int));
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 (type
, n_bits
)
38 enum bitset_type type
;
43 if (bitset_stats_enabled
)
44 return bitset_stats_bytes ();
49 bytes
= abitset_bytes (n_bits
);
53 bytes
= lbitset_bytes (n_bits
);
57 bytes
= ebitset_bytes (n_bits
);
68 /* Initialise bitset BSET of TYPE for N_BITS. */
70 bitset_init (bset
, n_bits
, type
)
73 enum bitset_type type
;
75 if (bitset_stats_enabled
)
76 return bitset_stats_init (bset
, n_bits
, type
);
81 return abitset_init (bset
, n_bits
);
84 return lbitset_init (bset
, n_bits
);
87 return ebitset_init (bset
, n_bits
);
95 /* Select a bitset type for a set of N_BITS and with attribute hints
96 specified by ATTR. For variable size bitsets, N_BITS is only a
97 hint and may be zero. */
99 bitset_type_choose (n_bits
, attr
)
100 bitset_bindex n_bits ATTRIBUTE_UNUSED
;
103 enum bitset_type type
;
105 /* Check attributes. */
106 if (attr
& BITSET_FIXED
&& attr
& BITSET_VARIABLE
)
108 if (attr
& BITSET_SPARSE
&& attr
& BITSET_DENSE
)
111 /* Note that sometimes we will be asked for a zero length
112 fixed size bitset. */
114 /* Choose the type of 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 /* Find next bit set in SRC starting from and including BITNO.
219 Return -1 if SRC empty. */
221 bitset_next (src
, bitno
)
226 bitset_bindex next
= bitno
;
228 if (!bitset_list (src
, &val
, 1, &next
))
234 /* Find previous bit set in SRC starting from and including BITNO.
235 Return -1 if SRC empty. */
237 bitset_prev (src
, bitno
)
242 bitset_bindex next
= bitno
;
244 if (!bitset_reverse_list (src
, &val
, 1, &next
))
250 /* Find first set bit. */
255 return bitset_next (src
, 0);
259 /* Find last set bit. */
264 return bitset_prev (src
, 0);
268 /* Return non-zero if BITNO in SRC is the only set bit. */
270 bitset_only_set_p (src
, bitno
)
274 bitset_bindex val
[2];
275 bitset_bindex next
= 0;
277 if (bitset_list (src
, val
, 2, &next
) != 1)
279 return val
[0] == bitno
;
283 /* Toggle bit BITNO in bitset BSET and return non-zero if now set. */
285 bitset_toggle (bset
, bitno
)
289 /* This routine is for completeness. It could be optimized if
291 if (bitset_test (bset
, bitno
))
293 bitset_reset (bset
, bitno
);
298 bitset_set (bset
, bitno
);
304 /* Print contents of bitset BSET to FILE. */
306 bitset_print (file
, bset
, verbose
)
312 bitset_iterator iter
;
315 fprintf (file
, "n_bits = %d, set = {", bitset_size (bset
));
318 BITSET_FOR_EACH (iter
, bset
, i
, 0)
322 fprintf (file
, "\n");
326 fprintf (file
, "%d ", i
);
327 pos
+= 1 + (i
>= 10) + (i
>= 100);
331 fprintf (file
, "}\n");
335 /* DST = SRC. Return non-zero if DST != SRC. */
337 bitset_copy (dst
, src
)
342 bitset_iterator iter
;
344 if (BITSET_COMPATIBLE_ (dst
, src
))
345 return BITSET_COPY_ (dst
, src
);
347 /* Convert bitset types. We assume that the DST bitset
348 is large enough to hold the SRC bitset. */
350 BITSET_FOR_EACH (iter
, src
, i
, 0)
359 /* Return size in bits of bitset SRC. */
364 return BITSET_SIZE_ (src
);
368 /* Return number of bits set in bitset SRC. */
373 bitset_bindex list
[BITSET_LIST_SIZE
];
378 /* This could be greatly sped up by adding a count method for each
379 bitset implementation that uses a direct technique (based on
380 masks) for counting the number of bits set in a word. */
383 for (count
= 0; (num
= bitset_list (src
, list
, BITSET_LIST_SIZE
, &next
));
396 return BITSET_ZERO_ (dst
);
405 return BITSET_ONES_ (dst
);
409 /* Return SRC == 0. */
414 return BITSET_EMPTY_P_ (src
);
418 /* Return DST == DST | SRC. */
420 bitset_subset_p (dst
, src
)
424 return BITSET_SUBSET_P_ (dst
, src
);
428 /* Return DST == SRC. */
430 bitset_equal_p (dst
, src
)
434 return BITSET_EQUAL_P_ (dst
, src
);
438 /* Return DST & SRC == 0. */
440 bitset_disjoint_p (dst
, src
)
444 return BITSET_DISJOINT_P_ (dst
, src
);
450 bitset_not (dst
, src
)
454 return BITSET_NOT_ (dst
, src
);
458 /* DST = SRC1 | SRC2. Return non-zero if DST != SRC1 | SRC2. */
460 bitset_or (dst
, src1
, src2
)
465 return BITSET_OR_ (dst
, src1
, src2
);
469 /* DST = SRC1 & SRC2. Return non-zero if DST != SRC1 & SRC2. */
471 bitset_and (dst
, src1
, src2
)
476 return BITSET_AND_ (dst
, src1
, src2
);
480 /* DST = SRC1 ^ SRC2. Return non-zero if DST != SRC1 ^ SRC2. */
482 bitset_xor (dst
, src1
, src2
)
487 return BITSET_XOR_ (dst
, src1
, src2
);
491 /* DST = SRC1 & ~SRC2. Return non-zero if DST != SRC1 & ~SRC2. */
493 bitset_andn (dst
, src1
, src2
)
498 return BITSET_ANDN_ (dst
, src1
, src2
);
502 /* This is a fallback for implementations that do not support
503 four operand operations. */
505 bitset_op4 (dst
, src1
, src2
, src3
, op
)
515 /* Create temporary bitset. */
516 tmp
= bitset_alloc (0, bitset_type_get (dst
));
520 case BITSET_OP_OR_AND
:
521 BITSET_OR_ (tmp
, src1
, src2
);
522 changed
= BITSET_AND_ (dst
, src3
, tmp
);
525 case BITSET_OP_AND_OR
:
526 BITSET_AND_ (tmp
, src1
, src2
);
527 changed
= BITSET_OR_ (dst
, src3
, tmp
);
530 case BITSET_OP_ANDN_OR
:
531 BITSET_ANDN_ (tmp
, src1
, src2
);
532 changed
= BITSET_OR_ (dst
, src3
, tmp
);
544 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
545 DST != (SRC1 | SRC2) & SRC3. */
547 bitset_or_and (dst
, src1
, src2
, src3
)
553 return BITSET_OR_AND_ (dst
, src1
, src2
, src3
);
557 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
558 DST != (SRC1 & SRC2) | SRC3. */
560 bitset_and_or (dst
, src1
, src2
, src3
)
566 return BITSET_AND_OR_ (dst
, src1
, src2
, src3
);
570 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
571 DST != (SRC1 & ~SRC2) | SRC3. */
573 bitset_andn_or (dst
, src1
, src2
, src3
)
579 return BITSET_ANDN_OR_ (dst
, src1
, src2
, src3
);
583 /* Dump bitset BSET to FILE. */
585 bitset_dump (file
, bset
)
589 bitset_print (file
, bset
, 0);
593 /* Function to be called from debugger to print bitset. */
599 bitset_print (stderr
, bset
, 1);
603 /* Release memory associated with bitsets. */
605 bitset_release_memory ()
607 lbitset_release_memory ();
608 ebitset_release_memory ();