ferritin_core/selection/
selection.rsuse std::ops::BitAnd;
#[derive(Clone, Debug)]
pub struct Selection {
pub(crate) indices: Vec<usize>,
}
impl Selection {
pub fn new(indices: Vec<usize>) -> Self {
Selection { indices }
}
pub fn and(&self, other: &Selection) -> Selection {
let indices: Vec<usize> = self
.indices
.iter()
.filter(|&&idx| other.indices.contains(&idx))
.cloned()
.collect();
Selection::new(indices)
}
pub fn or(&self, other: &Selection) -> Selection {
let mut indices = self.indices.clone();
indices.extend(
other
.indices
.iter()
.filter(|idx| !self.indices.contains(idx)),
);
indices.sort();
Selection::new(indices)
}
pub fn not(&self, other: &Selection) -> Selection {
let indices: Vec<_> = self
.indices
.iter()
.filter(|&&idx| !other.indices.contains(&idx))
.cloned()
.collect();
Selection::new(indices)
}
}
impl BitAnd for &Selection {
type Output = Selection;
fn bitand(self, other: Self) -> Selection {
self.and(other)
}
}