1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

bitset: add some documentation and more features

This commit is contained in:
Steffen Vogel 2017-12-14 14:04:16 +01:00
parent d7cb5ee9d4
commit e4d45a61d1
2 changed files with 47 additions and 0 deletions

View file

@ -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);

View file

@ -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);