#ifndef BITSET_H #define BITSET_H #include #include #include typedef struct bitset bitset_t; bitset_t *bitset_alloc(void); void bitset_free(bitset_t *bs); /* Ensure it can represent numbers up to at least 'maxbit'. */ void bitset_reserve(bitset_t *bs, size_t maxbit); /* Set the nth bit. */ void bitset_set(bitset_t *bs, size_t bit); /* Clear the nth bit. */ void bitset_clear(bitset_t *bs, size_t bit); /* Test a bit (returns false if out of range). */ bool bitset_get(const bitset_t *bs, size_t bit); /* Count bits set to 1. */ size_t bitset_count(const bitset_t *bs); // TODO check if this is needed /* bs1 |= bs2 with first 'skip' bits ignored */ void bitset_union_with_offset(bitset_t *bs1, const bitset_t *bs2, size_t skip); /* Shift all bits up by `n` positions (insert n zeros at LSB) */ void bitset_shift_up(bitset_t *bs, size_t n); /* Shift all bits down by `n` positions (discard LSB) */ void bitset_shift_down(bitset_t *bs, size_t n); /* Return highest set bit+1, or 0 if empty. */ size_t bitset_max(const bitset_t *bs); /* Clear all bits. */ void bitset_reset(bitset_t *bs); /* bs1 |= bs2 */ void bitset_union(bitset_t *bs1, const bitset_t *bs2); /* Iterate through set bits: returns next bit >= *start, or (size_t)-1 if none. Caller sets *start = 0 to begin. */ size_t bitset_next(const bitset_t *bs, size_t *start); /* dump the contents of a bitset to the console */ void bitset_dump(const bitset_t *it); #endif