]> git.saurik.com Git - bison.git/blame - lib/bitset.c
Regen.
[bison.git] / lib / bitset.c
CommitLineData
7086e707
AD
1/* General bitsets.
2 Copyright (C) 2002 Free Software Foundation, Inc.
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
AD
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. */
7086e707
AD
18
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include <stdlib.h>
24#include "bitset.h"
613f5e1a 25#include "abitset.h"
ef017502
AD
26#include "lbitset.h"
27#include "ebitset.h"
613f5e1a 28#include "bitset_stats.h"
7086e707
AD
29#include "obstack.h"
30
31static void bitset_print PARAMS ((FILE *, bitset, int));
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. */
36int
37bitset_bytes (type, n_bits)
38 enum bitset_type type;
39 bitset_bindex n_bits;
40{
41 unsigned int bytes;
42
613f5e1a
AD
43 if (bitset_stats_enabled)
44 return bitset_stats_bytes ();
45
7086e707
AD
46 switch (type)
47 {
48 case BITSET_ARRAY:
613f5e1a 49 bytes = abitset_bytes (n_bits);
7086e707
AD
50 break;
51
52 case BITSET_LIST:
53 bytes = lbitset_bytes (n_bits);
54 break;
55
56 case BITSET_TABLE:
57 bytes = ebitset_bytes (n_bits);
58 break;
59
60 default:
61 abort ();
62 }
63
64 return bytes;
65}
66
67
68/* Initialise bitset BSET of TYPE for N_BITS. */
69bitset
70bitset_init (bset, n_bits, type)
71 bitset bset;
72 bitset_bindex n_bits;
73 enum bitset_type type;
74{
613f5e1a
AD
75 if (bitset_stats_enabled)
76 return bitset_stats_init (bset, n_bits, type);
77
7086e707
AD
78 switch (type)
79 {
80 case BITSET_ARRAY:
613f5e1a 81 return abitset_init (bset, n_bits);
7086e707
AD
82
83 case BITSET_LIST:
84 return lbitset_init (bset, n_bits);
85
86 case BITSET_TABLE:
87 return ebitset_init (bset, n_bits);
88
89 default:
90 abort ();
91 }
92}
93
94
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. */
98enum bitset_type
99bitset_type_choose (n_bits, attr)
100 bitset_bindex n_bits ATTRIBUTE_UNUSED;
101 unsigned int attr;
102{
103 enum bitset_type type;
104
7086e707
AD
105 /* Check attributes. */
106 if (attr & BITSET_FIXED && attr & BITSET_VARIABLE)
107 abort ();
108 if (attr & BITSET_SPARSE && attr & BITSET_DENSE)
109 abort ();
110
111 /* Note that sometimes we will be asked for a zero length
112 fixed size bitset. */
7086e707
AD
113
114 /* Choose the type of bitset. */
115
116 type = BITSET_ARRAY;
117 /* Currently, the simple bitsets do not support a variable size. */
118 if (attr & BITSET_VARIABLE || attr & BITSET_SPARSE)
119 {
120 type = BITSET_LIST;
121 if (attr & BITSET_DENSE || attr & BITSET_GREEDY)
122 type = BITSET_TABLE;
123 }
124
125 return type;
126}
127
128
129/* Create a bitset of N_BITS of type TYPE. */
130bitset
131bitset_alloc (n_bits, type)
132 bitset_bindex n_bits;
133 enum bitset_type type;
134{
135 unsigned int bytes;
136 bitset bset;
137
7086e707
AD
138 bytes = bitset_bytes (type, n_bits);
139
ef017502
AD
140 bset = (bitset) xcalloc (1, bytes);
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
152bitset_obstack_alloc (bobstack, n_bits, type)
ef017502
AD
153 struct obstack *bobstack;
154 bitset_bindex n_bits;
155 enum bitset_type type;
7086e707
AD
156{
157 unsigned int bytes;
ef017502 158 bitset bset;
7086e707 159
7086e707
AD
160 bytes = bitset_bytes (type, n_bits);
161
ef017502
AD
162 bset = obstack_alloc (bobstack, bytes);
163 memset (bset, 0, bytes);
164
165 return bitset_init (bset, n_bits, type);
7086e707
AD
166}
167
168
169/* Create a bitset of N_BITS and with attribute hints specified by
170 ATTR. */
171bitset
172bitset_create (n_bits, attr)
173 bitset_bindex n_bits;
174 unsigned int attr;
175{
176 enum bitset_type type;
177
178 type = bitset_type_choose (n_bits, attr);
179
180 return bitset_alloc (n_bits, type);
181}
182
183
184/* Free bitset BSET. */
185void
186bitset_free (bset)
187 bitset bset;
188{
ef017502 189 BITSET_FREE_ (bset);
7086e707
AD
190 free (bset);
191}
192
193
194/* Free bitset BSET allocated on obstack. */
195void
196bitset_obstack_free (bset)
197 bitset bset;
198{
ef017502 199 BITSET_FREE_ (bset);
7086e707
AD
200}
201
202
613f5e1a
AD
203/* Return bitset type. */
204enum bitset_type
205bitset_type_get (bset)
206 bitset bset;
207{
208 enum bitset_type type;
209
210 type = BITSET_TYPE_ (bset);
211 if (type != BITSET_STATS)
212 return type;
213
214 return bitset_stats_type_get (bset);
215}
216
217
7086e707
AD
218/* Find next bit set in SRC starting from and including BITNO.
219 Return -1 if SRC empty. */
220int
221bitset_next (src, bitno)
222 bitset src;
223 bitset_bindex bitno;
224{
225 bitset_bindex val;
226 bitset_bindex next = bitno;
227
228 if (!bitset_list (src, &val, 1, &next))
229 return -1;
230 return val;
231}
232
233
234/* Find previous bit set in SRC starting from and including BITNO.
235 Return -1 if SRC empty. */
236int
237bitset_prev (src, bitno)
238 bitset src;
239 bitset_bindex bitno;
240{
241 bitset_bindex val;
242 bitset_bindex next = bitno;
243
244 if (!bitset_reverse_list (src, &val, 1, &next))
245 return -1;
246 return val;
247}
248
249
250/* Find first set bit. */
251int
252bitset_first (src)
253 bitset src;
254{
255 return bitset_next (src, 0);
256}
257
258
259/* Find last set bit. */
260int
261bitset_last (src)
262 bitset src;
263{
264 return bitset_prev (src, 0);
265}
266
267
345cea78
AD
268/* Return non-zero if BITNO in SRC is the only set bit. */
269int
270bitset_only_set_p (src, bitno)
271 bitset src;
272 bitset_bindex bitno;
273{
274 bitset_bindex val[2];
275 bitset_bindex next = 0;
276
277 if (bitset_list (src, val, 2, &next) != 1)
278 return 0;
279 return val[0] == bitno;
280}
281
282
283/* Toggle bit BITNO in bitset BSET and return non-zero if now set. */
284int
285bitset_toggle (bset, bitno)
286 bitset bset;
287 bitset_bindex bitno;
288{
289 /* This routine is for completeness. It could be optimized if
290 required. */
291 if (bitset_test (bset, bitno))
292 {
293 bitset_reset (bset, bitno);
294 return 0;
295 }
296 else
297 {
298 bitset_set (bset, bitno);
299 return 1;
300 }
301}
302
303
ef017502 304/* Print contents of bitset BSET to FILE. */
7086e707
AD
305static void
306bitset_print (file, bset, verbose)
307 FILE *file;
308 bitset bset;
309 int verbose;
310{
311 unsigned int i, pos;
613f5e1a 312 bitset_iterator iter;
7086e707
AD
313
314 if (verbose)
315 fprintf (file, "n_bits = %d, set = {", bitset_size (bset));
316
317 pos = 30;
613f5e1a 318 BITSET_FOR_EACH (iter, bset, i, 0)
7086e707
AD
319 {
320 if (pos > 70)
321 {
322 fprintf (file, "\n");
323 pos = 0;
324 }
325
326 fprintf (file, "%d ", i);
327 pos += 1 + (i >= 10) + (i >= 100);
613f5e1a 328 };
7086e707
AD
329
330 if (verbose)
331 fprintf (file, "}\n");
332}
333
334
ef017502 335/* DST = SRC. Return non-zero if DST != SRC. */
7086e707
AD
336int
337bitset_copy (dst, src)
ef017502
AD
338 bitset dst;
339 bitset src;
7086e707 340{
ef017502 341 unsigned int i;
613f5e1a 342 bitset_iterator iter;
7086e707 343
ef017502
AD
344 if (BITSET_COMPATIBLE_ (dst, src))
345 return BITSET_COPY_ (dst, src);
7086e707 346
ef017502
AD
347 /* Convert bitset types. We assume that the DST bitset
348 is large enough to hold the SRC bitset. */
349 bitset_zero (dst);
613f5e1a 350 BITSET_FOR_EACH (iter, src, i, 0)
ef017502
AD
351 {
352 bitset_set (dst, i);
613f5e1a 353 };
7086e707 354
ef017502 355 return 1;
7086e707
AD
356}
357
358
359/* Return size in bits of bitset SRC. */
360int
361bitset_size (src)
ef017502
AD
362 bitset src;
363{
364 return BITSET_SIZE_ (src);
365}
366
367
368/* Return number of bits set in bitset SRC. */
369int
370bitset_count (src)
371 bitset src;
7086e707 372{
ef017502
AD
373 bitset_bindex list[BITSET_LIST_SIZE];
374 bitset_bindex next;
375 int num;
376 int count;
377
613f5e1a
AD
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. */
381
ef017502
AD
382 next = 0;
383 for (count = 0; (num = bitset_list (src, list, BITSET_LIST_SIZE, &next));
384 count += num)
385 continue;
386
387 return count;
7086e707
AD
388}
389
390
391/* DST = 0. */
392int
393bitset_zero (dst)
ef017502 394 bitset dst;
7086e707 395{
ef017502 396 return BITSET_ZERO_ (dst);
7086e707
AD
397}
398
399
400/* DST = ~0. */
401int
402bitset_ones (dst)
ef017502 403 bitset dst;
7086e707 404{
ef017502 405 return BITSET_ONES_ (dst);
7086e707
AD
406}
407
408
ef017502 409/* Return SRC == 0. */
7086e707
AD
410int
411bitset_empty_p (src)
ef017502 412 bitset src;
7086e707 413{
ef017502 414 return BITSET_EMPTY_P_ (src);
7086e707
AD
415}
416
417
418/* Return DST == DST | SRC. */
419int
420bitset_subset_p (dst, src)
ef017502
AD
421 bitset dst;
422 bitset src;
7086e707 423{
ef017502 424 return BITSET_SUBSET_P_ (dst, src);
7086e707
AD
425}
426
427
428/* Return DST == SRC. */
429int
430bitset_equal_p (dst, src)
ef017502
AD
431 bitset dst;
432 bitset src;
7086e707 433{
ef017502
AD
434 return BITSET_EQUAL_P_ (dst, src);
435}
436
437
438/* Return DST & SRC == 0. */
439int
440bitset_disjoint_p (dst, src)
441 bitset dst;
442 bitset src;
443{
ef017502 444 return BITSET_DISJOINT_P_ (dst, src);
7086e707
AD
445}
446
447
448/* DST = ~SRC. */
449int
450bitset_not (dst, src)
ef017502
AD
451 bitset dst;
452 bitset src;
7086e707 453{
ef017502 454 return BITSET_NOT_ (dst, src);
7086e707
AD
455}
456
457
458/* DST = SRC1 | SRC2. Return non-zero if DST != SRC1 | SRC2. */
459int
460bitset_or (dst, src1, src2)
ef017502
AD
461 bitset dst;
462 bitset src1;
463 bitset src2;
7086e707 464{
ef017502 465 return BITSET_OR_ (dst, src1, src2);
7086e707
AD
466}
467
468
469/* DST = SRC1 & SRC2. Return non-zero if DST != SRC1 & SRC2. */
470int
471bitset_and (dst, src1, src2)
ef017502
AD
472 bitset dst;
473 bitset src1;
474 bitset src2;
7086e707 475{
ef017502 476 return BITSET_AND_ (dst, src1, src2);
7086e707
AD
477}
478
479
480/* DST = SRC1 ^ SRC2. Return non-zero if DST != SRC1 ^ SRC2. */
481int
482bitset_xor (dst, src1, src2)
ef017502
AD
483 bitset dst;
484 bitset src1;
485 bitset src2;
7086e707 486{
ef017502 487 return BITSET_XOR_ (dst, src1, src2);
7086e707
AD
488}
489
490
491/* DST = SRC1 & ~SRC2. Return non-zero if DST != SRC1 & ~SRC2. */
492int
493bitset_andn (dst, src1, src2)
ef017502
AD
494 bitset dst;
495 bitset src1;
496 bitset src2;
7086e707 497{
ef017502 498 return BITSET_ANDN_ (dst, src1, src2);
7086e707
AD
499}
500
501
613f5e1a
AD
502/* This is a fallback for implementations that do not support
503 four operand operations. */
ef017502
AD
504int
505bitset_op4 (dst, src1, src2, src3, op)
506 bitset dst;
507 bitset src1;
508 bitset src2;
509 bitset src3;
510 enum bitset_ops op;
511{
512 int changed = 0;
513 bitset tmp;
514
515 /* Create temporary bitset. */
613f5e1a 516 tmp = bitset_alloc (0, bitset_type_get (dst));
ef017502
AD
517
518 switch (op)
519 {
520 case BITSET_OP_OR_AND:
521 BITSET_OR_ (tmp, src1, src2);
522 changed = BITSET_AND_ (dst, src3, tmp);
523 break;
524
525 case BITSET_OP_AND_OR:
526 BITSET_AND_ (tmp, src1, src2);
527 changed = BITSET_OR_ (dst, src3, tmp);
528 break;
529
530 case BITSET_OP_ANDN_OR:
531 BITSET_ANDN_ (tmp, src1, src2);
532 changed = BITSET_OR_ (dst, src3, tmp);
533 break;
534
535 default:
536 abort ();
537 }
538
539 bitset_free (tmp);
540 return changed;
7086e707
AD
541}
542
543
544/* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
545 DST != (SRC1 | SRC2) & SRC3. */
546int
547bitset_or_and (dst, src1, src2, src3)
ef017502
AD
548 bitset dst;
549 bitset src1;
550 bitset src2;
551 bitset src3;
7086e707 552{
ef017502 553 return BITSET_OR_AND_ (dst, src1, src2, src3);
7086e707
AD
554}
555
556
557/* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
558 DST != (SRC1 & SRC2) | SRC3. */
559int
560bitset_and_or (dst, src1, src2, src3)
ef017502
AD
561 bitset dst;
562 bitset src1;
563 bitset src2;
564 bitset src3;
565{
ef017502
AD
566 return BITSET_AND_OR_ (dst, src1, src2, src3);
567}
568
569
570/* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
571 DST != (SRC1 & ~SRC2) | SRC3. */
572int
573bitset_andn_or (dst, src1, src2, src3)
574 bitset dst;
575 bitset src1;
576 bitset src2;
577 bitset src3;
7086e707 578{
ef017502 579 return BITSET_ANDN_OR_ (dst, src1, src2, src3);
7086e707
AD
580}
581
582
583/* Dump bitset BSET to FILE. */
584void
585bitset_dump (file, bset)
586 FILE *file;
587 bitset bset;
588{
589 bitset_print (file, bset, 0);
590}
591
592
593/* Function to be called from debugger to print bitset. */
594void
595debug_bitset (bset)
596 bitset bset;
597{
ef017502
AD
598 if (bset)
599 bitset_print (stderr, bset, 1);
7086e707
AD
600}
601
602
603/* Release memory associated with bitsets. */
604void
605bitset_release_memory ()
606{
607 lbitset_release_memory ();
608 ebitset_release_memory ();
609}