Skip to content

Commit

Permalink
fix issue 666
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Sep 30, 2024
1 parent 5f15802 commit ea014f0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
5 changes: 4 additions & 1 deletion include/roaring/bitset/bitset.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
12 changes: 11 additions & 1 deletion src/bitset.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,24 @@ 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];
if (w != 0) {
return roaring_trailing_zeroes(w) + k * 64;
}
}
return 0;
return SIZE_MAX;
}

bool bitset_grow(bitset_t *bitset, size_t newarraysize) {
Expand Down
1 change: 1 addition & 0 deletions tests/cbitset_unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ea014f0

Please sign in to comment.