]>
Commit | Line | Data |
---|---|---|
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 | #include "libiberty.h" | |
23 | ||
24 | #ifdef HAVE_LIMITS_H | |
25 | #include <limits.h> | |
26 | #endif | |
27 | ||
28 | /* Use the following types for function parameters where the | |
29 | corresponding enum would be the correct type if we could use | |
30 | prototyped function definitions. Standard C says that one cannot | |
31 | portably use an enum as a parameter of an old-style function | |
32 | definition that is also declared with a prototype. */ | |
33 | ||
34 | typedef int enum_bitset_ops; | |
35 | typedef int enum_bitset_type; | |
36 | ||
37 | /* Currently we support three flavours of bitsets: | |
38 | BITSET_ARRAY: Array of bits (fixed size, fast for dense bitsets). | |
39 | BITSET_LIST: Linked list of array of bits (variable size, least storage | |
40 | for large very sparse sets). | |
41 | BITSET_TABLE: Expandable table of pointers to array of bits | |
42 | (variable size, less storage for large sparse sets). | |
43 | ||
44 | BITSET_STATS: Wrapper bitset for internal use only. | |
45 | */ | |
46 | enum bitset_type {BITSET_ARRAY, BITSET_LIST, BITSET_TABLE, BITSET_TYPE_NUM, | |
47 | BITSET_STATS}; | |
48 | #define BITSET_TYPE_NAMES {"abitset", "lbitset", "ebitset"} | |
49 | ||
50 | extern const char * const bitset_type_names[]; | |
51 | ||
52 | enum bitset_alloc_type {BITSET_MALLOC, BITSET_OBALLOC}; | |
53 | ||
54 | /* Non-zero to use inline functions instead of macros. */ | |
55 | #define BITSET_INLINE 0 | |
56 | ||
57 | /* Data type used to store a word of bits. */ | |
58 | typedef unsigned long bitset_word; | |
59 | #define BITSET_WORD_BITS ((unsigned) (CHAR_BIT * sizeof (bitset_word))) | |
60 | ||
61 | /* Bit index. In theory we might need a type wider than size_t, but | |
62 | in practice we lose at most a factor of CHAR_BIT by going with | |
63 | size_t, and that is good enough. If this type is changed to be | |
64 | wider than size_t, the code needs to be modified to check for | |
65 | overflow when converting bit counts to byte or word counts. | |
66 | The bit and word index types must be unsigned. */ | |
67 | typedef size_t bitset_bindex; | |
68 | ||
69 | /* Word index. */ | |
70 | typedef size_t bitset_windex; | |
71 | ||
72 | /* Maximum values for commonly-used unsigned types. BITSET_SIZE_MAX | |
73 | always equals SIZE_MAX, but some older systems lack SIZE_MAX. */ | |
74 | #define BITSET_BINDEX_MAX ((bitset_bindex) -1) | |
75 | ||
76 | /* Limit max word index to the maximum value of a signed integer | |
77 | to simplify cache disabling. */ | |
78 | #define BITSET_WINDEX_MAX (((bitset_windex) -1) >> 1) | |
79 | #define BITSET_SIZE_MAX ((size_t) -1) | |
80 | ||
81 | #define BITSET_MSB ((bitset_word) 1 << (BITSET_WORD_BITS - 1)) | |
82 | ||
83 | #define BITSET_LIST_SIZE 1024 | |
84 | ||
85 | enum bitset_ops {BITSET_OP_ZERO, BITSET_OP_ONES, | |
86 | BITSET_OP_COPY, BITSET_OP_NOT, | |
87 | BITSET_OP_EMPTY_P, BITSET_OP_EQUAL_P, | |
88 | BITSET_OP_SUBSET_P, BITSET_OP_DISJOINT_P, | |
89 | BITSET_OP_AND, BITSET_OP_OR, BITSET_OP_XOR, BITSET_OP_ANDN, | |
90 | BITSET_OP_OR_AND, BITSET_OP_AND_OR, BITSET_OP_ANDN_OR}; | |
91 | ||
92 | struct bbitset_struct | |
93 | { | |
94 | const struct bitset_vtable * vtable; | |
95 | bitset_windex cindex; /* Cache word index. */ | |
96 | bitset_windex csize; /* Cache size in words. */ | |
97 | bitset_word *cdata; /* Cache data pointer. */ | |
98 | /* Perhaps we could sacrifice another word to indicate | |
99 | that the bitset is known to be zero, that a bit has been set | |
100 | in the cache, and that a bit has been cleared in the cache. | |
101 | This would speed up some of the searches but slightly slow down | |
102 | bit set/reset operations of cached bits. */ | |
103 | }; | |
104 | ||
105 | ||
106 | typedef union bitset_union *bitset; | |
107 | ||
108 | ||
109 | /* The contents of this structure should be considered private. */ | |
110 | struct bitset_vtable | |
111 | { | |
112 | void (*set) PARAMS ((bitset, bitset_bindex)); | |
113 | void (*reset) PARAMS ((bitset, bitset_bindex)); | |
114 | int (*toggle) PARAMS ((bitset, bitset_bindex)); | |
115 | int (*test) PARAMS ((bitset, bitset_bindex)); | |
116 | bitset_bindex (*size) PARAMS ((bitset)); | |
117 | bitset_bindex (*count) PARAMS ((bitset)); | |
118 | ||
119 | int (*empty_p) PARAMS ((bitset)); | |
120 | void (*ones) PARAMS ((bitset)); | |
121 | void (*zero) PARAMS ((bitset)); | |
122 | ||
123 | void (*copy) PARAMS ((bitset, bitset)); | |
124 | int (*disjoint_p) PARAMS ((bitset, bitset)); | |
125 | int (*equal_p) PARAMS ((bitset, bitset)); | |
126 | void (*not) PARAMS ((bitset, bitset)); | |
127 | int (*subset_p) PARAMS ((bitset, bitset)); | |
128 | ||
129 | void (*and) PARAMS ((bitset, bitset, bitset)); | |
130 | int (*and_cmp) PARAMS ((bitset, bitset, bitset)); | |
131 | void (*andn) PARAMS ((bitset, bitset, bitset)); | |
132 | int (*andn_cmp) PARAMS ((bitset, bitset, bitset)); | |
133 | void (*or) PARAMS ((bitset, bitset, bitset)); | |
134 | int (*or_cmp) PARAMS ((bitset, bitset, bitset)); | |
135 | void (*xor) PARAMS ((bitset, bitset, bitset)); | |
136 | int (*xor_cmp) PARAMS ((bitset, bitset, bitset)); | |
137 | ||
138 | void (*and_or) PARAMS ((bitset, bitset, bitset, bitset)); | |
139 | int (*and_or_cmp) PARAMS ((bitset, bitset, bitset, bitset)); | |
140 | void (*andn_or) PARAMS ((bitset, bitset, bitset, bitset)); | |
141 | int (*andn_or_cmp) PARAMS ((bitset, bitset, bitset, bitset)); | |
142 | void (*or_and) PARAMS ((bitset, bitset, bitset, bitset)); | |
143 | int (*or_and_cmp) PARAMS ((bitset, bitset, bitset, bitset)); | |
144 | ||
145 | bitset_bindex (*list) PARAMS ((bitset, bitset_bindex *, | |
146 | bitset_bindex, bitset_bindex *)); | |
147 | bitset_bindex (*list_reverse) PARAMS ((bitset, | |
148 | bitset_bindex *, bitset_bindex, | |
149 | bitset_bindex *)); | |
150 | void (*free) PARAMS ((bitset)); | |
151 | enum bitset_type type; | |
152 | }; | |
153 | ||
154 | #define BITSET_COMPATIBLE_(BSET1, BSET2) ((BSET1)->b.vtable == (BSET2)->b.vtable) | |
155 | ||
156 | #define BITSET_CHECK2_(DST, SRC) \ | |
157 | if (!BITSET_COMPATIBLE_ (DST, SRC)) abort (); | |
158 | ||
159 | #define BITSET_CHECK3_(DST, SRC1, SRC2) \ | |
160 | if (!BITSET_COMPATIBLE_ (DST, SRC1) \ | |
161 | || !BITSET_COMPATIBLE_ (DST, SRC2)) abort (); | |
162 | ||
163 | #define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) \ | |
164 | if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \ | |
165 | || !BITSET_COMPATIBLE_ (DST, SRC3)) abort (); | |
166 | ||
167 | ||
168 | /* Return size in bits of bitset SRC. */ | |
169 | #define BITSET_SIZE_(SRC) (SRC)->b.vtable->size (SRC) | |
170 | ||
171 | /* Return number of bits set in bitset SRC. */ | |
172 | #define BITSET_COUNT_(SRC) (SRC)->b.vtable->count (SRC) | |
173 | ||
174 | /* Return type of bitset SRC. */ | |
175 | #define BITSET_TYPE_(DST) (DST)->b.vtable->type | |
176 | ||
177 | /* Set bit BITNO in bitset DST. */ | |
178 | #define BITSET_SET_(DST, BITNO) (DST)->b.vtable->set (DST, BITNO) | |
179 | ||
180 | /* Reset bit BITNO in bitset DST. */ | |
181 | #define BITSET_RESET_(DST, BITNO) (DST)->b.vtable->reset (DST, BITNO) | |
182 | ||
183 | /* Toggle bit BITNO in bitset DST. */ | |
184 | #define BITSET_TOGGLE_(DST, BITNO) (DST)->b.vtable->toggle (DST, BITNO) | |
185 | ||
186 | /* Return non-zero if bit BITNO in bitset SRC is set. */ | |
187 | #define BITSET_TEST_(SRC, BITNO) (SRC)->b.vtable->test (SRC, BITNO) | |
188 | ||
189 | /* Free bitset SRC. */ | |
190 | #define BITSET_FREE_(SRC)\ | |
191 | ((SRC)->b.vtable->free ? (SRC)->b.vtable->free (SRC) :(void)0) | |
192 | ||
193 | ||
194 | /* Return SRC == 0. */ | |
195 | #define BITSET_EMPTY_P_(SRC) (SRC)->b.vtable->empty_p (SRC) | |
196 | ||
197 | /* DST = ~0. */ | |
198 | #define BITSET_ONES_(DST) (DST)->b.vtable->ones (DST) | |
199 | ||
200 | /* DST = 0. */ | |
201 | #define BITSET_ZERO_(DST) (DST)->b.vtable->zero (DST) | |
202 | ||
203 | ||
204 | ||
205 | /* DST = SRC. */ | |
206 | #define BITSET_COPY_(DST, SRC) (SRC)->b.vtable->copy (DST, SRC) | |
207 | ||
208 | /* Return DST & SRC == 0. */ | |
209 | #define BITSET_DISJOINT_P_(DST, SRC) (SRC)->b.vtable->disjoint_p (DST, SRC) | |
210 | ||
211 | /* Return DST == SRC. */ | |
212 | #define BITSET_EQUAL_P_(DST, SRC) (SRC)->b.vtable->equal_p (DST, SRC) | |
213 | ||
214 | /* DST = ~SRC. */ | |
215 | #define BITSET_NOT_(DST, SRC) (SRC)->b.vtable->not (DST, SRC) | |
216 | ||
217 | /* Return DST == DST | SRC. */ | |
218 | #define BITSET_SUBSET_P_(DST, SRC) (SRC)->b.vtable->subset_p (DST, SRC) | |
219 | ||
220 | ||
221 | /* DST = SRC1 & SRC2. */ | |
222 | #define BITSET_AND_(DST, SRC1, SRC2) (SRC1)->b.vtable->and (DST, SRC1, SRC2) | |
223 | #define BITSET_AND_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->and_cmp (DST, SRC1, SRC2) | |
224 | ||
225 | /* DST = SRC1 & ~SRC2. */ | |
226 | #define BITSET_ANDN_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn (DST, SRC1, SRC2) | |
227 | #define BITSET_ANDN_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn_cmp (DST, SRC1, SRC2) | |
228 | ||
229 | /* DST = SRC1 | SRC2. */ | |
230 | #define BITSET_OR_(DST, SRC1, SRC2) (SRC1)->b.vtable->or (DST, SRC1, SRC2) | |
231 | #define BITSET_OR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->or_cmp (DST, SRC1, SRC2) | |
232 | ||
233 | /* DST = SRC1 ^ SRC2. */ | |
234 | #define BITSET_XOR_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor (DST, SRC1, SRC2) | |
235 | #define BITSET_XOR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor_cmp (DST, SRC1, SRC2) | |
236 | ||
237 | ||
238 | ||
239 | /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if | |
240 | DST != (SRC1 & SRC2) | SRC3. */ | |
241 | #define BITSET_AND_OR_(DST, SRC1, SRC2, SRC3) \ | |
242 | (SRC1)->b.vtable->and_or (DST, SRC1, SRC2, SRC3) | |
243 | #define BITSET_AND_OR_CMP_(DST, SRC1, SRC2, SRC3) \ | |
244 | (SRC1)->b.vtable->and_or_cmp (DST, SRC1, SRC2, SRC3) | |
245 | ||
246 | /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if | |
247 | DST != (SRC1 & ~SRC2) | SRC3. */ | |
248 | #define BITSET_ANDN_OR_(DST, SRC1, SRC2, SRC3) \ | |
249 | (SRC1)->b.vtable->andn_or (DST, SRC1, SRC2, SRC3) | |
250 | #define BITSET_ANDN_OR_CMP_(DST, SRC1, SRC2, SRC3) \ | |
251 | (SRC1)->b.vtable->andn_or_cmp (DST, SRC1, SRC2, SRC3) | |
252 | ||
253 | /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if | |
254 | DST != (SRC1 | SRC2) & SRC3. */ | |
255 | #define BITSET_OR_AND_(DST, SRC1, SRC2, SRC3) \ | |
256 | (SRC1)->b.vtable->or_and (DST, SRC1, SRC2, SRC3) | |
257 | #define BITSET_OR_AND_CMP_(DST, SRC1, SRC2, SRC3) \ | |
258 | (SRC1)->b.vtable->or_and_cmp (DST, SRC1, SRC2, SRC3) | |
259 | ||
260 | ||
261 | /* Find list of up to NUM bits set in BSET starting from and including | |
262 | *NEXT. Return with actual number of bits found and with *NEXT | |
263 | indicating where search stopped. */ | |
264 | #define BITSET_LIST_(BSET, LIST, NUM, NEXT) \ | |
265 | (BSET)->b.vtable->list (BSET, LIST, NUM, NEXT) | |
266 | ||
267 | /* Find reverse list of up to NUM bits set in BSET starting from and | |
268 | including NEXT. Return with actual number of bits found and with | |
269 | *NEXT indicating where search stopped. */ | |
270 | #define BITSET_LIST_REVERSE_(BSET, LIST, NUM, NEXT) \ | |
271 | (BSET)->b.vtable->list_reverse (BSET, LIST, NUM, NEXT) | |
272 | ||
273 | ||
274 | /* Private functions for bitset implementations. */ | |
275 | ||
276 | extern int bitset_toggle_ PARAMS ((bitset, bitset_bindex)); | |
277 | ||
278 | extern bitset_bindex bitset_count_ PARAMS ((bitset)); | |
279 | ||
280 | extern int bitset_copy_ PARAMS ((bitset, bitset)); | |
281 | ||
282 | extern void bitset_and_or_ PARAMS ((bitset, bitset, bitset, bitset)); | |
283 | ||
284 | extern int bitset_and_or_cmp_ PARAMS ((bitset, bitset, bitset, bitset)); | |
285 | ||
286 | extern void bitset_andn_or_ PARAMS ((bitset, bitset, bitset, bitset)); | |
287 | ||
288 | extern int bitset_andn_or_cmp_ PARAMS ((bitset, bitset, bitset, bitset)); | |
289 | ||
290 | extern void bitset_or_and_ PARAMS ((bitset, bitset, bitset, bitset)); | |
291 | ||
292 | extern int bitset_or_and_cmp_ PARAMS ((bitset, bitset, bitset, bitset)); | |
293 | ||
294 | #endif /* _BBITSET_H */ |