Skip to content

Commit

Permalink
Further optimization of C++ API consistency
Browse files Browse the repository at this point in the history
Main changes:

- Add the `api::roaring_bitmap_range_cardinality_closed` method.
- Add boundary checks for methods: `api::roaring_bitmap_add_range` `api::roaring_bitmap_remove_range`.
  • Loading branch information
cubicYYY committed Aug 20, 2024
1 parent a7751a5 commit b910ab4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion benchmarks/containsmulti_benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void contains_multi_via_contains(roaring_bitmap_t* bm, const uint32_t* values,

void contains_multi_bulk(roaring_bitmap_t* bm, const uint32_t* values,
bool* results, const size_t count) {
roaring_bulk_context_t context = {0, 0};
roaring_bulk_context_t context = {0, 0, 0, 0};
for (size_t i = 0; i < count; ++i) {
results[i] = roaring_bitmap_contains_bulk(bm, &context, values[i]);
}
Expand Down
2 changes: 0 additions & 2 deletions cpp/roaring64map.hh
Original file line number Diff line number Diff line change
Expand Up @@ -840,8 +840,6 @@ class Roaring64Map {
});
}

// TODO: implement `rangeUint64Array`

/**
* Return true if the two bitmaps contain the same elements.
*/
Expand Down
10 changes: 8 additions & 2 deletions include/roaring/roaring.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ void roaring_bitmap_add_range_closed(roaring_bitmap_t *r, uint32_t min,
*/
inline void roaring_bitmap_add_range(roaring_bitmap_t *r, uint64_t min,
uint64_t max) {
if (max <= min) return;
if (max <= min || min > (uint64_t)UINT32_MAX + 1) return;
roaring_bitmap_add_range_closed(r, (uint32_t)min, (uint32_t)(max - 1));
}

Expand All @@ -407,7 +407,7 @@ void roaring_bitmap_remove_range_closed(roaring_bitmap_t *r, uint32_t min,
*/
inline void roaring_bitmap_remove_range(roaring_bitmap_t *r, uint64_t min,
uint64_t max) {
if (max <= min) return;
if (max <= min || min > (uint64_t)UINT32_MAX + 1) return;
roaring_bitmap_remove_range_closed(r, (uint32_t)min, (uint32_t)(max - 1));
}

Expand Down Expand Up @@ -474,6 +474,12 @@ uint64_t roaring_bitmap_range_cardinality(const roaring_bitmap_t *r,
uint64_t range_start,
uint64_t range_end);

/**
* Returns the number of elements in the range [range_start, range_end].
*/
uint64_t roaring_bitmap_range_cardinality_closed(const roaring_bitmap_t *r,
uint32_t range_start,
uint32_t range_end);
/**
* Returns true if the bitmap is empty (cardinality is zero).
*/
Expand Down
23 changes: 15 additions & 8 deletions src/roaring.c
Original file line number Diff line number Diff line change
Expand Up @@ -1331,15 +1331,22 @@ uint64_t roaring_bitmap_get_cardinality(const roaring_bitmap_t *r) {
uint64_t roaring_bitmap_range_cardinality(const roaring_bitmap_t *r,
uint64_t range_start,
uint64_t range_end) {
if (range_start >= range_end || range_start > (uint64_t)UINT32_MAX + 1) {
return 0;
}
return roaring_bitmap_range_cardinality_closed(r, (uint32_t)range_start,
(uint32_t)(range_end - 1));
}

uint64_t roaring_bitmap_range_cardinality_closed(const roaring_bitmap_t *r,
uint32_t range_start,
uint32_t range_end) {
const roaring_array_t *ra = &r->high_low_container;

if (range_end > UINT32_MAX) {
range_end = UINT32_MAX + UINT64_C(1);
}
if (range_start >= range_end) {
if (range_start > range_end) {
return 0;
}
range_end--; // make range_end inclusive

// now we have: 0 <= range_start <= range_end <= UINT32_MAX

uint16_t minhb = (uint16_t)(range_start >> 16);
Expand Down Expand Up @@ -2006,7 +2013,7 @@ static void inplace_fully_flip_container(roaring_array_t *x1_arr, uint16_t hb) {
roaring_bitmap_t *roaring_bitmap_flip(const roaring_bitmap_t *x1,
uint64_t range_start,
uint64_t range_end) {
if (range_start >= range_end || range_start > (uint64_t)UINT_MAX + 1)
if (range_start >= range_end || range_start > (uint64_t)UINT32_MAX + 1)
return roaring_bitmap_copy(x1);
return roaring_bitmap_flip_closed(x1, (uint32_t)range_start,
(uint32_t)(range_end - 1));
Expand Down Expand Up @@ -2066,7 +2073,7 @@ roaring_bitmap_t *roaring_bitmap_flip_closed(const roaring_bitmap_t *x1,

void roaring_bitmap_flip_inplace(roaring_bitmap_t *x1, uint64_t range_start,
uint64_t range_end) {
if (range_start >= range_end || range_start > (uint64_t)UINT_MAX + 1)
if (range_start >= range_end || range_start > (uint64_t)UINT32_MAX + 1)
return;
roaring_bitmap_flip_inplace_closed(x1, (uint32_t)range_start,
(uint32_t)(range_end - 1));
Expand Down Expand Up @@ -2840,7 +2847,7 @@ bool roaring_bitmap_contains(const roaring_bitmap_t *r, uint32_t val) {
*/
bool roaring_bitmap_contains_range(const roaring_bitmap_t *r,
uint64_t range_start, uint64_t range_end) {
if (range_start >= range_end || range_start > (uint64_t)UINT_MAX + 1)
if (range_start >= range_end || range_start > (uint64_t)UINT32_MAX + 1)
return true;
return roaring_bitmap_contains_range_closed(r, (uint32_t)range_start,
(uint32_t)(range_end - 1));
Expand Down

0 comments on commit b910ab4

Please sign in to comment.