]> git.saurik.com Git - bison.git/blame - lib/bitset.h
Add strdup.c, strdup.h.
[bison.git] / lib / bitset.h
CommitLineData
7086e707 1/* Generic bitsets.
7d7d6663 2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
7086e707
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 _BITSET_H
04af9e52 20#define _BITSET_H
7086e707 21
ef017502
AD
22/* This file is the public interface to the bitset abstract data type.
23 Only use the functions and macros defined in this file. */
7086e707 24
ef017502 25#include "bbitset.h"
7086e707
AD
26#include "obstack.h"
27#include <stdio.h>
7086e707 28
ef017502 29/* Attributes used to select a bitset implementation. */
7086e707
AD
30enum bitset_attr {BITSET_FIXED = 1, /* Bitset size fixed. */
31 BITSET_VARIABLE = 2, /* Bitset size variable. */
32 BITSET_DENSE = 4, /* Bitset dense. */
33 BITSET_SPARSE = 8, /* Bitset sparse. */
34 BITSET_FRUGAL = 16, /* Prefer most compact. */
6aa452a6 35 BITSET_GREEDY = 32}; /* Prefer fastest at memory expense. */
7086e707
AD
36
37typedef unsigned int bitset_attrs;
38
47c8386f
PE
39/* The contents of the union should be considered to be private.
40 While I would like to make this union opaque, it needs to be
41 visible for the inline bit set/test functions, and for delegation
42 to the proper implementation. */
43union bitset_union
7086e707 44{
47c8386f
PE
45 /* This must be the first member of every other structure that is a
46 member of this union. */
144c1e76 47 struct bbitset_struct b; /* Base bitset data. */
47c8386f
PE
48
49 struct abitset_struct
50 {
51 struct bbitset_struct b;
47c8386f
PE
52 bitset_word words[1]; /* The array of bits. */
53 } a;
54
55 struct ebitset_struct
56 {
57 struct bbitset_struct b;
58 bitset_windex size; /* Number of elements. */
59 struct ebitset_elt_struct **elts; /* Expanding array of ptrs to elts. */
60 } e;
61
62 struct lbitset_struct
63 {
64 struct bbitset_struct b;
65 struct lbitset_elt_struct *head; /* First element in linked list. */
66 struct lbitset_elt_struct *tail; /* Last element in linked list. */
67 } l;
68
69 struct bitset_stats_struct
70 {
71 struct bbitset_struct b;
72 bitset bset;
73 } s;
144c1e76
PE
74
75 struct vbitset_struct
76 {
77 struct bbitset_struct b;
78 bitset_windex size; /* Allocated size of array. */
79 } v;
80
7086e707
AD
81};
82
613f5e1a
AD
83
84/* The contents of this structure should be considered private.
85 It is used for iterating over set bits. */
86typedef struct
87{
88 bitset_bindex list[BITSET_LIST_SIZE];
89 bitset_bindex next;
808a5918
PE
90 bitset_bindex num;
91 bitset_bindex i;
613f5e1a
AD
92} bitset_iterator;
93
94
7086e707 95/* Return bytes required for bitset of desired type and size. */
7d7d6663 96extern size_t bitset_bytes (enum bitset_type, bitset_bindex);
7086e707
AD
97
98/* Initialise a bitset with desired type and size. */
7d7d6663 99extern bitset bitset_init (bitset, bitset_bindex, enum bitset_type);
7086e707 100
ef017502
AD
101/* Select an implementation type based on the desired bitset size
102 and attributes. */
7d7d6663 103extern enum bitset_type bitset_type_choose (bitset_bindex, bitset_attrs);
7086e707 104
ef017502 105/* Create a bitset of desired type and size. The bitset is zeroed. */
7d7d6663 106extern bitset bitset_alloc (bitset_bindex, enum bitset_type);
7086e707
AD
107
108/* Free bitset. */
7d7d6663 109extern void bitset_free (bitset);
7086e707 110
ef017502
AD
111/* Create a bitset of desired type and size using an obstack. The
112 bitset is zeroed. */
7d7d6663
PE
113extern bitset bitset_obstack_alloc (struct obstack *bobstack,
114 bitset_bindex, enum bitset_type);
7086e707
AD
115
116/* Free bitset allocated on obstack. */
7d7d6663 117extern void bitset_obstack_free (bitset);
7086e707 118
ef017502 119/* Create a bitset of desired size and attributes. The bitset is zeroed. */
7d7d6663 120extern bitset bitset_create (bitset_bindex, bitset_attrs);
7086e707 121
613f5e1a 122/* Return bitset type. */
7d7d6663 123extern enum bitset_type bitset_type_get (bitset);
613f5e1a 124
6aa452a6 125/* Return bitset type name. */
7d7d6663 126extern const char *bitset_type_name_get (bitset);
6aa452a6 127
7086e707
AD
128
129/* Set bit BITNO in bitset BSET. */
04af9e52
PE
130static inline void
131bitset_set (bitset bset, bitset_bindex bitno)
7086e707 132{
dbba6a3b
PE
133 bitset_windex windex = bitno / BITSET_WORD_BITS;
134 bitset_windex offset = windex - bset->b.cindex;
04af9e52 135
ef017502 136 if (offset < bset->b.csize)
e601ff27 137 bset->b.cdata[offset] |= ((bitset_word) 1 << (bitno % BITSET_WORD_BITS));
7086e707 138 else
ef017502 139 BITSET_SET_ (bset, bitno);
7086e707
AD
140}
141
142
143/* Reset bit BITNO in bitset BSET. */
04af9e52
PE
144static inline void
145bitset_reset (bitset bset, bitset_bindex bitno)
7086e707 146{
dbba6a3b
PE
147 bitset_windex windex = bitno / BITSET_WORD_BITS;
148 bitset_windex offset = windex - bset->b.cindex;
04af9e52 149
ef017502 150 if (offset < bset->b.csize)
e601ff27 151 bset->b.cdata[offset] &= ~((bitset_word) 1 << (bitno % BITSET_WORD_BITS));
7086e707 152 else
ef017502 153 BITSET_RESET_ (bset, bitno);
7086e707
AD
154}
155
156
157/* Test bit BITNO in bitset BSET. */
d0829076 158static inline bool
04af9e52 159bitset_test (bitset bset, bitset_bindex bitno)
7086e707 160{
dbba6a3b
PE
161 bitset_windex windex = bitno / BITSET_WORD_BITS;
162 bitset_windex offset = windex - bset->b.cindex;
04af9e52 163
ef017502
AD
164 if (offset < bset->b.csize)
165 return (bset->b.cdata[offset] >> (bitno % BITSET_WORD_BITS)) & 1;
7086e707 166 else
ef017502 167 return BITSET_TEST_ (bset, bitno);
7086e707 168}
7086e707 169
7086e707 170
345cea78 171/* Toggle bit BITNO in bitset BSET and return non-zero if now set. */
6aa452a6 172#define bitset_toggle(bset, bitno) BITSET_TOGGLE_ (bset, bitno)
345cea78 173
6aa452a6
AD
174/* Return size in bits of bitset SRC. */
175#define bitset_size(SRC) BITSET_SIZE_ (SRC)
176
144c1e76 177/* Change size of bitset. */
7d7d6663 178extern void bitset_resize (bitset, bitset_bindex);
144c1e76 179
6aa452a6
AD
180/* Return number of bits set in bitset SRC. */
181#define bitset_count(SRC) BITSET_COUNT_ (SRC)
182
183
184/* Return SRC == 0. */
185#define bitset_empty_p(SRC) BITSET_EMPTY_P_ (SRC)
7086e707
AD
186
187/* DST = ~0. */
6aa452a6 188#define bitset_ones(DST) BITSET_ONES_ (DST)
7086e707 189
6aa452a6
AD
190/* DST = 0. */
191#define bitset_zero(DST) BITSET_ZERO_ (DST)
7086e707 192
7086e707 193
6aa452a6
AD
194
195/* DST = SRC. */
196#define bitset_copy(DST, SRC) BITSET_COPY_ (DST, SRC)
7086e707 197
ef017502 198/* Return DST & SRC == 0. */
6aa452a6 199#define bitset_disjoint_p(DST, SRC) BITSET_DISJOINT_P_ (DST, SRC)
ef017502 200
6aa452a6
AD
201/* Return DST == SRC. */
202#define bitset_equal_p(DST, SRC) BITSET_EQUAL_P_ (DST, SRC)
7086e707
AD
203
204/* DST = ~SRC. */
6aa452a6
AD
205#define bitset_not(DST, SRC) BITSET_NOT_ (DST, SRC)
206
207/* Return DST == DST | SRC. */
208#define bitset_subset_p(DST, SRC) BITSET_SUBSET_P_ (DST, SRC)
7086e707 209
6aa452a6
AD
210
211
212/* DST = SRC1 & SRC2. */
213#define bitset_and(DST, SRC1, SRC2) BITSET_AND_ (DST, SRC1, SRC2)
7086e707
AD
214
215/* DST = SRC1 & SRC2. Return non-zero if DST != SRC1 & SRC2. */
6aa452a6 216#define bitset_and_cmp(DST, SRC1, SRC2) BITSET_AND_CMP_ (DST, SRC1, SRC2)
7086e707 217
6aa452a6
AD
218/* DST = SRC1 & ~SRC2. */
219#define bitset_andn(DST, SRC1, SRC2) BITSET_ANDN_ (DST, SRC1, SRC2)
7086e707
AD
220
221/* DST = SRC1 & ~SRC2. Return non-zero if DST != SRC1 & ~SRC2. */
6aa452a6 222#define bitset_andn_cmp(DST, SRC1, SRC2) BITSET_ANDN_CMP_ (DST, SRC1, SRC2)
7086e707 223
6aa452a6
AD
224/* DST = SRC1 | SRC2. */
225#define bitset_or(DST, SRC1, SRC2) BITSET_OR_ (DST, SRC1, SRC2)
226
227/* DST = SRC1 | SRC2. Return non-zero if DST != SRC1 | SRC2. */
228#define bitset_or_cmp(DST, SRC1, SRC2) BITSET_OR_CMP_ (DST, SRC1, SRC2)
229
230/* DST = SRC1 ^ SRC2. */
231#define bitset_xor(DST, SRC1, SRC2) BITSET_XOR_ (DST, SRC1, SRC2)
232
233/* DST = SRC1 ^ SRC2. Return non-zero if DST != SRC1 ^ SRC2. */
234#define bitset_xor_cmp(DST, SRC1, SRC2) BITSET_XOR_CMP_ (DST, SRC1, SRC2)
235
236
237
238/* DST = (SRC1 & SRC2) | SRC3. */
239#define bitset_and_or(DST, SRC1, SRC2, SRC3) \
240 BITSET_AND_OR_ (DST, SRC1, SRC2, SRC3)
7086e707
AD
241
242/* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
243 DST != (SRC1 & SRC2) | SRC3. */
6aa452a6
AD
244#define bitset_and_or_cmp(DST, SRC1, SRC2, SRC3) \
245 BITSET_AND_OR_CMP_ (DST, SRC1, SRC2, SRC3)
246
247/* DST = (SRC1 & ~SRC2) | SRC3. */
248#define bitset_andn_or(DST, SRC1, SRC2, SRC3) \
249 BITSET_ANDN_OR_ (DST, SRC1, SRC2, SRC3)
7086e707
AD
250
251/* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
252 DST != (SRC1 & ~SRC2) | SRC3. */
6aa452a6
AD
253#define bitset_andn_or_cmp(DST, SRC1, SRC2, SRC3) \
254 BITSET_ANDN_OR_CMP_ (DST, SRC1, SRC2, SRC3)
7086e707 255
6aa452a6
AD
256/* DST = (SRC1 | SRC2) & SRC3. */
257#define bitset_or_and(DST, SRC1, SRC2, SRC3)\
258 BITSET_OR_AND_ (DST, SRC1, SRC2, SRC3)
7086e707 259
6aa452a6
AD
260/* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
261 DST != (SRC1 | SRC2) & SRC3. */
262#define bitset_or_and_cmp(DST, SRC1, SRC2, SRC3)\
263 BITSET_OR_AND_CMP_ (DST, SRC1, SRC2, SRC3)
345cea78 264
04af9e52 265/* Find list of up to NUM bits set in BSET starting from and including
7086e707
AD
266 *NEXT. Return with actual number of bits found and with *NEXT
267 indicating where search stopped. */
7086e707 268#define bitset_list(BSET, LIST, NUM, NEXT) \
04af9e52 269 BITSET_LIST_ (BSET, LIST, NUM, NEXT)
7086e707
AD
270
271/* Find reverse list of up to NUM bits set in BSET starting from and
272 including NEXT. Return with actual number of bits found and with
273 *NEXT indicating where search stopped. */
6aa452a6 274#define bitset_list_reverse(BSET, LIST, NUM, NEXT) \
04af9e52 275 BITSET_LIST_REVERSE_ (BSET, LIST, NUM, NEXT)
6aa452a6 276
144c1e76 277/* Return true if both bitsets are of the same type and size. */
fa8db4ba 278extern bool bitset_compatible_p (bitset bset1, bitset bset2);
7086e707 279
144c1e76 280/* Find next set bit from the given bit index. */
7d7d6663 281extern bitset_bindex bitset_next (bitset, bitset_bindex);
47c8386f 282
144c1e76 283/* Find previous set bit from the given bit index. */
7d7d6663 284extern bitset_bindex bitset_prev (bitset, bitset_bindex);
47c8386f 285
7086e707 286/* Find first set bit. */
7d7d6663 287extern bitset_bindex bitset_first (bitset);
7086e707
AD
288
289/* Find last set bit. */
7d7d6663 290extern bitset_bindex bitset_last (bitset);
7086e707 291
47c8386f 292/* Return nonzero if this is the only set bit. */
7d7d6663 293extern bool bitset_only_set_p (bitset, bitset_bindex);
47c8386f 294
7086e707 295/* Dump bitset. */
7d7d6663 296extern void bitset_dump (FILE *, bitset);
7086e707 297
144c1e76 298/* Loop over all elements of BSET, starting with MIN, setting INDEX
6aa452a6
AD
299 to the index of each set bit. For example, the following will print
300 the bits set in a bitset:
301
fa8db4ba 302 bitset_bindex i;
6aa452a6
AD
303 bitset_iterator iter;
304
fa8db4ba 305 BITSET_FOR_EACH (iter, src, i, 0)
6aa452a6 306 {
fa8db4ba 307 printf ("%lu ", (unsigned long int) i);
6aa452a6
AD
308 };
309*/
144c1e76 310#define BITSET_FOR_EACH(ITER, BSET, INDEX, MIN) \
613f5e1a
AD
311 for (ITER.next = (MIN), ITER.num = BITSET_LIST_SIZE; \
312 (ITER.num == BITSET_LIST_SIZE) \
313 && (ITER.num = bitset_list (BSET, ITER.list, \
04af9e52 314 BITSET_LIST_SIZE, &ITER.next));) \
2175bfbd 315 for (ITER.i = 0; \
144c1e76 316 ITER.i < ITER.num && ((INDEX) = ITER.list[ITER.i], 1); \
2175bfbd 317 ITER.i++)
7086e707
AD
318
319
320/* Loop over all elements of BSET, in reverse order starting with
fa8db4ba 321 MIN, setting INDEX to the index of each set bit. For example, the
6aa452a6
AD
322 following will print the bits set in a bitset in reverse order:
323
fa8db4ba 324 bitset_bindex i;
6aa452a6
AD
325 bitset_iterator iter;
326
fa8db4ba 327 BITSET_FOR_EACH_REVERSE (iter, src, i, 0)
6aa452a6 328 {
fa8db4ba 329 printf ("%lu ", (unsigned long int) i);
6aa452a6
AD
330 };
331*/
144c1e76 332#define BITSET_FOR_EACH_REVERSE(ITER, BSET, INDEX, MIN) \
613f5e1a
AD
333 for (ITER.next = (MIN), ITER.num = BITSET_LIST_SIZE; \
334 (ITER.num == BITSET_LIST_SIZE) \
335 && (ITER.num = bitset_list_reverse (BSET, ITER.list, \
144c1e76 336 BITSET_LIST_SIZE, &ITER.next));) \
2175bfbd 337 for (ITER.i = 0; \
144c1e76 338 ITER.i < ITER.num && ((INDEX) = ITER.list[ITER.i], 1); \
2175bfbd 339 ITER.i++)
7086e707
AD
340
341
ef017502 342/* Define set operations in terms of logical operations. */
7086e707 343
04af9e52
PE
344#define bitset_diff(DST, SRC1, SRC2) bitset_andn (DST, SRC1, SRC2)
345#define bitset_diff_cmp(DST, SRC1, SRC2) bitset_andn_cmp (DST, SRC1, SRC2)
7086e707 346
04af9e52
PE
347#define bitset_intersection(DST, SRC1, SRC2) bitset_and (DST, SRC1, SRC2)
348#define bitset_intersection_cmp(DST, SRC1, SRC2) bitset_and_cmp (DST, SRC1, SRC2)
7086e707 349
04af9e52
PE
350#define bitset_union(DST, SRC1, SRC2) bitset_or (DST, SRC1, SRC2)
351#define bitset_union_cmp(DST, SRC1, SRC2) bitset_or_cmp (DST, SRC1, SRC2)
7086e707 352
6aa452a6 353/* Symmetrical difference. */
04af9e52
PE
354#define bitset_symdiff(DST, SRC1, SRC2) bitset_xor (DST, SRC1, SRC2)
355#define bitset_symdiff_cmp(DST, SRC1, SRC2) bitset_xor_cmp (DST, SRC1, SRC2)
6aa452a6
AD
356
357/* Union of difference. */
7086e707 358#define bitset_diff_union(DST, SRC1, SRC2, SRC3) \
04af9e52 359 bitset_andn_or (DST, SRC1, SRC2, SRC3)
6aa452a6 360#define bitset_diff_union_cmp(DST, SRC1, SRC2, SRC3) \
04af9e52 361 bitset_andn_or_cmp (DST, SRC1, SRC2, SRC3)
6aa452a6 362
ef017502 363
7086e707 364/* Release any memory tied up with bitsets. */
7d7d6663 365extern void bitset_release_memory (void);
7086e707 366
613f5e1a 367/* Enable bitset stats gathering. */
7d7d6663 368extern void bitset_stats_enable (void);
613f5e1a
AD
369
370/* Disable bitset stats gathering. */
7d7d6663 371extern void bitset_stats_disable (void);
613f5e1a
AD
372
373/* Read bitset stats file of accummulated stats. */
7d7d6663 374void bitset_stats_read (const char *filename);
613f5e1a
AD
375
376/* Write bitset stats file of accummulated stats. */
7d7d6663 377void bitset_stats_write (const char *filename);
7086e707
AD
378
379/* Dump bitset stats. */
7d7d6663 380extern void bitset_stats_dump (FILE *);
7086e707
AD
381
382/* Function to debug bitset from debugger. */
7d7d6663 383extern void debug_bitset (bitset);
7086e707
AD
384
385/* Function to debug bitset stats from debugger. */
7d7d6663 386extern void debug_bitset_stats (void);
7086e707 387
7086e707 388#endif /* _BITSET_H */
613f5e1a 389