]> git.saurik.com Git - bison.git/blame - lib/bbitset.h
(lbitset_bytes): Use size_t, not unsigned int, to count bytes.
[bison.git] / lib / bbitset.h
CommitLineData
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
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
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
6aa452a6 31 for large very sparse sets).
613f5e1a 32 BITSET_TABLE: Expandable table of pointers to array of bits
6aa452a6 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
37enum 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
41extern const char * const bitset_type_names[];
42
613f5e1a 43enum 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. */
49typedef 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 58typedef size_t bitset_bindex;
ef017502
AD
59
60/* Word index. */
50f095c9 61typedef 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)
66#define BITSET_WINDEX_MAX ((bitset_windex) -1)
67#define BITSET_SIZE_MAX ((size_t) -1)
ef017502 68
50f095c9 69#define BITSET_MSB ((bitset_word) 1 << (BITSET_WORD_BITS - 1))
ef017502
AD
70
71#define BITSET_LIST_SIZE 1024
72
613f5e1a
AD
73enum bitset_ops {BITSET_OP_ZERO, BITSET_OP_ONES,
74 BITSET_OP_COPY, BITSET_OP_NOT,
ef017502
AD
75 BITSET_OP_EMPTY_P, BITSET_OP_EQUAL_P,
76 BITSET_OP_SUBSET_P, BITSET_OP_DISJOINT_P,
613f5e1a 77 BITSET_OP_AND, BITSET_OP_OR, BITSET_OP_XOR, BITSET_OP_ANDN,
ef017502
AD
78 BITSET_OP_OR_AND, BITSET_OP_AND_OR, BITSET_OP_ANDN_OR};
79
6aa452a6
AD
80struct bbitset_struct
81{
82 const struct bitset_vtable * vtable;
83 bitset_windex cindex; /* Cache word index. */
84 bitset_windex csize; /* Cache size in words. */
85 bitset_word *cdata; /* Cache data pointer. */
86 /* Perhaps we could sacrifice another word to indicate
87 that the bitset is known to be zero, that a bit has been set
88 in the cache, and that a bit has been cleared in the cache.
89 This would speed up some of the searches but slightly slow down
90 bit set/reset operations of cached bits. */
91};
92
93
94typedef struct bitset_struct *bitset;
95
96
97/* The contents of this structure should be considered private. */
98struct bitset_vtable
99{
100 void (*set) PARAMS ((struct bitset_struct *, bitset_bindex));
101 void (*reset) PARAMS ((struct bitset_struct *, bitset_bindex));
102 int (*toggle) PARAMS ((struct bitset_struct *, bitset_bindex));
103 int (*test) PARAMS ((struct bitset_struct *, bitset_bindex));
f6ebdb31
PE
104 bitset_bindex (*size) PARAMS ((struct bitset_struct *));
105 bitset_bindex (*count) PARAMS ((struct bitset_struct *));
6aa452a6
AD
106
107 int (*empty_p) PARAMS ((struct bitset_struct *));
108 void (*ones) PARAMS ((struct bitset_struct *));
109 void (*zero) PARAMS ((struct bitset_struct *));
110
111 void (*copy) PARAMS ((struct bitset_struct *, struct bitset_struct *));
112 int (*disjoint_p) PARAMS ((struct bitset_struct *, struct bitset_struct *));
113 int (*equal_p) PARAMS ((struct bitset_struct *, struct bitset_struct *));
114 void (*not) PARAMS ((struct bitset_struct *, struct bitset_struct *));
115 int (*subset_p) PARAMS ((struct bitset_struct *, struct bitset_struct *));
116
117 void (*and) PARAMS ((struct bitset_struct *, struct bitset_struct *,
118 struct bitset_struct *));
119 int (*and_cmp) PARAMS ((struct bitset_struct *, struct bitset_struct *,
120 struct bitset_struct *));
121 void (*andn) PARAMS ((struct bitset_struct *, struct bitset_struct *,
122 struct bitset_struct *));
123 int (*andn_cmp) PARAMS ((struct bitset_struct *, struct bitset_struct *,
124 struct bitset_struct *));
125 void (*or) PARAMS ((struct bitset_struct *, struct bitset_struct *,
126 struct bitset_struct *));
127 int (*or_cmp) PARAMS ((struct bitset_struct *, struct bitset_struct *,
128 struct bitset_struct *));
129 void (*xor) PARAMS ((struct bitset_struct *, struct bitset_struct *,
130 struct bitset_struct *));
131 int (*xor_cmp) PARAMS ((struct bitset_struct *, struct bitset_struct *,
132 struct bitset_struct *));
133
134 void (*and_or) PARAMS ((struct bitset_struct *, struct bitset_struct *,
135 struct bitset_struct *, struct bitset_struct *));
136 int (*and_or_cmp) PARAMS ((struct bitset_struct *, struct bitset_struct *,
137 struct bitset_struct *, struct bitset_struct *));
138 void (*andn_or) PARAMS ((struct bitset_struct *, struct bitset_struct *,
139 struct bitset_struct *, struct bitset_struct *));
140 int (*andn_or_cmp) PARAMS ((struct bitset_struct *, struct bitset_struct *,
141 struct bitset_struct *, struct bitset_struct *));
142 void (*or_and) PARAMS ((struct bitset_struct *, struct bitset_struct *,
143 struct bitset_struct *, struct bitset_struct *));
144 int (*or_and_cmp) PARAMS ((struct bitset_struct *, struct bitset_struct *,
145 struct bitset_struct *, struct bitset_struct *));
146
f6ebdb31
PE
147 bitset_bindex (*list) PARAMS ((struct bitset_struct *, bitset_bindex *,
148 bitset_bindex, bitset_bindex *));
149 bitset_bindex (*list_reverse) PARAMS ((struct bitset_struct *,
150 bitset_bindex *, bitset_bindex,
151 bitset_bindex *));
6aa452a6
AD
152 void (*free) PARAMS ((struct bitset_struct *));
153 enum bitset_type type;
154};
155
156#define BITSET_COMPATIBLE_(BSET1, BSET2) ((BSET1)->b.vtable == (BSET2)->b.vtable)
157
158#define BITSET_CHECK2_(DST, SRC) \
159if (!BITSET_COMPATIBLE_ (DST, SRC)) abort ();
160
161#define BITSET_CHECK3_(DST, SRC1, SRC2) \
162if (!BITSET_COMPATIBLE_ (DST, SRC1) \
163 || !BITSET_COMPATIBLE_ (DST, SRC2)) abort ();
164
165#define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) \
166if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \
167 || !BITSET_COMPATIBLE_ (DST, SRC3)) abort ();
168
169
ef017502 170/* Return size in bits of bitset SRC. */
6aa452a6
AD
171#define BITSET_SIZE_(SRC) (SRC)->b.vtable->size (SRC)
172
173/* Return number of bits set in bitset SRC. */
174#define BITSET_COUNT_(SRC) (SRC)->b.vtable->count (SRC)
ef017502
AD
175
176/* Return type of bitset SRC. */
6aa452a6 177#define BITSET_TYPE_(DST) (DST)->b.vtable->type
ef017502
AD
178
179/* Set bit BITNO in bitset DST. */
6aa452a6 180#define BITSET_SET_(DST, BITNO) (DST)->b.vtable->set (DST, BITNO)
ef017502
AD
181
182/* Reset bit BITNO in bitset DST. */
6aa452a6
AD
183#define BITSET_RESET_(DST, BITNO) (DST)->b.vtable->reset (DST, BITNO)
184
185/* Toggle bit BITNO in bitset DST. */
186#define BITSET_TOGGLE_(DST, BITNO) (DST)->b.vtable->toggle (DST, BITNO)
ef017502
AD
187
188/* Return non-zero if bit BITNO in bitset SRC is set. */
6aa452a6 189#define BITSET_TEST_(SRC, BITNO) (SRC)->b.vtable->test (SRC, BITNO)
ef017502
AD
190
191/* Free bitset SRC. */
192#define BITSET_FREE_(SRC)\
6aa452a6 193 ((SRC)->b.vtable->free ? (SRC)->b.vtable->free (SRC) :(void)0)
ef017502 194
ef017502 195
6aa452a6
AD
196/* Return SRC == 0. */
197#define BITSET_EMPTY_P_(SRC) (SRC)->b.vtable->empty_p (SRC)
ef017502 198
6aa452a6
AD
199/* DST = ~0. */
200#define BITSET_ONES_(DST) (DST)->b.vtable->ones (DST)
ef017502
AD
201
202/* DST = 0. */
6aa452a6 203#define BITSET_ZERO_(DST) (DST)->b.vtable->zero (DST)
ef017502 204
ef017502 205
6aa452a6
AD
206
207/* DST = SRC. */
208#define BITSET_COPY_(DST, SRC) (SRC)->b.vtable->copy (DST, SRC)
209
210/* Return DST & SRC == 0. */
211#define BITSET_DISJOINT_P_(DST, SRC) (SRC)->b.vtable->disjoint_p (DST, SRC)
ef017502
AD
212
213/* Return DST == SRC. */
6aa452a6
AD
214#define BITSET_EQUAL_P_(DST, SRC) (SRC)->b.vtable->equal_p (DST, SRC)
215
216/* DST = ~SRC. */
217#define BITSET_NOT_(DST, SRC) (SRC)->b.vtable->not (DST, SRC)
ef017502
AD
218
219/* Return DST == DST | SRC. */
6aa452a6 220#define BITSET_SUBSET_P_(DST, SRC) (SRC)->b.vtable->subset_p (DST, SRC)
ef017502 221
ef017502 222
6aa452a6
AD
223/* DST = SRC1 & SRC2. */
224#define BITSET_AND_(DST, SRC1, SRC2) (SRC1)->b.vtable->and (DST, SRC1, SRC2)
225#define BITSET_AND_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->and_cmp (DST, SRC1, SRC2)
ef017502 226
6aa452a6
AD
227/* DST = SRC1 & ~SRC2. */
228#define BITSET_ANDN_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn (DST, SRC1, SRC2)
229#define BITSET_ANDN_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn_cmp (DST, SRC1, SRC2)
ef017502
AD
230
231/* DST = SRC1 | SRC2. */
6aa452a6
AD
232#define BITSET_OR_(DST, SRC1, SRC2) (SRC1)->b.vtable->or (DST, SRC1, SRC2)
233#define BITSET_OR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->or_cmp (DST, SRC1, SRC2)
ef017502
AD
234
235/* DST = SRC1 ^ SRC2. */
6aa452a6
AD
236#define BITSET_XOR_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor (DST, SRC1, SRC2)
237#define BITSET_XOR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor_cmp (DST, SRC1, SRC2)
ef017502 238
ef017502 239
ef017502 240
ef017502
AD
241/* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
242 DST != (SRC1 & SRC2) | SRC3. */
6aa452a6
AD
243#define BITSET_AND_OR_(DST, SRC1, SRC2, SRC3) \
244 (SRC1)->b.vtable->and_or (DST, SRC1, SRC2, SRC3)
245#define BITSET_AND_OR_CMP_(DST, SRC1, SRC2, SRC3) \
246 (SRC1)->b.vtable->and_or_cmp (DST, SRC1, SRC2, SRC3)
247
248/* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
249 DST != (SRC1 & ~SRC2) | SRC3. */
250#define BITSET_ANDN_OR_(DST, SRC1, SRC2, SRC3) \
251 (SRC1)->b.vtable->andn_or (DST, SRC1, SRC2, SRC3)
252#define BITSET_ANDN_OR_CMP_(DST, SRC1, SRC2, SRC3) \
253 (SRC1)->b.vtable->andn_or_cmp (DST, SRC1, SRC2, SRC3)
ef017502
AD
254
255/* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
256 DST != (SRC1 | SRC2) & SRC3. */
6aa452a6
AD
257#define BITSET_OR_AND_(DST, SRC1, SRC2, SRC3) \
258 (SRC1)->b.vtable->or_and (DST, SRC1, SRC2, SRC3)
259#define BITSET_OR_AND_CMP_(DST, SRC1, SRC2, SRC3) \
260 (SRC1)->b.vtable->or_and_cmp (DST, SRC1, SRC2, SRC3)
ef017502 261
ef017502 262
613f5e1a 263/* Find list of up to NUM bits set in BSET starting from and including
ef017502
AD
264 *NEXT. Return with actual number of bits found and with *NEXT
265 indicating where search stopped. */
266#define BITSET_LIST_(BSET, LIST, NUM, NEXT) \
6aa452a6 267 (BSET)->b.vtable->list (BSET, LIST, NUM, NEXT)
ef017502
AD
268
269/* Find reverse list of up to NUM bits set in BSET starting from and
270 including NEXT. Return with actual number of bits found and with
271 *NEXT indicating where search stopped. */
6aa452a6
AD
272#define BITSET_LIST_REVERSE_(BSET, LIST, NUM, NEXT) \
273 (BSET)->b.vtable->list_reverse (BSET, LIST, NUM, NEXT)
ef017502
AD
274
275
6aa452a6
AD
276/* Private functions for bitset implementations. */
277
278extern int bitset_toggle_ PARAMS ((bitset, bitset_bindex));
ef017502 279
f6ebdb31 280extern bitset_bindex bitset_count_ PARAMS ((bitset));
ef017502 281
6aa452a6 282extern int bitset_copy_ PARAMS ((bitset, bitset));
ef017502 283
6aa452a6 284extern int bitset_and_or_cmp_ PARAMS ((bitset, bitset, bitset, bitset));
ef017502 285
6aa452a6 286extern int bitset_andn_or_cmp_ PARAMS ((bitset, bitset, bitset, bitset));
ef017502 287
6aa452a6 288extern int bitset_or_and_cmp_ PARAMS ((bitset, bitset, bitset, bitset));
ef017502
AD
289
290#endif /* _BBITSET_H */