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