use std::fmt::Debug;
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum JoinerType {
Equal,
LessThan,
LessThanOrEqual,
GreaterThan,
GreaterThanOrEqual,
NotEqual,
RangeOverlaps,
RangeContains,
RangeWithin,
}
impl JoinerType {
pub fn inverse(&self) -> JoinerType {
match self {
JoinerType::Equal => JoinerType::Equal,
JoinerType::NotEqual => JoinerType::NotEqual,
JoinerType::LessThan => JoinerType::GreaterThan,
JoinerType::LessThanOrEqual => JoinerType::GreaterThanOrEqual,
JoinerType::GreaterThan => JoinerType::LessThan,
JoinerType::GreaterThanOrEqual => JoinerType::LessThanOrEqual,
JoinerType::RangeOverlaps => JoinerType::RangeOverlaps,
JoinerType::RangeContains => JoinerType::RangeWithin,
JoinerType::RangeWithin => JoinerType::RangeContains,
}
}
pub fn create_comparator<T>(&self) -> Box<dyn Fn(&T, &T) -> bool>
where
T: PartialOrd + 'static,
{
match self {
JoinerType::Equal => Box::new(|a, b| a == b),
JoinerType::LessThan => Box::new(|a, b| a < b),
JoinerType::LessThanOrEqual => Box::new(|a, b| a <= b),
JoinerType::GreaterThan => Box::new(|a, b| a > b),
JoinerType::GreaterThanOrEqual => Box::new(|a, b| a >= b),
JoinerType::NotEqual => Box::new(|a, b| a != b),
JoinerType::RangeOverlaps => {
Box::new(|_a, _b| panic!("Range operations require special handling"))
}
JoinerType::RangeContains => {
Box::new(|_a, _b| panic!("Range operations require special handling"))
}
JoinerType::RangeWithin => {
Box::new(|_a, _b| panic!("Range operations require special handling"))
}
}
}
}
pub trait Comparator<T> {
fn compare(&self, left: &T, right: &T) -> bool;
}
pub struct RangeUtils;
impl RangeUtils {
pub fn ranges_overlap<T: PartialOrd>(range_a: (T, T), range_b: (T, T)) -> bool {
!(range_a.1 < range_b.0 || range_b.1 < range_a.0)
}
pub fn range_contains<T: PartialOrd>(container: (T, T), content: (T, T)) -> bool {
container.0 <= content.0 && content.1 <= container.1
}
pub fn range_within<T: PartialOrd>(content: (T, T), container: (T, T)) -> bool {
Self::range_contains(container, content)
}
}