diff --git a/include/villas/bitset.h b/include/villas/bitset.h index c92a95187..757774361 100644 --- a/include/villas/bitset.h +++ b/include/villas/bitset.h @@ -31,18 +31,35 @@ struct bitset { size_t dimension; }; +/** Allocate memory for a new betset */ int bitset_init(struct bitset *b, size_t dim); + +/** Release memory of bit set */ int bitset_destroy(struct bitset *b); +void bitset_set_value(struct bitset *b, uintmax_t val); +uintmax_t bitset_get_value(struct bitset *b); +/** Return the number of bits int the set which are set (1) */ +size_t bitset_count(struct bitset *b); + +/** Set a single bit in the set */ int bitset_set(struct bitset *b, size_t bit); + +/** Clear a single bit in the set */ int bitset_clear(struct bitset *b, size_t bit); +/** Set all bits in the set */ void bitset_set_all(struct bitset *b); + +/** Clear all bits in the set */ void bitset_clear_all(struct bitset *b); +/** Test if a single bit in the set is set */ int bitset_test(struct bitset *b, size_t bit); +/** Compare two bit sets bit-by-bit */ int bitset_cmp(struct bitset *a, struct bitset *b); +/** Return an human readable representation of the bit set */ char * bitset_dump(struct bitset *b); diff --git a/lib/bitset.c b/lib/bitset.c index a08e4b995..0963668c6 100644 --- a/lib/bitset.c +++ b/lib/bitset.c @@ -47,6 +47,36 @@ int bitset_destroy(struct bitset *b) return 0; } +void bitset_set_value(struct bitset *b, uintmax_t val) +{ + bitset_clear_all(b); + + for (size_t i = 0; i < b->dim; i++) { + if (val & (1 << i)) + bitset_set(b, i); + } +} + +uintmax_t bitset_get_value(struct bitset *b) +{ + uintmax_t v = 0; + + for (size_t i = 0; i < b->dim; i++) + v += bitset_test(b, i) << i; + + return v; +} + +size_t bitset_count(struct bitset *b) +{ + size_t cnt = 0; + + for (size_t i = 0; i < b->dim; i++) + cnt += bitset_test(b, i); + + return cnt; +} + int bitset_set(struct bitset *b, size_t bit) { int s = bitset_slot(bit);