]> git.saurik.com Git - bison.git/blame - lib/bitset.c
* src/Makefile.am (yacc): Quote target action commands properly so
[bison.git] / lib / bitset.c
CommitLineData
7086e707 1/* General bitsets.
68cae94e 2 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
7086e707
AD
3 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
4
ef017502
AD
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.
7086e707 9
ef017502
AD
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.
7086e707 14
ef017502 15 You should have received a copy of the GNU General Public License
02650b7f
PE
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. */
7086e707 18
231ed89a
PE
19#include <config.h>
20
21#include "bitset.h"
7086e707
AD
22
23#include <stdlib.h>
eaef5507 24#include <string.h>
613f5e1a 25#include "abitset.h"
ef017502
AD
26#include "lbitset.h"
27#include "ebitset.h"
eaef5507 28#include "vbitset.h"
613f5e1a 29#include "bitset_stats.h"
7086e707
AD
30#include "obstack.h"
31
6aa452a6
AD
32const char * const bitset_type_names[] = BITSET_TYPE_NAMES;
33
7086e707 34
7086e707
AD
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. */
d32fe6f6 37size_t
447e90bc 38bitset_bytes (enum bitset_type type, bitset_bindex n_bits)
7086e707 39{
d32fe6f6 40 size_t bytes;
7086e707 41
613f5e1a
AD
42 if (bitset_stats_enabled)
43 return bitset_stats_bytes ();
44
7086e707
AD
45 switch (type)
46 {
68cae94e
PE
47 default:
48 abort ();
49
7086e707 50 case BITSET_ARRAY:
613f5e1a 51 bytes = abitset_bytes (n_bits);
7086e707
AD
52 break;
53
54 case BITSET_LIST:
55 bytes = lbitset_bytes (n_bits);
56 break;
57
58 case BITSET_TABLE:
59 bytes = ebitset_bytes (n_bits);
60 break;
61
eaef5507
PE
62 case BITSET_VARRAY:
63 bytes = vbitset_bytes (n_bits);
64 break;
7086e707
AD
65 }
66
67 return bytes;
68}
69
70
71/* Initialise bitset BSET of TYPE for N_BITS. */
72bitset
447e90bc 73bitset_init (bitset bset, bitset_bindex n_bits, enum bitset_type type)
7086e707 74{
613f5e1a
AD
75 if (bitset_stats_enabled)
76 return bitset_stats_init (bset, n_bits, type);
447e90bc 77
7086e707
AD
78 switch (type)
79 {
68cae94e
PE
80 default:
81 abort ();
82
7086e707 83 case BITSET_ARRAY:
613f5e1a 84 return abitset_init (bset, n_bits);
7086e707
AD
85
86 case BITSET_LIST:
87 return lbitset_init (bset, n_bits);
88
89 case BITSET_TABLE:
90 return ebitset_init (bset, n_bits);
91
eaef5507
PE
92 case BITSET_VARRAY:
93 return vbitset_init (bset, n_bits);
7086e707
AD
94 }
95}
96
97
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. */
101enum bitset_type
447e90bc 102bitset_type_choose (bitset_bindex n_bits ATTRIBUTE_UNUSED, unsigned int attr)
7086e707 103{
7086e707
AD
104 /* Check attributes. */
105 if (attr & BITSET_FIXED && attr & BITSET_VARIABLE)
106 abort ();
107 if (attr & BITSET_SPARSE && attr & BITSET_DENSE)
108 abort ();
109
eaef5507 110 /* Choose the type of bitset. Note that sometimes we will be asked
6aa452a6 111 for a zero length fixed size bitset. */
7086e707 112
4d1801f1 113
eaef5507
PE
114 /* If no attributes selected, choose a good compromise. */
115 if (!attr)
116 return BITSET_VARRAY;
117
118 if (attr & BITSET_SPARSE)
119 return BITSET_LIST;
120
121 if (attr & BITSET_FIXED)
122 return BITSET_ARRAY;
7086e707 123
eaef5507
PE
124 if (attr & BITSET_GREEDY)
125 return BITSET_TABLE;
126
127 return BITSET_VARRAY;
7086e707
AD
128}
129
130
131/* Create a bitset of N_BITS of type TYPE. */
132bitset
447e90bc 133bitset_alloc (bitset_bindex n_bits, enum bitset_type type)
7086e707 134{
d32fe6f6 135 size_t bytes;
7086e707
AD
136 bitset bset;
137
7086e707
AD
138 bytes = bitset_bytes (type, n_bits);
139
89519adf 140 bset = xcalloc (1, bytes);
ef017502
AD
141
142 /* The cache is disabled until some elements are allocated. If we
613f5e1a 143 have variable length arrays, then we may need to allocate a dummy
ef017502 144 element. */
7086e707
AD
145
146 return bitset_init (bset, n_bits, type);
147}
148
149
150/* Create a bitset of N_BITS of type TYPE. */
151bitset
447e90bc
PE
152bitset_obstack_alloc (struct obstack *bobstack,
153 bitset_bindex n_bits, enum bitset_type type)
7086e707 154{
d32fe6f6 155 size_t bytes;
ef017502 156 bitset bset;
7086e707 157
7086e707
AD
158 bytes = bitset_bytes (type, n_bits);
159
ef017502
AD
160 bset = obstack_alloc (bobstack, bytes);
161 memset (bset, 0, bytes);
162
163 return bitset_init (bset, n_bits, type);
7086e707
AD
164}
165
166
167/* Create a bitset of N_BITS and with attribute hints specified by
168 ATTR. */
169bitset
447e90bc 170bitset_create (bitset_bindex n_bits, unsigned int attr)
7086e707
AD
171{
172 enum bitset_type type;
173
174 type = bitset_type_choose (n_bits, attr);
175
176 return bitset_alloc (n_bits, type);
177}
178
179
180/* Free bitset BSET. */
181void
447e90bc 182bitset_free (bitset bset)
7086e707 183{
ef017502 184 BITSET_FREE_ (bset);
7086e707
AD
185 free (bset);
186}
187
188
189/* Free bitset BSET allocated on obstack. */
190void
447e90bc 191bitset_obstack_free (bitset bset)
7086e707 192{
ef017502 193 BITSET_FREE_ (bset);
7086e707
AD
194}
195
196
613f5e1a
AD
197/* Return bitset type. */
198enum bitset_type
447e90bc 199bitset_type_get (bitset bset)
613f5e1a
AD
200{
201 enum bitset_type type;
202
203 type = BITSET_TYPE_ (bset);
204 if (type != BITSET_STATS)
205 return type;
447e90bc 206
613f5e1a
AD
207 return bitset_stats_type_get (bset);
208}
209
210
6aa452a6
AD
211/* Return name of bitset type. */
212const char *
447e90bc 213bitset_type_name_get (bitset bset)
6aa452a6
AD
214{
215 enum bitset_type type;
216
217 type = bitset_type_get (bset);
218
219 return bitset_type_names[type];
220}
221
222
7086e707 223/* Find next bit set in SRC starting from and including BITNO.
d32fe6f6
PE
224 Return BITSET_BINDEX_MAX if SRC empty. */
225bitset_bindex
447e90bc 226bitset_next (bitset src, bitset_bindex bitno)
7086e707
AD
227{
228 bitset_bindex val;
229 bitset_bindex next = bitno;
230
231 if (!bitset_list (src, &val, 1, &next))
d32fe6f6 232 return BITSET_BINDEX_MAX;
7086e707
AD
233 return val;
234}
235
236
eaef5507
PE
237/* Return true if both bitsets are of the same type and size. */
238extern bool
239bitset_compatible_p (bitset bset1, bitset bset2)
240{
241 return BITSET_COMPATIBLE_ (bset1, bset2);
242}
243
244
7086e707 245/* Find previous bit set in SRC starting from and including BITNO.
d32fe6f6
PE
246 Return BITSET_BINDEX_MAX if SRC empty. */
247bitset_bindex
447e90bc 248bitset_prev (bitset src, bitset_bindex bitno)
7086e707
AD
249{
250 bitset_bindex val;
251 bitset_bindex next = bitno;
252
6aa452a6 253 if (!bitset_list_reverse (src, &val, 1, &next))
d32fe6f6 254 return BITSET_BINDEX_MAX;
7086e707
AD
255 return val;
256}
257
258
259/* Find first set bit. */
d32fe6f6 260bitset_bindex
447e90bc 261bitset_first (bitset src)
7086e707
AD
262{
263 return bitset_next (src, 0);
264}
265
266
267/* Find last set bit. */
d32fe6f6 268bitset_bindex
447e90bc 269bitset_last (bitset src)
7086e707
AD
270{
271 return bitset_prev (src, 0);
272}
273
274
d0829076
PE
275/* Is BITNO in SRC the only set bit? */
276bool
447e90bc 277bitset_only_set_p (bitset src, bitset_bindex bitno)
345cea78
AD
278{
279 bitset_bindex val[2];
280 bitset_bindex next = 0;
281
282 if (bitset_list (src, val, 2, &next) != 1)
d0829076 283 return false;
345cea78
AD
284 return val[0] == bitno;
285}
286
287
ef017502 288/* Print contents of bitset BSET to FILE. */
7086e707 289static void
d0829076 290bitset_print (FILE *file, bitset bset, bool verbose)
7086e707 291{
6aa452a6
AD
292 unsigned int pos;
293 bitset_bindex i;
613f5e1a 294 bitset_iterator iter;
7086e707
AD
295
296 if (verbose)
d32fe6f6 297 fprintf (file, "n_bits = %lu, set = {",
779e7ceb 298 (unsigned long int) bitset_size (bset));
7086e707
AD
299
300 pos = 30;
613f5e1a 301 BITSET_FOR_EACH (iter, bset, i, 0)
7086e707
AD
302 {
303 if (pos > 70)
304 {
2f4f028d 305 fprintf (file, "\n");
7086e707
AD
306 pos = 0;
307 }
308
4d1801f1 309 fprintf (file, "%lu ", (unsigned long int) i);
7086e707 310 pos += 1 + (i >= 10) + (i >= 100);
613f5e1a 311 };
7086e707
AD
312
313 if (verbose)
2f4f028d 314 fprintf (file, "}\n");
7086e707
AD
315}
316
317
6aa452a6
AD
318/* Dump bitset BSET to FILE. */
319void
447e90bc 320bitset_dump (FILE *file, bitset bset)
7086e707 321{
d0829076 322 bitset_print (file, bset, false);
6aa452a6 323}
7086e707 324
7086e707 325
6aa452a6
AD
326/* Release memory associated with bitsets. */
327void
447e90bc 328bitset_release_memory (void)
6aa452a6
AD
329{
330 lbitset_release_memory ();
331 ebitset_release_memory ();
7086e707
AD
332}
333
334
d0829076
PE
335/* Toggle bit BITNO in bitset BSET and the new value of the bit. */
336bool
447e90bc 337bitset_toggle_ (bitset bset, bitset_bindex bitno)
ef017502 338{
6aa452a6
AD
339 /* This routine is for completeness. It could be optimized if
340 required. */
341 if (bitset_test (bset, bitno))
342 {
343 bitset_reset (bset, bitno);
d0829076 344 return false;
6aa452a6
AD
345 }
346 else
347 {
348 bitset_set (bset, bitno);
d0829076 349 return true;
6aa452a6 350 }
ef017502
AD
351}
352
353
eaef5507
PE
354/* Return number of bits in bitset SRC. */
355bitset_bindex
356bitset_size_ (bitset src)
357{
358 return BITSET_NBITS_ (src);
359}
360
361
ef017502 362/* Return number of bits set in bitset SRC. */
d32fe6f6 363bitset_bindex
447e90bc 364bitset_count_ (bitset src)
7086e707 365{
ef017502
AD
366 bitset_bindex list[BITSET_LIST_SIZE];
367 bitset_bindex next;
d32fe6f6
PE
368 bitset_bindex num;
369 bitset_bindex count;
447e90bc 370
613f5e1a
AD
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. */
374
ef017502
AD
375 next = 0;
376 for (count = 0; (num = bitset_list (src, list, BITSET_LIST_SIZE, &next));
377 count += num)
378 continue;
447e90bc 379
ef017502 380 return count;
7086e707
AD
381}
382
383
d0829076 384/* DST = SRC. Return true if DST != SRC.
6aa452a6
AD
385 This is a fallback for the case where SRC and DST are different
386 bitset types. */
d0829076 387bool
447e90bc 388bitset_copy_ (bitset dst, bitset src)
ef017502 389{
6aa452a6
AD
390 bitset_bindex i;
391 bitset_iterator iter;
7086e707 392
6aa452a6
AD
393 /* Convert bitset types. We assume that the DST bitset
394 is large enough to hold the SRC bitset. */
395 bitset_zero (dst);
396 BITSET_FOR_EACH (iter, src, i, 0)
397 {
398 bitset_set (dst, i);
399 };
7086e707 400
d0829076 401 return true;
7086e707
AD
402}
403
404
613f5e1a
AD
405/* This is a fallback for implementations that do not support
406 four operand operations. */
d0829076 407static inline bool
447e90bc
PE
408bitset_op4_cmp (bitset dst, bitset src1, bitset src2, bitset src3,
409 enum bitset_ops op)
ef017502 410{
d0829076
PE
411 bool changed = false;
412 bool stats_enabled_save;
ef017502
AD
413 bitset tmp;
414
415 /* Create temporary bitset. */
6aa452a6 416 stats_enabled_save = bitset_stats_enabled;
d0829076 417 bitset_stats_enabled = false;
613f5e1a 418 tmp = bitset_alloc (0, bitset_type_get (dst));
6aa452a6 419 bitset_stats_enabled = stats_enabled_save;
ef017502
AD
420
421 switch (op)
422 {
68cae94e
PE
423 default:
424 abort ();
425
ef017502 426 case BITSET_OP_OR_AND:
6aa452a6
AD
427 bitset_or (tmp, src1, src2);
428 changed = bitset_and_cmp (dst, src3, tmp);
ef017502
AD
429 break;
430
431 case BITSET_OP_AND_OR:
6aa452a6
AD
432 bitset_and (tmp, src1, src2);
433 changed = bitset_or_cmp (dst, src3, tmp);
ef017502
AD
434 break;
435
436 case BITSET_OP_ANDN_OR:
6aa452a6
AD
437 bitset_andn (tmp, src1, src2);
438 changed = bitset_or_cmp (dst, src3, tmp);
ef017502 439 break;
ef017502
AD
440 }
441
442 bitset_free (tmp);
443 return changed;
7086e707
AD
444}
445
446
d5c559cd
PE
447/* DST = (SRC1 & SRC2) | SRC3. */
448void
447e90bc 449bitset_and_or_ (bitset dst, bitset src1, bitset src2, bitset src3)
d5c559cd
PE
450{
451 bitset_and_or_cmp_ (dst, src1, src2, src3);
452}
453
454
6aa452a6
AD
455/* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
456 DST != (SRC1 & SRC2) | SRC3. */
d0829076 457bool
447e90bc 458bitset_and_or_cmp_ (bitset dst, bitset src1, bitset src2, bitset src3)
7086e707 459{
6aa452a6 460 return bitset_op4_cmp (dst, src1, src2, src3, BITSET_OP_AND_OR);
7086e707
AD
461}
462
463
d5c559cd
PE
464/* DST = (SRC1 & ~SRC2) | SRC3. */
465void
447e90bc 466bitset_andn_or_ (bitset dst, bitset src1, bitset src2, bitset src3)
d5c559cd
PE
467{
468 bitset_andn_or_cmp_ (dst, src1, src2, src3);
469}
470
471
6aa452a6
AD
472/* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
473 DST != (SRC1 & ~SRC2) | SRC3. */
d0829076 474bool
447e90bc 475bitset_andn_or_cmp_ (bitset dst, bitset src1, bitset src2, bitset src3)
ef017502 476{
6aa452a6 477 return bitset_op4_cmp (dst, src1, src2, src3, BITSET_OP_ANDN_OR);
ef017502
AD
478}
479
480
d5c559cd
PE
481/* DST = (SRC1 | SRC2) & SRC3. */
482void
447e90bc 483bitset_or_and_ (bitset dst, bitset src1, bitset src2, bitset src3)
d5c559cd
PE
484{
485 bitset_or_and_cmp_ (dst, src1, src2, src3);
486}
487
488
6aa452a6
AD
489/* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
490 DST != (SRC1 | SRC2) & SRC3. */
d0829076 491bool
447e90bc 492bitset_or_and_cmp_ (bitset dst, bitset src1, bitset src2, bitset src3)
7086e707 493{
6aa452a6 494 return bitset_op4_cmp (dst, src1, src2, src3, BITSET_OP_OR_AND);
7086e707
AD
495}
496
497
498/* Function to be called from debugger to print bitset. */
499void
447e90bc 500debug_bitset (bitset bset)
7086e707 501{
ef017502 502 if (bset)
d0829076 503 bitset_print (stderr, bset, true);
7086e707 504}