Created
June 11, 2020 13:22
-
-
Save ayende/f5453fe5a217d20d8614a35dd5c7bbcc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void init_range(uint64_t *bitmap, size_t bitmap_size, | |
size_t size_required, | |
struct range_finder *restrict range) { | |
range->bitmap = bitmap; | |
range->bitmap_size = bitmap_size; | |
range->size_required = size_required; | |
range->current = bitmap[0]; | |
range->previous_set_bit = ULONG_MAX; | |
range->index = 0; | |
} | |
static bool find_next(struct range_finder *restrict range) { | |
do { | |
if (find_range_once(range)) { | |
if (range->current_set_bit % 64) { | |
// mask the already found item | |
uint64_t mask = ~(ULONG_MAX << (range->current_set_bit % 64)); | |
range->current |= mask; | |
} else { | |
// run out in the current word, but maybe we | |
// have more in the next? | |
if (range->index + 1 < range->bitmap_size) { | |
range->index++; | |
range->current = range->bitmap[range->index]; | |
continue; | |
} else { | |
range->current = ULONG_MAX; | |
} | |
} | |
return true; | |
} | |
range->index++; | |
if (range->index >= range->bitmap_size) | |
return false; | |
range->current = range->bitmap[range->index]; | |
} while (true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment