pick_smaller -> compare

This commit is contained in:
AF 2023-06-28 13:52:43 +00:00
parent 6ad8dcc93f
commit 0fdf7532dc
5 changed files with 15 additions and 15 deletions

View File

@ -97,7 +97,7 @@ pub trait BinaryTreesMutable<'a>: BinaryTreesEmpty<'a> + BinaryTreesTreeOf<'a> {
key: Self::Key,
) -> BTWrap<'a, Self, KeySplit<'a, Self>> {
let (tl, tr, k) = self.split(&node);
match self.comparator().pick_smaller(&key, &k) {
match self.comparator().compare(&key, &k) {
Comparison::L => Self::bind(self.clone().split_key(tl, key), move |(tll, tlr)| {
Self::fmap(self.join_key_tree(tlr, k, tr), |t| (tll, t))
}),

View File

@ -18,7 +18,7 @@ impl<A: Clone> Bounds<A> {
}
fn ordered(l: &A, r: &A, comparator: &impl Comparator<A>) -> Result<(), BoundsError<A>> {
if let Comparison::R = comparator.pick_smaller(l, r) {
if let Comparison::R = comparator.compare(l, r) {
Err(BoundsError::BoundsViolated {
l: l.clone(),
r: r.clone(),
@ -72,7 +72,7 @@ impl<A: Clone> Bounds<A> {
fn equal_bound(l: &Option<A>, r: &Option<A>, comparator: &impl Comparator<A>) -> bool {
match (l, r) {
(None, None) => true,
(Some(kl), Some(kr)) => matches!(comparator.pick_smaller(kl, kr), Comparison::E),
(Some(kl), Some(kr)) => matches!(comparator.compare(kl, kr), Comparison::E),
_ => false,
}
}

View File

@ -1,4 +1,4 @@
/// Result returned by [`Comparator::pick_smaller`].
/// Result returned by [`Comparator::compare`].
#[derive(Debug, PartialEq)]
pub enum Comparison {
L,
@ -11,19 +11,19 @@ pub enum Comparison {
///
/// ```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);
/// assert_eq!(DefaultComparator.compare(&1, &3), Comparison::L);
/// assert_eq!(DefaultComparator.compare(&2, &2), Comparison::E);
/// assert_eq!(DefaultComparator.compare(&3, &1), Comparison::R);
/// ```
pub trait Comparator<A> {
fn pick_smaller(&self, kl: &A, kr: &A) -> Comparison;
fn compare(&self, kl: &A, kr: &A) -> Comparison;
}
/// Implementation of a [Comparator] relying on [`Ord`].
pub struct DefaultComparator;
impl<A: Ord> Comparator<A> for DefaultComparator {
fn pick_smaller(&self, kl: &A, kr: &A) -> Comparison {
fn compare(&self, kl: &A, kr: &A) -> Comparison {
match kl.cmp(kr) {
std::cmp::Ordering::Less => Comparison::L,
std::cmp::Ordering::Equal => Comparison::E,
@ -39,8 +39,8 @@ mod rc_comparator_impl {
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)
fn compare(&self, kl: &Rc<A>, kr: &Rc<A>) -> Comparison {
self.0.compare(kl, kr)
}
}
}

View File

@ -6,7 +6,7 @@ pub fn n_contains<'a, T: MonadFail<'a, ()>, A: 'a + Clone, D: 'a + PartialEq>(
key: A,
) -> T::F<()> {
let (t_setl, t_setr, k_set) = n_set.split();
match comparator.pick_smaller(&key, &k_set) {
match comparator.compare(&key, &k_set) {
Comparison::L => t_contains(comparator, t_setl, key),
Comparison::E => T::pure(()),
Comparison::R => t_contains(comparator, t_setr, key),

View File

@ -86,7 +86,7 @@ impl<'a, T: 'a + MonadFail<'a, ()>, A: 'a + Clone, D: 'a + PartialEq> SubsetCont
fn test(self) -> T::F<()> {
let split = self.n_subset.split();
match self.comparator.pick_smaller(&split.2, &self.k_super) {
match self.comparator.compare(&split.2, &self.k_super) {
Comparison::L => self.on_l(split),
Comparison::E => self.on_e(split),
Comparison::R => self.on_r(split),
@ -103,12 +103,12 @@ pub fn n_subset_of_n<'a, T: MonadFail<'a, ()>, A: 'a + Clone, D: 'a + PartialEq>
) -> T::F<()> {
let (t_superl, t_superr, k_super) = n_superset.split();
if let Some(ref a_l) = k_l {
if let Comparison::R = comparator.pick_smaller(a_l, &k_super) {
if let Comparison::R = comparator.compare(a_l, &k_super) {
return t_subset_of_t(comparator, n_subset.to_tree(), t_superr, k_l, k_r);
}
}
if let Some(ref a_r) = k_r {
if let Comparison::L = comparator.pick_smaller(a_r, &k_super) {
if let Comparison::L = comparator.compare(a_r, &k_super) {
return t_subset_of_t(comparator, n_subset.to_tree(), t_superl, k_l, k_r);
}
}