From 574c123d97f1ef234ee3d9e492a373598e369791 Mon Sep 17 00:00:00 2001 From: Zachary Dremann Date: Tue, 25 Jun 2024 16:00:22 -0400 Subject: [PATCH] Add Bitmap64::statistics --- croaring/src/bitmap64/imp.rs | 55 ++++++++++++++++++++++++++++++++++-- croaring/src/bitmap64/mod.rs | 5 ++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/croaring/src/bitmap64/imp.rs b/croaring/src/bitmap64/imp.rs index f12865a..21c0cec 100644 --- a/croaring/src/bitmap64/imp.rs +++ b/croaring/src/bitmap64/imp.rs @@ -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::*; @@ -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::::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: diff --git a/croaring/src/bitmap64/mod.rs b/croaring/src/bitmap64/mod.rs index 0b96ed5..04521b4 100644 --- a/croaring/src/bitmap64/mod.rs +++ b/croaring/src/bitmap64/mod.rs @@ -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;