flow::comparator

This commit is contained in:
AF 2023-05-28 20:55:36 +00:00
parent ea161728a6
commit 7296d97ecb
4 changed files with 40 additions and 38 deletions

View File

@ -1,5 +1,6 @@
//! Data structures and algorithms, independent of [`crate::rcore`] concepts.
pub mod binary;
pub mod comparator;
pub mod speculative;
pub mod traversible;

35
src/flow/comparator.rs Normal file
View File

@ -0,0 +1,35 @@
/// 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::traversible::*;
/// 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
}
}
}

View File

@ -5,46 +5,9 @@ pub mod unbalanced;
use std::rc::Rc;
use crate::flow::comparator::*;
use crate::func::*;
/// Result returned by [`Comparator::pick_smaller`].
#[derive(Debug, PartialEq)]
pub enum Comparison {
L,
/// Compared values were equal.
E,
R,
}
fn and(_l: (), _r: ()) {}
/// Returns [`Comparison`] saying which value is smaller.
///
/// ```rust
/// # use radn_rs::flow::traversible::*;
/// 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 type Split<'a, T, A, D> = (
Rc<dyn TraversibleBinaryTree<'a, T, A, D>>,
Rc<dyn TraversibleBinaryTree<'a, T, A, D>>,

View File

@ -1,5 +1,8 @@
use crate::flow::comparator::*;
use crate::flow::traversible::*;
fn and(_l: (), _r: ()) {}
struct SubsetContext<'a, T: MonadFail<'a, ()>, A: 'a, D: 'a + PartialEq> {
comparator: &'a dyn Comparator<A>,
n_subset: Rc<dyn TraversibleBinaryNode<'a, T, A, D>>,