]> git.saurik.com Git - bison.git/blob - lib/bbitset.h
* src/options.h, src/options.c: Remove.
[bison.git] / lib / bbitset.h
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 /* Bison modifications: BEGIN. */
23 #define IN_BISON 1
24 #if IN_BISON
25 # if HAVE_CONFIG_H
26 # include <config.h>
27 # endif
28 # include <stdlib.h>
29
30 # define BITSET_STATS 1
31 /* This file is the public interface to the bitset abstract data type.
32 Only use the functions and macros defined in this file. */
33
34 # ifndef PARAMS
35 # if defined PROTOTYPES || (defined __STDC__ && __STDC__)
36 # define PARAMS(Args) Args
37 # else
38 # define PARAMS(Args) ()
39 # endif
40 # endif
41
42 # ifndef __attribute__
43 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
44 # define __attribute__(x)
45 # endif
46 # endif
47
48 # ifndef ATTRIBUTE_NORETURN
49 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
50 # endif
51
52 # ifndef ATTRIBUTE_UNUSED
53 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
54 # endif
55 # include "xalloc.h"
56 #else /* ! IN_BISON */
57 # include "libiberty.h"
58 #endif
59
60 #ifdef HAVE_LIMITS_H
61 #include <limits.h>
62 #endif
63
64 /* Currently we support three flavours of bitsets:
65 BITSET_ARRAY: Array of bits (fixed size, faster).
66 BITSET_LIST: Linked list of array of bits (variable size, least storage
67 for large very sparse sets).
68 BITSET_TABLE: Expandable table of pointers to array of bits
69 (variable size, less storage for large sparse sets).
70 */
71 enum bitset_type {BITSET_ARRAY, BITSET_LIST, BITSET_TABLE, BITSET_TYPE_NUM};
72 #define BITSET_TYPE_NAMES {"sbitset", "lbitset", "ebitset"}
73
74 /* Non-zero to enable bitset caching. */
75 #define BITSET_CACHE 1
76
77 /* Non-zero to use inline functions instead of macros. */
78 #define BITSET_INLINE 0
79
80 /* Non-zero to enable bitset statistics gathering. */
81 #define BITSET_STATS 1
82
83 /* Non-zero to enable bitset type checking. */
84 #define BITSET_CHECK 1
85
86 /* Data type used to store a word of bits. */
87 typedef unsigned long bitset_word;
88 #define BITSET_WORD_BITS ((unsigned) CHAR_BIT * sizeof (bitset_word))
89
90 /* Bit index. */
91 typedef unsigned long bitset_bindex;
92
93 /* Word index. */
94 typedef unsigned long bitset_windex;
95
96 #define BITSET_INDEX_MAX ((1U << (BITSET_WORD_BITS - 1)))
97
98 #define BITSET_MSB (1U << (BITSET_WORD_BITS - 1))
99
100 #define BITSET_LIST_SIZE 1024
101
102 enum bitset_ops {BITSET_OP_ZERO, BITSET_OP_ONES,
103 BITSET_OP_COPY, BITSET_OP_NOT,
104 BITSET_OP_EMPTY_P, BITSET_OP_EQUAL_P,
105 BITSET_OP_SUBSET_P, BITSET_OP_DISJOINT_P,
106 BITSET_OP_AND, BITSET_OP_OR, BITSET_OP_XOR, BITSET_OP_ANDN,
107 BITSET_OP_OR_AND, BITSET_OP_AND_OR, BITSET_OP_ANDN_OR};
108
109 /* Return size in bits of bitset SRC. */
110 #define BITSET_SIZE_(SRC) (SRC)->b.ops->size (SRC)
111
112 /* Return type of bitset SRC. */
113 #define BITSET_TYPE_(DST) (DST)->b.ops->type
114
115 /* Set bit BITNO in bitset DST. */
116 #define BITSET_SET_(DST, BITNO) (DST)->b.ops->set (DST, BITNO)
117
118 /* Reset bit BITNO in bitset DST. */
119 #define BITSET_RESET_(DST, BITNO) (DST)->b.ops->reset (DST, BITNO)
120
121 /* Return non-zero if bit BITNO in bitset SRC is set. */
122 #define BITSET_TEST_(SRC, BITNO) (SRC)->b.ops->test (SRC, BITNO)
123
124 /* Free bitset SRC. */
125 #define BITSET_FREE_(SRC)\
126 ((SRC)->b.ops->free ? (SRC)->b.ops->free (SRC) :(void)0)
127
128 /* Perform operation OP on DST. */
129 #define BITSET_OP1_(DST, OP) (DST)->b.ops->op1 (DST, OP)
130
131 /* Perform operation OP on SRC and store in DST. */
132 #define BITSET_OP2_(DST, SRC, OP) (DST)->b.ops->op2 (DST, SRC, OP)
133
134 /* DST = SRC1 OP SRC2. */
135 #define BITSET_OP3_(DST, SRC1, SRC2, OP) \
136 (DST)->b.ops->op3 (DST, SRC1, SRC2, OP)
137
138 /* DST = (SRC1 OP1 SRC2) OP2 SRC3. */
139 #define BITSET_OP4_(DST, SRC1, SRC2, SRC3, OP) \
140 (DST)->b.ops->op4 (DST, SRC1, SRC2, SRC3, OP)
141
142 /* DST = 0. */
143 #define BITSET_ZERO_(DST) BITSET_OP1_ (DST, BITSET_OP_ZERO);
144
145 /* DST = ~0. */
146 #define BITSET_ONES_(DST) BITSET_OP1_ (DST, BITSET_OP_ONES);
147
148 /* Return SRC == 0. */
149 #define BITSET_EMPTY_P_(SRC) BITSET_OP1_ (SRC, BITSET_OP_EMPTY_P);
150
151 /* Return DST == SRC. */
152 #define BITSET_EQUAL_P_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_EQUAL_P)
153
154 /* Return DST == DST | SRC. */
155 #define BITSET_SUBSET_P_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_SUBSET_P)
156
157 /* Return DST & SRC == 0. */
158 #define BITSET_DISJOINT_P_(DST, SRC)\
159 BITSET_OP2_ (DST, SRC, BITSET_OP_DISJOINT_P)
160
161 /* DST = SRC. */
162 #define BITSET_COPY_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_COPY)
163
164 /* DST = ~SRC. */
165 #define BITSET_NOT_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_NOT)
166
167 /* DST = SRC1 | SRC2. */
168 #define BITSET_OR_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2,\
169 BITSET_OP_OR)
170
171 /* DST = SRC1 ^ SRC2. */
172 #define BITSET_XOR_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2,\
173 BITSET_OP_XOR)
174
175 /* DST = SRC1 & SRC2. */
176 #define BITSET_AND_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2, \
177 BITSET_OP_AND)
178
179 /* DST = SRC1 & ~SRC2. */
180 #define BITSET_ANDN_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2, \
181 BITSET_OP_ANDN)
182
183 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
184 DST != (SRC1 & SRC2) | SRC3. */
185 #define BITSET_AND_OR_(DST, SRC1, SRC2, SRC3)\
186 BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_AND_OR)
187
188 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
189 DST != (SRC1 | SRC2) & SRC3. */
190 #define BITSET_OR_AND_(DST, SRC1, SRC2, SRC3)\
191 BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_OR_AND)
192
193 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
194 DST != (SRC1 & ~SRC2) | SRC3. */
195 #define BITSET_ANDN_OR_(DST, SRC1, SRC2, SRC3)\
196 BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_ANDN_OR)
197
198 /* Find list of up to NUM bits set in BSET starting from and including
199 *NEXT. Return with actual number of bits found and with *NEXT
200 indicating where search stopped. */
201 #define BITSET_LIST_(BSET, LIST, NUM, NEXT) \
202 (BSET)->b.ops->list (BSET, LIST, NUM, NEXT)
203
204 /* Find reverse list of up to NUM bits set in BSET starting from and
205 including NEXT. Return with actual number of bits found and with
206 *NEXT indicating where search stopped. */
207 #define BITSET_REVERSE_LIST_(BSET, LIST, NUM, NEXT) \
208 (BSET)->b.ops->reverse_list (BSET, LIST, NUM, NEXT)
209
210
211 struct bbitset_struct
212 {
213 struct bitset_ops_struct *ops;
214 bitset_windex cindex; /* Cache word index. */
215 bitset_windex csize; /* Cache size in words. */
216 bitset_word *cdata; /* Cache data pointer. */
217 /* Perhaps we could sacrifice another word to indicate
218 that the bitset is known to be zero, that a bit has been set
219 in the cache, and that a bit has been cleared in the cache.
220 This would speed up some of the searches but slightly slow down
221 bit set/reset operations of cached bits. */
222 };
223
224
225 typedef struct bitset_struct *bitset;
226
227
228 /* The contents of this structure should be considered private. */
229 struct bitset_ops_struct
230 {
231 void (*set) PARAMS ((struct bitset_struct *, bitset_bindex));
232 void (*reset) PARAMS ((struct bitset_struct *, bitset_bindex));
233 int (*test) PARAMS ((struct bitset_struct *, bitset_bindex));
234 int (*size) PARAMS ((struct bitset_struct *));
235 int (*op1) PARAMS ((struct bitset_struct *, enum bitset_ops));
236 int (*op2) PARAMS ((struct bitset_struct *, struct bitset_struct *,
237 enum bitset_ops));
238 int (*op3) PARAMS ((struct bitset_struct *, struct bitset_struct *,
239 struct bitset_struct *, enum bitset_ops));
240 int (*op4) PARAMS ((struct bitset_struct *, struct bitset_struct *,
241 struct bitset_struct *, struct bitset_struct *,
242 enum bitset_ops));
243 int (*list) PARAMS ((struct bitset_struct *, bitset_bindex *,
244 bitset_bindex, bitset_bindex *));
245 int (*reverse_list) PARAMS ((struct bitset_struct *, bitset_bindex *,
246 bitset_bindex, bitset_bindex *));
247 void (*free) PARAMS ((struct bitset_struct *));
248 enum bitset_type type;
249 };
250
251 #define BITSET_COMPATIBLE_(BSET1, BSET2) ((BSET1)->b.ops == (BSET2)->b.ops)
252
253 #if BITSET_CHECK
254 #define BITSET_CHECK2_(DST, SRC) \
255 if (!BITSET_COMPATIBLE_ (DST, SRC)) abort ();
256
257 #define BITSET_CHECK3_(DST, SRC1, SRC2) \
258 if (!BITSET_COMPATIBLE_ (DST, SRC1) \
259 || !BITSET_COMPATIBLE_ (DST, SRC2)) abort ();
260
261 #define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) \
262 if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \
263 || !BITSET_COMPATIBLE_ (DST, SRC3)) abort ();
264 #else
265 #define BITSET_CHECK2_(DST, SRC)
266
267 #define BITSET_CHECK3_(DST, SRC1, SRC2)
268
269 #define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3)
270 #endif
271
272 extern int bitset_op4 PARAMS ((bitset, bitset, bitset, bitset,
273 enum bitset_ops op));
274
275 #endif /* _BBITSET_H */