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
;
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 ();
47 bytes
= abitset_bytes (n_bits
);
51 bytes
= lbitset_bytes (n_bits
);
55 bytes
= ebitset_bytes (n_bits
);
66 /* Initialise bitset BSET of TYPE for N_BITS. */
68 bitset_init (bitset bset
, bitset_bindex n_bits
, enum bitset_type type
)
70 if (bitset_stats_enabled
)
71 return bitset_stats_init (bset
, n_bits
, type
);
76 return abitset_init (bset
, n_bits
);
79 return lbitset_init (bset
, n_bits
);
82 return ebitset_init (bset
, n_bits
);
90 /* Select a bitset type for a set of N_BITS and with attribute hints
91 specified by ATTR. For variable size bitsets, N_BITS is only a
92 hint and may be zero. */
94 bitset_type_choose (bitset_bindex n_bits ATTRIBUTE_UNUSED
, unsigned int attr
)
96 enum bitset_type type
;
98 /* Check attributes. */
99 if (attr
& BITSET_FIXED
&& attr
& BITSET_VARIABLE
)
101 if (attr
& BITSET_SPARSE
&& attr
& BITSET_DENSE
)
104 /* Choose the type of bitset. Note that sometimes we will be asked
105 for a zero length fixed size bitset. */
108 /* Currently, the simple bitsets do not support a variable size. */
109 if (attr
& BITSET_VARIABLE
|| attr
& BITSET_SPARSE
)
112 if (attr
& BITSET_DENSE
|| attr
& BITSET_GREEDY
)
120 /* Create a bitset of N_BITS of type TYPE. */
122 bitset_alloc (bitset_bindex n_bits
, enum bitset_type type
)
127 bytes
= bitset_bytes (type
, n_bits
);
129 bset
= (bitset
) xcalloc (1, bytes
);
131 /* The cache is disabled until some elements are allocated. If we
132 have variable length arrays, then we may need to allocate a dummy
135 return bitset_init (bset
, n_bits
, type
);
139 /* Create a bitset of N_BITS of type TYPE. */
141 bitset_obstack_alloc (struct obstack
*bobstack
,
142 bitset_bindex n_bits
, enum bitset_type type
)
147 bytes
= bitset_bytes (type
, n_bits
);
149 bset
= obstack_alloc (bobstack
, bytes
);
150 memset (bset
, 0, bytes
);
152 return bitset_init (bset
, n_bits
, type
);
156 /* Create a bitset of N_BITS and with attribute hints specified by
159 bitset_create (bitset_bindex n_bits
, unsigned int attr
)
161 enum bitset_type type
;
163 type
= bitset_type_choose (n_bits
, attr
);
165 return bitset_alloc (n_bits
, type
);
169 /* Free bitset BSET. */
171 bitset_free (bitset bset
)
178 /* Free bitset BSET allocated on obstack. */
180 bitset_obstack_free (bitset bset
)
186 /* Return bitset type. */
188 bitset_type_get (bitset bset
)
190 enum bitset_type type
;
192 type
= BITSET_TYPE_ (bset
);
193 if (type
!= BITSET_STATS
)
196 return bitset_stats_type_get (bset
);
200 /* Return name of bitset type. */
202 bitset_type_name_get (bitset bset
)
204 enum bitset_type type
;
206 type
= bitset_type_get (bset
);
208 return bitset_type_names
[type
];
212 /* Find next bit set in SRC starting from and including BITNO.
213 Return BITSET_BINDEX_MAX if SRC empty. */
215 bitset_next (bitset src
, bitset_bindex bitno
)
218 bitset_bindex next
= bitno
;
220 if (!bitset_list (src
, &val
, 1, &next
))
221 return BITSET_BINDEX_MAX
;
226 /* Find previous bit set in SRC starting from and including BITNO.
227 Return BITSET_BINDEX_MAX if SRC empty. */
229 bitset_prev (bitset src
, bitset_bindex bitno
)
232 bitset_bindex next
= bitno
;
234 if (!bitset_list_reverse (src
, &val
, 1, &next
))
235 return BITSET_BINDEX_MAX
;
240 /* Find first set bit. */
242 bitset_first (bitset src
)
244 return bitset_next (src
, 0);
248 /* Find last set bit. */
250 bitset_last (bitset src
)
252 return bitset_prev (src
, 0);
256 /* Return non-zero if BITNO in SRC is the only set bit. */
258 bitset_only_set_p (bitset src
, bitset_bindex bitno
)
260 bitset_bindex val
[2];
261 bitset_bindex next
= 0;
263 if (bitset_list (src
, val
, 2, &next
) != 1)
265 return val
[0] == bitno
;
269 /* Print contents of bitset BSET to FILE. */
271 bitset_print (FILE *file
, bitset bset
, int verbose
)
275 bitset_iterator iter
;
278 fprintf (file
, "n_bits = %lu, set = {",
279 (unsigned long) bitset_size (bset
));
282 BITSET_FOR_EACH (iter
, bset
, i
, 0)
286 fprintf (file
, "\n");
290 fprintf (file
, "%d ", i
);
291 pos
+= 1 + (i
>= 10) + (i
>= 100);
295 fprintf (file
, "}\n");
299 /* Dump bitset BSET to FILE. */
301 bitset_dump (FILE *file
, bitset bset
)
303 bitset_print (file
, bset
, 0);
308 /* Release memory associated with bitsets. */
310 bitset_release_memory (void)
312 lbitset_release_memory ();
313 ebitset_release_memory ();
318 /* Toggle bit BITNO in bitset BSET and return non-zero if not set. */
320 bitset_toggle_ (bitset bset
, bitset_bindex bitno
)
322 /* This routine is for completeness. It could be optimized if
324 if (bitset_test (bset
, bitno
))
326 bitset_reset (bset
, bitno
);
331 bitset_set (bset
, bitno
);
337 /* Return number of bits set in bitset SRC. */
339 bitset_count_ (bitset src
)
341 bitset_bindex list
[BITSET_LIST_SIZE
];
346 /* This could be greatly sped up by adding a count method for each
347 bitset implementation that uses a direct technique (based on
348 masks) for counting the number of bits set in a word. */
351 for (count
= 0; (num
= bitset_list (src
, list
, BITSET_LIST_SIZE
, &next
));
359 /* DST = SRC. Return non-zero if DST != SRC.
360 This is a fallback for the case where SRC and DST are different
363 bitset_copy_ (bitset dst
, bitset src
)
366 bitset_iterator iter
;
368 /* Convert bitset types. We assume that the DST bitset
369 is large enough to hold the SRC bitset. */
371 BITSET_FOR_EACH (iter
, src
, i
, 0)
380 /* This is a fallback for implementations that do not support
381 four operand operations. */
383 bitset_op4_cmp (bitset dst
, bitset src1
, bitset src2
, bitset src3
,
387 int stats_enabled_save
;
390 /* Create temporary bitset. */
391 stats_enabled_save
= bitset_stats_enabled
;
392 bitset_stats_enabled
= 0;
393 tmp
= bitset_alloc (0, bitset_type_get (dst
));
394 bitset_stats_enabled
= stats_enabled_save
;
398 case BITSET_OP_OR_AND
:
399 bitset_or (tmp
, src1
, src2
);
400 changed
= bitset_and_cmp (dst
, src3
, tmp
);
403 case BITSET_OP_AND_OR
:
404 bitset_and (tmp
, src1
, src2
);
405 changed
= bitset_or_cmp (dst
, src3
, tmp
);
408 case BITSET_OP_ANDN_OR
:
409 bitset_andn (tmp
, src1
, src2
);
410 changed
= bitset_or_cmp (dst
, src3
, tmp
);
422 /* DST = (SRC1 & SRC2) | SRC3. */
424 bitset_and_or_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
426 bitset_and_or_cmp_ (dst
, src1
, src2
, src3
);
430 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
431 DST != (SRC1 & SRC2) | SRC3. */
433 bitset_and_or_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
435 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_AND_OR
);
439 /* DST = (SRC1 & ~SRC2) | SRC3. */
441 bitset_andn_or_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
443 bitset_andn_or_cmp_ (dst
, src1
, src2
, src3
);
447 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
448 DST != (SRC1 & ~SRC2) | SRC3. */
450 bitset_andn_or_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
452 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_ANDN_OR
);
456 /* DST = (SRC1 | SRC2) & SRC3. */
458 bitset_or_and_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
460 bitset_or_and_cmp_ (dst
, src1
, src2
, src3
);
464 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
465 DST != (SRC1 | SRC2) & SRC3. */
467 bitset_or_and_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
469 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_OR_AND
);
473 /* Function to be called from debugger to print bitset. */
475 debug_bitset (bitset bset
)
478 bitset_print (stderr
, bset
, 1);