]> git.saurik.com Git - bison.git/blame - lib/bbitset.h
Update.
[bison.git] / lib / bbitset.h
CommitLineData
ef017502
AD
1/* Base bitset stuff.
2 Copyright (C) 2002 Free Software Foundation, Inc.
3 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 2 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18
19#ifndef _BBITSET_H
20#define _BBITSET_H
21
613f5e1a 22#include "libiberty.h"
ef017502
AD
23
24#ifdef HAVE_LIMITS_H
25#include <limits.h>
26#endif
27
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
613f5e1a
AD
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).
34
35 BITSET_STATS: Wrapper bitset for internal use only.
ef017502 36*/
613f5e1a
AD
37enum bitset_type {BITSET_ARRAY, BITSET_LIST, BITSET_TABLE, BITSET_TYPE_NUM,
38 BITSET_STATS};
39#define BITSET_TYPE_NAMES {"abitset", "lbitset", "ebitset"}
ef017502 40
613f5e1a 41enum bitset_alloc_type {BITSET_MALLOC, BITSET_OBALLOC};
ef017502
AD
42
43/* Non-zero to use inline functions instead of macros. */
44#define BITSET_INLINE 0
45
ef017502
AD
46/* Data type used to store a word of bits. */
47typedef unsigned long bitset_word;
50f095c9 48#define BITSET_WORD_BITS ((unsigned) (CHAR_BIT * sizeof (bitset_word)))
ef017502 49
50f095c9
PE
50/* Bit index. In theory we might need a type wider than size_t, but
51 in practice we lose at most a factor of CHAR_BIT by going with
52 size_t, and that is good enough. */
53typedef size_t bitset_bindex;
ef017502
AD
54
55/* Word index. */
50f095c9 56typedef size_t bitset_windex;
ef017502 57
50f095c9 58#define BITSET_INDEX_MAX ((bitset_word) 1 << (BITSET_WORD_BITS - 1))
ef017502 59
50f095c9 60#define BITSET_MSB ((bitset_word) 1 << (BITSET_WORD_BITS - 1))
ef017502
AD
61
62#define BITSET_LIST_SIZE 1024
63
613f5e1a
AD
64enum bitset_ops {BITSET_OP_ZERO, BITSET_OP_ONES,
65 BITSET_OP_COPY, BITSET_OP_NOT,
ef017502
AD
66 BITSET_OP_EMPTY_P, BITSET_OP_EQUAL_P,
67 BITSET_OP_SUBSET_P, BITSET_OP_DISJOINT_P,
613f5e1a 68 BITSET_OP_AND, BITSET_OP_OR, BITSET_OP_XOR, BITSET_OP_ANDN,
ef017502
AD
69 BITSET_OP_OR_AND, BITSET_OP_AND_OR, BITSET_OP_ANDN_OR};
70
71/* Return size in bits of bitset SRC. */
613f5e1a 72#define BITSET_SIZE_(SRC) (SRC)->b.ops->size (SRC)
ef017502
AD
73
74/* Return type of bitset SRC. */
613f5e1a 75#define BITSET_TYPE_(DST) (DST)->b.ops->type
ef017502
AD
76
77/* Set bit BITNO in bitset DST. */
613f5e1a 78#define BITSET_SET_(DST, BITNO) (DST)->b.ops->set (DST, BITNO)
ef017502
AD
79
80/* Reset bit BITNO in bitset DST. */
613f5e1a 81#define BITSET_RESET_(DST, BITNO) (DST)->b.ops->reset (DST, BITNO)
ef017502
AD
82
83/* Return non-zero if bit BITNO in bitset SRC is set. */
613f5e1a 84#define BITSET_TEST_(SRC, BITNO) (SRC)->b.ops->test (SRC, BITNO)
ef017502
AD
85
86/* Free bitset SRC. */
87#define BITSET_FREE_(SRC)\
88 ((SRC)->b.ops->free ? (SRC)->b.ops->free (SRC) :(void)0)
89
90/* Perform operation OP on DST. */
613f5e1a 91#define BITSET_OP1_(DST, OP) (DST)->b.ops->op1 (DST, OP)
ef017502
AD
92
93/* Perform operation OP on SRC and store in DST. */
613f5e1a 94#define BITSET_OP2_(DST, SRC, OP) (DST)->b.ops->op2 (DST, SRC, OP)
ef017502
AD
95
96/* DST = SRC1 OP SRC2. */
97#define BITSET_OP3_(DST, SRC1, SRC2, OP) \
613f5e1a 98(DST)->b.ops->op3 (DST, SRC1, SRC2, OP)
ef017502
AD
99
100/* DST = (SRC1 OP1 SRC2) OP2 SRC3. */
101#define BITSET_OP4_(DST, SRC1, SRC2, SRC3, OP) \
613f5e1a 102(DST)->b.ops->op4 (DST, SRC1, SRC2, SRC3, OP)
ef017502
AD
103
104/* DST = 0. */
105#define BITSET_ZERO_(DST) BITSET_OP1_ (DST, BITSET_OP_ZERO);
106
107/* DST = ~0. */
108#define BITSET_ONES_(DST) BITSET_OP1_ (DST, BITSET_OP_ONES);
109
110/* Return SRC == 0. */
111#define BITSET_EMPTY_P_(SRC) BITSET_OP1_ (SRC, BITSET_OP_EMPTY_P);
112
113/* Return DST == SRC. */
613f5e1a 114#define BITSET_EQUAL_P_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_EQUAL_P)
ef017502
AD
115
116/* Return DST == DST | SRC. */
613f5e1a 117#define BITSET_SUBSET_P_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_SUBSET_P)
ef017502
AD
118
119/* Return DST & SRC == 0. */
120#define BITSET_DISJOINT_P_(DST, SRC)\
613f5e1a 121 BITSET_OP2_ (DST, SRC, BITSET_OP_DISJOINT_P)
ef017502
AD
122
123/* DST = SRC. */
613f5e1a 124#define BITSET_COPY_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_COPY)
ef017502
AD
125
126/* DST = ~SRC. */
613f5e1a 127#define BITSET_NOT_(DST, SRC) BITSET_OP2_ (DST, SRC, BITSET_OP_NOT)
ef017502
AD
128
129/* DST = SRC1 | SRC2. */
130#define BITSET_OR_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2,\
613f5e1a 131 BITSET_OP_OR)
ef017502
AD
132
133/* DST = SRC1 ^ SRC2. */
134#define BITSET_XOR_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2,\
613f5e1a 135 BITSET_OP_XOR)
ef017502
AD
136
137/* DST = SRC1 & SRC2. */
138#define BITSET_AND_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2, \
613f5e1a 139 BITSET_OP_AND)
ef017502
AD
140
141/* DST = SRC1 & ~SRC2. */
142#define BITSET_ANDN_(DST, SRC1, SRC2) BITSET_OP3_ (DST, SRC1, SRC2, \
613f5e1a 143 BITSET_OP_ANDN)
ef017502 144
ef017502
AD
145/* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
146 DST != (SRC1 & SRC2) | SRC3. */
147#define BITSET_AND_OR_(DST, SRC1, SRC2, SRC3)\
613f5e1a 148 BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_AND_OR)
ef017502
AD
149
150/* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
151 DST != (SRC1 | SRC2) & SRC3. */
152#define BITSET_OR_AND_(DST, SRC1, SRC2, SRC3)\
613f5e1a 153 BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_OR_AND)
ef017502
AD
154
155/* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
156 DST != (SRC1 & ~SRC2) | SRC3. */
157#define BITSET_ANDN_OR_(DST, SRC1, SRC2, SRC3)\
613f5e1a 158 BITSET_OP4_ (DST, SRC1, SRC2, SRC3, BITSET_OP_ANDN_OR)
ef017502 159
613f5e1a 160/* Find list of up to NUM bits set in BSET starting from and including
ef017502
AD
161 *NEXT. Return with actual number of bits found and with *NEXT
162 indicating where search stopped. */
163#define BITSET_LIST_(BSET, LIST, NUM, NEXT) \
613f5e1a 164(BSET)->b.ops->list (BSET, LIST, NUM, NEXT)
ef017502
AD
165
166/* Find reverse list of up to NUM bits set in BSET starting from and
167 including NEXT. Return with actual number of bits found and with
168 *NEXT indicating where search stopped. */
169#define BITSET_REVERSE_LIST_(BSET, LIST, NUM, NEXT) \
613f5e1a 170(BSET)->b.ops->reverse_list (BSET, LIST, NUM, NEXT)
ef017502
AD
171
172
173struct bbitset_struct
174{
175 struct bitset_ops_struct *ops;
176 bitset_windex cindex; /* Cache word index. */
177 bitset_windex csize; /* Cache size in words. */
178 bitset_word *cdata; /* Cache data pointer. */
345cea78
AD
179 /* Perhaps we could sacrifice another word to indicate
180 that the bitset is known to be zero, that a bit has been set
181 in the cache, and that a bit has been cleared in the cache.
182 This would speed up some of the searches but slightly slow down
183 bit set/reset operations of cached bits. */
ef017502
AD
184};
185
186
187typedef struct bitset_struct *bitset;
188
189
190/* The contents of this structure should be considered private. */
191struct bitset_ops_struct
192{
193 void (*set) PARAMS ((struct bitset_struct *, bitset_bindex));
194 void (*reset) PARAMS ((struct bitset_struct *, bitset_bindex));
195 int (*test) PARAMS ((struct bitset_struct *, bitset_bindex));
196 int (*size) PARAMS ((struct bitset_struct *));
197 int (*op1) PARAMS ((struct bitset_struct *, enum bitset_ops));
198 int (*op2) PARAMS ((struct bitset_struct *, struct bitset_struct *,
199 enum bitset_ops));
200 int (*op3) PARAMS ((struct bitset_struct *, struct bitset_struct *,
201 struct bitset_struct *, enum bitset_ops));
202 int (*op4) PARAMS ((struct bitset_struct *, struct bitset_struct *,
203 struct bitset_struct *, struct bitset_struct *,
204 enum bitset_ops));
205 int (*list) PARAMS ((struct bitset_struct *, bitset_bindex *,
206 bitset_bindex, bitset_bindex *));
207 int (*reverse_list) PARAMS ((struct bitset_struct *, bitset_bindex *,
208 bitset_bindex, bitset_bindex *));
209 void (*free) PARAMS ((struct bitset_struct *));
210 enum bitset_type type;
211};
212
213#define BITSET_COMPATIBLE_(BSET1, BSET2) ((BSET1)->b.ops == (BSET2)->b.ops)
214
ef017502
AD
215#define BITSET_CHECK2_(DST, SRC) \
216if (!BITSET_COMPATIBLE_ (DST, SRC)) abort ();
217
218#define BITSET_CHECK3_(DST, SRC1, SRC2) \
219if (!BITSET_COMPATIBLE_ (DST, SRC1) \
220 || !BITSET_COMPATIBLE_ (DST, SRC2)) abort ();
221
222#define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) \
223if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \
224 || !BITSET_COMPATIBLE_ (DST, SRC3)) abort ();
ef017502 225
ef017502 226
613f5e1a 227extern int bitset_op4 PARAMS ((bitset, bitset, bitset, bitset,
ef017502
AD
228 enum bitset_ops op));
229
230#endif /* _BBITSET_H */