]>
Commit | Line | Data |
---|---|---|
ef017502 AD |
1 | /* Base bitset stuff. |
2 | Copyright (C) 2002 Free Software Foundation, Inc. | |
3 | Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz). | |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
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. */ | |
18 | ||
19 | #ifndef _BBITSET_H | |
20 | #define _BBITSET_H | |
21 | ||
22 | /* Bison modifications: BEGIN. */ | |
23 | #define IN_BISON 1 | |
24 | #if IN_BISON | |
25 | # if HAVE_CONFIG_H | |
26 | # include <config.h> | |
27 | # endif | |
28 | # include <stdlib.h> | |
29 | ||
30 | # define BITSET_STATS 1 | |
31 | /* This file is the public interface to the bitset abstract data type. | |
32 | Only use the functions and macros defined in this file. */ | |
33 | ||
34 | # ifndef PARAMS | |
35 | # if defined PROTOTYPES || (defined __STDC__ && __STDC__) | |
36 | # define PARAMS(Args) Args | |
37 | # else | |
38 | # define PARAMS(Args) () | |
39 | # endif | |
40 | # endif | |
41 | ||
42 | # ifndef __attribute__ | |
43 | # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__ | |
44 | # define __attribute__(x) | |
45 | # endif | |
46 | # endif | |
47 | ||
48 | # ifndef ATTRIBUTE_NORETURN | |
49 | # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__)) | |
50 | # endif | |
51 | ||
52 | # ifndef ATTRIBUTE_UNUSED | |
53 | # define ATTRIBUTE_UNUSED __attribute__ ((__unused__)) | |
54 | # endif | |
55 | # include "xalloc.h" | |
56 | #else /* ! IN_BISON */ | |
57 | # include "libiberty.h" | |
58 | #endif | |
59 | ||
60 | #ifdef HAVE_LIMITS_H | |
61 | #include <limits.h> | |
62 | #endif | |
63 | ||
64 | /* Currently we support three flavours of bitsets: | |
65 | BITSET_ARRAY: Array of bits (fixed size, faster). | |
66 | BITSET_LIST: Linked list of array of bits (variable size, least storage | |
67 | for large very sparse sets). | |
68 | BITSET_TABLE: Expandable table of pointers to array of bits | |
69 | (variable size, less storage for large sparse sets). | |
70 | */ | |
71 | enum bitset_type {BITSET_ARRAY, BITSET_LIST, BITSET_TABLE, BITSET_TYPE_NUM}; | |
72 | #define BITSET_TYPE_NAMES {"sbitset", "lbitset", "ebitset"} | |
73 | ||
74 | /* Non-zero to enable bitset caching. */ | |
75 | #define BITSET_CACHE 1 | |
76 | ||
77 | /* Non-zero to use inline functions instead of macros. */ | |
78 | #define BITSET_INLINE 0 | |
79 | ||
80 | /* Non-zero to enable bitset statistics gathering. */ | |
81 | #define BITSET_STATS 1 | |
82 | ||
83 | /* Non-zero to enable bitset type checking. */ | |
84 | #define BITSET_CHECK 1 | |
85 | ||
86 | /* Data type used to store a word of bits. */ | |
87 | typedef unsigned long bitset_word; | |
88 | #define BITSET_WORD_BITS ((unsigned) CHAR_BIT * sizeof (bitset_word)) | |
89 | ||
90 | /* Bit index. */ | |
91 | typedef unsigned long bitset_bindex; | |
92 | ||
93 | /* Word index. */ | |
94 | typedef unsigned long bitset_windex; | |
95 | ||
96 | #define BITSET_INDEX_MAX ((1U << (BITSET_WORD_BITS - 1))) | |
97 | ||
98 | #define BITSET_MSB (1U << (BITSET_WORD_BITS - 1)) | |
99 | ||
100 | #define BITSET_LIST_SIZE 1024 | |
101 | ||
102 | enum bitset_ops {BITSET_OP_ZERO, BITSET_OP_ONES, | |
103 | BITSET_OP_COPY, BITSET_OP_NOT, | |
104 | BITSET_OP_EMPTY_P, BITSET_OP_EQUAL_P, | |
105 | BITSET_OP_SUBSET_P, BITSET_OP_DISJOINT_P, | |
106 | BITSET_OP_AND, BITSET_OP_OR, BITSET_OP_XOR, | |
107 | BITSET_OP_ANDN, BITSET_OP_ORN, | |
108 | BITSET_OP_OR_AND, BITSET_OP_AND_OR, BITSET_OP_ANDN_OR}; | |
109 | ||
110 | /* Return size in bits of bitset SRC. */ | |
111 | #define BITSET_SIZE_(SRC) (SRC)->b.ops->size (SRC) | |
112 | ||
113 | /* Return type of bitset SRC. */ | |
114 | #define BITSET_TYPE_(DST) (DST)->b.ops->type | |
115 | ||
116 | /* Set bit BITNO in bitset DST. */ | |
117 | #define BITSET_SET_(DST, BITNO) (DST)->b.ops->set (DST, BITNO) | |
118 | ||
119 | /* Reset bit BITNO in bitset DST. */ | |
120 | #define BITSET_RESET_(DST, BITNO) (DST)->b.ops->reset (DST, BITNO) | |
121 | ||
122 | /* Return non-zero if bit BITNO in bitset SRC is set. */ | |
123 | #define BITSET_TEST_(SRC, BITNO) (SRC)->b.ops->test (SRC, BITNO) | |
124 | ||
125 | /* Free bitset SRC. */ | |
126 | #define BITSET_FREE_(SRC)\ | |
127 | ((SRC)->b.ops->free ? (SRC)->b.ops->free (SRC) :(void)0) | |
128 | ||
129 | /* Perform operation OP on DST. */ | |
130 | #define BITSET_OP1_(DST, OP) (DST)->b.ops->op1 (DST, OP) | |
131 | ||
132 | /* Perform operation OP on SRC and store in DST. */ | |
133 | #define BITSET_OP2_(DST, SRC, OP) (DST)->b.ops->op2 (DST, SRC, OP) | |
134 | ||
135 | /* DST = SRC1 OP SRC2. */ | |
136 | #define BITSET_OP3_(DST, SRC1, SRC2, OP) \ | |
137 | (DST)->b.ops->op3 (DST, SRC1, SRC2, OP) | |
138 | ||
139 | /* DST = (SRC1 OP1 SRC2) OP2 SRC3. */ | |
140 | #define BITSET_OP4_(DST, SRC1, SRC2, SRC3, OP) \ | |
141 | (DST)->b.ops->op4 (DST, SRC1, SRC2, SRC3, OP) | |
142 | ||
143 | /* DST = 0. */ | |
144 | #define BITSET_ZERO_(DST) BITSET_OP1_ (DST, BITSET_OP_ZERO); | |
145 | ||
146 | /* DST = ~0. */ | |
147 | #define BITSET_ONES_(DST) BITSET_OP1_ (DST, BITSET_OP_ONES); | |
148 | ||
149 | /* Return SRC == 0. */ | |
150 | #define BITSET_EMPTY_P_(SRC) BITSET_OP1_ (SRC, BITSET_OP_EMPTY_P); | |
151 | ||
152 | /* Return DST == SRC. */ | |
153 | #define BITSET_EQUAL_P_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_EQUAL_P) | |
154 | ||
155 | /* Return DST == DST | SRC. */ | |
156 | #define BITSET_SUBSET_P_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_SUBSET_P) | |
157 | ||
158 | /* Return DST & SRC == 0. */ | |
159 | #define BITSET_DISJOINT_P_(DST, SRC)\ | |
160 | BITSET_OP2_ (DST, SRC, BITSET_OP_DISJOINT_P) | |
161 | ||
162 | /* DST = SRC. */ | |
163 | #define BITSET_COPY_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_COPY) | |
164 | ||
165 | /* DST = ~SRC. */ | |
166 | #define BITSET_NOT_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_NOT) | |
167 | ||
168 | /* DST = SRC1 | SRC2. */ | |
169 | #define BITSET_OR_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2,\ | |
170 | BITSET_OP_OR) | |
171 | ||
172 | /* DST = SRC1 ^ SRC2. */ | |
173 | #define BITSET_XOR_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2,\ | |
174 | BITSET_OP_XOR) | |
175 | ||
176 | /* DST = SRC1 & SRC2. */ | |
177 | #define BITSET_AND_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2, \ | |
178 | BITSET_OP_AND) | |
179 | ||
180 | /* DST = SRC1 & ~SRC2. */ | |
181 | #define BITSET_ANDN_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2, \ | |
182 | BITSET_OP_ANDN) | |
183 | ||
184 | /* DST = SRC1 | ~SRC2. */ | |
185 | #define BITSET_ORN_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2, \ | |
186 | BITSET_OP_ORN) | |
187 | ||
188 | /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if | |
189 | DST != (SRC1 & SRC2) | SRC3. */ | |
190 | #define BITSET_AND_OR_(DST, SRC1, SRC2, SRC3)\ | |
191 | BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_AND_OR) | |
192 | ||
193 | /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if | |
194 | DST != (SRC1 | SRC2) & SRC3. */ | |
195 | #define BITSET_OR_AND_(DST, SRC1, SRC2, SRC3)\ | |
196 | BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_OR_AND) | |
197 | ||
198 | /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if | |
199 | DST != (SRC1 & ~SRC2) | SRC3. */ | |
200 | #define BITSET_ANDN_OR_(DST, SRC1, SRC2, SRC3)\ | |
201 | BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_ANDN_OR) | |
202 | ||
203 | /* Find list of up to NUM bits set in BSET starting from and including | |
204 | *NEXT. Return with actual number of bits found and with *NEXT | |
205 | indicating where search stopped. */ | |
206 | #define BITSET_LIST_(BSET, LIST, NUM, NEXT) \ | |
207 | (BSET)->b.ops->list (BSET, LIST, NUM, NEXT) | |
208 | ||
209 | /* Find reverse list of up to NUM bits set in BSET starting from and | |
210 | including NEXT. Return with actual number of bits found and with | |
211 | *NEXT indicating where search stopped. */ | |
212 | #define BITSET_REVERSE_LIST_(BSET, LIST, NUM, NEXT) \ | |
213 | (BSET)->b.ops->reverse_list (BSET, LIST, NUM, NEXT) | |
214 | ||
215 | ||
216 | struct bbitset_struct | |
217 | { | |
218 | struct bitset_ops_struct *ops; | |
219 | bitset_windex cindex; /* Cache word index. */ | |
220 | bitset_windex csize; /* Cache size in words. */ | |
221 | bitset_word *cdata; /* Cache data pointer. */ | |
222 | }; | |
223 | ||
224 | ||
225 | typedef struct bitset_struct *bitset; | |
226 | ||
227 | ||
228 | /* The contents of this structure should be considered private. */ | |
229 | struct bitset_ops_struct | |
230 | { | |
231 | void (*set) PARAMS ((struct bitset_struct *, bitset_bindex)); | |
232 | void (*reset) PARAMS ((struct bitset_struct *, bitset_bindex)); | |
233 | int (*test) PARAMS ((struct bitset_struct *, bitset_bindex)); | |
234 | int (*size) PARAMS ((struct bitset_struct *)); | |
235 | int (*op1) PARAMS ((struct bitset_struct *, enum bitset_ops)); | |
236 | int (*op2) PARAMS ((struct bitset_struct *, struct bitset_struct *, | |
237 | enum bitset_ops)); | |
238 | int (*op3) PARAMS ((struct bitset_struct *, struct bitset_struct *, | |
239 | struct bitset_struct *, enum bitset_ops)); | |
240 | int (*op4) PARAMS ((struct bitset_struct *, struct bitset_struct *, | |
241 | struct bitset_struct *, struct bitset_struct *, | |
242 | enum bitset_ops)); | |
243 | int (*list) PARAMS ((struct bitset_struct *, bitset_bindex *, | |
244 | bitset_bindex, bitset_bindex *)); | |
245 | int (*reverse_list) PARAMS ((struct bitset_struct *, bitset_bindex *, | |
246 | bitset_bindex, bitset_bindex *)); | |
247 | void (*free) PARAMS ((struct bitset_struct *)); | |
248 | enum bitset_type type; | |
249 | }; | |
250 | ||
251 | #define BITSET_COMPATIBLE_(BSET1, BSET2) ((BSET1)->b.ops == (BSET2)->b.ops) | |
252 | ||
253 | #if BITSET_CHECK | |
254 | #define BITSET_CHECK2_(DST, SRC) \ | |
255 | if (!BITSET_COMPATIBLE_ (DST, SRC)) abort (); | |
256 | ||
257 | #define BITSET_CHECK3_(DST, SRC1, SRC2) \ | |
258 | if (!BITSET_COMPATIBLE_ (DST, SRC1) \ | |
259 | || !BITSET_COMPATIBLE_ (DST, SRC2)) abort (); | |
260 | ||
261 | #define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) \ | |
262 | if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \ | |
263 | || !BITSET_COMPATIBLE_ (DST, SRC3)) abort (); | |
264 | #else | |
265 | #define BITSET_CHECK2_(DST, SRC) | |
266 | ||
267 | #define BITSET_CHECK3_(DST, SRC1, SRC2) | |
268 | ||
269 | #define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) | |
270 | #endif | |
271 | ||
272 | extern int bitset_op4 PARAMS ((bitset, bitset, bitset, bitset, | |
273 | enum bitset_ops op)); | |
274 | ||
275 | #endif /* _BBITSET_H */ |