use crate::set::Set;
mod union;
mod intersection;
mod difference;
mod difference_by_key;
mod symmetric_difference;
pub use self::union::Union;
pub use self::intersection::Intersection;
pub use self::difference::Difference;
pub use self::difference_by_key::DifferenceByKey;
pub use self::symmetric_difference::SymmetricDifference;
#[derive(Clone)]
pub struct OpBuilder<'a, T: 'a> {
slices: Vec<&'a Set<T>>,
}
impl<'a, T> OpBuilder<'a, T> {
pub fn new() -> Self {
Self { slices: Vec::new() }
}
pub fn with_capacity(capacity: usize) -> Self {
Self { slices: Vec::with_capacity(capacity) }
}
pub fn from_vec(slices: Vec<&'a Set<T>>) -> Self {
Self { slices }
}
pub fn reserve(&mut self, additional: usize) {
self.slices.reserve(additional);
}
pub fn add(mut self, set: &'a Set<T>) -> Self {
self.push(set);
self
}
pub fn push(&mut self, set: &'a Set<T>) {
self.slices.push(set);
}
pub fn union(self) -> Union<'a, T> {
Union::new(self.slices)
}
pub fn intersection(self) -> Intersection<'a, T> {
Intersection::new(self.slices)
}
pub fn difference(self) -> Difference<'a, T> {
Difference::new(self.slices)
}
pub fn symmetric_difference(self) -> SymmetricDifference<'a, T> {
SymmetricDifference::new(self.slices)
}
}
#[derive(Clone)]
pub struct OpBuilderByKey<'a, T: 'a, U: 'a, F, G, K>
where F: Fn(&T) -> K,
G: Fn(&U) -> K,
K: Ord,
{
base: &'a Set<T>,
others: Vec<&'a Set<U>>,
f: F,
g: G,
}
impl<'a, T, U, F, G, K> OpBuilderByKey<'a, T, U, F, G, K>
where F: Fn(&T) -> K,
G: Fn(&U) -> K,
K: Ord,
{
pub fn new(base: &'a Set<T>, f: F, g: G) -> Self {
Self { base, others: Vec::new(), f, g }
}
pub fn from_vec(base: &'a Set<T>, others: Vec<&'a Set<U>>, f: F, g: G) -> Self {
Self { base, others, f, g }
}
pub fn push(&mut self, set: &'a Set<U>) {
self.others.push(set);
}
pub fn add(mut self, set: &'a Set<U>) -> Self {
self.push(set);
self
}
pub fn difference(self) -> DifferenceByKey<'a, T, U, F, G, K> {
DifferenceByKey::new(self.base, self.others, self.f, self.g)
}
}