the hito embeddable programming language
at main 54 lines 1.5 kB view raw
1#ifndef BITSET_H 2#define BITSET_H 3 4#include <stddef.h> 5#include <stdint.h> 6#include <stdbool.h> 7 8typedef struct bitset bitset_t; 9 10bitset_t *bitset_alloc(void); 11 12void bitset_free(bitset_t *bs); 13 14/* Ensure it can represent numbers up to at least 'maxbit'. */ 15void bitset_reserve(bitset_t *bs, size_t maxbit); 16 17/* Set the nth bit. */ 18void bitset_set(bitset_t *bs, size_t bit); 19 20/* Clear the nth bit. */ 21void bitset_clear(bitset_t *bs, size_t bit); 22 23/* Test a bit (returns false if out of range). */ 24bool bitset_get(const bitset_t *bs, size_t bit); 25 26/* Count bits set to 1. */ 27size_t bitset_count(const bitset_t *bs); 28 29// TODO check if this is needed 30/* bs1 |= bs2 with first 'skip' bits ignored */ 31void bitset_union_with_offset(bitset_t *bs1, const bitset_t *bs2, size_t skip); 32 33/* Shift all bits up by `n` positions (insert n zeros at LSB) */ 34void bitset_shift_up(bitset_t *bs, size_t n); 35 36/* Shift all bits down by `n` positions (discard LSB) */ 37void bitset_shift_down(bitset_t *bs, size_t n); 38 39/* Return highest set bit+1, or 0 if empty. */ 40size_t bitset_max(const bitset_t *bs); 41 42/* Clear all bits. */ 43void bitset_reset(bitset_t *bs); 44 45/* bs1 |= bs2 */ 46void bitset_union(bitset_t *bs1, const bitset_t *bs2); 47 48/* Iterate through set bits: returns next bit >= *start, 49 or (size_t)-1 if none. Caller sets *start = 0 to begin. */ 50size_t bitset_next(const bitset_t *bs, size_t *start); 51 52/* dump the contents of a bitset to the console */ 53void bitset_dump(const bitset_t *it); 54#endif