Skip to content
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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions roaring/src/bitmap/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
pub store: Store,
}

#[derive(Clone)]
pub struct Iter<'a> {
pub key: u16,
inner: store::Iter<'a>,
Expand All @@ -41,6 +42,7 @@
self.store.is_empty()
}

#[inline]
pub fn insert(&mut self, index: u16) -> bool {
if self.store.insert(index) {
self.ensure_correct_store();
Expand Down Expand Up @@ -159,6 +161,7 @@
self.store.min()
}

#[inline]
pub fn max(&self) -> Option<u16> {
self.store.max()
}
Expand Down Expand Up @@ -295,7 +298,7 @@
}
}

impl<'a> Iterator for Iter<'a> {

Check failure on line 301 in roaring/src/bitmap/container.rs

View workflow job for this annotation

GitHub Actions / ci (nightly)

the following explicit lifetimes could be elided: 'a
type Item = u32;
fn next(&mut self) -> Option<u32> {
self.inner.next().map(|i| util::join(self.key, i))
Expand Down
2 changes: 2 additions & 0 deletions roaring/src/bitmap/inherent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl RoaringBitmap {
/// assert_eq!(rb.insert(3), false);
/// assert_eq!(rb.contains(3), true);
/// ```
#[inline]
pub fn insert(&mut self, value: u32) -> bool {
let (key, index) = util::split(value);
let container = match self.containers.binary_search_by_key(&key, |c| c.key) {
Expand Down Expand Up @@ -512,6 +513,7 @@ impl RoaringBitmap {
/// rb.insert(4);
/// assert_eq!(rb.max(), Some(4));
/// ```
#[inline]
pub fn max(&self) -> Option<u32> {
self.containers.last().and_then(|tail| tail.max().map(|max| util::join(tail.key, max)))
}
Expand Down
2 changes: 2 additions & 0 deletions roaring/src/bitmap/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ use crate::{NonSortedIntegers, RoaringBitmap};
use alloc::vec::Vec;

/// An iterator for `RoaringBitmap`.
#[derive(Clone)]
pub struct Iter<'a> {
inner: iter::Flatten<slice::Iter<'a, Container>>,
size_hint: u64,
}

/// An iterator for `RoaringBitmap`.
#[derive(Clone)]
pub struct IntoIter {
inner: iter::Flatten<vec::IntoIter<Container>>,
size_hint: u64,
Expand Down
2 changes: 2 additions & 0 deletions roaring/src/bitmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod container;
mod fmt;
mod multiops;
mod proptests;
mod statistics;
mod store;
mod util;

Expand All @@ -22,6 +23,7 @@ pub(crate) mod serialization;
use self::cmp::Pairs;
pub use self::iter::IntoIter;
pub use self::iter::Iter;
pub use self::statistics::Statistics;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
Expand Down
106 changes: 106 additions & 0 deletions roaring/src/bitmap/statistics.rs
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 {
Copy link
Member

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.

/// 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could theoretically overflow n_values_bitset_containers if you have a full bitmap. Not sure if that's a problem or not.

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,
}
}
}
6 changes: 6 additions & 0 deletions roaring/src/bitmap/store/array_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ impl ArrayStore {
ArrayStore { vec: Vec::with_capacity(capacity) }
}

pub fn capacity(&self) -> usize {
Copy link
Member

Choose a reason for hiding this comment

The 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
Expand All @@ -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()
}
Expand Down Expand Up @@ -208,6 +213,7 @@ impl ArrayStore {
self.vec.first().copied()
}

#[inline]
pub fn max(&self) -> Option<u16> {
self.vec.last().copied()
}
Expand Down
7 changes: 7 additions & 0 deletions roaring/src/bitmap/store/bitmap_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ impl BitmapStore {
BitmapStore { len: (BITMAP_LENGTH as u64) * 64, bits: Box::new([u64::MAX; BITMAP_LENGTH]) }
}

pub fn capacity(&self) -> usize {
BITMAP_LENGTH * u64::BITS as usize
}

pub fn try_from(len: u64, bits: Box<[u64; BITMAP_LENGTH]>) -> Result<BitmapStore, Error> {
let actual_len = bits.iter().map(|v| v.count_ones() as u64).sum();
if len != actual_len {
Expand All @@ -52,6 +56,7 @@ impl BitmapStore {
}
}

#[inline]
pub fn insert(&mut self, index: u16) -> bool {
let (key, bit) = (key(index), bit(index));
let old_w = self.bits[key];
Expand Down Expand Up @@ -253,6 +258,7 @@ impl BitmapStore {
.map(|(index, bit)| (index * 64 + (bit.trailing_zeros() as usize)) as u16)
}

#[inline]
pub fn max(&self) -> Option<u16> {
self.bits
.iter()
Expand Down Expand Up @@ -403,6 +409,7 @@ impl Display for Error {
#[cfg(feature = "std")]
impl std::error::Error for Error {}

#[derive(Clone)]
pub struct BitmapIter<B: Borrow<[u64; BITMAP_LENGTH]>> {
key: usize,
value: u64,
Expand Down
3 changes: 3 additions & 0 deletions roaring/src/bitmap/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Bitmap(BitmapStore),
}

#[derive(Clone)]
pub enum Iter<'a> {
Array(slice::Iter<'a, u16>),
Vec(vec::IntoIter<u16>),
Expand All @@ -49,6 +50,7 @@
Store::Bitmap(BitmapStore::full())
}

#[inline]
pub fn insert(&mut self, index: u16) -> bool {
match self {
Array(vec) => vec.insert(index),
Expand Down Expand Up @@ -191,6 +193,7 @@
}
}

#[inline]
pub fn max(&self) -> Option<u16> {
match self {
Array(vec) => vec.max(),
Expand Down Expand Up @@ -497,7 +500,7 @@
}
}

impl<'a> Iterator for Iter<'a> {

Check failure on line 503 in roaring/src/bitmap/store/mod.rs

View workflow job for this annotation

GitHub Actions / ci (nightly)

the following explicit lifetimes could be elided: 'a
type Item = u16;

fn next(&mut self) -> Option<u16> {
Expand Down
Loading