pub trait BetterThanOperator<T> {
fn better_than(self: &Self, a: &T, b: &T) -> bool;
fn ordering(&self, a: &T, b: &T) -> std::cmp::Ordering {
if self.better_than(a, b) {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Greater
}
}
}
pub struct MinimizingOperator;
impl MinimizingOperator {
pub fn new() -> Self {
Self
}
}
impl<T> BetterThanOperator<T> for MinimizingOperator
where T: PartialOrd
{
fn better_than(self: &Self, a: &T, b: &T) -> bool {
a < b
}
}
pub struct MaximizingOperator;
impl MaximizingOperator {
pub fn new() -> Self {
Self
}
}
impl<T> BetterThanOperator<T> for MaximizingOperator
where T: PartialOrd
{
fn better_than(self: &Self, a: &T, b: &T) -> bool {
a > b
}
}