Skip to content

Commit

Permalink
Add Bitmap64::statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-Emann committed Jun 25, 2024
1 parent 6ed4288 commit 574c123
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
55 changes: 53 additions & 2 deletions croaring/src/bitmap64/imp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::bitmap64::Bitmap64;
use crate::bitmap64::{Deserializer, Serializer};
use super::{Bitmap64, Deserializer, Serializer, Statistics};
use core::mem::MaybeUninit;
use core::ops::{Bound, RangeBounds};
use core::prelude::v1::*;
Expand Down Expand Up @@ -947,6 +946,58 @@ impl Bitmap64 {
}
}

/// Returns statistics about the composition of a roaring bitmap64.
///
/// # Examples
///
/// ```
/// use croaring::Bitmap64;
///
/// let mut bitmap: Bitmap64 = (1..100).collect();
/// let statistics = bitmap.statistics();
///
/// assert_eq!(statistics.n_containers, 1);
/// assert_eq!(statistics.n_array_containers, 1);
/// assert_eq!(statistics.n_run_containers, 0);
/// assert_eq!(statistics.n_bitset_containers, 0);
/// assert_eq!(statistics.n_values_array_containers, 99);
/// assert_eq!(statistics.n_values_run_containers, 0);
/// assert_eq!(statistics.n_values_bitset_containers, 0);
/// assert_eq!(statistics.n_bytes_array_containers, 198);
/// assert_eq!(statistics.n_bytes_run_containers, 0);
/// assert_eq!(statistics.n_bytes_bitset_containers, 0);
/// assert_eq!(statistics.max_value, 99);
/// assert_eq!(statistics.min_value, 1);
/// assert_eq!(statistics.cardinality, 99);
///
/// bitmap.run_optimize();
/// let statistics = bitmap.statistics();
///
/// assert_eq!(statistics.n_containers, 1);
/// assert_eq!(statistics.n_array_containers, 0);
/// assert_eq!(statistics.n_run_containers, 1);
/// assert_eq!(statistics.n_bitset_containers, 0);
/// assert_eq!(statistics.n_values_array_containers, 0);
/// assert_eq!(statistics.n_values_run_containers, 99);
/// assert_eq!(statistics.n_values_bitset_containers, 0);
/// assert_eq!(statistics.n_bytes_array_containers, 0);
/// assert_eq!(statistics.n_bytes_run_containers, 6);
/// assert_eq!(statistics.n_bytes_bitset_containers, 0);
/// assert_eq!(statistics.max_value, 99);
/// assert_eq!(statistics.min_value, 1);
/// assert_eq!(statistics.cardinality, 99);
/// ```
#[inline]
#[doc(alias = "roaring64_bitmap_statistics")]
#[must_use]
pub fn statistics(&self) -> Statistics {
let mut stats = MaybeUninit::<ffi::roaring64_statistics_t>::zeroed();
unsafe {
ffi::roaring64_bitmap_statistics(self.raw.as_ptr(), stats.as_mut_ptr());
stats.assume_init()
}
}

/// Ensure the bitmap is internally valid
///
/// This is useful for development, but is not needed for normal use:
Expand Down
5 changes: 5 additions & 0 deletions croaring/src/bitmap64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ pub struct Bitmap64 {
}
unsafe impl Sync for Bitmap64 {}
unsafe impl Send for Bitmap64 {}

/// Detailed statistics on the composition of a bitmap
///
/// See [`Bitmap64::statistics`] for more information
pub type Statistics = ffi::roaring64_statistics_t;

0 comments on commit 574c123

Please sign in to comment.