-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Clone on the bitmap::Iter type #289
base: main
Are you sure you want to change the base?
Changes from all commits
abfabf4
348e58c
d8f1829
6bba84b
8ff028e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use core::mem; | ||
|
||
use crate::bitmap::container::Container; | ||
use crate::RoaringBitmap; | ||
|
||
use super::store::Store; | ||
|
||
/// Detailed statistics on the composition of a bitmap. | ||
#[derive(Clone, Copy, PartialEq, Debug)] | ||
pub struct Statistics { | ||
/// Number of containers in the bitmap | ||
pub n_containers: u32, | ||
/// Number of array containers in the bitmap | ||
pub n_array_containers: u32, | ||
/// Number of run containers in the bitmap | ||
pub n_run_containers: u32, | ||
/// Number of bitset containers in the bitmap | ||
pub n_bitset_containers: u32, | ||
/// Number of values stored in array containers | ||
pub n_values_array_containers: u32, | ||
/// Number of values stored in run containers | ||
pub n_values_run_containers: u32, | ||
/// Number of values stored in bitset containers | ||
pub n_values_bitset_containers: u32, | ||
/// Number of bytes used by array containers | ||
pub n_bytes_array_containers: u64, | ||
/// Number of bytes used by run containers | ||
pub n_bytes_run_containers: u64, | ||
/// Number of bytes used by bitset containers | ||
pub n_bytes_bitset_containers: u64, | ||
/// Maximum value stored in the bitmap | ||
pub max_value: Option<u32>, | ||
/// Minimum value stored in the bitmap | ||
pub min_value: Option<u32>, | ||
/// Number of values stored in the bitmap | ||
pub cardinality: u64, | ||
} | ||
|
||
impl RoaringBitmap { | ||
/// Returns statistics about the composition of a roaring bitmap. | ||
/// | ||
/// ``` | ||
/// use roaring::RoaringBitmap; | ||
/// | ||
/// let mut bitmap: RoaringBitmap = (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, 512); | ||
/// assert_eq!(statistics.n_bytes_run_containers, 0); | ||
/// assert_eq!(statistics.n_bytes_bitset_containers, 0); | ||
/// assert_eq!(statistics.max_value, Some(99)); | ||
/// assert_eq!(statistics.min_value, Some(1)); | ||
/// assert_eq!(statistics.cardinality, 99); | ||
/// ``` | ||
pub fn statistics(&self) -> Statistics { | ||
let mut n_containers = 0; | ||
let mut n_array_containers = 0; | ||
let mut n_bitset_containers = 0; | ||
let mut n_values_array_containers = 0; | ||
let mut n_values_bitset_containers = 0; | ||
let mut n_bytes_array_containers = 0; | ||
let mut n_bytes_bitset_containers = 0; | ||
let mut cardinality = 0; | ||
|
||
for Container { key: _, store } in &self.containers { | ||
match store { | ||
Store::Array(array) => { | ||
cardinality += array.len(); | ||
n_values_array_containers += array.len() as u32; | ||
n_bytes_array_containers += (array.capacity() * mem::size_of::<u32>()) as u64; | ||
n_array_containers += 1; | ||
} | ||
Store::Bitmap(bitmap) => { | ||
cardinality += bitmap.len(); | ||
n_values_bitset_containers += bitmap.len() as u32; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could theoretically overflow If we ever implement run containers, that's much more likely, since it'd take much less memory to hold a totally full bitmap of all full run containers |
||
n_bytes_bitset_containers += bitmap.capacity() as u64; | ||
n_bitset_containers += 1; | ||
} | ||
} | ||
n_containers += 1; | ||
} | ||
|
||
Statistics { | ||
n_containers, | ||
n_array_containers, | ||
n_run_containers: 0, | ||
n_bitset_containers, | ||
n_values_array_containers, | ||
n_values_run_containers: 0, | ||
n_values_bitset_containers, | ||
n_bytes_array_containers, | ||
n_bytes_run_containers: 0, | ||
n_bytes_bitset_containers, | ||
max_value: self.max(), | ||
min_value: self.min(), | ||
cardinality, | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,10 @@ impl ArrayStore { | |
ArrayStore { vec: Vec::with_capacity(capacity) } | ||
} | ||
|
||
pub fn capacity(&self) -> usize { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'd at least need to document that this is the capacity of containers, not the capacity of values, or maybe have a different name? |
||
self.vec.capacity() | ||
} | ||
|
||
/// | ||
/// Create a new SortedU16Vec from a given vec | ||
/// It is up to the caller to ensure the vec is sorted and deduplicated | ||
|
@@ -47,6 +51,7 @@ impl ArrayStore { | |
} | ||
} | ||
|
||
#[inline] | ||
pub fn insert(&mut self, index: u16) -> bool { | ||
self.vec.binary_search(&index).map_err(|loc| self.vec.insert(loc, index)).is_err() | ||
} | ||
|
@@ -208,6 +213,7 @@ impl ArrayStore { | |
self.vec.first().copied() | ||
} | ||
|
||
#[inline] | ||
pub fn max(&self) -> Option<u16> { | ||
self.vec.last().copied() | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably worth a
#[non_exhaustive]
, to be able to add fields backwards-compatibly.