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