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. */
26 #define BITSET_STATS 1
29 # if defined PROTOTYPES || (defined __STDC__ && __STDC__)
30 # define PARAMS(Args) Args
32 # define PARAMS(Args) ()
36 # ifndef __attribute__
37 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
38 # define __attribute__(x)
42 # ifndef ATTRIBUTE_NORETURN
43 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
46 # ifndef ATTRIBUTE_UNUSED
47 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
50 #include "bitset-int.h"
58 enum bitset_attr
{BITSET_FIXED
= 1, /* Bitset size fixed. */
59 BITSET_VARIABLE
= 2, /* Bitset size variable. */
60 BITSET_DENSE
= 4, /* Bitset dense. */
61 BITSET_SPARSE
= 8, /* Bitset sparse. */
62 BITSET_FRUGAL
= 16, /* Prefer most compact. */
63 BITSET_GREEDY
= 32}; /* Prefer fastest. */
65 typedef unsigned int bitset_attrs
;
67 /* The elements of these structures should be considered
69 typedef struct bitset_struct
71 struct bitset_ops_struct
*ops
;
72 bitset_windex cindex
; /* Cache word index. */
73 bitset_windex csize
; /* Cache size in words. */
74 bitset_word
*cdata
; /* Cache data pointer. */
77 struct sbitset_struct s
;
78 struct lbitset_struct l
;
79 struct ebitset_struct e
;
84 /* The contents of this structure should be considered private. */
85 struct bitset_ops_struct
87 void (*set
) PARAMS ((struct bitset_struct
*, bitset_bindex
));
88 void (*reset
) PARAMS ((struct bitset_struct
*, bitset_bindex
));
89 int (*test
) PARAMS ((struct bitset_struct
*, bitset_bindex
));
90 int (*size
) PARAMS ((struct bitset_struct
*));
91 int (*op1
) PARAMS ((struct bitset_struct
*, enum bitset_ops
));
92 int (*op2
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
94 int (*op3
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
95 struct bitset_struct
*, enum bitset_ops
));
96 int (*op4
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
97 struct bitset_struct
*, struct bitset_struct
*,
99 int (*list
) PARAMS ((struct bitset_struct
*, bitset_bindex
*,
100 bitset_bindex
, bitset_bindex
*));
101 int (*reverse_list
) PARAMS ((struct bitset_struct
*, bitset_bindex
*,
102 bitset_bindex
, bitset_bindex
*));
103 void (*free
) PARAMS ((struct bitset_struct
*));
104 enum bitset_type type
;
108 /* Return bytes required for bitset of desired type and size. */
109 extern int bitset_bytes
PARAMS ((enum bitset_type
, bitset_bindex
));
111 /* Initialise a bitset with desired type and size. */
112 extern bitset bitset_init
PARAMS ((bitset
, bitset_bindex
, enum bitset_type
));
114 extern enum bitset_type bitset_type_choose
PARAMS ((bitset_bindex
,
117 /* Create a bitset of desired type and size. */
118 extern bitset bitset_alloc
PARAMS ((bitset_bindex
, enum bitset_type
));
121 extern void bitset_free
PARAMS ((bitset
));
123 /* Create a bitset of desired type and size using obstack. */
124 extern bitset bitset_obstack_alloc
PARAMS ((struct obstack
*bobstack
,
125 bitset_bindex
, enum bitset_type
));
127 /* Free bitset allocated on obstack. */
128 extern void bitset_obstack_free
PARAMS ((bitset
));
130 /* Create a bitset of desired size and attributes. */
131 extern bitset bitset_create
PARAMS ((bitset_bindex
, bitset_attrs
));
133 /* Return size in bits of bitset SRC. */
134 extern int bitset_size
PARAMS ((bitset
));
136 #if BITSET_CACHE && BITSET_INLINE
137 static inline void bitset_set
PARAMS ((bitset
, bitset_bindex
));
138 static inline void bitset_reset
PARAMS ((bitset
, bitset_bindex
));
139 static inline int bitset_test
PARAMS ((bitset
, bitset_bindex
));
141 /* Set bit BITNO in bitset BSET. */
142 static inline void bitset_set (bset
, bitno
)
146 bitset_windex index
= bitno
/ BITSET_WORD_BITS
;
147 bitset_windex offset
= index
- bset
->cindex
;
149 if (offset
< bset
->csize
)
150 bset
->cdata
[offset
] |= (1 << (bitno
% BITSET_WORD_BITS
));
152 BITSET__SET (bset
, bitno
);
156 /* Reset bit BITNO in bitset BSET. */
157 static inline void bitset_reset (bset
, bitno
)
161 bitset_windex index
= bitno
/ BITSET_WORD_BITS
;
162 bitset_windex offset
= index
- bset
->cindex
;
164 if (offset
< bset
->csize
)
165 bset
->cdata
[offset
] &= ~(1 << (bitno
% BITSET_WORD_BITS
));
167 BITSET__RESET (bset
, bitno
);
171 /* Test bit BITNO in bitset BSET. */
172 static inline int bitset_test (bset
, bitno
)
176 bitset_windex index
= bitno
/ BITSET_WORD_BITS
;
177 bitset_windex offset
= index
- bset
->cindex
;
179 if (offset
< bset
->csize
)
180 return (bset
->cdata
[offset
] >> (bitno
% BITSET_WORD_BITS
)) & 1;
182 return BITSET__TEST (bset
, bitno
);
186 #if BITSET_CACHE && ! BITSET_INLINE
188 /* Set bit BITNO in bitset BSET. */
189 #define bitset_set(bset, bitno) \
192 bitset_bindex _bitno = (bitno); \
193 bitset_windex _index = _bitno / BITSET_WORD_BITS; \
194 bitset_windex _offset = _index - (bset)->cindex; \
196 if (_offset < (bset)->csize) \
197 (bset)->cdata[_offset] |= (1 << (_bitno % BITSET_WORD_BITS)); \
199 BITSET__SET ((bset), _bitno); \
203 /* Reset bit BITNO in bitset BSET. */
204 #define bitset_reset(bset, bitno) \
207 bitset_bindex _bitno = (bitno); \
208 bitset_windex _index = _bitno / BITSET_WORD_BITS; \
209 bitset_windex _offset = _index - (bset)->cindex; \
211 if (_offset < (bset)->csize) \
212 (bset)->cdata[_offset] &= ~(1 << (_bitno % BITSET_WORD_BITS)); \
214 BITSET__RESET ((bset), _bitno); \
218 /* Test bit BITNO in bitset BSET. */
219 #define bitset_test(bset, bitno) \
220 (((((bitno) / BITSET_WORD_BITS) - (bset)->cindex) < (bset)->csize) \
221 ? ((bset)->cdata[(((bitno) / BITSET_WORD_BITS) - (bset)->cindex)] \
222 >> ((bitno) % BITSET_WORD_BITS)) & 1 \
223 : (unsigned int) BITSET__TEST ((bset), (bitno)))
227 /* Set bit BITNO in bitset SRC. */
228 #define bitset_set(SRC, BITNO) BITSET__SET (SRC, BITNO)
230 /* Reset bit BITNO in bitset SRC. */
231 #define bitset_reset(SRC, BITNO) BITSET__RESET (SRC, BITNO)
233 /* Return non-zero if bit BITNO in bitset SRC is set. */
234 #define bitset_test(SRC, BITNO) BITSET__TEST (SRC, BITNO)
238 extern int bitset_zero
PARAMS ((bitset
));
241 extern int bitset_ones
PARAMS ((bitset
));
243 /* Return non-zero if all bits in bitset SRC are reset. */
244 extern int bitset_empty_p
PARAMS ((bitset
));
246 /* Return DST == DST | SRC. */
247 extern int bitset_subset_p
PARAMS ((bitset
, bitset
));
249 /* Return DST == SRC. */
250 extern int bitset_equal_p
PARAMS ((bitset
, bitset
));
253 extern int bitset_copy
PARAMS ((bitset
, bitset
));
256 extern int bitset_not
PARAMS ((bitset
, bitset
));
258 /* DST = SRC1 | SRC2. Return non-zero if DST != SRC1 | SRC2. */
259 extern int bitset_or
PARAMS ((bitset
, bitset
, bitset
));
261 /* DST = SRC1 & SRC2. Return non-zero if DST != SRC1 & SRC2. */
262 extern int bitset_and
PARAMS ((bitset
, bitset
, bitset
));
264 /* DST = SRC1 ^ SRC2. Return non-zero if DST != SRC1 ^ SRC2. */
265 extern int bitset_xor
PARAMS ((bitset
, bitset
, bitset
));
267 /* DST = SRC1 & ~SRC2. Return non-zero if DST != SRC1 & ~SRC2. */
268 extern int bitset_andn
PARAMS ((bitset
, bitset
, bitset
));
270 /* DST = SRC1 | ~SRC2. Return non-zero if DST != SRC1 | ~SRC2. */
271 extern int bitset_orn
PARAMS ((bitset
, bitset
, bitset
));
273 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
274 DST != (SRC1 | SRC2) & SRC3. */
275 extern int bitset_or_and
PARAMS ((bitset
, bitset
, bitset
, bitset
));
277 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
278 DST != (SRC1 & SRC2) | SRC3. */
279 extern int bitset_and_or
PARAMS ((bitset
, bitset
, bitset
, bitset
));
281 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
282 DST != (SRC1 & ~SRC2) | SRC3. */
283 #define bitset_andn_or(DST, SRC1, SRC2, SRC3) \
285 /* Find next bit set in BSET starting from and including BITNO. */
286 extern int bitset_next
PARAMS ((bitset
, bitset_bindex
));
288 /* Find previous bit set in BSET starting from and including BITNO. */
289 extern int bitset_prev
PARAMS ((bitset
, bitset_bindex
));
291 /* Find list of up to NUM bits set in BSET starting from and including
292 *NEXT. Return with actual number of bits found and with *NEXT
293 indicating where search stopped. */
295 extern int bitset_list
PARAMS ((bitset
, bitset_bindex
*, bitset_bindex
,
298 #define bitset_list(BSET, LIST, NUM, NEXT) \
299 BITSET__LIST (BSET, LIST, NUM, NEXT)
302 /* Find reverse list of up to NUM bits set in BSET starting from and
303 including NEXT. Return with actual number of bits found and with
304 *NEXT indicating where search stopped. */
305 #define bitset_reverse_list(BSET, LIST, NUM, NEXT) \
306 BITSET__REVERSE_LIST (BSET, LIST, NUM, NEXT)
308 /* Find first set bit. */
309 extern int bitset_first
PARAMS ((bitset
));
311 /* Find last set bit. */
312 extern int bitset_last
PARAMS ((bitset
));
315 extern void bitset_dump
PARAMS ((FILE *, bitset
));
317 /* Loop over all elements of BSET, starting with MIN, executing CODE. */
318 #define BITSET_EXECUTE(BSET, MIN, N, CODE) \
320 bitset_bindex _list[BITSET_LIST_SIZE]; \
321 bitset_bindex _next = (MIN); \
324 while ((_num = bitset_list ((BSET), _list, BITSET_LIST_SIZE, &_next)))\
328 for (_i = 0; _i < _num; _i++) \
333 if (_num < BITSET_LIST_SIZE) \
339 /* Loop over all elements of BSET, in reverse order starting with
340 MIN, executing CODE. */
341 #define BITSET_REVERSE_EXECUTE(BSET, MIN, N, CODE) \
343 bitset_bindex _list[BITSET_LIST_SIZE]; \
344 bitset_bindex _next = (MIN); \
347 while ((_num = bitset_reverse_list ((BSET), _list, \
348 BITSET_LIST_SIZE, &_next))) \
352 for (_i = 0; _i < _num; _i++) \
357 if (_num < BITSET_LIST_SIZE) \
363 /* Define set operations in terms of bit operations. */
365 #define bitset_diff(DST, SRC1, SRC2) bitset_andn (DST, SRC1, SRC2)
367 #define bitset_intersection(DST, SRC1, SRC2) bitset_and (DST, SRC1, SRC2)
369 #define bitset_union(DST, SRC1, SRC2) bitset_or (DST, SRC1, SRC2)
371 #define bitset_diff_union(DST, SRC1, SRC2, SRC3) \
372 bitset_andn_or (DST, SRC1, SRC2, SRC3)
374 /* Release any memory tied up with bitsets. */
375 extern void bitset_release_memory
PARAMS ((void));
377 /* Initialise bitset stats. */
378 extern void bitset_stats_init
PARAMS ((void));
380 /* Dump bitset stats. */
381 extern void bitset_stats_dump
PARAMS ((FILE *));
383 /* Function to debug bitset from debugger. */
384 extern void debug_bitset
PARAMS ((bitset
));
386 /* Function to debug bitset stats from debugger. */
387 extern void debug_bitset_stats
PARAMS ((void));
389 extern bitset sbitset_init
PARAMS ((bitset
, bitset_bindex
));
391 extern bitset lbitset_init
PARAMS ((bitset
, bitset_bindex
));
393 extern bitset ebitset_init
PARAMS ((bitset
, bitset_bindex
));
395 #endif /* _BITSET_H */