33 #define INT_BIT (sizeof(int) * CHAR_BIT) 40 size =
MAX(0, (size + INT_BIT - 1) / INT_BIT);
42 unsigned int *bits = malloc(size *
sizeof(
unsigned int));
67 return set->size * INT_BIT;
74 assert(set->size >= 0);
76 size =
MAX(0, (size + INT_BIT - 1) / INT_BIT);
78 unsigned int *bits = realloc(set->bits, size *
sizeof(
unsigned int));
85 for (
int i = set->size; i < size; i++)
95 bitset_get_size(
const struct bitset *
set)
97 return set->size * INT_BIT;
105 return (set->bits[n / INT_BIT] >> (n & (INT_BIT - 1))) & 1u;
113 set->bits[n / INT_BIT] |= 1u << (n & (INT_BIT - 1));
119 for (
int i = 0; i <
set->size; i++)
128 set->bits[n / INT_BIT] &= ~(1u << (n & (INT_BIT - 1)));
134 for (
int i = 0; i <
set->size; i++)
141 for (
int i = 0; i <
set->size; i++)
142 set->bits[i] = ~set->bits[i];
148 const unsigned int *bits =
set->bits;
150 for (
int i = 0; i <
set->size; i++) {
151 unsigned int x = *bits++;
153 return offset + ffs(x);
162 const unsigned int *bits =
set->bits;
164 for (
int i = 0; i <
set->size; i++) {
165 unsigned int x = *bits++;
167 return offset + ffs(~x);
181 int size =
set->size - n / INT_BIT;
182 const unsigned int *bits =
set->bits + n / INT_BIT;
183 int offset = n & ~(INT_BIT - 1);
186 unsigned int x = *bits++;
193 return offset + ffs(x);
208 int size =
set->size - n / INT_BIT;
209 const unsigned int *bits =
set->bits + n / INT_BIT;
210 int offset = n & ~(INT_BIT - 1);
213 unsigned int x = *bits++;
216 x |= ~0u >> (INT_BIT - n);
220 return offset + ffs(~x);
This header file is part of the C11 and POSIX compatibility library; it includes <strings.h>, if it exists, and defines any missing functionality.
int bitset_size(const struct bitset *set)
Returns the size (in number of bits) of set.
void bitset_set_all(struct bitset *set)
Sets all bits in set.
int bitset_test(const struct bitset *set, int n)
Returns 1 if bit n in set is set, and 0 otherwise.
void set_errc(int errc)
Sets the current (thread-specific) native error code to errc.
int bitset_fns(const struct bitset *set, int n)
Returns the index (starting from one) of the first set bit higher or equal to n in set...
void bitset_clr_all(struct bitset *set)
Clears all bits in set.
This header file is part of the utilities library; it contains the native and platform-independent er...
void bitset_clr(struct bitset *set, int n)
Clears bit n in set.
This is the internal header file of the utilities library.
#define MAX(a, b)
Returns the maximum of a and b.
int bitset_resize(struct bitset *set, int size)
Resizes a bitset.
int errno2c(int errnum)
Transforms a standard C error number to a native error code.
void bitset_compl(struct bitset *set)
Flip all bits in set.
int bitset_ffs(const struct bitset *set)
Returns the index (starting from one) of the first set bit in set, or 0 if all bits are zero...
int bitset_fnz(const struct bitset *set, int n)
Returns the index (starting from one) of the first zero bit higher or equal to n in set...
This header file is part of the C11 and POSIX compatibility library; it includes <stdlib.h> and defines any missing functionality.
This header file is part of the utilities library; it contains the bitset declarations.
void bitset_set(struct bitset *set, int n)
Sets bit n in set.
int bitset_init(struct bitset *set, int size)
Initializes a bitset.
void bitset_fini(struct bitset *set)
Finalizes a bitset.
int bitset_ffz(const struct bitset *set)
Returns the index (starting from one) of the first zero bit in set, or 0 if all bits are set...