diff --git a/include/roaring/bitset/bitset.h b/include/roaring/bitset/bitset.h index c57d73d10..38ed39e80 100644 --- a/include/roaring/bitset/bitset.h +++ b/include/roaring/bitset/bitset.h @@ -131,7 +131,10 @@ inline bool bitset_get(const bitset_t *bitset, size_t i) { /* Count number of bits set. */ size_t bitset_count(const bitset_t *bitset); -/* Find the index of the first bit set. Or zero if the bitset is empty. */ +/* Returns true if no bit is set. */ +bool bitset_empty(const bitset_t *bitset); + +/* Find the index of the first bit set. Or SIZE_MAX if the bitset is empty. */ size_t bitset_minimum(const bitset_t *bitset); /* Find the index of the last bit set. Or zero if the bitset is empty. */ diff --git a/src/bitset.c b/src/bitset.c index 5d23af1e7..16b6fc5f4 100644 --- a/src/bitset.c +++ b/src/bitset.c @@ -216,6 +216,16 @@ bool bitset_inplace_union(bitset_t *CROARING_CBITSET_RESTRICT b1, return true; } +bool bitset_empty(const bitset_t *bitset) { + for (size_t k = 0; k < bitset->arraysize; k++) { + if (bitset->array[k] != 0) { + return false; + } + } + return true; +} + + size_t bitset_minimum(const bitset_t *bitset) { for (size_t k = 0; k < bitset->arraysize; k++) { uint64_t w = bitset->array[k]; @@ -223,7 +233,7 @@ size_t bitset_minimum(const bitset_t *bitset) { return roaring_trailing_zeroes(w) + k * 64; } } - return 0; + return SIZE_MAX; } bool bitset_grow(bitset_t *bitset, size_t newarraysize) { diff --git a/tests/cbitset_unit.c b/tests/cbitset_unit.c index 271a712b7..bf1aa1d51 100644 --- a/tests/cbitset_unit.c +++ b/tests/cbitset_unit.c @@ -60,6 +60,7 @@ void test_construct() { void test_max_min() { bitset_t *b = bitset_create(); + assert_true(bitset_empty(b)); for (size_t k = 100; k < 1000; ++k) { bitset_set(b, 3 * k); assert_true(bitset_minimum(b) == 3 * 100);