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