]>
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 /* Bison modifications: BEGIN. */
30 # define BITSET_STATS 1
31 /* This file is the public interface to the bitset abstract data type.
32 Only use the functions and macros defined in this file. */
35 # if defined PROTOTYPES || (defined __STDC__ && __STDC__)
36 # define PARAMS(Args) Args
38 # define PARAMS(Args) ()
42 # ifndef __attribute__
43 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
44 # define __attribute__(x)
48 # ifndef ATTRIBUTE_NORETURN
49 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
52 # ifndef ATTRIBUTE_UNUSED
53 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
56 #else /* ! IN_BISON */
57 # include "libiberty.h"
64 /* Currently we support three flavours of bitsets:
65 BITSET_ARRAY: Array of bits (fixed size, faster).
66 BITSET_LIST: Linked list of array of bits (variable size, least storage
67 for large very sparse sets).
68 BITSET_TABLE: Expandable table of pointers to array of bits
69 (variable size, less storage for large sparse sets).
71 enum bitset_type
{BITSET_ARRAY
, BITSET_LIST
, BITSET_TABLE
, BITSET_TYPE_NUM
};
72 #define BITSET_TYPE_NAMES {"sbitset", "lbitset", "ebitset"}
74 /* Non-zero to enable bitset caching. */
75 #define BITSET_CACHE 1
77 /* Non-zero to use inline functions instead of macros. */
78 #define BITSET_INLINE 0
80 /* Non-zero to enable bitset statistics gathering. */
81 #define BITSET_STATS 1
83 /* Non-zero to enable bitset type checking. */
84 #define BITSET_CHECK 1
86 /* Data type used to store a word of bits. */
87 typedef unsigned long bitset_word
;
88 #define BITSET_WORD_BITS ((unsigned) CHAR_BIT * sizeof (bitset_word))
91 typedef unsigned long bitset_bindex
;
94 typedef unsigned long bitset_windex
;
96 #define BITSET_INDEX_MAX ((1U << (BITSET_WORD_BITS - 1)))
98 #define BITSET_MSB (1U << (BITSET_WORD_BITS - 1))
100 #define BITSET_LIST_SIZE 1024
102 enum bitset_ops
{BITSET_OP_ZERO
, BITSET_OP_ONES
,
103 BITSET_OP_COPY
, BITSET_OP_NOT
,
104 BITSET_OP_EMPTY_P
, BITSET_OP_EQUAL_P
,
105 BITSET_OP_SUBSET_P
, BITSET_OP_DISJOINT_P
,
106 BITSET_OP_AND
, BITSET_OP_OR
, BITSET_OP_XOR
, BITSET_OP_ANDN
,
107 BITSET_OP_OR_AND
, BITSET_OP_AND_OR
, BITSET_OP_ANDN_OR
};
109 /* Return size in bits of bitset SRC. */
110 #define BITSET_SIZE_(SRC) (SRC)->b.ops->size (SRC)
112 /* Return type of bitset SRC. */
113 #define BITSET_TYPE_(DST) (DST)->b.ops->type
115 /* Set bit BITNO in bitset DST. */
116 #define BITSET_SET_(DST, BITNO) (DST)->b.ops->set (DST, BITNO)
118 /* Reset bit BITNO in bitset DST. */
119 #define BITSET_RESET_(DST, BITNO) (DST)->b.ops->reset (DST, BITNO)
121 /* Return non-zero if bit BITNO in bitset SRC is set. */
122 #define BITSET_TEST_(SRC, BITNO) (SRC)->b.ops->test (SRC, BITNO)
124 /* Free bitset SRC. */
125 #define BITSET_FREE_(SRC)\
126 ((SRC)->b.ops->free ? (SRC)->b.ops->free (SRC) :(void)0)
128 /* Perform operation OP on DST. */
129 #define BITSET_OP1_(DST, OP) (DST)->b.ops->op1 (DST, OP)
131 /* Perform operation OP on SRC and store in DST. */
132 #define BITSET_OP2_(DST, SRC, OP) (DST)->b.ops->op2 (DST, SRC, OP)
134 /* DST = SRC1 OP SRC2. */
135 #define BITSET_OP3_(DST, SRC1, SRC2, OP) \
136 (DST)->b.ops->op3 (DST, SRC1, SRC2, OP)
138 /* DST = (SRC1 OP1 SRC2) OP2 SRC3. */
139 #define BITSET_OP4_(DST, SRC1, SRC2, SRC3, OP) \
140 (DST)->b.ops->op4 (DST, SRC1, SRC2, SRC3, OP)
143 #define BITSET_ZERO_(DST) BITSET_OP1_ (DST, BITSET_OP_ZERO);
146 #define BITSET_ONES_(DST) BITSET_OP1_ (DST, BITSET_OP_ONES);
148 /* Return SRC == 0. */
149 #define BITSET_EMPTY_P_(SRC) BITSET_OP1_ (SRC, BITSET_OP_EMPTY_P);
151 /* Return DST == SRC. */
152 #define BITSET_EQUAL_P_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_EQUAL_P)
154 /* Return DST == DST | SRC. */
155 #define BITSET_SUBSET_P_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_SUBSET_P)
157 /* Return DST & SRC == 0. */
158 #define BITSET_DISJOINT_P_(DST, SRC)\
159 BITSET_OP2_ (DST, SRC, BITSET_OP_DISJOINT_P)
162 #define BITSET_COPY_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_COPY)
165 #define BITSET_NOT_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_NOT)
167 /* DST = SRC1 | SRC2. */
168 #define BITSET_OR_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2,\
171 /* DST = SRC1 ^ SRC2. */
172 #define BITSET_XOR_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2,\
175 /* DST = SRC1 & SRC2. */
176 #define BITSET_AND_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2, \
179 /* DST = SRC1 & ~SRC2. */
180 #define BITSET_ANDN_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2, \
183 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
184 DST != (SRC1 & SRC2) | SRC3. */
185 #define BITSET_AND_OR_(DST, SRC1, SRC2, SRC3)\
186 BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_AND_OR)
188 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
189 DST != (SRC1 | SRC2) & SRC3. */
190 #define BITSET_OR_AND_(DST, SRC1, SRC2, SRC3)\
191 BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_OR_AND)
193 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
194 DST != (SRC1 & ~SRC2) | SRC3. */
195 #define BITSET_ANDN_OR_(DST, SRC1, SRC2, SRC3)\
196 BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_ANDN_OR)
198 /* Find list of up to NUM bits set in BSET starting from and including
199 *NEXT. Return with actual number of bits found and with *NEXT
200 indicating where search stopped. */
201 #define BITSET_LIST_(BSET, LIST, NUM, NEXT) \
202 (BSET)->b.ops->list (BSET, LIST, NUM, NEXT)
204 /* Find reverse list of up to NUM bits set in BSET starting from and
205 including NEXT. Return with actual number of bits found and with
206 *NEXT indicating where search stopped. */
207 #define BITSET_REVERSE_LIST_(BSET, LIST, NUM, NEXT) \
208 (BSET)->b.ops->reverse_list (BSET, LIST, NUM, NEXT)
211 struct bbitset_struct
213 struct bitset_ops_struct
*ops
;
214 bitset_windex cindex
; /* Cache word index. */
215 bitset_windex csize
; /* Cache size in words. */
216 bitset_word
*cdata
; /* Cache data pointer. */
217 /* Perhaps we could sacrifice another word to indicate
218 that the bitset is known to be zero, that a bit has been set
219 in the cache, and that a bit has been cleared in the cache.
220 This would speed up some of the searches but slightly slow down
221 bit set/reset operations of cached bits. */
225 typedef struct bitset_struct
*bitset
;
228 /* The contents of this structure should be considered private. */
229 struct bitset_ops_struct
231 void (*set
) PARAMS ((struct bitset_struct
*, bitset_bindex
));
232 void (*reset
) PARAMS ((struct bitset_struct
*, bitset_bindex
));
233 int (*test
) PARAMS ((struct bitset_struct
*, bitset_bindex
));
234 int (*size
) PARAMS ((struct bitset_struct
*));
235 int (*op1
) PARAMS ((struct bitset_struct
*, enum bitset_ops
));
236 int (*op2
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
238 int (*op3
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
239 struct bitset_struct
*, enum bitset_ops
));
240 int (*op4
) PARAMS ((struct bitset_struct
*, struct bitset_struct
*,
241 struct bitset_struct
*, struct bitset_struct
*,
243 int (*list
) PARAMS ((struct bitset_struct
*, bitset_bindex
*,
244 bitset_bindex
, bitset_bindex
*));
245 int (*reverse_list
) PARAMS ((struct bitset_struct
*, bitset_bindex
*,
246 bitset_bindex
, bitset_bindex
*));
247 void (*free
) PARAMS ((struct bitset_struct
*));
248 enum bitset_type type
;
251 #define BITSET_COMPATIBLE_(BSET1, BSET2) ((BSET1)->b.ops == (BSET2)->b.ops)
254 #define BITSET_CHECK2_(DST, SRC) \
255 if (!BITSET_COMPATIBLE_ (DST, SRC)) abort ();
257 #define BITSET_CHECK3_(DST, SRC1, SRC2) \
258 if (!BITSET_COMPATIBLE_ (DST, SRC1) \
259 || !BITSET_COMPATIBLE_ (DST, SRC2)) abort ();
261 #define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) \
262 if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \
263 || !BITSET_COMPATIBLE_ (DST, SRC3)) abort ();
265 #define BITSET_CHECK2_(DST, SRC)
267 #define BITSET_CHECK3_(DST, SRC1, SRC2)
269 #define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3)
272 extern int bitset_op4
PARAMS ((bitset
, bitset
, bitset
, bitset
,
273 enum bitset_ops op
));
275 #endif /* _BBITSET_H */