1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//! Comparator combinators.

use core::cmp::Ordering;
use core::fmt;
use Comparator;

/// A comparator that reverse its underlying comparator.
#[derive(Copy, Clone, Debug)]
pub struct Reversed<T>(pub(crate) T);

impl<T, U> Comparator<U> for Reversed<T>
where
    T: Comparator<U>,
    U: ?Sized,
{
    fn compare(&self, a: &U, b: &U) -> Ordering {
        self.0.compare(a, b).reverse()
    }
}

#[derive(Copy, Clone, Debug)]
/// A comparator that chains another comparator.
pub struct ThenComparing<T, U>(pub(crate) T, pub(crate) U);

impl<T, U, W> Comparator<W> for ThenComparing<T, U>
where
    T: Comparator<W>,
    U: Comparator<W>,
    W: ?Sized,
{
    fn compare(&self, a: &W, b: &W) -> Ordering {
        self.0.compare(a, b).then_with(|| self.1.compare(a, b))
    }
}

#[derive(Copy, Clone)]
/// A comparator that chains comparison function.
pub struct ThenComparingByKey<T, F> {
    pub(crate) comparator: T,
    pub(crate) f: F,
}

impl<T, F> fmt::Debug for ThenComparingByKey<T, F>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("ThenComparingByKey")
            .field("comparator", &self.comparator)
            .finish()
    }
}

impl<T, F, U, W> Comparator<U> for ThenComparingByKey<T, F>
where
    T: Comparator<U>,
    F: Fn(&U) -> W,
    U: ?Sized,
    W: Ord,
{
    fn compare(&self, a: &U, b: &U) -> Ordering {
        self.comparator
            .compare(a, b)
            .then_with(|| (self.f)(&a).cmp(&(self.f)(&b)))
    }
}

/// Create a combinator comparing by key.
pub fn comparing<F, T, U>(f: F) -> Comparing<F>
where
    F: Fn(&T) -> U,
    T: ?Sized,
    U: Ord,
{
    Comparing { f }
}

/// A comparator that compares using its key function.
#[derive(Clone, Copy)]
pub struct Comparing<F> {
    f: F,
}

impl<F> fmt::Debug for Comparing<F> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Comparing").finish()
    }
}

impl<F, T, U> Comparator<T> for Comparing<F>
where
    F: Fn(&T) -> U,
    T: ?Sized,
    U: Ord,
{
    fn compare(&self, a: &T, b: &T) -> Ordering {
        (self.f)(a).cmp(&(self.f)(b))
    }
}

/// Creates a natural ordering comparator.
pub fn natural_order() -> NaturalOrder {
    NaturalOrder { _priv: () }
}

/// A natural ordering comparator.
#[derive(Copy, Clone, Default)]
pub struct NaturalOrder {
    _priv: (),
}

impl fmt::Debug for NaturalOrder {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("NaturalOrder").finish()
    }
}

impl<T> Comparator<T> for NaturalOrder
where
    T: ?Sized + Ord,
{
    fn compare(&self, a: &T, b: &T) -> Ordering {
        a.cmp(b)
    }
}

/// Creates a reverse ordering comparator.
pub fn reverse_order() -> ReverseOrder {
    ReverseOrder { _priv: () }
}

/// Reverse ordering comparator.
#[derive(Copy, Clone, Default)]
pub struct ReverseOrder {
    _priv: (),
}

impl fmt::Debug for ReverseOrder {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("ReverseOrder").finish()
    }
}

impl<T> Comparator<T> for ReverseOrder
where
    T: ?Sized + Ord,
{
    fn compare(&self, a: &T, b: &T) -> Ordering {
        b.cmp(a)
    }
}