use std::convert::TryFrom;
use roaring::RoaringBitmap;
pub type RowId = u32;
pub type RowIdIter<'a> = Box<dyn Iterator<Item = RowId> + Send + 'a>;
pub trait RowSet: Send + Sync {
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn is_full(&self) -> bool;
fn iter(&self) -> RowIdIter<'_>;
fn intersect(&self, other: &Self) -> Self
where
Self: Sized;
fn union(&self, other: &Self) -> Self
where
Self: Sized;
fn difference(&self, other: &Self) -> Self
where
Self: Sized;
}
#[derive(Clone, Debug, Default)]
pub struct BitmapRowSet {
bitmap: RoaringBitmap,
}
impl BitmapRowSet {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn insert(&mut self, row: RowId) {
self.bitmap.insert(row);
}
#[must_use]
pub fn contains(&self, row: RowId) -> bool {
self.bitmap.contains(row)
}
}
impl RowSet for BitmapRowSet {
fn len(&self) -> usize {
usize::try_from(self.bitmap.len()).unwrap_or(usize::MAX)
}
fn is_full(&self) -> bool {
self.bitmap.is_full()
}
fn iter(&self) -> RowIdIter<'_> {
Box::new(self.bitmap.iter())
}
fn intersect(&self, other: &Self) -> Self {
let bitmap = &self.bitmap & &other.bitmap;
Self { bitmap }
}
fn union(&self, other: &Self) -> Self {
let bitmap = &self.bitmap | &other.bitmap;
Self { bitmap }
}
fn difference(&self, other: &Self) -> Self {
let bitmap = &self.bitmap - &other.bitmap;
Self { bitmap }
}
}