]> git.saurik.com Git - bison.git/blame - lib/bbitset.h
Update.
[bison.git] / lib / bbitset.h
CommitLineData
ef017502 1/* Base bitset stuff.
4f8a9f6d 2 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
ef017502
AD
3 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 2 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, 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 23
d0829076 24#include <stdbool.h>
ef017502 25#include <limits.h>
8a2757b9 26#include <stddef.h>
ef017502 27
25e845d1 28/* Currently we support five flavours of bitsets:
6aa452a6 29 BITSET_ARRAY: Array of bits (fixed size, fast for dense bitsets).
25e845d1
PE
30 Memory for bit array and bitset structure allocated
31 contiguously.
32 BITSET_LIST: Linked list of arrays of bits (variable size, least storage
59fc3dcd 33 for large very sparse sets).
25e845d1 34 BITSET_TABLE: Expandable table of pointers to arrays of bits
59fc3dcd 35 (variable size, less storage for large sparse sets).
25e845d1
PE
36 Faster than BITSET_LIST for random access.
37 BITSET_VARRAY: Variable array of bits (variable size, fast for
38 dense bitsets).
39 BITSET_STATS: Wrapper bitset for internal use only. Used for gathering
40 statistics and/or better run-time checking.
ef017502 41*/
25e845d1
PE
42enum bitset_type {BITSET_ARRAY, BITSET_LIST, BITSET_TABLE, BITSET_VARRAY,
43 BITSET_TYPE_NUM, BITSET_STATS};
44#define BITSET_TYPE_NAMES {"abitset", "lbitset", "ebitset", "vbitset"}
ef017502 45
6aa452a6
AD
46extern const char * const bitset_type_names[];
47
613f5e1a 48enum bitset_alloc_type {BITSET_MALLOC, BITSET_OBALLOC};
ef017502 49
ef017502
AD
50/* Data type used to store a word of bits. */
51typedef unsigned long bitset_word;
50f095c9 52#define BITSET_WORD_BITS ((unsigned) (CHAR_BIT * sizeof (bitset_word)))
ef017502 53
50f095c9
PE
54/* Bit index. In theory we might need a type wider than size_t, but
55 in practice we lose at most a factor of CHAR_BIT by going with
f6ebdb31
PE
56 size_t, and that is good enough. If this type is changed to be
57 wider than size_t, the code needs to be modified to check for
58 overflow when converting bit counts to byte or word counts.
59 The bit and word index types must be unsigned. */
50f095c9 60typedef size_t bitset_bindex;
ef017502
AD
61
62/* Word index. */
50f095c9 63typedef size_t bitset_windex;
ef017502 64
f6ebdb31
PE
65/* Maximum values for commonly-used unsigned types. BITSET_SIZE_MAX
66 always equals SIZE_MAX, but some older systems lack SIZE_MAX. */
67#define BITSET_BINDEX_MAX ((bitset_bindex) -1)
5beedd9b
PE
68
69/* Limit max word index to the maximum value of a signed integer
70 to simplify cache disabling. */
71#define BITSET_WINDEX_MAX (((bitset_windex) -1) >> 1)
f6ebdb31 72#define BITSET_SIZE_MAX ((size_t) -1)
ef017502 73
50f095c9 74#define BITSET_MSB ((bitset_word) 1 << (BITSET_WORD_BITS - 1))
ef017502
AD
75
76#define BITSET_LIST_SIZE 1024
77
59fc3dcd
PE
78enum bitset_ops {BITSET_OP_ZERO, BITSET_OP_ONES,
79 BITSET_OP_COPY, BITSET_OP_NOT,
ef017502
AD
80 BITSET_OP_EMPTY_P, BITSET_OP_EQUAL_P,
81 BITSET_OP_SUBSET_P, BITSET_OP_DISJOINT_P,
59fc3dcd 82 BITSET_OP_AND, BITSET_OP_OR, BITSET_OP_XOR, BITSET_OP_ANDN,
ef017502
AD
83 BITSET_OP_OR_AND, BITSET_OP_AND_OR, BITSET_OP_ANDN_OR};
84
6aa452a6
AD
85struct bbitset_struct
86{
25e845d1 87 const struct bitset_vtable *vtable;
6aa452a6
AD
88 bitset_windex cindex; /* Cache word index. */
89 bitset_windex csize; /* Cache size in words. */
90 bitset_word *cdata; /* Cache data pointer. */
25e845d1 91 bitset_bindex n_bits; /* Number of bits. */
6aa452a6
AD
92 /* Perhaps we could sacrifice another word to indicate
93 that the bitset is known to be zero, that a bit has been set
94 in the cache, and that a bit has been cleared in the cache.
95 This would speed up some of the searches but slightly slow down
96 bit set/reset operations of cached bits. */
97};
98
99
d9d83ef2 100typedef union bitset_union *bitset;
6aa452a6
AD
101
102
25e845d1
PE
103/* Private accessor macros to bitset structure. */
104#define BITSET_VTABLE_(SRC) (SRC)->b.vtable
105#define BITSET_CINDEX_(SRC) (SRC)->b.cindex
106#define BITSET_CDATA_(SRC) (SRC)->b.cdata
107#define BITSET_CSIZE_(SRC) (SRC)->b.csize
108#define BITSET_NBITS_(SRC) (SRC)->b.n_bits
109
110
6aa452a6
AD
111/* The contents of this structure should be considered private. */
112struct bitset_vtable
113{
d9d83ef2
PE
114 void (*set) PARAMS ((bitset, bitset_bindex));
115 void (*reset) PARAMS ((bitset, bitset_bindex));
d0829076
PE
116 bool (*toggle) PARAMS ((bitset, bitset_bindex));
117 bool (*test) PARAMS ((bitset, bitset_bindex));
25e845d1 118 bitset_bindex (*resize) PARAMS ((bitset, bitset_bindex));
d9d83ef2
PE
119 bitset_bindex (*size) PARAMS ((bitset));
120 bitset_bindex (*count) PARAMS ((bitset));
121
d0829076 122 bool (*empty_p) PARAMS ((bitset));
d9d83ef2
PE
123 void (*ones) PARAMS ((bitset));
124 void (*zero) PARAMS ((bitset));
125
126 void (*copy) PARAMS ((bitset, bitset));
d0829076
PE
127 bool (*disjoint_p) PARAMS ((bitset, bitset));
128 bool (*equal_p) PARAMS ((bitset, bitset));
d9d83ef2 129 void (*not) PARAMS ((bitset, bitset));
d0829076 130 bool (*subset_p) PARAMS ((bitset, bitset));
d9d83ef2
PE
131
132 void (*and) PARAMS ((bitset, bitset, bitset));
d0829076 133 bool (*and_cmp) PARAMS ((bitset, bitset, bitset));
d9d83ef2 134 void (*andn) PARAMS ((bitset, bitset, bitset));
d0829076 135 bool (*andn_cmp) PARAMS ((bitset, bitset, bitset));
d9d83ef2 136 void (*or) PARAMS ((bitset, bitset, bitset));
d0829076 137 bool (*or_cmp) PARAMS ((bitset, bitset, bitset));
d9d83ef2 138 void (*xor) PARAMS ((bitset, bitset, bitset));
d0829076 139 bool (*xor_cmp) PARAMS ((bitset, bitset, bitset));
d9d83ef2
PE
140
141 void (*and_or) PARAMS ((bitset, bitset, bitset, bitset));
d0829076 142 bool (*and_or_cmp) PARAMS ((bitset, bitset, bitset, bitset));
d9d83ef2 143 void (*andn_or) PARAMS ((bitset, bitset, bitset, bitset));
d0829076 144 bool (*andn_or_cmp) PARAMS ((bitset, bitset, bitset, bitset));
d9d83ef2 145 void (*or_and) PARAMS ((bitset, bitset, bitset, bitset));
d0829076 146 bool (*or_and_cmp) PARAMS ((bitset, bitset, bitset, bitset));
d9d83ef2
PE
147
148 bitset_bindex (*list) PARAMS ((bitset, bitset_bindex *,
f6ebdb31 149 bitset_bindex, bitset_bindex *));
d9d83ef2 150 bitset_bindex (*list_reverse) PARAMS ((bitset,
f6ebdb31
PE
151 bitset_bindex *, bitset_bindex,
152 bitset_bindex *));
d9d83ef2 153 void (*free) PARAMS ((bitset));
6aa452a6
AD
154 enum bitset_type type;
155};
156
25e845d1
PE
157#define BITSET_COMPATIBLE_(BSET1, BSET2) \
158((BSET1)->b.vtable == (BSET2)->b.vtable)
6aa452a6
AD
159
160#define BITSET_CHECK2_(DST, SRC) \
161if (!BITSET_COMPATIBLE_ (DST, SRC)) abort ();
162
163#define BITSET_CHECK3_(DST, SRC1, SRC2) \
164if (!BITSET_COMPATIBLE_ (DST, SRC1) \
165 || !BITSET_COMPATIBLE_ (DST, SRC2)) abort ();
166
167#define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) \
168if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \
169 || !BITSET_COMPATIBLE_ (DST, SRC3)) abort ();
170
171
25e845d1
PE
172/* Redefine number of bits in bitset DST. */
173#define BITSET_RESIZE_(DST, SIZE) (DST)->b.vtable->resize (DST, SIZE)
174
ef017502 175/* Return size in bits of bitset SRC. */
59fc3dcd 176#define BITSET_SIZE_(SRC) (SRC)->b.vtable->size (SRC)
6aa452a6
AD
177
178/* Return number of bits set in bitset SRC. */
59fc3dcd 179#define BITSET_COUNT_(SRC) (SRC)->b.vtable->count (SRC)
ef017502
AD
180
181/* Return type of bitset SRC. */
59fc3dcd 182#define BITSET_TYPE_(DST) (DST)->b.vtable->type
ef017502
AD
183
184/* Set bit BITNO in bitset DST. */
59fc3dcd 185#define BITSET_SET_(DST, BITNO) (DST)->b.vtable->set (DST, BITNO)
ef017502
AD
186
187/* Reset bit BITNO in bitset DST. */
59fc3dcd 188#define BITSET_RESET_(DST, BITNO) (DST)->b.vtable->reset (DST, BITNO)
6aa452a6
AD
189
190/* Toggle bit BITNO in bitset DST. */
59fc3dcd 191#define BITSET_TOGGLE_(DST, BITNO) (DST)->b.vtable->toggle (DST, BITNO)
ef017502
AD
192
193/* Return non-zero if bit BITNO in bitset SRC is set. */
59fc3dcd 194#define BITSET_TEST_(SRC, BITNO) (SRC)->b.vtable->test (SRC, BITNO)
ef017502
AD
195
196/* Free bitset SRC. */
197#define BITSET_FREE_(SRC)\
6aa452a6 198 ((SRC)->b.vtable->free ? (SRC)->b.vtable->free (SRC) :(void)0)
ef017502 199
ef017502 200
6aa452a6
AD
201/* Return SRC == 0. */
202#define BITSET_EMPTY_P_(SRC) (SRC)->b.vtable->empty_p (SRC)
ef017502 203
6aa452a6
AD
204/* DST = ~0. */
205#define BITSET_ONES_(DST) (DST)->b.vtable->ones (DST)
ef017502
AD
206
207/* DST = 0. */
6aa452a6 208#define BITSET_ZERO_(DST) (DST)->b.vtable->zero (DST)
ef017502 209
ef017502 210
6aa452a6
AD
211
212/* DST = SRC. */
213#define BITSET_COPY_(DST, SRC) (SRC)->b.vtable->copy (DST, SRC)
214
215/* Return DST & SRC == 0. */
216#define BITSET_DISJOINT_P_(DST, SRC) (SRC)->b.vtable->disjoint_p (DST, SRC)
ef017502
AD
217
218/* Return DST == SRC. */
6aa452a6
AD
219#define BITSET_EQUAL_P_(DST, SRC) (SRC)->b.vtable->equal_p (DST, SRC)
220
221/* DST = ~SRC. */
222#define BITSET_NOT_(DST, SRC) (SRC)->b.vtable->not (DST, SRC)
ef017502
AD
223
224/* Return DST == DST | SRC. */
6aa452a6 225#define BITSET_SUBSET_P_(DST, SRC) (SRC)->b.vtable->subset_p (DST, SRC)
ef017502 226
ef017502 227
6aa452a6
AD
228/* DST = SRC1 & SRC2. */
229#define BITSET_AND_(DST, SRC1, SRC2) (SRC1)->b.vtable->and (DST, SRC1, SRC2)
230#define BITSET_AND_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->and_cmp (DST, SRC1, SRC2)
ef017502 231
6aa452a6
AD
232/* DST = SRC1 & ~SRC2. */
233#define BITSET_ANDN_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn (DST, SRC1, SRC2)
234#define BITSET_ANDN_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn_cmp (DST, SRC1, SRC2)
ef017502
AD
235
236/* DST = SRC1 | SRC2. */
6aa452a6
AD
237#define BITSET_OR_(DST, SRC1, SRC2) (SRC1)->b.vtable->or (DST, SRC1, SRC2)
238#define BITSET_OR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->or_cmp (DST, SRC1, SRC2)
ef017502
AD
239
240/* DST = SRC1 ^ SRC2. */
6aa452a6
AD
241#define BITSET_XOR_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor (DST, SRC1, SRC2)
242#define BITSET_XOR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor_cmp (DST, SRC1, SRC2)
ef017502 243
ef017502 244
ef017502 245
ef017502
AD
246/* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
247 DST != (SRC1 & SRC2) | SRC3. */
6aa452a6
AD
248#define BITSET_AND_OR_(DST, SRC1, SRC2, SRC3) \
249 (SRC1)->b.vtable->and_or (DST, SRC1, SRC2, SRC3)
250#define BITSET_AND_OR_CMP_(DST, SRC1, SRC2, SRC3) \
251 (SRC1)->b.vtable->and_or_cmp (DST, SRC1, SRC2, SRC3)
252
253/* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
254 DST != (SRC1 & ~SRC2) | SRC3. */
255#define BITSET_ANDN_OR_(DST, SRC1, SRC2, SRC3) \
256 (SRC1)->b.vtable->andn_or (DST, SRC1, SRC2, SRC3)
257#define BITSET_ANDN_OR_CMP_(DST, SRC1, SRC2, SRC3) \
258 (SRC1)->b.vtable->andn_or_cmp (DST, SRC1, SRC2, SRC3)
ef017502
AD
259
260/* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
261 DST != (SRC1 | SRC2) & SRC3. */
6aa452a6
AD
262#define BITSET_OR_AND_(DST, SRC1, SRC2, SRC3) \
263 (SRC1)->b.vtable->or_and (DST, SRC1, SRC2, SRC3)
264#define BITSET_OR_AND_CMP_(DST, SRC1, SRC2, SRC3) \
265 (SRC1)->b.vtable->or_and_cmp (DST, SRC1, SRC2, SRC3)
ef017502 266
ef017502 267
59fc3dcd 268/* Find list of up to NUM bits set in BSET starting from and including
ef017502
AD
269 *NEXT. Return with actual number of bits found and with *NEXT
270 indicating where search stopped. */
271#define BITSET_LIST_(BSET, LIST, NUM, NEXT) \
59fc3dcd 272 (BSET)->b.vtable->list (BSET, LIST, NUM, NEXT)
ef017502
AD
273
274/* Find reverse list of up to NUM bits set in BSET starting from and
275 including NEXT. Return with actual number of bits found and with
276 *NEXT indicating where search stopped. */
6aa452a6 277#define BITSET_LIST_REVERSE_(BSET, LIST, NUM, NEXT) \
59fc3dcd 278 (BSET)->b.vtable->list_reverse (BSET, LIST, NUM, NEXT)
ef017502
AD
279
280
6aa452a6 281/* Private functions for bitset implementations. */
59fc3dcd 282
d0829076 283extern bool bitset_toggle_ PARAMS ((bitset, bitset_bindex));
ef017502 284
f6ebdb31 285extern bitset_bindex bitset_count_ PARAMS ((bitset));
ef017502 286
25e845d1
PE
287extern bitset_bindex bitset_size_ PARAMS ((bitset));
288
d0829076 289extern bool bitset_copy_ PARAMS ((bitset, bitset));
ef017502 290
d9d83ef2
PE
291extern void bitset_and_or_ PARAMS ((bitset, bitset, bitset, bitset));
292
d0829076 293extern bool bitset_and_or_cmp_ PARAMS ((bitset, bitset, bitset, bitset));
ef017502 294
d9d83ef2
PE
295extern void bitset_andn_or_ PARAMS ((bitset, bitset, bitset, bitset));
296
d0829076 297extern bool bitset_andn_or_cmp_ PARAMS ((bitset, bitset, bitset, bitset));
ef017502 298
d9d83ef2
PE
299extern void bitset_or_and_ PARAMS ((bitset, bitset, bitset, bitset));
300
d0829076 301extern bool bitset_or_and_cmp_ PARAMS ((bitset, bitset, bitset, bitset));
ef017502
AD
302
303#endif /* _BBITSET_H */