49 lines
1.2 KiB
Rust
49 lines
1.2 KiB
Rust
/// Result returned by [`Comparator::pick_smaller`].
|
|
#[derive(Debug, PartialEq)]
|
|
pub enum Comparison {
|
|
L,
|
|
/// Compared values were equal.
|
|
E,
|
|
R,
|
|
}
|
|
|
|
/// Returns [`Comparison`] saying which value is smaller.
|
|
///
|
|
/// ```rust
|
|
/// # use radn_rs::flow::comparator::*;
|
|
/// assert_eq!(DefaultComparator.pick_smaller(&1, &3), Comparison::L);
|
|
/// assert_eq!(DefaultComparator.pick_smaller(&2, &2), Comparison::E);
|
|
/// assert_eq!(DefaultComparator.pick_smaller(&3, &1), Comparison::R);
|
|
/// ```
|
|
pub trait Comparator<A> {
|
|
fn pick_smaller(&self, kl: &A, kr: &A) -> Comparison;
|
|
}
|
|
|
|
/// Implementation of a [Comparator] relying on [`PartialOrd`].
|
|
pub struct DefaultComparator;
|
|
|
|
impl<A: PartialOrd> Comparator<A> for DefaultComparator {
|
|
fn pick_smaller(&self, kl: &A, kr: &A) -> Comparison {
|
|
if kl < kr {
|
|
Comparison::L
|
|
} else if kr < kl {
|
|
Comparison::R
|
|
} else {
|
|
Comparison::E
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct RcComparator<C>(C);
|
|
|
|
mod rc_comparator_impl {
|
|
use super::*;
|
|
use std::rc::Rc;
|
|
|
|
impl<A, C: Comparator<A>> Comparator<Rc<A>> for RcComparator<C> {
|
|
fn pick_smaller(&self, kl: &Rc<A>, kr: &Rc<A>) -> Comparison {
|
|
self.0.pick_smaller(kl, kr)
|
|
}
|
|
}
|
|
}
|