@@ -248,47 +248,49 @@ bool bitset_container_intersect(const bitset_container_t *src_1,
248248CROARING_ALLOW_UNALIGNED
249249bool bitset_container_iterator_read_into_bool (const bitset_container_t * bc ,
250250 roaring_container_iterator_t * it ,
251- bool * buf ,
252- const uint16_t * max_value ,
251+ bool * buf , uint32_t max_value ,
253252 uint16_t * value_out ) {
254- uint32_t max_wordindex = BITSET_CONTAINER_SIZE_IN_WORDS ;
255- // If max_value is not NULL, get the wordindex of the max_value.
256- if (max_value != NULL ) {
257- max_wordindex = * max_value / 64 ;
258- assert (max_wordindex < BITSET_CONTAINER_SIZE_IN_WORDS );
253+ uint32_t max_wordindex = max_value / 64 ;
254+ if (max_wordindex >= BITSET_CONTAINER_SIZE_IN_WORDS ) {
255+ max_wordindex = BITSET_CONTAINER_SIZE_IN_WORDS - 1 ;
259256 }
260257 uint32_t wordindex = it -> index / 64 ;
261258 uint64_t word = bc -> words [wordindex ] & (UINT64_MAX << (it -> index % 64 ));
262259 uint16_t initial_value = it -> index ;
263- if (max_wordindex > 0 ) {
264- while (wordindex < max_wordindex ) {
265- // TODO: SIMD optimization
266- while (word != 0 ) {
267- * value_out = wordindex * 64 + roaring_trailing_zeroes (word );
268- buf [* value_out - initial_value ] = true;
269- word = word & (word - 1 );
270- }
271- wordindex ++ ;
272- if (wordindex < BITSET_CONTAINER_SIZE_IN_WORDS ) {
273- word = bc -> words [wordindex ];
274- }
260+ // Remain the last word to process out of loop for reducing `if` branches
261+ while (wordindex < max_wordindex ) {
262+ // TODO: SIMD optimization
263+ while (word != 0 ) {
264+ it -> index = wordindex * 64 + roaring_trailing_zeroes (word );
265+ buf [it -> index - initial_value ] = true;
266+ word = word & (word - 1 );
267+ }
268+ wordindex ++ ;
269+ if (wordindex < BITSET_CONTAINER_SIZE_IN_WORDS ) {
270+ word = bc -> words [wordindex ];
275271 }
276272 }
277- // All the words are processed.
278- if (max_value == NULL ) return false;
279273 // Process the last word (which is at max_wordindex)
280274 while (word != 0 ) {
281- * value_out = wordindex * 64 + roaring_trailing_zeroes (word );
282- if (* value_out >= * max_value ) {
283- it -> index = * value_out ;
275+ it -> index = wordindex * 64 + roaring_trailing_zeroes (word );
276+ if (( uint32_t ) it -> index >= max_value ) {
277+ * value_out = it -> index ;
284278 return true;
285279 }
286- buf [* value_out - initial_value ] = true;
280+ buf [it -> index - initial_value ] = true;
287281 word = word & (word - 1 );
288282 }
289- // If max_value is not NULL, its wordindex must be less than
290- // BITSET_CONTAINER_SIZE_IN_WORDS. So if reach this line, the bitset must be
291- // drained.
283+
284+ /// If the bitset is not drained, iterate to the next set bit.
285+ while (word == 0 && (wordindex + 1 < BITSET_CONTAINER_SIZE_IN_WORDS )) {
286+ wordindex ++ ;
287+ word = bc -> words [wordindex ];
288+ }
289+ if (word != 0 ) {
290+ it -> index = wordindex * 64 + roaring_trailing_zeroes (word );
291+ * value_out = it -> index ;
292+ return true;
293+ }
292294 return false;
293295}
294296
0 commit comments