sys_stat: Fix 'implicit declaration of function' warning on OS/2 kLIBC.
[gnulib.git] / lib / bitset / base.h
blob4fcafac8b67d18b8a9d6c885eed4ec27b5e17a5d
1 /* Base bitset stuff.
3 Copyright (C) 2002-2004, 2006, 2009-2015, 2018-2019 Free Software
4 Foundation, Inc.
6 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #ifndef _BITSET_BASE_H
22 #define _BITSET_BASE_H
24 #include <limits.h>
25 #include <stdbool.h>
26 #include <stddef.h>
28 #include "xalloc.h"
30 #ifndef __attribute__
31 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
32 # define __attribute__(x)
33 # endif
34 #endif
36 #define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
38 /* Currently we support five flavours of bitsets:
39 BITSET_ARRAY: Array of bits (fixed size, fast for dense bitsets).
40 Memory for bit array and bitset structure allocated
41 contiguously.
42 BITSET_LIST: Linked list of arrays of bits (variable size, least storage
43 for large very sparse sets).
44 BITSET_TABLE: Expandable table of pointers to arrays of bits
45 (variable size, less storage for large sparse sets).
46 Faster than BITSET_LIST for random access.
47 BITSET_VECTOR: Variable array of bits (variable size, fast for
48 dense bitsets).
49 BITSET_STATS: Wrapper bitset for internal use only. Used for gathering
50 statistics and/or better run-time checking.
52 enum bitset_type {BITSET_ARRAY, BITSET_LIST, BITSET_TABLE, BITSET_VECTOR,
53 BITSET_TYPE_NUM, BITSET_STATS};
54 #define BITSET_TYPE_NAMES {"abitset", "lbitset", "tbitset", "vbitset"}
56 extern const char * const bitset_type_names[];
58 enum bitset_alloc_type {BITSET_MALLOC, BITSET_OBALLOC};
60 /* Data type used to store a word of bits. */
61 typedef unsigned long bitset_word;
62 #define BITSET_WORD_BITS ((unsigned) (CHAR_BIT * sizeof (bitset_word)))
64 /* Bit index. In theory we might need a type wider than size_t, but
65 in practice we lose at most a factor of CHAR_BIT by going with
66 size_t, and that is good enough. If this type is changed to be
67 wider than size_t, the code needs to be modified to check for
68 overflow when converting bit counts to byte or word counts.
69 The bit and word index types must be unsigned. */
70 typedef size_t bitset_bindex;
72 /* Word index. */
73 typedef size_t bitset_windex;
75 /* Maximum values for commonly-used unsigned types. BITSET_SIZE_MAX
76 always equals SIZE_MAX, but some older systems lack SIZE_MAX. */
77 #define BITSET_BINDEX_MAX ((bitset_bindex) -1)
79 /* Limit max word index to the maximum value of a signed integer
80 to simplify cache disabling. */
81 #define BITSET_WINDEX_MAX (((bitset_windex) -1) >> 1)
82 #define BITSET_SIZE_MAX ((size_t) -1)
84 #define BITSET_MSB ((bitset_word) 1 << (BITSET_WORD_BITS - 1))
86 #define BITSET_LIST_SIZE 1024
88 enum bitset_ops {BITSET_OP_ZERO, BITSET_OP_ONES,
89 BITSET_OP_COPY, BITSET_OP_NOT,
90 BITSET_OP_EMPTY_P, BITSET_OP_EQUAL_P,
91 BITSET_OP_SUBSET_P, BITSET_OP_DISJOINT_P,
92 BITSET_OP_AND, BITSET_OP_OR, BITSET_OP_XOR, BITSET_OP_ANDN,
93 BITSET_OP_OR_AND, BITSET_OP_AND_OR, BITSET_OP_ANDN_OR};
95 struct bbitset_struct
97 const struct bitset_vtable *vtable;
98 bitset_windex cindex; /* Cache word index. */
99 bitset_windex csize; /* Cache size in words. */
100 bitset_word *cdata; /* Cache data pointer. */
101 bitset_bindex n_bits; /* Number of bits. */
102 /* Perhaps we could sacrifice another word to indicate
103 that the bitset is known to be zero, that a bit has been set
104 in the cache, and that a bit has been cleared in the cache.
105 This would speed up some of the searches but slightly slow down
106 bit set/reset operations of cached bits. */
110 typedef union bitset_union *bitset;
113 /* Private accessor macros to bitset structure. */
114 #define BITSET_VTABLE_(SRC) (SRC)->b.vtable
115 #define BITSET_CINDEX_(SRC) (SRC)->b.cindex
116 #define BITSET_CDATA_(SRC) (SRC)->b.cdata
117 #define BITSET_CSIZE_(SRC) (SRC)->b.csize
118 #define BITSET_NBITS_(SRC) (SRC)->b.n_bits
121 /* The contents of this structure should be considered private. */
122 struct bitset_vtable
124 void (*set) (bitset, bitset_bindex);
125 void (*reset) (bitset, bitset_bindex);
126 bool (*toggle) (bitset, bitset_bindex);
127 bool (*test) (bitset, bitset_bindex);
128 bitset_bindex (*resize) (bitset, bitset_bindex);
129 bitset_bindex (*size) (bitset);
130 bitset_bindex (*count) (bitset);
132 bool (*empty_p) (bitset);
133 void (*ones) (bitset);
134 void (*zero) (bitset);
136 void (*copy) (bitset, bitset);
137 bool (*disjoint_p) (bitset, bitset);
138 bool (*equal_p) (bitset, bitset);
139 void (*not_) (bitset, bitset);
140 bool (*subset_p) (bitset, bitset);
142 void (*and_) (bitset, bitset, bitset);
143 bool (*and_cmp) (bitset, bitset, bitset);
144 void (*andn) (bitset, bitset, bitset);
145 bool (*andn_cmp) (bitset, bitset, bitset);
146 void (*or_) (bitset, bitset, bitset);
147 bool (*or_cmp) (bitset, bitset, bitset);
148 void (*xor_) (bitset, bitset, bitset);
149 bool (*xor_cmp) (bitset, bitset, bitset);
151 void (*and_or) (bitset, bitset, bitset, bitset);
152 bool (*and_or_cmp) (bitset, bitset, bitset, bitset);
153 void (*andn_or) (bitset, bitset, bitset, bitset);
154 bool (*andn_or_cmp) (bitset, bitset, bitset, bitset);
155 void (*or_and) (bitset, bitset, bitset, bitset);
156 bool (*or_and_cmp) (bitset, bitset, bitset, bitset);
158 bitset_bindex (*list) (bitset, bitset_bindex *, bitset_bindex,
159 bitset_bindex *);
160 bitset_bindex (*list_reverse) (bitset, bitset_bindex *, bitset_bindex,
161 bitset_bindex *);
162 void (*free) (bitset);
163 enum bitset_type type;
166 #define BITSET_COMPATIBLE_(BSET1, BSET2) \
167 ((BSET1)->b.vtable == (BSET2)->b.vtable)
169 #define BITSET_CHECK2_(DST, SRC) \
170 if (!BITSET_COMPATIBLE_ (DST, SRC)) abort ();
172 #define BITSET_CHECK3_(DST, SRC1, SRC2) \
173 if (!BITSET_COMPATIBLE_ (DST, SRC1) \
174 || !BITSET_COMPATIBLE_ (DST, SRC2)) abort ();
176 #define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) \
177 if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \
178 || !BITSET_COMPATIBLE_ (DST, SRC3)) abort ();
181 /* Redefine number of bits in bitset DST. */
182 #define BITSET_RESIZE_(DST, SIZE) (DST)->b.vtable->resize (DST, SIZE)
184 /* Return size in bits of bitset SRC. */
185 #define BITSET_SIZE_(SRC) (SRC)->b.vtable->size (SRC)
187 /* Return number of bits set in bitset SRC. */
188 #define BITSET_COUNT_(SRC) (SRC)->b.vtable->count (SRC)
190 /* Return type of bitset SRC. */
191 #define BITSET_TYPE_(DST) (DST)->b.vtable->type
193 /* Set bit BITNO in bitset DST. */
194 #define BITSET_SET_(DST, BITNO) (DST)->b.vtable->set (DST, BITNO)
196 /* Reset bit BITNO in bitset DST. */
197 #define BITSET_RESET_(DST, BITNO) (DST)->b.vtable->reset (DST, BITNO)
199 /* Toggle bit BITNO in bitset DST. */
200 #define BITSET_TOGGLE_(DST, BITNO) (DST)->b.vtable->toggle (DST, BITNO)
202 /* Return non-zero if bit BITNO in bitset SRC is set. */
203 #define BITSET_TEST_(SRC, BITNO) (SRC)->b.vtable->test (SRC, BITNO)
205 /* Free bitset SRC. */
206 #define BITSET_FREE_(SRC)\
207 ((SRC)->b.vtable->free ? (SRC)->b.vtable->free (SRC) :(void)0)
210 /* Return SRC == 0. */
211 #define BITSET_EMPTY_P_(SRC) (SRC)->b.vtable->empty_p (SRC)
213 /* DST = ~0. */
214 #define BITSET_ONES_(DST) (DST)->b.vtable->ones (DST)
216 /* DST = 0. */
217 #define BITSET_ZERO_(DST) (DST)->b.vtable->zero (DST)
221 /* DST = SRC. */
222 #define BITSET_COPY_(DST, SRC) (SRC)->b.vtable->copy (DST, SRC)
224 /* Return DST & SRC == 0. */
225 #define BITSET_DISJOINT_P_(DST, SRC) (SRC)->b.vtable->disjoint_p (DST, SRC)
227 /* Return DST == SRC. */
228 #define BITSET_EQUAL_P_(DST, SRC) (SRC)->b.vtable->equal_p (DST, SRC)
230 /* DST = ~SRC. */
231 #define BITSET_NOT_(DST, SRC) (SRC)->b.vtable->not_ (DST, SRC)
233 /* Return DST == DST | SRC. */
234 #define BITSET_SUBSET_P_(DST, SRC) (SRC)->b.vtable->subset_p (DST, SRC)
237 /* DST = SRC1 & SRC2. */
238 #define BITSET_AND_(DST, SRC1, SRC2) (SRC1)->b.vtable->and_ (DST, SRC1, SRC2)
239 #define BITSET_AND_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->and_cmp (DST, SRC1, SRC2)
241 /* DST = SRC1 & ~SRC2. */
242 #define BITSET_ANDN_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn (DST, SRC1, SRC2)
243 #define BITSET_ANDN_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn_cmp (DST, SRC1, SRC2)
245 /* DST = SRC1 | SRC2. */
246 #define BITSET_OR_(DST, SRC1, SRC2) (SRC1)->b.vtable->or_ (DST, SRC1, SRC2)
247 #define BITSET_OR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->or_cmp (DST, SRC1, SRC2)
249 /* DST = SRC1 ^ SRC2. */
250 #define BITSET_XOR_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor_ (DST, SRC1, SRC2)
251 #define BITSET_XOR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor_cmp (DST, SRC1, SRC2)
255 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
256 DST != (SRC1 & SRC2) | SRC3. */
257 #define BITSET_AND_OR_(DST, SRC1, SRC2, SRC3) \
258 (SRC1)->b.vtable->and_or (DST, SRC1, SRC2, SRC3)
259 #define BITSET_AND_OR_CMP_(DST, SRC1, SRC2, SRC3) \
260 (SRC1)->b.vtable->and_or_cmp (DST, SRC1, SRC2, SRC3)
262 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
263 DST != (SRC1 & ~SRC2) | SRC3. */
264 #define BITSET_ANDN_OR_(DST, SRC1, SRC2, SRC3) \
265 (SRC1)->b.vtable->andn_or (DST, SRC1, SRC2, SRC3)
266 #define BITSET_ANDN_OR_CMP_(DST, SRC1, SRC2, SRC3) \
267 (SRC1)->b.vtable->andn_or_cmp (DST, SRC1, SRC2, SRC3)
269 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
270 DST != (SRC1 | SRC2) & SRC3. */
271 #define BITSET_OR_AND_(DST, SRC1, SRC2, SRC3) \
272 (SRC1)->b.vtable->or_and (DST, SRC1, SRC2, SRC3)
273 #define BITSET_OR_AND_CMP_(DST, SRC1, SRC2, SRC3) \
274 (SRC1)->b.vtable->or_and_cmp (DST, SRC1, SRC2, SRC3)
277 /* Find list of up to NUM bits set in BSET starting from and including
278 *NEXT. Return with actual number of bits found and with *NEXT
279 indicating where search stopped. */
280 #define BITSET_LIST_(BSET, LIST, NUM, NEXT) \
281 (BSET)->b.vtable->list (BSET, LIST, NUM, NEXT)
283 /* Find reverse list of up to NUM bits set in BSET starting from and
284 including NEXT. Return with actual number of bits found and with
285 *NEXT indicating where search stopped. */
286 #define BITSET_LIST_REVERSE_(BSET, LIST, NUM, NEXT) \
287 (BSET)->b.vtable->list_reverse (BSET, LIST, NUM, NEXT)
290 /* Private functions for bitset implementations. */
292 bool bitset_toggle_ (bitset, bitset_bindex);
294 bitset_bindex bitset_count_ (bitset);
296 bitset_bindex bitset_size_ (bitset);
298 bool bitset_copy_ (bitset, bitset);
300 void bitset_and_or_ (bitset, bitset, bitset, bitset);
302 bool bitset_and_or_cmp_ (bitset, bitset, bitset, bitset);
304 void bitset_andn_or_ (bitset, bitset, bitset, bitset);
306 bool bitset_andn_or_cmp_ (bitset, bitset, bitset, bitset);
308 void bitset_or_and_ (bitset, bitset, bitset, bitset);
310 bool bitset_or_and_cmp_ (bitset, bitset, bitset, bitset);
312 #endif /* _BBITSET_H */