[−][src]Trait classific::Comparator
Implementations define a linear ordering over the type T
, which can be semantically
different from Ord::cmp
.
By convention, the ordering should fulfill the following properties
(for all a
, b
and c
):
- connex:
a <= b
orb <= a
- antisymmetric: if
a <= b
andb <= a
thena == b
- transitive: if
a <= b
andb <= c
thena <= c
(where a <= b
can be also understood as !(a > b)
)
Examples
use std::cmp::Ordering; use classific::{Comparator, comparing, natural_order, reverse_order}; assert_eq!(natural_order().cmp(&1, &2), Ordering::Less); assert_eq!(comparing(|v| v & !3).cmp(&1, &2), Ordering::Equal); assert_eq!(reverse_order().cmp(&1, &2), Ordering::Greater);
Required methods
fn cmp(&self, left: &T, right: &T) -> Ordering
This method returns an Ordering
between left
and right
.
By convention, comparator.cmp(&left, &right)
returns the ordering matching the expression
left <operator> right
if true.
Examples
use std::cmp::Ordering; use classific::{Comparator, comparing, natural_order, reverse_order}; assert_eq!(natural_order().cmp(&1, &2), Ordering::Less); assert_eq!(comparing(|v| v & !3).cmp(&1, &2), Ordering::Equal); assert_eq!(reverse_order().cmp(&1, &2), Ordering::Greater);
Provided methods
fn reversed(self) -> ReversedOrder<T, Self> where
Self: Sized,
Self: Sized,
This method returns a Comparator
which is semantically reversed from self
.
The ordering of the result Comparator
will fulfill the following
(for all a
, b
):
use classific::Comparator; fn test<T>(a: &T, b: &T, comparator: impl Comparator<T>) { assert_eq!(comparator.cmp(a, b).reverse(), comparator.reversed().cmp(a, b)); }
fn then<N>(self, next: N) -> CompareThen<T, Self, N> where
Self: Sized,
N: Comparator<T>,
Self: Sized,
N: Comparator<T>,
This method returns a Comparator
which is further refining the semantics of self
.
next::cmp
is called only when self::cmp
returns Ordering::Equal
.
Examples
use std::cmp::Ordering; use classific::{Comparator, comparing, natural_order, reverse_order}; assert_eq!(comparing(|t: &(i8, i8)| t.0).then(comparing(|t: &(i8, i8)| t.1)).cmp(&(1, 2), &(1, 3)), Ordering::Less);
fn max<'a>(&self, left: &'a T, right: &'a T) -> &'a T
This function returns the greater instace (out of left
and right
)
according to cmp
.
Examples
use classific::{Comparator, reverse_order}; assert_eq!(reverse_order().max(&1, &2), &1);
fn min<'a>(&self, left: &'a T, right: &'a T) -> &'a T
Implementors
impl<S, T, F> Comparator<S> for Comparing<S, T, F> where
S: ?Sized,
T: Ord,
F: Fn(&S) -> T,
[src]
S: ?Sized,
T: Ord,
F: Fn(&S) -> T,
impl<S, T, F, C> Comparator<S> for ComparingWith<S, T, F, C> where
S: ?Sized,
T: Ord,
F: Fn(&S) -> T,
C: Comparator<T>,
[src]
S: ?Sized,
T: Ord,
F: Fn(&S) -> T,
C: Comparator<T>,
impl<S, T: ?Sized, F> Comparator<S> for ComparingRef<S, T, F> where
S: ?Sized,
T: Ord,
F: Fn(&S) -> &T,
[src]
S: ?Sized,
T: Ord,
F: Fn(&S) -> &T,
impl<S, T: ?Sized, F, C> Comparator<S> for ComparingRefWith<S, T, F, C> where
S: ?Sized,
T: Ord,
F: Fn(&S) -> &T,
C: Comparator<T>,
[src]
S: ?Sized,
T: Ord,
F: Fn(&S) -> &T,
C: Comparator<T>,
impl<T, C> Comparator<T> for ReversedOrder<T, C> where
T: ?Sized,
C: Comparator<T>,
[src]
T: ?Sized,
C: Comparator<T>,
impl<T, C, N> Comparator<T> for CompareThen<T, C, N> where
T: ?Sized,
C: Comparator<T>,
N: Comparator<T>,
[src]
T: ?Sized,
C: Comparator<T>,
N: Comparator<T>,
impl<T, F> Comparator<T> for AtGreatest<T, F> where
T: ?Sized,
F: Fn(&T) -> bool,
[src]
T: ?Sized,
F: Fn(&T) -> bool,
impl<T, F> Comparator<T> for AtLeast<T, F> where
T: ?Sized,
F: Fn(&T) -> bool,
[src]
T: ?Sized,
F: Fn(&T) -> bool,
impl<T, F> Comparator<T> for F where
T: ?Sized,
F: Fn(&T, &T) -> Ordering,
[src]
T: ?Sized,
F: Fn(&T, &T) -> Ordering,
impl<T: ?Sized> Comparator<T> for NaturalOrder<T> where
T: Ord,
[src]
T: Ord,
impl<T: ?Sized> Comparator<T> for ReversedNaturalOrder<T> where
T: Ord,
[src]
T: Ord,
impl<T: ?Sized, C> Comparator<T> for PartialOrderOr<T, C> where
T: PartialOrd<T>,
C: Comparator<T>,
[src]
T: PartialOrd<T>,
C: Comparator<T>,