flow::comparator
This commit is contained in:
parent
ea161728a6
commit
7296d97ecb
@ -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
35
src/flow/comparator.rs
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
@ -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>>,
|
||||
|
@ -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>>,
|
||||
|
Loading…
Reference in New Issue
Block a user