From c863be8652e5664dc26a88f9f8365b24d0b1908b Mon Sep 17 00:00:00 2001 From: longqm Date: Fri, 1 Dec 2023 09:38:51 +0800 Subject: [PATCH] explicit cast uint32 to uint16; add more precise document on `rank_many(begin,end,ans)` --- include/roaring/containers/array.h | 6 +++--- include/roaring/roaring.h | 4 +++- src/containers/bitset.c | 6 +++--- src/containers/run.c | 4 ++-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/include/roaring/containers/array.h b/include/roaring/containers/array.h index 5603125e4..bf4e4e412 100644 --- a/include/roaring/containers/array.h +++ b/include/roaring/containers/array.h @@ -414,15 +414,15 @@ inline int array_container_rank(const array_container_t *arr, uint16_t x) { // bulk version of array_container_rank(); return number of consumed elements inline uint32_t array_container_rank_many(const array_container_t *arr, uint64_t start_rank, const uint32_t* begin, const uint32_t* end, uint64_t* ans){ - const uint16_t high = (*begin) >> 16; + const uint16_t high = (uint16_t)((*begin) >> 16); uint32_t pos = 0; const uint32_t* iter = begin; for(; iter != end; iter++) { uint32_t x = *iter; - uint16_t xhigh = x >> 16; + uint16_t xhigh = (uint16_t)(x >> 16); if(xhigh != high) return iter - begin;// stop at next container - const int32_t idx = binarySearch(arr->array+pos, arr->cardinality-pos, x&0xFFFF); + const int32_t idx = binarySearch(arr->array+pos, arr->cardinality-pos, (uint16_t)x); const bool is_present = idx >= 0; if (is_present) { *(ans++) = start_rank + pos + (idx + 1); diff --git a/include/roaring/roaring.h b/include/roaring/roaring.h index d82f03da8..d32a08b24 100644 --- a/include/roaring/roaring.h +++ b/include/roaring/roaring.h @@ -820,7 +820,9 @@ uint64_t roaring_bitmap_rank(const roaring_bitmap_t *r, uint32_t x); * it puts rank value of each element in `[begin .. end)` to `ans[]` * * the values in `[begin .. end)` must be sorted in Ascending order; - * the `ans` must have enough size. + * Caller is responsible to ensure that there is enough memory allocated, e.g. + * + * ans = malloc((end-begin) * sizeof(uint64_t)); */ void roaring_bitmap_rank_many(const roaring_bitmap_t *r, const uint32_t* begin, const uint32_t* end, uint64_t* ans); diff --git a/src/containers/bitset.c b/src/containers/bitset.c index fad229af1..5d38817c4 100644 --- a/src/containers/bitset.c +++ b/src/containers/bitset.c @@ -1233,16 +1233,16 @@ int bitset_container_rank(const bitset_container_t *container, uint16_t x) { } uint32_t bitset_container_rank_many(const bitset_container_t *container, uint64_t start_rank, const uint32_t* begin, const uint32_t* end, uint64_t* ans){ - const uint16_t high = (*begin) >> 16; + const uint16_t high = (uint16_t)((*begin) >> 16); int i = 0; int sum = 0; const uint32_t* iter = begin; for(; iter != end; iter++) { uint32_t x = *iter; - uint16_t xhigh = x >> 16; + uint16_t xhigh = (uint16_t)(x >> 16); if(xhigh != high) return iter - begin; // stop at next container - uint16_t xlow = x & 0xFFFF; + uint16_t xlow = (uint16_t)x; for(int count = xlow / 64; i < count; i++){ sum += roaring_hamming(container->words[i]); } diff --git a/src/containers/run.c b/src/containers/run.c index b9777cc2f..b3c90b06a 100644 --- a/src/containers/run.c +++ b/src/containers/run.c @@ -884,13 +884,13 @@ int run_container_rank(const run_container_t *container, uint16_t x) { return sum; } uint32_t run_container_rank_many(const run_container_t *container, uint64_t start_rank, const uint32_t* begin, const uint32_t* end, uint64_t* ans){ - const uint16_t high = (*begin) >> 16; + const uint16_t high = (uint16_t)((*begin) >> 16); const uint32_t* iter = begin; int sum = 0; int i = 0; for(;iter != end; iter++) { uint32_t x = *iter; - uint16_t xhigh = x >> 16; + uint16_t xhigh = (uint16_t)(x >> 16); if(xhigh != high) return iter - begin; // stop at next container uint32_t x32 = x & 0xFFFF;