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 /* This file is the public interface to the bitset abstract data type.
23 Only use the functions and macros defined in this file. */
29 /* Attributes used to select a bitset implementation. */
30 enum 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. */
35 BITSET_GREEDY
= 32}; /* Prefer fastest. */
37 typedef unsigned int bitset_attrs
;
39 /* The contents of the structure should be considered to be private.
40 While I would like to make this structure opaque, it needs to be
41 visible for the inline bit set/test functions. */
44 struct bbitset_struct b
;
47 /* Return bytes required for bitset of desired type and size. */
48 extern int bitset_bytes
PARAMS ((enum bitset_type
, bitset_bindex
));
50 /* Initialise a bitset with desired type and size. */
51 extern bitset bitset_init
PARAMS ((bitset
, bitset_bindex
, enum bitset_type
));
53 /* Select an implementation type based on the desired bitset size
55 extern enum bitset_type bitset_type_choose
PARAMS ((bitset_bindex
,
58 /* Create a bitset of desired type and size. The bitset is zeroed. */
59 extern bitset bitset_alloc
PARAMS ((bitset_bindex
, enum bitset_type
));
62 extern void bitset_free
PARAMS ((bitset
));
64 /* Create a bitset of desired type and size using an obstack. The
66 extern bitset bitset_obstack_alloc
PARAMS ((struct obstack
*bobstack
,
67 bitset_bindex
, enum bitset_type
));
69 /* Free bitset allocated on obstack. */
70 extern void bitset_obstack_free
PARAMS ((bitset
));
72 /* Create a bitset of desired size and attributes. The bitset is zeroed. */
73 extern bitset bitset_create
PARAMS ((bitset_bindex
, bitset_attrs
));
75 /* Return size in bits of bitset SRC. */
76 extern int bitset_size
PARAMS ((bitset
));
78 /* Return number of bits set in bitset SRC. */
79 extern int bitset_count
PARAMS ((bitset
));
81 #if BITSET_CACHE && BITSET_INLINE
82 static inline void bitset_set
PARAMS ((bitset
, bitset_bindex
));
83 static inline void bitset_reset
PARAMS ((bitset
, bitset_bindex
));
84 static inline int bitset_test
PARAMS ((bitset
, bitset_bindex
));
86 /* Set bit BITNO in bitset BSET. */
87 static inline void bitset_set (bset
, bitno
)
91 bitset_windex index
= bitno
/ BITSET_WORD_BITS
;
92 bitset_windex offset
= index
- bset
->b
.cindex
;
94 if (offset
< bset
->b
.csize
)
95 bset
->b
.cdata
[offset
] |= (1 << (bitno
% BITSET_WORD_BITS
));
97 BITSET_SET_ (bset
, bitno
);
101 /* Reset bit BITNO in bitset BSET. */
102 static inline void bitset_reset (bset
, bitno
)
106 bitset_windex index
= bitno
/ BITSET_WORD_BITS
;
107 bitset_windex offset
= index
- bset
->b
.cindex
;
109 if (offset
< bset
->b
.csize
)
110 bset
->b
.cdata
[offset
] &= ~(1 << (bitno
% BITSET_WORD_BITS
));
112 BITSET_RESET_ (bset
, bitno
);
116 /* Test bit BITNO in bitset BSET. */
117 static inline int bitset_test (bset
, bitno
)
121 bitset_windex index
= bitno
/ BITSET_WORD_BITS
;
122 bitset_windex offset
= index
- bset
->b
.cindex
;
124 if (offset
< bset
->b
.csize
)
125 return (bset
->b
.cdata
[offset
] >> (bitno
% BITSET_WORD_BITS
)) & 1;
127 return BITSET_TEST_ (bset
, bitno
);
131 #if BITSET_CACHE && ! BITSET_INLINE
133 /* Set bit BITNO in bitset BSET. */
134 #define bitset_set(bset, bitno) \
137 bitset_bindex _bitno = (bitno); \
138 bitset_windex _index = _bitno / BITSET_WORD_BITS; \
139 bitset_windex _offset = _index - (bset)->b.cindex; \
141 if (_offset < (bset)->b.csize) \
142 (bset)->b.cdata[_offset] |= (1 << (_bitno % BITSET_WORD_BITS)); \
144 BITSET_SET_ ((bset), _bitno); \
148 /* Reset bit BITNO in bitset BSET. */
149 #define bitset_reset(bset, bitno) \
152 bitset_bindex _bitno = (bitno); \
153 bitset_windex _index = _bitno / BITSET_WORD_BITS; \
154 bitset_windex _offset = _index - (bset)->b.cindex; \
156 if (_offset < (bset)->b.csize) \
157 (bset)->b.cdata[_offset] &= ~(1 << (_bitno % BITSET_WORD_BITS)); \
159 BITSET_RESET_ ((bset), _bitno); \
163 /* Test bit BITNO in bitset BSET. */
164 #define bitset_test(bset, bitno) \
165 (((((bitno) / BITSET_WORD_BITS) - (bset)->b.cindex) < (bset)->b.csize) \
166 ? ((bset)->b.cdata[(((bitno) / BITSET_WORD_BITS) - (bset)->b.cindex)] \
167 >> ((bitno) % BITSET_WORD_BITS)) & 1 \
168 : (unsigned int) BITSET_TEST_ ((bset), (bitno)))
172 /* Set bit BITNO in bitset SRC. */
173 #define bitset_set(SRC, BITNO) BITSET_SET_ (SRC, BITNO)
175 /* Reset bit BITNO in bitset SRC. */
176 #define bitset_reset(SRC, BITNO) BITSET_RESET_ (SRC, BITNO)
178 /* Return non-zero if bit BITNO in bitset SRC is set. */
179 #define bitset_test(SRC, BITNO) BITSET_TEST_ (SRC, BITNO)
182 /* Toggle bit BITNO in bitset BSET and return non-zero if now set. */
183 extern int bitset_toggle
PARAMS ((bitset
, bitset_bindex
));
186 extern int bitset_zero
PARAMS ((bitset
));
189 extern int bitset_ones
PARAMS ((bitset
));
191 /* Return non-zero if all bits in bitset SRC are reset. */
192 extern int bitset_empty_p
PARAMS ((bitset
));
194 /* Return DST == DST | SRC. */
195 extern int bitset_subset_p
PARAMS ((bitset
, bitset
));
197 /* Return DST == SRC. */
198 extern int bitset_equal_p
PARAMS ((bitset
, bitset
));
200 /* Return DST & SRC == 0. */
201 extern int bitset_disjoint_p
PARAMS ((bitset
, bitset
));
204 extern int bitset_copy
PARAMS ((bitset
, bitset
));
207 extern int bitset_not
PARAMS ((bitset
, bitset
));
209 /* DST = SRC1 | SRC2. Return non-zero if DST != SRC1 | SRC2. */
210 extern int bitset_or
PARAMS ((bitset
, bitset
, bitset
));
212 /* DST = SRC1 & SRC2. Return non-zero if DST != SRC1 & SRC2. */
213 extern int bitset_and
PARAMS ((bitset
, bitset
, bitset
));
215 /* DST = SRC1 ^ SRC2. Return non-zero if DST != SRC1 ^ SRC2. */
216 extern int bitset_xor
PARAMS ((bitset
, bitset
, bitset
));
218 /* DST = SRC1 & ~SRC2. Return non-zero if DST != SRC1 & ~SRC2. */
219 extern int bitset_andn
PARAMS ((bitset
, bitset
, bitset
));
221 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
222 DST != (SRC1 | SRC2) & SRC3. */
223 extern int bitset_or_and
PARAMS ((bitset
, bitset
, bitset
, bitset
));
225 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
226 DST != (SRC1 & SRC2) | SRC3. */
227 extern int bitset_and_or
PARAMS ((bitset
, bitset
, bitset
, bitset
));
229 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
230 DST != (SRC1 & ~SRC2) | SRC3. */
231 extern int bitset_andn_or
PARAMS ((bitset
, bitset
, bitset
, bitset
));
233 /* Find next bit set in BSET starting from and including BITNO. */
234 extern int bitset_next
PARAMS ((bitset
, bitset_bindex
));
236 /* Find previous bit set in BSET starting from and including BITNO. */
237 extern int bitset_prev
PARAMS ((bitset
, bitset_bindex
));
239 /* Return non-zero if BITNO in SRC is the only set bit. */
240 extern int bitset_only_set_p
PARAMS ((bitset
, bitset_bindex
));
242 /* Find list of up to NUM bits set in BSET starting from and including
243 *NEXT. Return with actual number of bits found and with *NEXT
244 indicating where search stopped. */
246 extern int bitset_list
PARAMS ((bitset
, bitset_bindex
*, bitset_bindex
,
249 #define bitset_list(BSET, LIST, NUM, NEXT) \
250 BITSET_LIST_ (BSET, LIST, NUM, NEXT)
253 /* Find reverse list of up to NUM bits set in BSET starting from and
254 including NEXT. Return with actual number of bits found and with
255 *NEXT indicating where search stopped. */
256 #define bitset_reverse_list(BSET, LIST, NUM, NEXT) \
257 BITSET_REVERSE_LIST_ (BSET, LIST, NUM, NEXT)
259 /* Find first set bit. */
260 extern int bitset_first
PARAMS ((bitset
));
262 /* Find last set bit. */
263 extern int bitset_last
PARAMS ((bitset
));
266 extern void bitset_dump
PARAMS ((FILE *, bitset
));
268 /* Loop over all elements of BSET, starting with MIN, executing CODE. */
269 #define BITSET_EXECUTE(BSET, MIN, N, CODE) \
271 bitset_bindex _list[BITSET_LIST_SIZE]; \
272 bitset_bindex _next = (MIN); \
275 while ((_num = bitset_list ((BSET), _list, BITSET_LIST_SIZE, &_next)))\
279 for (_i = 0; _i < _num; _i++) \
284 if (_num < BITSET_LIST_SIZE) \
290 /* Loop over all elements of BSET, in reverse order starting with
291 MIN, executing CODE. */
292 #define BITSET_REVERSE_EXECUTE(BSET, MIN, N, CODE) \
294 bitset_bindex _list[BITSET_LIST_SIZE]; \
295 bitset_bindex _next = (MIN); \
298 while ((_num = bitset_reverse_list ((BSET), _list, \
299 BITSET_LIST_SIZE, &_next))) \
303 for (_i = 0; _i < _num; _i++) \
308 if (_num < BITSET_LIST_SIZE) \
314 /* Define set operations in terms of logical operations. */
316 #define bitset_diff(DST, SRC1, SRC2) bitset_andn (DST, SRC1, SRC2)
318 #define bitset_intersection(DST, SRC1, SRC2) bitset_and (DST, SRC1, SRC2)
320 #define bitset_union(DST, SRC1, SRC2) bitset_or (DST, SRC1, SRC2)
322 #define bitset_diff_union(DST, SRC1, SRC2, SRC3) \
323 bitset_andn_or (DST, SRC1, SRC2, SRC3)
326 /* Release any memory tied up with bitsets. */
327 extern void bitset_release_memory
PARAMS ((void));
329 /* Initialise bitset stats. */
330 extern void bitset_stats_init
PARAMS ((void));
332 /* Dump bitset stats. */
333 extern void bitset_stats_dump
PARAMS ((FILE *));
335 /* Function to debug bitset from debugger. */
336 extern void debug_bitset
PARAMS ((bitset
));
338 /* Function to debug bitset stats from debugger. */
339 extern void debug_bitset_stats
PARAMS ((void));
341 #endif /* _BITSET_H */