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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//! Comparator combinators.

use core::cmp::Ordering;
use core::fmt;
use core::marker::PhantomData;
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>,
{
    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>,
{
    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,
    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,
    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,
    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<T>() -> NaturalOrder<T>
where
    T: Ord,
{
    NaturalOrder {
        _phantom: PhantomData,
    }
}

/// A natural ordering comparator.
pub struct NaturalOrder<T> {
    _phantom: PhantomData<fn(&T)>,
}

impl<T> Clone for NaturalOrder<T> {
    fn clone(&self) -> Self {
        Self {
            _phantom: PhantomData,
        }
    }
}

impl<T> Copy for NaturalOrder<T> {}

impl<T> Default for NaturalOrder<T>
where
    T: Ord,
{
    fn default() -> Self {
        Self {
            _phantom: PhantomData,
        }
    }
}

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

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

/// Creates a reverse ordering comparator.
pub fn reverse_order<T>() -> ReverseOrder<T>
where
    T: Ord,
{
    ReverseOrder {
        _phantom: PhantomData,
    }
}

/// Reverse ordering comparator.
pub struct ReverseOrder<T> {
    _phantom: PhantomData<fn(&T)>,
}

impl<T> Clone for ReverseOrder<T> {
    fn clone(&self) -> Self {
        Self {
            _phantom: PhantomData,
        }
    }
}

impl<T> Copy for ReverseOrder<T> {}

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

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