]>
git.saurik.com Git - bison.git/blob - lib/bbitset.h
2 Copyright (C) 2002 Free Software Foundation, Inc.
3 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
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.
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.
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. */
22 #include "libiberty.h"
28 /* Currently we support three flavours of bitsets:
29 BITSET_ARRAY: Array of bits (fixed size, fast for dense bitsets).
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).
35 BITSET_STATS: Wrapper bitset for internal use only.
37 enum bitset_type
{BITSET_ARRAY
, BITSET_LIST
, BITSET_TABLE
, BITSET_TYPE_NUM
,
39 #define BITSET_TYPE_NAMES {"abitset", "lbitset", "ebitset"}
41 extern const char * const bitset_type_names
[];
43 enum bitset_alloc_type
{BITSET_MALLOC
, BITSET_OBALLOC
};
45 /* Non-zero to use inline functions instead of macros. */
46 #define BITSET_INLINE 0
48 /* Data type used to store a word of bits. */
49 typedef unsigned long bitset_word
;
50 #define BITSET_WORD_BITS ((unsigned) (CHAR_BIT * sizeof (bitset_word)))
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
54 size_t, and that is good enough. */
55 typedef size_t bitset_bindex
;
58 typedef size_t bitset_windex
;
60 #define BITSET_INDEX_MAX ((bitset_word) 1 << (BITSET_WORD_BITS - 1))
62 #define BITSET_MSB ((bitset_word) 1 << (BITSET_WORD_BITS - 1))
64 #define BITSET_LIST_SIZE 1024
66 enum bitset_ops
{BITSET_OP_ZERO
, BITSET_OP_ONES
,
67 BITSET_OP_COPY
, BITSET_OP_NOT
,
68 BITSET_OP_EMPTY_P
, BITSET_OP_EQUAL_P
,
69 BITSET_OP_SUBSET_P
, BITSET_OP_DISJOINT_P
,
70 BITSET_OP_AND
, BITSET_OP_OR
, BITSET_OP_XOR
, BITSET_OP_ANDN
,
71 BITSET_OP_OR_AND
, BITSET_OP_AND_OR
, BITSET_OP_ANDN_OR
};
75 const struct bitset_vtable
* vtable
;
76 bitset_windex cindex
; /* Cache word index. */
77 bitset_windex csize
; /* Cache size in words. */
78 bitset_word
*cdata
; /* Cache data pointer. */
79 /* Perhaps we could sacrifice another word to indicate
80 that the bitset is known to be zero, that a bit has been set
81 in the cache, and that a bit has been cleared in the cache.
82 This would speed up some of the searches but slightly slow down
83 bit set/reset operations of cached bits. */
87 typedef struct bitset_struct
*bitset
;
90 /* The contents of this structure should be considered private. */
93 void (*set
) PARAMS ((struct bitset_struct
*, bitset_bindex
));
94 void (*reset
) PARAMS ((struct bitset_struct
*, bitset_bindex
));
95 int (*toggle
) PARAMS ((struct bitset_struct
*, bitset_bindex
));
96 int (*test
) PARAMS ((struct bitset_struct
*, bitset_bindex
));
97 int (*size
) PARAMS ((struct bitset_struct
*));
98 int (*count
) PARAMS ((struct bitset_struct
*));
100 int (*empty_p
) PARAMS ((struct bitset_struct
*));
101 void (*ones
) PARAMS ((struct bitset_struct
*));
102 void (*zero
) PARAMS ((struct bitset_struct
*));
104 void (*copy
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*));
105 int (*disjoint_p
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*));
106 int (*equal_p
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*));
107 void (*not) PARAMS ((struct bitset_struct
*, struct bitset_struct
*));
108 int (*subset_p
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*));
110 void (*and) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
111 struct bitset_struct
*));
112 int (*and_cmp
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
113 struct bitset_struct
*));
114 void (*andn
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
115 struct bitset_struct
*));
116 int (*andn_cmp
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
117 struct bitset_struct
*));
118 void (*or) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
119 struct bitset_struct
*));
120 int (*or_cmp
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
121 struct bitset_struct
*));
122 void (*xor) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
123 struct bitset_struct
*));
124 int (*xor_cmp
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
125 struct bitset_struct
*));
127 void (*and_or
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
128 struct bitset_struct
*, struct bitset_struct
*));
129 int (*and_or_cmp
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
130 struct bitset_struct
*, struct bitset_struct
*));
131 void (*andn_or
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
132 struct bitset_struct
*, struct bitset_struct
*));
133 int (*andn_or_cmp
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
134 struct bitset_struct
*, struct bitset_struct
*));
135 void (*or_and
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
136 struct bitset_struct
*, struct bitset_struct
*));
137 int (*or_and_cmp
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
138 struct bitset_struct
*, struct bitset_struct
*));
140 int (*list
) PARAMS ((struct bitset_struct
*, bitset_bindex
*,
141 bitset_bindex
, bitset_bindex
*));
142 int (*list_reverse
) PARAMS ((struct bitset_struct
*, bitset_bindex
*,
143 bitset_bindex
, bitset_bindex
*));
144 void (*free
) PARAMS ((struct bitset_struct
*));
145 enum bitset_type type
;
148 #define BITSET_COMPATIBLE_(BSET1, BSET2) ((BSET1)->b.vtable == (BSET2)->b.vtable)
150 #define BITSET_CHECK2_(DST, SRC) \
151 if (!BITSET_COMPATIBLE_ (DST, SRC)) abort ();
153 #define BITSET_CHECK3_(DST, SRC1, SRC2) \
154 if (!BITSET_COMPATIBLE_ (DST, SRC1) \
155 || !BITSET_COMPATIBLE_ (DST, SRC2)) abort ();
157 #define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) \
158 if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \
159 || !BITSET_COMPATIBLE_ (DST, SRC3)) abort ();
162 /* Return size in bits of bitset SRC. */
163 #define BITSET_SIZE_(SRC) (SRC)->b.vtable->size (SRC)
165 /* Return number of bits set in bitset SRC. */
166 #define BITSET_COUNT_(SRC) (SRC)->b.vtable->count (SRC)
168 /* Return type of bitset SRC. */
169 #define BITSET_TYPE_(DST) (DST)->b.vtable->type
171 /* Set bit BITNO in bitset DST. */
172 #define BITSET_SET_(DST, BITNO) (DST)->b.vtable->set (DST, BITNO)
174 /* Reset bit BITNO in bitset DST. */
175 #define BITSET_RESET_(DST, BITNO) (DST)->b.vtable->reset (DST, BITNO)
177 /* Toggle bit BITNO in bitset DST. */
178 #define BITSET_TOGGLE_(DST, BITNO) (DST)->b.vtable->toggle (DST, BITNO)
180 /* Return non-zero if bit BITNO in bitset SRC is set. */
181 #define BITSET_TEST_(SRC, BITNO) (SRC)->b.vtable->test (SRC, BITNO)
183 /* Free bitset SRC. */
184 #define BITSET_FREE_(SRC)\
185 ((SRC)->b.vtable->free ? (SRC)->b.vtable->free (SRC) :(void)0)
188 /* Return SRC == 0. */
189 #define BITSET_EMPTY_P_(SRC) (SRC)->b.vtable->empty_p (SRC)
192 #define BITSET_ONES_(DST) (DST)->b.vtable->ones (DST)
195 #define BITSET_ZERO_(DST) (DST)->b.vtable->zero (DST)
200 #define BITSET_COPY_(DST, SRC) (SRC)->b.vtable->copy (DST, SRC)
202 /* Return DST & SRC == 0. */
203 #define BITSET_DISJOINT_P_(DST, SRC) (SRC)->b.vtable->disjoint_p (DST, SRC)
205 /* Return DST == SRC. */
206 #define BITSET_EQUAL_P_(DST, SRC) (SRC)->b.vtable->equal_p (DST, SRC)
209 #define BITSET_NOT_(DST, SRC) (SRC)->b.vtable->not (DST, SRC)
211 /* Return DST == DST | SRC. */
212 #define BITSET_SUBSET_P_(DST, SRC) (SRC)->b.vtable->subset_p (DST, SRC)
215 /* DST = SRC1 & SRC2. */
216 #define BITSET_AND_(DST, SRC1, SRC2) (SRC1)->b.vtable->and (DST, SRC1, SRC2)
217 #define BITSET_AND_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->and_cmp (DST, SRC1, SRC2)
219 /* DST = SRC1 & ~SRC2. */
220 #define BITSET_ANDN_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn (DST, SRC1, SRC2)
221 #define BITSET_ANDN_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn_cmp (DST, SRC1, SRC2)
223 /* DST = SRC1 | SRC2. */
224 #define BITSET_OR_(DST, SRC1, SRC2) (SRC1)->b.vtable->or (DST, SRC1, SRC2)
225 #define BITSET_OR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->or_cmp (DST, SRC1, SRC2)
227 /* DST = SRC1 ^ SRC2. */
228 #define BITSET_XOR_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor (DST, SRC1, SRC2)
229 #define BITSET_XOR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor_cmp (DST, SRC1, SRC2)
233 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
234 DST != (SRC1 & SRC2) | SRC3. */
235 #define BITSET_AND_OR_(DST, SRC1, SRC2, SRC3) \
236 (SRC1)->b.vtable->and_or (DST, SRC1, SRC2, SRC3)
237 #define BITSET_AND_OR_CMP_(DST, SRC1, SRC2, SRC3) \
238 (SRC1)->b.vtable->and_or_cmp (DST, SRC1, SRC2, SRC3)
240 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
241 DST != (SRC1 & ~SRC2) | SRC3. */
242 #define BITSET_ANDN_OR_(DST, SRC1, SRC2, SRC3) \
243 (SRC1)->b.vtable->andn_or (DST, SRC1, SRC2, SRC3)
244 #define BITSET_ANDN_OR_CMP_(DST, SRC1, SRC2, SRC3) \
245 (SRC1)->b.vtable->andn_or_cmp (DST, SRC1, SRC2, SRC3)
247 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
248 DST != (SRC1 | SRC2) & SRC3. */
249 #define BITSET_OR_AND_(DST, SRC1, SRC2, SRC3) \
250 (SRC1)->b.vtable->or_and (DST, SRC1, SRC2, SRC3)
251 #define BITSET_OR_AND_CMP_(DST, SRC1, SRC2, SRC3) \
252 (SRC1)->b.vtable->or_and_cmp (DST, SRC1, SRC2, SRC3)
255 /* Find list of up to NUM bits set in BSET starting from and including
256 *NEXT. Return with actual number of bits found and with *NEXT
257 indicating where search stopped. */
258 #define BITSET_LIST_(BSET, LIST, NUM, NEXT) \
259 (BSET)->b.vtable->list (BSET, LIST, NUM, NEXT)
261 /* Find reverse list of up to NUM bits set in BSET starting from and
262 including NEXT. Return with actual number of bits found and with
263 *NEXT indicating where search stopped. */
264 #define BITSET_LIST_REVERSE_(BSET, LIST, NUM, NEXT) \
265 (BSET)->b.vtable->list_reverse (BSET, LIST, NUM, NEXT)
268 /* Private functions for bitset implementations. */
270 extern int bitset_toggle_
PARAMS ((bitset
, bitset_bindex
));
272 extern int bitset_count_
PARAMS ((bitset
));
274 extern int bitset_copy_
PARAMS ((bitset
, bitset
));
276 extern int bitset_and_or_cmp_
PARAMS ((bitset
, bitset
, bitset
, bitset
));
278 extern int bitset_andn_or_cmp_
PARAMS ((bitset
, bitset
, bitset
, bitset
));
280 extern int bitset_or_and_cmp_
PARAMS ((bitset
, bitset
, bitset
, bitset
));
282 #endif /* _BBITSET_H */