]> git.saurik.com Git - bison.git/blob - lib/bbitset.h
(BITSET_WORD_BITS): Now of type unsigned, not
[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 #include "libiberty.h"
23
24 #ifdef HAVE_LIMITS_H
25 #include <limits.h>
26 #endif
27
28 /* Currently we support three flavours of bitsets:
29 BITSET_ARRAY: Array of bits (fixed size, faster).
30 BITSET_LIST: Linked list of array of bits (variable size, least storage
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).
34
35 BITSET_STATS: Wrapper bitset for internal use only.
36 */
37 enum bitset_type {BITSET_ARRAY, BITSET_LIST, BITSET_TABLE, BITSET_TYPE_NUM,
38 BITSET_STATS};
39 #define BITSET_TYPE_NAMES {"abitset", "lbitset", "ebitset"}
40
41 enum bitset_alloc_type {BITSET_MALLOC, BITSET_OBALLOC};
42
43 /* Non-zero to use inline functions instead of macros. */
44 #define BITSET_INLINE 0
45
46 /* Data type used to store a word of bits. */
47 typedef unsigned long bitset_word;
48 #define BITSET_WORD_BITS ((unsigned) (CHAR_BIT * sizeof (bitset_word)))
49
50 /* Bit index. In theory we might need a type wider than size_t, but
51 in practice we lose at most a factor of CHAR_BIT by going with
52 size_t, and that is good enough. */
53 typedef size_t bitset_bindex;
54
55 /* Word index. */
56 typedef size_t bitset_windex;
57
58 #define BITSET_INDEX_MAX ((bitset_word) 1 << (BITSET_WORD_BITS - 1))
59
60 #define BITSET_MSB ((bitset_word) 1 << (BITSET_WORD_BITS - 1))
61
62 #define BITSET_LIST_SIZE 1024
63
64 enum bitset_ops {BITSET_OP_ZERO, BITSET_OP_ONES,
65 BITSET_OP_COPY, BITSET_OP_NOT,
66 BITSET_OP_EMPTY_P, BITSET_OP_EQUAL_P,
67 BITSET_OP_SUBSET_P, BITSET_OP_DISJOINT_P,
68 BITSET_OP_AND, BITSET_OP_OR, BITSET_OP_XOR, BITSET_OP_ANDN,
69 BITSET_OP_OR_AND, BITSET_OP_AND_OR, BITSET_OP_ANDN_OR};
70
71 /* Return size in bits of bitset SRC. */
72 #define BITSET_SIZE_(SRC) (SRC)->b.ops->size (SRC)
73
74 /* Return type of bitset SRC. */
75 #define BITSET_TYPE_(DST) (DST)->b.ops->type
76
77 /* Set bit BITNO in bitset DST. */
78 #define BITSET_SET_(DST, BITNO) (DST)->b.ops->set (DST, BITNO)
79
80 /* Reset bit BITNO in bitset DST. */
81 #define BITSET_RESET_(DST, BITNO) (DST)->b.ops->reset (DST, BITNO)
82
83 /* Return non-zero if bit BITNO in bitset SRC is set. */
84 #define BITSET_TEST_(SRC, BITNO) (SRC)->b.ops->test (SRC, BITNO)
85
86 /* Free bitset SRC. */
87 #define BITSET_FREE_(SRC)\
88 ((SRC)->b.ops->free ? (SRC)->b.ops->free (SRC) :(void)0)
89
90 /* Perform operation OP on DST. */
91 #define BITSET_OP1_(DST, OP) (DST)->b.ops->op1 (DST, OP)
92
93 /* Perform operation OP on SRC and store in DST. */
94 #define BITSET_OP2_(DST, SRC, OP) (DST)->b.ops->op2 (DST, SRC, OP)
95
96 /* DST = SRC1 OP SRC2. */
97 #define BITSET_OP3_(DST, SRC1, SRC2, OP) \
98 (DST)->b.ops->op3 (DST, SRC1, SRC2, OP)
99
100 /* DST = (SRC1 OP1 SRC2) OP2 SRC3. */
101 #define BITSET_OP4_(DST, SRC1, SRC2, SRC3, OP) \
102 (DST)->b.ops->op4 (DST, SRC1, SRC2, SRC3, OP)
103
104 /* DST = 0. */
105 #define BITSET_ZERO_(DST) BITSET_OP1_ (DST, BITSET_OP_ZERO);
106
107 /* DST = ~0. */
108 #define BITSET_ONES_(DST) BITSET_OP1_ (DST, BITSET_OP_ONES);
109
110 /* Return SRC == 0. */
111 #define BITSET_EMPTY_P_(SRC) BITSET_OP1_ (SRC, BITSET_OP_EMPTY_P);
112
113 /* Return DST == SRC. */
114 #define BITSET_EQUAL_P_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_EQUAL_P)
115
116 /* Return DST == DST | SRC. */
117 #define BITSET_SUBSET_P_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_SUBSET_P)
118
119 /* Return DST & SRC == 0. */
120 #define BITSET_DISJOINT_P_(DST, SRC)\
121 BITSET_OP2_ (DST, SRC, BITSET_OP_DISJOINT_P)
122
123 /* DST = SRC. */
124 #define BITSET_COPY_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_COPY)
125
126 /* DST = ~SRC. */
127 #define BITSET_NOT_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_NOT)
128
129 /* DST = SRC1 | SRC2. */
130 #define BITSET_OR_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2,\
131 BITSET_OP_OR)
132
133 /* DST = SRC1 ^ SRC2. */
134 #define BITSET_XOR_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2,\
135 BITSET_OP_XOR)
136
137 /* DST = SRC1 & SRC2. */
138 #define BITSET_AND_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2, \
139 BITSET_OP_AND)
140
141 /* DST = SRC1 & ~SRC2. */
142 #define BITSET_ANDN_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2, \
143 BITSET_OP_ANDN)
144
145 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
146 DST != (SRC1 & SRC2) | SRC3. */
147 #define BITSET_AND_OR_(DST, SRC1, SRC2, SRC3)\
148 BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_AND_OR)
149
150 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
151 DST != (SRC1 | SRC2) & SRC3. */
152 #define BITSET_OR_AND_(DST, SRC1, SRC2, SRC3)\
153 BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_OR_AND)
154
155 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
156 DST != (SRC1 & ~SRC2) | SRC3. */
157 #define BITSET_ANDN_OR_(DST, SRC1, SRC2, SRC3)\
158 BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_ANDN_OR)
159
160 /* Find list of up to NUM bits set in BSET starting from and including
161 *NEXT. Return with actual number of bits found and with *NEXT
162 indicating where search stopped. */
163 #define BITSET_LIST_(BSET, LIST, NUM, NEXT) \
164 (BSET)->b.ops->list (BSET, LIST, NUM, NEXT)
165
166 /* Find reverse list of up to NUM bits set in BSET starting from and
167 including NEXT. Return with actual number of bits found and with
168 *NEXT indicating where search stopped. */
169 #define BITSET_REVERSE_LIST_(BSET, LIST, NUM, NEXT) \
170 (BSET)->b.ops->reverse_list (BSET, LIST, NUM, NEXT)
171
172
173 struct bbitset_struct
174 {
175 struct bitset_ops_struct *ops;
176 bitset_windex cindex; /* Cache word index. */
177 bitset_windex csize; /* Cache size in words. */
178 bitset_word *cdata; /* Cache data pointer. */
179 /* Perhaps we could sacrifice another word to indicate
180 that the bitset is known to be zero, that a bit has been set
181 in the cache, and that a bit has been cleared in the cache.
182 This would speed up some of the searches but slightly slow down
183 bit set/reset operations of cached bits. */
184 };
185
186
187 typedef struct bitset_struct *bitset;
188
189
190 /* The contents of this structure should be considered private. */
191 struct bitset_ops_struct
192 {
193 void (*set) PARAMS ((struct bitset_struct *, bitset_bindex));
194 void (*reset) PARAMS ((struct bitset_struct *, bitset_bindex));
195 int (*test) PARAMS ((struct bitset_struct *, bitset_bindex));
196 int (*size) PARAMS ((struct bitset_struct *));
197 int (*op1) PARAMS ((struct bitset_struct *, enum bitset_ops));
198 int (*op2) PARAMS ((struct bitset_struct *, struct bitset_struct *,
199 enum bitset_ops));
200 int (*op3) PARAMS ((struct bitset_struct *, struct bitset_struct *,
201 struct bitset_struct *, enum bitset_ops));
202 int (*op4) PARAMS ((struct bitset_struct *, struct bitset_struct *,
203 struct bitset_struct *, struct bitset_struct *,
204 enum bitset_ops));
205 int (*list) PARAMS ((struct bitset_struct *, bitset_bindex *,
206 bitset_bindex, bitset_bindex *));
207 int (*reverse_list) PARAMS ((struct bitset_struct *, bitset_bindex *,
208 bitset_bindex, bitset_bindex *));
209 void (*free) PARAMS ((struct bitset_struct *));
210 enum bitset_type type;
211 };
212
213 #define BITSET_COMPATIBLE_(BSET1, BSET2) ((BSET1)->b.ops == (BSET2)->b.ops)
214
215 #define BITSET_CHECK2_(DST, SRC) \
216 if (!BITSET_COMPATIBLE_ (DST, SRC)) abort ();
217
218 #define BITSET_CHECK3_(DST, SRC1, SRC2) \
219 if (!BITSET_COMPATIBLE_ (DST, SRC1) \
220 || !BITSET_COMPATIBLE_ (DST, SRC2)) abort ();
221
222 #define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) \
223 if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \
224 || !BITSET_COMPATIBLE_ (DST, SRC3)) abort ();
225
226
227 extern int bitset_op4 PARAMS ((bitset, bitset, bitset, bitset,
228 enum bitset_ops op));
229
230 #endif /* _BBITSET_H */