]> git.saurik.com Git - bison.git/blame - src/Sbitset.h
build: fix VPATH issue
[bison.git] / src / Sbitset.h
CommitLineData
7fe11bb5
JD
1/* A simple, memory-efficient bitset implementation.
2
34136e65 3 Copyright (C) 2009-2012 Free Software Foundation, Inc.
7fe11bb5
JD
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#ifndef SBITSET_H_
21# define SBITSET_H_
22
5297ebb3 23typedef unsigned char *Sbitset;
7fe11bb5 24typedef size_t Sbitset__Index;
a99ec53e 25# define SBITSET__INDEX__CONVERSION_SPEC "zu"
7fe11bb5 26
a99ec53e 27# define Sbitset__nbytes(NBITS) \
175620d3 28 (((NBITS) + CHAR_BIT - 1) / CHAR_BIT)
a99ec53e 29# define Sbitset__byteAddress(SELF, INDEX) \
175620d3 30 (((SELF) + (INDEX) / CHAR_BIT))
a99ec53e 31# define Sbitset__bit_mask(INDEX) \
175620d3 32 (1 << (CHAR_BIT - 1 - (INDEX) % CHAR_BIT))
a99ec53e 33# define Sbitset__last_byte_mask(NBITS) \
175620d3 34 (UCHAR_MAX << (CHAR_BIT - 1 - ((NBITS) - 1) % CHAR_BIT))
7fe11bb5
JD
35
36/* nbits must not be 0. */
37Sbitset Sbitset__new (Sbitset__Index nbits);
38Sbitset Sbitset__new_on_obstack (Sbitset__Index nbits,
39 struct obstack *obstackp);
40void Sbitset__delete (Sbitset self);
41
a99ec53e 42# define Sbitset__test(SELF, INDEX) \
7fe11bb5
JD
43 ((*Sbitset__byteAddress ((SELF), (INDEX)) & Sbitset__bit_mask (INDEX)) != 0)
44
45bool Sbitset__isEmpty (Sbitset self, Sbitset__Index nbits);
46
47void Sbitset__fprint(Sbitset self, Sbitset__Index nbits, FILE *file);
48
a99ec53e
AD
49# define Sbitset__set(SELF, INDEX) \
50 do { \
51 *Sbitset__byteAddress ((SELF), (INDEX)) = \
52 *Sbitset__byteAddress ((SELF), (INDEX)) | Sbitset__bit_mask (INDEX); \
53 } while(0)
7fe11bb5 54
a99ec53e
AD
55# define Sbitset__reset(SELF, INDEX) \
56 do { \
57 *Sbitset__byteAddress ((SELF), (INDEX)) = \
58 *Sbitset__byteAddress ((SELF), (INDEX)) & ~Sbitset__bit_mask (INDEX); \
59 } while(0)
7fe11bb5
JD
60
61/* NBITS is the size of the bitset. More than NBITS bits might be reset. */
a99ec53e
AD
62# define Sbitset__zero(SELF, NBITS) \
63 do { \
64 memset (SELF, 0, Sbitset__nbytes (NBITS)); \
65 } while(0)
7fe11bb5
JD
66
67/* NBITS is the size of the bitset. More than NBITS bits might be set. */
a99ec53e
AD
68# define Sbitset__ones(SELF, NBITS) \
69 do { \
70 memset (SELF, UCHAR_MAX, Sbitset__nbytes (NBITS)); \
71 } while(0)
7fe11bb5
JD
72
73/* NBITS is the size of every bitset. More than NBITS bits might be set. */
a99ec53e
AD
74# define Sbitset__or(SELF, OTHER1, OTHER2, NBITS) \
75 do { \
76 Sbitset ptr_self = (SELF); \
77 Sbitset ptr_other1 = (OTHER1); \
78 Sbitset ptr_other2 = (OTHER2); \
79 Sbitset end_self = ptr_self + Sbitset__nbytes (NBITS); \
80 for (; ptr_self < end_self; ++ptr_self, ++ptr_other1, ++ptr_other2) \
81 *ptr_self = *ptr_other1 | *ptr_other2; \
82 } while(0)
83
84# define SBITSET__FOR_EACH(SELF, NBITS, ITER, INDEX) \
85 for ((ITER) = (SELF); (ITER) < (SELF) + Sbitset__nbytes (NBITS); ++(ITER)) \
86 if (*(ITER) != 0) \
87 for ((INDEX) = ((ITER)-(SELF))*CHAR_BIT; \
88 (INDEX) < (NBITS) && (SELF)+(INDEX)/CHAR_BIT < (ITER)+1; \
89 ++(INDEX)) \
7fe11bb5
JD
90 if (((*ITER) & Sbitset__bit_mask (INDEX)) != 0)
91
92#endif /* !SBITSET_H_ */