@@ -2434,15 +2434,131 @@ roaring_container_iterator_t container_init_iterator_last(const container_t *c,
24342434 * Moves the iterator to the next entry. Returns true and sets `value` if a
24352435 * value is present.
24362436 */
2437- bool container_iterator_next (const container_t *c, uint8_t typecode,
2438- roaring_container_iterator_t *it, uint16_t *value);
2437+ inline bool container_iterator_next (const container_t *c, uint8_t typecode,
2438+ roaring_container_iterator_t *it,
2439+ uint16_t *value) {
2440+ switch (typecode) {
2441+ case BITSET_CONTAINER_TYPE: {
2442+ const bitset_container_t *bc = const_CAST_bitset (c);
2443+ it->index ++;
2444+
2445+ uint32_t wordindex = it->index / 64 ;
2446+ if (wordindex >= BITSET_CONTAINER_SIZE_IN_WORDS) {
2447+ return false ;
2448+ }
2449+
2450+ uint64_t word =
2451+ bc->words [wordindex] & (UINT64_MAX << (it->index % 64 ));
2452+ // next part could be optimized/simplified
2453+ while (word == 0 &&
2454+ (wordindex + 1 < BITSET_CONTAINER_SIZE_IN_WORDS)) {
2455+ wordindex++;
2456+ word = bc->words [wordindex];
2457+ }
2458+ if (word != 0 ) {
2459+ it->index = wordindex * 64 + roaring_trailing_zeroes (word);
2460+ *value = it->index ;
2461+ return true ;
2462+ }
2463+ return false ;
2464+ }
2465+ case ARRAY_CONTAINER_TYPE: {
2466+ const array_container_t *ac = const_CAST_array (c);
2467+ it->index ++;
2468+ if (it->index < ac->cardinality ) {
2469+ *value = ac->array [it->index ];
2470+ return true ;
2471+ }
2472+ return false ;
2473+ }
2474+ case RUN_CONTAINER_TYPE: {
2475+ if (*value == UINT16_MAX) { // Avoid overflow to zero
2476+ return false ;
2477+ }
2478+
2479+ const run_container_t *rc = const_CAST_run (c);
2480+ uint32_t limit =
2481+ rc->runs [it->index ].value + rc->runs [it->index ].length ;
2482+ if (*value < limit) {
2483+ (*value)++;
2484+ return true ;
2485+ }
2486+
2487+ it->index ++;
2488+ if (it->index < rc->n_runs ) {
2489+ *value = rc->runs [it->index ].value ;
2490+ return true ;
2491+ }
2492+ return false ;
2493+ }
2494+ default :
2495+ assert (false );
2496+ roaring_unreachable;
2497+ return false ;
2498+ }
2499+ }
24392500
24402501/* *
24412502 * Moves the iterator to the previous entry. Returns true and sets `value` if a
24422503 * value is present.
24432504 */
2444- bool container_iterator_prev (const container_t *c, uint8_t typecode,
2445- roaring_container_iterator_t *it, uint16_t *value);
2505+ inline bool container_iterator_prev (const container_t *c, uint8_t typecode,
2506+ roaring_container_iterator_t *it,
2507+ uint16_t *value) {
2508+ switch (typecode) {
2509+ case BITSET_CONTAINER_TYPE: {
2510+ if (--it->index < 0 ) {
2511+ return false ;
2512+ }
2513+
2514+ const bitset_container_t *bc = const_CAST_bitset (c);
2515+ int32_t wordindex = it->index / 64 ;
2516+ uint64_t word =
2517+ bc->words [wordindex] & (UINT64_MAX >> (63 - (it->index % 64 )));
2518+
2519+ while (word == 0 && --wordindex >= 0 ) {
2520+ word = bc->words [wordindex];
2521+ }
2522+ if (word == 0 ) {
2523+ return false ;
2524+ }
2525+
2526+ it->index = (wordindex * 64 ) + (63 - roaring_leading_zeroes (word));
2527+ *value = it->index ;
2528+ return true ;
2529+ }
2530+ case ARRAY_CONTAINER_TYPE: {
2531+ if (--it->index < 0 ) {
2532+ return false ;
2533+ }
2534+ const array_container_t *ac = const_CAST_array (c);
2535+ *value = ac->array [it->index ];
2536+ return true ;
2537+ }
2538+ case RUN_CONTAINER_TYPE: {
2539+ if (*value == 0 ) {
2540+ return false ;
2541+ }
2542+
2543+ const run_container_t *rc = const_CAST_run (c);
2544+ (*value)--;
2545+ if (*value >= rc->runs [it->index ].value ) {
2546+ return true ;
2547+ }
2548+
2549+ if (--it->index < 0 ) {
2550+ return false ;
2551+ }
2552+
2553+ *value = rc->runs [it->index ].value + rc->runs [it->index ].length ;
2554+ return true ;
2555+ }
2556+ default :
2557+ assert (false );
2558+ roaring_unreachable;
2559+ return false ;
2560+ }
2561+ }
24462562
24472563/* *
24482564 * Moves the iterator to the smallest entry that is greater than or equal to
0 commit comments