Skip to content

Commit

Permalink
Simplify, and optimize roaring64_bitmap_remove_range_closed
Browse files Browse the repository at this point in the history
Only visit existing nodes, rather than every possible container in the range

This makes it feasible to use
`roaring64_bitmap_remove_range_closed(r, 0, UINT64_MAX)`. Before, this would
have taken forever, but now, it takes proportional time to the number of
containers to remove
  • Loading branch information
Dr-Emann committed Feb 17, 2024
1 parent 877a672 commit 2cc7239
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/roaring64.c
Original file line number Diff line number Diff line change
Expand Up @@ -696,17 +696,12 @@ void roaring64_bitmap_remove_range_closed(roaring64_bitmap_t *r, uint64_t min,
// Remove a range across containers. Remove intermediate containers
// entirely.
remove_range_closed_at(art, min_high48, min_low16, 0xffff);
uint64_t min_high_bits = min >> 16;
uint64_t max_high_bits = max >> 16;
for (uint64_t current = min_high_bits + 1; current < max_high_bits;
++current) {
uint8_t current_high48[ART_KEY_BYTES];
split_key(current << 16, current_high48);
leaf_t *leaf = (leaf_t *)art_erase(art, current_high48);
if (leaf != NULL) {
container_free(leaf->container, leaf->typecode);
free_leaf(leaf);
}

art_iterator_t it = art_upper_bound(art, min_high48);
while (it.value != NULL && art_compare_keys(it.key, max_high48) < 0) {
leaf_t *leaf = (leaf_t *)art_iterator_erase(art, &it);
container_free(leaf->container, leaf->typecode);
free_leaf(leaf);
}
remove_range_closed_at(art, max_high48, 0, max_low16);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/roaring64_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,12 @@ DEFINE_TEST(test_remove_range_closed) {
assert_int_equal(roaring64_bitmap_maximum(r), UINT64_MAX - 6);
roaring64_bitmap_free(r);
}
{
// Remove a huge range
roaring64_bitmap_t* r = roaring64_bitmap_from(1, UINT64_MAX - 1);
roaring64_bitmap_remove_range_closed(r, 0, UINT64_MAX);
roaring64_bitmap_free(r);
}
}

DEFINE_TEST(test_get_cardinality) {
Expand Down

0 comments on commit 2cc7239

Please sign in to comment.