use crate::bits::Bitfield;
use crate::idxsets::intersection::Intersection;
use std::iter::Flatten;
pub trait IdxSet: Sized + Clone {
type IdxIter: Iterator<Item = Bitfield> + DoubleEndedIterator;
fn into_idx_iter(self) -> Self::IdxIter;
fn size(&self) -> usize;
fn intersect(&self, idx: &Bitfield) -> Bitfield;
fn intersection<B>(self, b: B) -> Intersection<Self, B>
where
B: IdxSet,
{
Intersection::new(self, b)
}
}
impl<T> IdxSet for Option<T>
where
T: IdxSet,
{
type IdxIter = Flatten<<Option<T::IdxIter> as IntoIterator>::IntoIter>;
fn into_idx_iter(self) -> Self::IdxIter {
self.map(|x| x.into_idx_iter()).into_iter().flatten()
}
fn size(&self) -> usize {
self.as_ref().map(T::size).unwrap_or(0)
}
fn intersect(&self, idx: &Bitfield) -> Bitfield {
self.as_ref()
.map(|x| x.intersect(idx))
.unwrap_or_else(|| Bitfield::new_empty(idx.start()))
}
}