Skip to content

Commit 173ef04

Browse files
committed
Update README.md.
1 parent 82d6b8d commit 173ef04

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,48 @@ You should be aware that a convention bitset (`bitset_t *`) may use much more
805805
memory than a Roaring bitmap in some cases. You should run benchmarks to determine
806806
whether the conversion to a bitset has performance benefits in your case.
807807
808+
809+
# Convert to boolean array (C)
810+
811+
This example shows how to convert a Roaring bitmap to a boolean array using `roaring_bitmap_to_bool_array`:
812+
813+
```c
814+
roaring_bitmap_t *r1 = roaring_bitmap_create();
815+
for (uint32_t i = 100; i < 100000; i+= 1 + (i%5)) {
816+
roaring_bitmap_add(r1, i);
817+
}
818+
for (uint32_t i = 100000; i < 500000; i+= 100) {
819+
roaring_bitmap_add(r1, i);
820+
}
821+
roaring_bitmap_add_range(r1, 500000, 600000);
822+
823+
// Get the maximum value to determine array size
824+
uint32_t max_value = roaring_bitmap_maximum(r1);
825+
bool *bool_array = malloc((max_value + 1) * sizeof(bool));
826+
827+
// Convert to boolean array
828+
bool success = roaring_bitmap_to_bool_array(r1, bool_array, max_value);
829+
assert(success); // always returns true
830+
831+
// Verify the conversion
832+
for (uint32_t i = 100; i < 100000; i+= 1 + (i%5)) {
833+
assert(bool_array[i]);
834+
}
835+
for (uint32_t i = 100000; i < 500000; i+= 100) {
836+
assert(bool_array[i]);
837+
}
838+
839+
// Check that non-set values are false
840+
assert(!bool_array[0]); // 0 should not be set
841+
assert(!bool_array[99]); // 99 should not be set
842+
843+
// you must free the memory:
844+
free(bool_array);
845+
roaring_bitmap_free(r1);
846+
```
847+
848+
This function stores each bit in a single byte as a boolean value, which can be useful when you need to work with boolean arrays directly. Note that this approach uses more memory than the Roaring bitmap itself, but provides fast random access to individual elements.
849+
808850
# Example (C++)
809851

810852

0 commit comments

Comments
 (0)