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
extern crate serde;
#[macro_use]
extern crate serde_derive;

use std::cmp::Ordering;

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct Invert<T: Ord>(T);

impl<T: Ord> PartialOrd for Invert<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(other.0.cmp(&self.0))
    }
}

impl<T: Ord> Ord for Invert<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        other.0.cmp(&self.0)
    }
}

impl<T: Ord> Invert<T> {
    pub fn new(t: T) -> Self {
        Invert(t)
    }
    pub fn inner(&self) -> &T {
        &self.0
    }
    pub fn into_inner(self) -> T {
        self.0
    }
}

pub fn invert<T: Ord>(t: T) -> Invert<T> {
    Invert::new(t)
}

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct PartialInvert<T: PartialOrd>(T);

impl<T: PartialOrd> PartialOrd for PartialInvert<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        other.0.partial_cmp(&self.0)
    }
}

impl<T: PartialOrd> PartialInvert<T> {
    pub fn new(t: T) -> Self {
        PartialInvert(t)
    }
    pub fn inner(&self) -> &T {
        &self.0
    }
    pub fn into_inner(self) -> T {
        self.0
    }
}

pub fn partial_invert<T: Ord>(t: T) -> PartialInvert<T> {
    PartialInvert::new(t)
}