]>
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, 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).
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 enum bitset_alloc_type
{BITSET_MALLOC
, BITSET_OBALLOC
};
43 /* Non-zero to use inline functions instead of macros. */
44 #define BITSET_INLINE 0
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))
51 typedef unsigned long bitset_bindex
;
54 typedef unsigned long bitset_windex
;
56 #define BITSET_INDEX_MAX ((1U << (BITSET_WORD_BITS - 1)))
58 #define BITSET_MSB (1U << (BITSET_WORD_BITS - 1))
60 #define BITSET_LIST_SIZE 1024
62 enum bitset_ops
{BITSET_OP_ZERO
, BITSET_OP_ONES
,
63 BITSET_OP_COPY
, BITSET_OP_NOT
,
64 BITSET_OP_EMPTY_P
, BITSET_OP_EQUAL_P
,
65 BITSET_OP_SUBSET_P
, BITSET_OP_DISJOINT_P
,
66 BITSET_OP_AND
, BITSET_OP_OR
, BITSET_OP_XOR
, BITSET_OP_ANDN
,
67 BITSET_OP_OR_AND
, BITSET_OP_AND_OR
, BITSET_OP_ANDN_OR
};
69 /* Return size in bits of bitset SRC. */
70 #define BITSET_SIZE_(SRC) (SRC)->b.ops->size (SRC)
72 /* Return type of bitset SRC. */
73 #define BITSET_TYPE_(DST) (DST)->b.ops->type
75 /* Set bit BITNO in bitset DST. */
76 #define BITSET_SET_(DST, BITNO) (DST)->b.ops->set (DST, BITNO)
78 /* Reset bit BITNO in bitset DST. */
79 #define BITSET_RESET_(DST, BITNO) (DST)->b.ops->reset (DST, BITNO)
81 /* Return non-zero if bit BITNO in bitset SRC is set. */
82 #define BITSET_TEST_(SRC, BITNO) (SRC)->b.ops->test (SRC, BITNO)
84 /* Free bitset SRC. */
85 #define BITSET_FREE_(SRC)\
86 ((SRC)->b.ops->free ? (SRC)->b.ops->free (SRC) :(void)0)
88 /* Perform operation OP on DST. */
89 #define BITSET_OP1_(DST, OP) (DST)->b.ops->op1 (DST, OP)
91 /* Perform operation OP on SRC and store in DST. */
92 #define BITSET_OP2_(DST, SRC, OP) (DST)->b.ops->op2 (DST, SRC, OP)
94 /* DST = SRC1 OP SRC2. */
95 #define BITSET_OP3_(DST, SRC1, SRC2, OP) \
96 (DST)->b.ops->op3 (DST, SRC1, SRC2, OP)
98 /* DST = (SRC1 OP1 SRC2) OP2 SRC3. */
99 #define BITSET_OP4_(DST, SRC1, SRC2, SRC3, OP) \
100 (DST)->b.ops->op4 (DST, SRC1, SRC2, SRC3, OP)
103 #define BITSET_ZERO_(DST) BITSET_OP1_ (DST, BITSET_OP_ZERO);
106 #define BITSET_ONES_(DST) BITSET_OP1_ (DST, BITSET_OP_ONES);
108 /* Return SRC == 0. */
109 #define BITSET_EMPTY_P_(SRC) BITSET_OP1_ (SRC, BITSET_OP_EMPTY_P);
111 /* Return DST == SRC. */
112 #define BITSET_EQUAL_P_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_EQUAL_P)
114 /* Return DST == DST | SRC. */
115 #define BITSET_SUBSET_P_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_SUBSET_P)
117 /* Return DST & SRC == 0. */
118 #define BITSET_DISJOINT_P_(DST, SRC)\
119 BITSET_OP2_ (DST, SRC, BITSET_OP_DISJOINT_P)
122 #define BITSET_COPY_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_COPY)
125 #define BITSET_NOT_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_NOT)
127 /* DST = SRC1 | SRC2. */
128 #define BITSET_OR_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2,\
131 /* DST = SRC1 ^ SRC2. */
132 #define BITSET_XOR_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2,\
135 /* DST = SRC1 & SRC2. */
136 #define BITSET_AND_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2, \
139 /* DST = SRC1 & ~SRC2. */
140 #define BITSET_ANDN_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2, \
143 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
144 DST != (SRC1 & SRC2) | SRC3. */
145 #define BITSET_AND_OR_(DST, SRC1, SRC2, SRC3)\
146 BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_AND_OR)
148 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
149 DST != (SRC1 | SRC2) & SRC3. */
150 #define BITSET_OR_AND_(DST, SRC1, SRC2, SRC3)\
151 BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_OR_AND)
153 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
154 DST != (SRC1 & ~SRC2) | SRC3. */
155 #define BITSET_ANDN_OR_(DST, SRC1, SRC2, SRC3)\
156 BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_ANDN_OR)
158 /* Find list of up to NUM bits set in BSET starting from and including
159 *NEXT. Return with actual number of bits found and with *NEXT
160 indicating where search stopped. */
161 #define BITSET_LIST_(BSET, LIST, NUM, NEXT) \
162 (BSET)->b.ops->list (BSET, LIST, NUM, NEXT)
164 /* Find reverse list of up to NUM bits set in BSET starting from and
165 including NEXT. Return with actual number of bits found and with
166 *NEXT indicating where search stopped. */
167 #define BITSET_REVERSE_LIST_(BSET, LIST, NUM, NEXT) \
168 (BSET)->b.ops->reverse_list (BSET, LIST, NUM, NEXT)
171 struct bbitset_struct
173 struct bitset_ops_struct
*ops
;
174 bitset_windex cindex
; /* Cache word index. */
175 bitset_windex csize
; /* Cache size in words. */
176 bitset_word
*cdata
; /* Cache data pointer. */
177 /* Perhaps we could sacrifice another word to indicate
178 that the bitset is known to be zero, that a bit has been set
179 in the cache, and that a bit has been cleared in the cache.
180 This would speed up some of the searches but slightly slow down
181 bit set/reset operations of cached bits. */
185 typedef struct bitset_struct
*bitset
;
188 /* The contents of this structure should be considered private. */
189 struct bitset_ops_struct
191 void (*set
) PARAMS ((struct bitset_struct
*, bitset_bindex
));
192 void (*reset
) PARAMS ((struct bitset_struct
*, bitset_bindex
));
193 int (*test
) PARAMS ((struct bitset_struct
*, bitset_bindex
));
194 int (*size
) PARAMS ((struct bitset_struct
*));
195 int (*op1
) PARAMS ((struct bitset_struct
*, enum bitset_ops
));
196 int (*op2
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
198 int (*op3
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
199 struct bitset_struct
*, enum bitset_ops
));
200 int (*op4
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
201 struct bitset_struct
*, struct bitset_struct
*,
203 int (*list
) PARAMS ((struct bitset_struct
*, bitset_bindex
*,
204 bitset_bindex
, bitset_bindex
*));
205 int (*reverse_list
) PARAMS ((struct bitset_struct
*, bitset_bindex
*,
206 bitset_bindex
, bitset_bindex
*));
207 void (*free
) PARAMS ((struct bitset_struct
*));
208 enum bitset_type type
;
211 #define BITSET_COMPATIBLE_(BSET1, BSET2) ((BSET1)->b.ops == (BSET2)->b.ops)
213 #define BITSET_CHECK2_(DST, SRC) \
214 if (!BITSET_COMPATIBLE_ (DST, SRC)) abort ();
216 #define BITSET_CHECK3_(DST, SRC1, SRC2) \
217 if (!BITSET_COMPATIBLE_ (DST, SRC1) \
218 || !BITSET_COMPATIBLE_ (DST, SRC2)) abort ();
220 #define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) \
221 if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \
222 || !BITSET_COMPATIBLE_ (DST, SRC3)) abort ();
225 extern int bitset_op4
PARAMS ((bitset
, bitset
, bitset
, bitset
,
226 enum bitset_ops op
));
228 #endif /* _BBITSET_H */