19 |
#ifndef _BITSET_H |
#ifndef _BITSET_H |
20 |
#define _BITSET_H |
#define _BITSET_H |
21 |
|
|
22 |
#if HAVE_CONFIG_H |
/* This file is the public interface to the bitset abstract data type. |
23 |
# include <config.h> |
Only use the functions and macros defined in this file. */ |
|
#endif |
|
|
|
|
|
#define BITSET_STATS 1 |
|
24 |
|
|
25 |
# ifndef PARAMS |
#include "bbitset.h" |
|
# if defined PROTOTYPES || (defined __STDC__ && __STDC__) |
|
|
# define PARAMS(Args) Args |
|
|
# else |
|
|
# define PARAMS(Args) () |
|
|
# endif |
|
|
# endif |
|
|
|
|
|
# ifndef __attribute__ |
|
|
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__ |
|
|
# define __attribute__(x) |
|
|
# endif |
|
|
# endif |
|
|
|
|
|
# ifndef ATTRIBUTE_NORETURN |
|
|
# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__)) |
|
|
# endif |
|
|
|
|
|
# ifndef ATTRIBUTE_UNUSED |
|
|
# define ATTRIBUTE_UNUSED __attribute__ ((__unused__)) |
|
|
# endif |
|
|
|
|
|
#include "bitset-int.h" |
|
|
#include "sbitset.h" |
|
|
#include "lbitset.h" |
|
|
#include "ebitset.h" |
|
26 |
#include "obstack.h" |
#include "obstack.h" |
27 |
#include <stdio.h> |
#include <stdio.h> |
|
#include "xalloc.h" |
|
28 |
|
|
29 |
|
/* Attributes used to select a bitset implementation. */ |
30 |
enum bitset_attr {BITSET_FIXED = 1, /* Bitset size fixed. */ |
enum bitset_attr {BITSET_FIXED = 1, /* Bitset size fixed. */ |
31 |
BITSET_VARIABLE = 2, /* Bitset size variable. */ |
BITSET_VARIABLE = 2, /* Bitset size variable. */ |
32 |
BITSET_DENSE = 4, /* Bitset dense. */ |
BITSET_DENSE = 4, /* Bitset dense. */ |
36 |
|
|
37 |
typedef unsigned int bitset_attrs; |
typedef unsigned int bitset_attrs; |
38 |
|
|
39 |
/* The elements of these structures should be considered |
/* The contents of the structure should be considered to be private. |
40 |
to be private. */ |
While I would like to make this structure opaque, it needs to be |
41 |
typedef struct bitset_struct |
visible for the inline bit set/test functions. */ |
42 |
{ |
struct bitset_struct |
|
struct bitset_ops_struct *ops; |
|
|
bitset_windex cindex; /* Cache word index. */ |
|
|
bitset_windex csize; /* Cache size in words. */ |
|
|
bitset_word *cdata; /* Cache data pointer. */ |
|
|
union bitset_data |
|
|
{ |
|
|
struct sbitset_struct s; |
|
|
struct lbitset_struct l; |
|
|
struct ebitset_struct e; |
|
|
} u; |
|
|
} *bitset; |
|
|
|
|
|
|
|
|
/* The contents of this structure should be considered private. */ |
|
|
struct bitset_ops_struct |
|
43 |
{ |
{ |
44 |
void (*set) PARAMS ((struct bitset_struct *, bitset_bindex)); |
struct bbitset_struct b; |
|
void (*reset) PARAMS ((struct bitset_struct *, bitset_bindex)); |
|
|
int (*test) PARAMS ((struct bitset_struct *, bitset_bindex)); |
|
|
int (*size) PARAMS ((struct bitset_struct *)); |
|
|
int (*op1) PARAMS ((struct bitset_struct *, enum bitset_ops)); |
|
|
int (*op2) PARAMS ((struct bitset_struct *, struct bitset_struct *, |
|
|
enum bitset_ops)); |
|
|
int (*op3) PARAMS ((struct bitset_struct *, struct bitset_struct *, |
|
|
struct bitset_struct *, enum bitset_ops)); |
|
|
int (*op4) PARAMS ((struct bitset_struct *, struct bitset_struct *, |
|
|
struct bitset_struct *, struct bitset_struct *, |
|
|
enum bitset_ops)); |
|
|
int (*list) PARAMS ((struct bitset_struct *, bitset_bindex *, |
|
|
bitset_bindex, bitset_bindex *)); |
|
|
int (*reverse_list) PARAMS ((struct bitset_struct *, bitset_bindex *, |
|
|
bitset_bindex, bitset_bindex *)); |
|
|
void (*free) PARAMS ((struct bitset_struct *)); |
|
|
enum bitset_type type; |
|
45 |
}; |
}; |
46 |
|
|
|
|
|
47 |
/* Return bytes required for bitset of desired type and size. */ |
/* Return bytes required for bitset of desired type and size. */ |
48 |
extern int bitset_bytes PARAMS ((enum bitset_type, bitset_bindex)); |
extern int bitset_bytes PARAMS ((enum bitset_type, bitset_bindex)); |
49 |
|
|
50 |
/* Initialise a bitset with desired type and size. */ |
/* Initialise a bitset with desired type and size. */ |
51 |
extern bitset bitset_init PARAMS ((bitset, bitset_bindex, enum bitset_type)); |
extern bitset bitset_init PARAMS ((bitset, bitset_bindex, enum bitset_type)); |
52 |
|
|
53 |
|
/* Select an implementation type based on the desired bitset size |
54 |
|
and attributes. */ |
55 |
extern enum bitset_type bitset_type_choose PARAMS ((bitset_bindex, |
extern enum bitset_type bitset_type_choose PARAMS ((bitset_bindex, |
56 |
bitset_attrs)); |
bitset_attrs)); |
57 |
|
|
58 |
/* Create a bitset of desired type and size. */ |
/* Create a bitset of desired type and size. The bitset is zeroed. */ |
59 |
extern bitset bitset_alloc PARAMS ((bitset_bindex, enum bitset_type)); |
extern bitset bitset_alloc PARAMS ((bitset_bindex, enum bitset_type)); |
60 |
|
|
61 |
/* Free bitset. */ |
/* Free bitset. */ |
62 |
extern void bitset_free PARAMS ((bitset)); |
extern void bitset_free PARAMS ((bitset)); |
63 |
|
|
64 |
/* Create a bitset of desired type and size using obstack. */ |
/* Create a bitset of desired type and size using an obstack. The |
65 |
|
bitset is zeroed. */ |
66 |
extern bitset bitset_obstack_alloc PARAMS ((struct obstack *bobstack, |
extern bitset bitset_obstack_alloc PARAMS ((struct obstack *bobstack, |
67 |
bitset_bindex, enum bitset_type)); |
bitset_bindex, enum bitset_type)); |
68 |
|
|
69 |
/* Free bitset allocated on obstack. */ |
/* Free bitset allocated on obstack. */ |
70 |
extern void bitset_obstack_free PARAMS ((bitset)); |
extern void bitset_obstack_free PARAMS ((bitset)); |
71 |
|
|
72 |
/* Create a bitset of desired size and attributes. */ |
/* Create a bitset of desired size and attributes. The bitset is zeroed. */ |
73 |
extern bitset bitset_create PARAMS ((bitset_bindex, bitset_attrs)); |
extern bitset bitset_create PARAMS ((bitset_bindex, bitset_attrs)); |
74 |
|
|
75 |
/* Return size in bits of bitset SRC. */ |
/* Return size in bits of bitset SRC. */ |
76 |
extern int bitset_size PARAMS ((bitset)); |
extern int bitset_size PARAMS ((bitset)); |
77 |
|
|
78 |
|
/* Return number of bits set in bitset SRC. */ |
79 |
|
extern int bitset_count PARAMS ((bitset)); |
80 |
|
|
81 |
#if BITSET_CACHE && BITSET_INLINE |
#if BITSET_CACHE && BITSET_INLINE |
82 |
static inline void bitset_set PARAMS ((bitset, bitset_bindex)); |
static inline void bitset_set PARAMS ((bitset, bitset_bindex)); |
83 |
static inline void bitset_reset PARAMS ((bitset, bitset_bindex)); |
static inline void bitset_reset PARAMS ((bitset, bitset_bindex)); |
89 |
bitset_bindex bitno; |
bitset_bindex bitno; |
90 |
{ |
{ |
91 |
bitset_windex index = bitno / BITSET_WORD_BITS; |
bitset_windex index = bitno / BITSET_WORD_BITS; |
92 |
bitset_windex offset = index - bset->cindex; |
bitset_windex offset = index - bset->b.cindex; |
93 |
|
|
94 |
if (offset < bset->csize) |
if (offset < bset->b.csize) |
95 |
bset->cdata[offset] |= (1 << (bitno % BITSET_WORD_BITS)); |
bset->b.cdata[offset] |= (1 << (bitno % BITSET_WORD_BITS)); |
96 |
else |
else |
97 |
BITSET__SET (bset, bitno); |
BITSET_SET_ (bset, bitno); |
98 |
} |
} |
99 |
|
|
100 |
|
|
104 |
bitset_bindex bitno; |
bitset_bindex bitno; |
105 |
{ |
{ |
106 |
bitset_windex index = bitno / BITSET_WORD_BITS; |
bitset_windex index = bitno / BITSET_WORD_BITS; |
107 |
bitset_windex offset = index - bset->cindex; |
bitset_windex offset = index - bset->b.cindex; |
108 |
|
|
109 |
if (offset < bset->csize) |
if (offset < bset->b.csize) |
110 |
bset->cdata[offset] &= ~(1 << (bitno % BITSET_WORD_BITS)); |
bset->b.cdata[offset] &= ~(1 << (bitno % BITSET_WORD_BITS)); |
111 |
else |
else |
112 |
BITSET__RESET (bset, bitno); |
BITSET_RESET_ (bset, bitno); |
113 |
} |
} |
114 |
|
|
115 |
|
|
119 |
bitset_bindex bitno; |
bitset_bindex bitno; |
120 |
{ |
{ |
121 |
bitset_windex index = bitno / BITSET_WORD_BITS; |
bitset_windex index = bitno / BITSET_WORD_BITS; |
122 |
bitset_windex offset = index - bset->cindex; |
bitset_windex offset = index - bset->b.cindex; |
123 |
|
|
124 |
if (offset < bset->csize) |
if (offset < bset->b.csize) |
125 |
return (bset->cdata[offset] >> (bitno % BITSET_WORD_BITS)) & 1; |
return (bset->b.cdata[offset] >> (bitno % BITSET_WORD_BITS)) & 1; |
126 |
else |
else |
127 |
return BITSET__TEST (bset, bitno); |
return BITSET_TEST_ (bset, bitno); |
128 |
} |
} |
129 |
#endif |
#endif |
130 |
|
|
136 |
{ \ |
{ \ |
137 |
bitset_bindex _bitno = (bitno); \ |
bitset_bindex _bitno = (bitno); \ |
138 |
bitset_windex _index = _bitno / BITSET_WORD_BITS; \ |
bitset_windex _index = _bitno / BITSET_WORD_BITS; \ |
139 |
bitset_windex _offset = _index - (bset)->cindex; \ |
bitset_windex _offset = _index - (bset)->b.cindex; \ |
140 |
\ |
\ |
141 |
if (_offset < (bset)->csize) \ |
if (_offset < (bset)->b.csize) \ |
142 |
(bset)->cdata[_offset] |= (1 << (_bitno % BITSET_WORD_BITS)); \ |
(bset)->b.cdata[_offset] |= (1 << (_bitno % BITSET_WORD_BITS)); \ |
143 |
else \ |
else \ |
144 |
BITSET__SET ((bset), _bitno); \ |
BITSET_SET_ ((bset), _bitno); \ |
145 |
} while (0) |
} while (0) |
146 |
|
|
147 |
|
|
151 |
{ \ |
{ \ |
152 |
bitset_bindex _bitno = (bitno); \ |
bitset_bindex _bitno = (bitno); \ |
153 |
bitset_windex _index = _bitno / BITSET_WORD_BITS; \ |
bitset_windex _index = _bitno / BITSET_WORD_BITS; \ |
154 |
bitset_windex _offset = _index - (bset)->cindex; \ |
bitset_windex _offset = _index - (bset)->b.cindex; \ |
155 |
\ |
\ |
156 |
if (_offset < (bset)->csize) \ |
if (_offset < (bset)->b.csize) \ |
157 |
(bset)->cdata[_offset] &= ~(1 << (_bitno % BITSET_WORD_BITS)); \ |
(bset)->b.cdata[_offset] &= ~(1 << (_bitno % BITSET_WORD_BITS)); \ |
158 |
else \ |
else \ |
159 |
BITSET__RESET ((bset), _bitno); \ |
BITSET_RESET_ ((bset), _bitno); \ |
160 |
} while (0) |
} while (0) |
161 |
|
|
162 |
|
|
163 |
/* Test bit BITNO in bitset BSET. */ |
/* Test bit BITNO in bitset BSET. */ |
164 |
#define bitset_test(bset, bitno) \ |
#define bitset_test(bset, bitno) \ |
165 |
(((((bitno) / BITSET_WORD_BITS) - (bset)->cindex) < (bset)->csize) \ |
(((((bitno) / BITSET_WORD_BITS) - (bset)->b.cindex) < (bset)->b.csize) \ |
166 |
? ((bset)->cdata[(((bitno) / BITSET_WORD_BITS) - (bset)->cindex)] \ |
? ((bset)->b.cdata[(((bitno) / BITSET_WORD_BITS) - (bset)->b.cindex)] \ |
167 |
>> ((bitno) % BITSET_WORD_BITS)) & 1 \ |
>> ((bitno) % BITSET_WORD_BITS)) & 1 \ |
168 |
: (unsigned int) BITSET__TEST ((bset), (bitno))) |
: (unsigned int) BITSET_TEST_ ((bset), (bitno))) |
169 |
#endif |
#endif |
170 |
|
|
171 |
#if ! BITSET_CACHE |
#if ! BITSET_CACHE |
172 |
/* Set bit BITNO in bitset SRC. */ |
/* Set bit BITNO in bitset SRC. */ |
173 |
#define bitset_set(SRC, BITNO) BITSET__SET (SRC, BITNO) |
#define bitset_set(SRC, BITNO) BITSET_SET_ (SRC, BITNO) |
174 |
|
|
175 |
/* Reset bit BITNO in bitset SRC. */ |
/* Reset bit BITNO in bitset SRC. */ |
176 |
#define bitset_reset(SRC, BITNO) BITSET__RESET (SRC, BITNO) |
#define bitset_reset(SRC, BITNO) BITSET_RESET_ (SRC, BITNO) |
177 |
|
|
178 |
/* Return non-zero if bit BITNO in bitset SRC is set. */ |
/* Return non-zero if bit BITNO in bitset SRC is set. */ |
179 |
#define bitset_test(SRC, BITNO) BITSET__TEST (SRC, BITNO) |
#define bitset_test(SRC, BITNO) BITSET_TEST_ (SRC, BITNO) |
180 |
#endif |
#endif |
181 |
|
|
182 |
/* DST = 0. */ |
/* DST = 0. */ |
194 |
/* Return DST == SRC. */ |
/* Return DST == SRC. */ |
195 |
extern int bitset_equal_p PARAMS ((bitset, bitset)); |
extern int bitset_equal_p PARAMS ((bitset, bitset)); |
196 |
|
|
197 |
|
/* Return DST & SRC == 0. */ |
198 |
|
extern int bitset_disjoint_p PARAMS ((bitset, bitset)); |
199 |
|
|
200 |
/* DST == SRC. */ |
/* DST == SRC. */ |
201 |
extern int bitset_copy PARAMS ((bitset, bitset)); |
extern int bitset_copy PARAMS ((bitset, bitset)); |
202 |
|
|
228 |
|
|
229 |
/* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if |
/* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if |
230 |
DST != (SRC1 & ~SRC2) | SRC3. */ |
DST != (SRC1 & ~SRC2) | SRC3. */ |
231 |
#define bitset_andn_or(DST, SRC1, SRC2, SRC3) \ |
extern int bitset_andn_or PARAMS ((bitset, bitset, bitset, bitset)); |
232 |
|
|
233 |
/* Find next bit set in BSET starting from and including BITNO. */ |
/* Find next bit set in BSET starting from and including BITNO. */ |
234 |
extern int bitset_next PARAMS ((bitset, bitset_bindex)); |
extern int bitset_next PARAMS ((bitset, bitset_bindex)); |
244 |
bitset_bindex *)); |
bitset_bindex *)); |
245 |
#else |
#else |
246 |
#define bitset_list(BSET, LIST, NUM, NEXT) \ |
#define bitset_list(BSET, LIST, NUM, NEXT) \ |
247 |
BITSET__LIST (BSET, LIST, NUM, NEXT) |
BITSET_LIST_ (BSET, LIST, NUM, NEXT) |
248 |
#endif |
#endif |
249 |
|
|
250 |
/* Find reverse list of up to NUM bits set in BSET starting from and |
/* Find reverse list of up to NUM bits set in BSET starting from and |
251 |
including NEXT. Return with actual number of bits found and with |
including NEXT. Return with actual number of bits found and with |
252 |
*NEXT indicating where search stopped. */ |
*NEXT indicating where search stopped. */ |
253 |
#define bitset_reverse_list(BSET, LIST, NUM, NEXT) \ |
#define bitset_reverse_list(BSET, LIST, NUM, NEXT) \ |
254 |
BITSET__REVERSE_LIST (BSET, LIST, NUM, NEXT) |
BITSET_REVERSE_LIST_ (BSET, LIST, NUM, NEXT) |
255 |
|
|
256 |
/* Find first set bit. */ |
/* Find first set bit. */ |
257 |
extern int bitset_first PARAMS ((bitset)); |
extern int bitset_first PARAMS ((bitset)); |
308 |
} while (0) |
} while (0) |
309 |
|
|
310 |
|
|
311 |
/* Define set operations in terms of bit operations. */ |
/* Define set operations in terms of logical operations. */ |
312 |
|
|
313 |
#define bitset_diff(DST, SRC1, SRC2) bitset_andn (DST, SRC1, SRC2) |
#define bitset_diff(DST, SRC1, SRC2) bitset_andn (DST, SRC1, SRC2) |
314 |
|
|
317 |
#define bitset_union(DST, SRC1, SRC2) bitset_or (DST, SRC1, SRC2) |
#define bitset_union(DST, SRC1, SRC2) bitset_or (DST, SRC1, SRC2) |
318 |
|
|
319 |
#define bitset_diff_union(DST, SRC1, SRC2, SRC3) \ |
#define bitset_diff_union(DST, SRC1, SRC2, SRC3) \ |
320 |
bitset_andn_or (DST, SRC1, SRC2, SRC3) |
bitset_andn_or (DST, SRC1, SRC2, SRC3) |
321 |
|
|
322 |
|
|
323 |
/* Release any memory tied up with bitsets. */ |
/* Release any memory tied up with bitsets. */ |
324 |
extern void bitset_release_memory PARAMS ((void)); |
extern void bitset_release_memory PARAMS ((void)); |
335 |
/* Function to debug bitset stats from debugger. */ |
/* Function to debug bitset stats from debugger. */ |
336 |
extern void debug_bitset_stats PARAMS ((void)); |
extern void debug_bitset_stats PARAMS ((void)); |
337 |
|
|
|
extern bitset sbitset_init PARAMS ((bitset, bitset_bindex)); |
|
|
|
|
|
extern bitset lbitset_init PARAMS ((bitset, bitset_bindex)); |
|
|
|
|
|
extern bitset ebitset_init PARAMS ((bitset, bitset_bindex)); |
|
|
|
|
338 |
#endif /* _BITSET_H */ |
#endif /* _BITSET_H */ |