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 -1 if SRC empty. */
234 bitset_next (src
, bitno
)
239 bitset_bindex next
= bitno
;
241 if (!bitset_list (src
, &val
, 1, &next
))
247 /* Find previous bit set in SRC starting from and including BITNO.
248 Return -1 if SRC empty. */
250 bitset_prev (src
, bitno
)
255 bitset_bindex next
= bitno
;
257 if (!bitset_list_reverse (src
, &val
, 1, &next
))
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 = %d, set = {", bitset_size (bset
));
311 BITSET_FOR_EACH (iter
, bset
, i
, 0)
315 fprintf (file
, "\n");
319 fprintf (file
, "%d ", i
);
320 pos
+= 1 + (i
>= 10) + (i
>= 100);
324 fprintf (file
, "}\n");
328 /* Dump bitset BSET to FILE. */
330 bitset_dump (file
, bset
)
334 bitset_print (file
, bset
, 0);
339 /* Release memory associated with bitsets. */
341 bitset_release_memory ()
343 lbitset_release_memory ();
344 ebitset_release_memory ();
349 /* Toggle bit BITNO in bitset BSET and return non-zero if not set. */
351 bitset_toggle_ (bset
, bitno
)
355 /* This routine is for completeness. It could be optimized if
357 if (bitset_test (bset
, bitno
))
359 bitset_reset (bset
, bitno
);
364 bitset_set (bset
, bitno
);
370 /* Return number of bits set in bitset SRC. */
375 bitset_bindex list
[BITSET_LIST_SIZE
];
380 /* This could be greatly sped up by adding a count method for each
381 bitset implementation that uses a direct technique (based on
382 masks) for counting the number of bits set in a word. */
385 for (count
= 0; (num
= bitset_list (src
, list
, BITSET_LIST_SIZE
, &next
));
393 /* DST = SRC. Return non-zero if DST != SRC.
394 This is a fallback for the case where SRC and DST are different
397 bitset_copy_ (dst
, src
)
402 bitset_iterator iter
;
404 /* Convert bitset types. We assume that the DST bitset
405 is large enough to hold the SRC bitset. */
407 BITSET_FOR_EACH (iter
, src
, i
, 0)
416 /* This is a fallback for implementations that do not support
417 four operand operations. */
419 bitset_op4_cmp (dst
, src1
, src2
, src3
, op
)
427 int stats_enabled_save
;
430 /* Create temporary bitset. */
431 stats_enabled_save
= bitset_stats_enabled
;
432 bitset_stats_enabled
= 0;
433 tmp
= bitset_alloc (0, bitset_type_get (dst
));
434 bitset_stats_enabled
= stats_enabled_save
;
438 case BITSET_OP_OR_AND
:
439 bitset_or (tmp
, src1
, src2
);
440 changed
= bitset_and_cmp (dst
, src3
, tmp
);
443 case BITSET_OP_AND_OR
:
444 bitset_and (tmp
, src1
, src2
);
445 changed
= bitset_or_cmp (dst
, src3
, tmp
);
448 case BITSET_OP_ANDN_OR
:
449 bitset_andn (tmp
, src1
, src2
);
450 changed
= bitset_or_cmp (dst
, src3
, tmp
);
462 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
463 DST != (SRC1 & SRC2) | SRC3. */
465 bitset_and_or_cmp_ (dst
, src1
, src2
, src3
)
471 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_AND_OR
);
475 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
476 DST != (SRC1 & ~SRC2) | SRC3. */
478 bitset_andn_or_cmp_ (dst
, src1
, src2
, src3
)
484 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_ANDN_OR
);
488 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
489 DST != (SRC1 | SRC2) & SRC3. */
491 bitset_or_and_cmp_ (dst
, src1
, src2
, src3
)
497 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_OR_AND
);
501 /* Function to be called from debugger to print bitset. */
507 bitset_print (stderr
, bset
, 1);