~ruther/ctu-fee-eoa

ref: c604f7c5c47c53d20995f32a9139d647566f674d ctu-fee-eoa/codes/eoa_lib/src/comparison/mod.rs -rw-r--r-- 827 bytes
c604f7c5 — Rutherther tests(tsp): move tests to proper modules, fix uses a month ago
                                                                                
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
pub trait BetterThanOperator<T> {
    fn better_than(self: &Self, a: &T, b: &T) -> bool;

    fn ordering(&self, a: &T, b: &T) -> std::cmp::Ordering {
        if self.better_than(a, b) {
            std::cmp::Ordering::Less
        } else {
            std::cmp::Ordering::Greater
        }
    }
}

pub struct MinimizingOperator;
impl MinimizingOperator {
    pub fn new() -> Self {
        Self
    }
}

impl<T> BetterThanOperator<T> for MinimizingOperator
    where T: PartialOrd
{
    fn better_than(self: &Self, a: &T, b: &T) -> bool {
        a < b
    }
}

pub struct MaximizingOperator;
impl MaximizingOperator {
    pub fn new() -> Self {
        Self
    }
}

impl<T> BetterThanOperator<T> for MaximizingOperator
    where T: PartialOrd
{
    fn better_than(self: &Self, a: &T, b: &T) -> bool {
        a > b
    }
}