gnulib: update
[bison.git] / src / Sbitset.c
blobaa31885850efe8262eee411867b3a85f27500952
1 /* A simple, memory-efficient bitset implementation.
3 Copyright (C) 2009-2015, 2018-2021 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <https://www.gnu.org/licenses/>. */
20 #include <config.h>
21 #include "system.h"
23 #include "Sbitset.h"
25 Sbitset
26 Sbitset__new (Sbitset__Index nbits)
28 /* Some functions, like Sbitset__last_byte_mask, will fail if nbits = 0. */
29 aver (nbits);
30 return xcalloc (1, Sbitset__nbytes (nbits));
33 Sbitset
34 Sbitset__new_on_obstack (Sbitset__Index nbits, struct obstack *obstackp)
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;
46 void
47 Sbitset__delete (Sbitset self)
49 free (self);
52 bool
53 Sbitset__isEmpty (Sbitset self, Sbitset__Index nbits)
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;
62 void
63 Sbitset__fprint (Sbitset self, Sbitset__Index nbits, FILE *file)
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)
73 if (first)
74 first = false;
75 else
76 fprintf (file, ",");
77 fprintf (file, " %" SBITSET__INDEX__CONVERSION_SPEC, i);
79 fprintf (file, " }");