]> git.saurik.com Git - bison.git/blob - src/Sbitset.c
Bison 2.5.1_rc2.
[bison.git] / src / Sbitset.c
1 /* A simple, memory-efficient bitset implementation.
2
3 Copyright (C) 2009-2012 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include <config.h>
21 #include "system.h"
22
23 #include "Sbitset.h"
24
25 Sbitset
26 Sbitset__new (Sbitset__Index nbits)
27 {
28 /* Some functions, like Sbitset__last_byte_mask, will fail if nbits = 0. */
29 aver (nbits);
30 return xcalloc (1, Sbitset__nbytes (nbits));
31 }
32
33 Sbitset
34 Sbitset__new_on_obstack (Sbitset__Index nbits, struct obstack *obstackp)
35 {
36 Sbitset result;
37 Sbitset ptr;
38 Sbitset end;
39 aver (nbits);
40 result = obstack_alloc (obstackp, Sbitset__nbytes (nbits));
41 for (ptr = result, end = result + Sbitset__nbytes (nbits); ptr < end; ++ptr)
42 *ptr = 0;
43 return result;
44 }
45
46 void
47 Sbitset__delete (Sbitset self)
48 {
49 free (self);
50 }
51
52 bool
53 Sbitset__isEmpty (Sbitset self, Sbitset__Index nbits)
54 {
55 Sbitset last = self + Sbitset__nbytes (nbits) - 1;
56 for (; self < last; ++self)
57 if (*self != 0)
58 return false;
59 return ((*last) & Sbitset__last_byte_mask (nbits)) == 0;
60 }
61
62 void
63 Sbitset__fprint(Sbitset self, Sbitset__Index nbits, FILE *file)
64 {
65 Sbitset__Index i;
66 Sbitset itr;
67 bool first = true;
68 fprintf (file,
69 "nbits = %" SBITSET__INDEX__CONVERSION_SPEC ", set = {",
70 nbits);
71 SBITSET__FOR_EACH (self, nbits, itr, i)
72 {
73 if (first)
74 first = false;
75 else
76 fprintf (file, ",");
77 fprintf (file, " %" SBITSET__INDEX__CONVERSION_SPEC, i);
78 }
79 fprintf (file, " }");
80 }