Skip to content

Commit

Permalink
Implement bitset
Browse files Browse the repository at this point in the history
CRoaring introduced a `bitset` type, and the abilty to convert a roaring
bitmap into a dense bitset. This adds wrappers around the new bitset
functions
  • Loading branch information
Dr-Emann committed Jun 13, 2023
1 parent 128bdb5 commit 40b4660
Show file tree
Hide file tree
Showing 9 changed files with 698 additions and 0 deletions.
24 changes: 24 additions & 0 deletions croaring/src/bitmap/imp.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::Bitset;
use ffi::roaring_bitmap_t;
use std::convert::TryInto;
use std::ffi::c_char;
Expand Down Expand Up @@ -1486,6 +1487,29 @@ impl Bitmap {

statistics
}

/// Store the bitmap to a bitset
///
/// This can be useful for those who need the performance and simplicity of a standard bitset.
///
/// # Errors
///
/// This function will return None on allocation failure
///
/// # Examples
/// ```
/// use croaring::Bitmap;
/// let bitmap = Bitmap::from_range(0..100);
/// let bitset = bitmap.to_bitset().unwrap();
/// assert_eq!(bitset.count(), 100);
/// ```
#[inline]
#[doc(alias = "roaring_bitmap_to_bitset")]
pub fn to_bitset(&self) -> Option<Bitset> {
let mut bitset = Bitset::new();
let success = unsafe { ffi::roaring_bitmap_to_bitset(&self.bitmap, bitset.as_raw_mut()) };
success.then_some(bitset)
}
}

fn range_to_inclusive<R: RangeBounds<u32>>(range: R) -> (u32, u32) {
Expand Down
1 change: 1 addition & 0 deletions croaring/src/bitmap/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::marker::PhantomData;

use super::Bitmap;

/// Iterator over the values of a bitmap
#[derive(Clone)]
pub struct BitmapIterator<'a> {
iterator: ffi::roaring_uint32_iterator_s,
Expand Down
1 change: 1 addition & 0 deletions croaring/src/bitmap/lazy.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::Bitmap;

/// A handle to lazily perform multiple bitwise operations on a bitmap
pub struct LazyBitmap<'a> {
bitmap: &'a mut Bitmap,
}
Expand Down
2 changes: 2 additions & 0 deletions croaring/src/bitmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

use std::marker::PhantomData;

/// A compressed bitmap
// Must be repr(transparent) and match BitmapView, to allow safe transmute between
// &BitmapView and &Bitmap
#[repr(transparent)]
Expand All @@ -78,6 +79,7 @@ pub struct BitmapView<'a> {
unsafe impl<'a> Sync for BitmapView<'a> {}
unsafe impl<'a> Send for BitmapView<'a> {}

/// Detailed statistics on the composition of a bitmap
pub type Statistics = ffi::roaring_statistics_s;

mod imp;
Expand Down
Loading

0 comments on commit 40b4660

Please sign in to comment.