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
// Copyright 2019 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

use std::{cmp::Ordering, time::Instant};

pub trait HasKey<Key>
where Key: Clone
{
    fn key(&self) -> Key;
}

/// Items whose priority is based on their creation times.
/// I.e. items created earlier will have higher priority.
/// Example: TimeOrdered(yesterday) > TimeOrdered(now)
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TimeOrdered<K> {
    pub key: K,
    pub since: Instant,
}

impl<K> TimeOrdered<K> {
    pub fn new(key: K) -> Self {
        TimeOrdered {
            key,
            since: Instant::now(),
        }
    }
}

impl<K> HasKey<K> for TimeOrdered<K>
where K: Clone
{
    fn key(&self) -> K { self.key.clone() }
}

impl<K> Ord for TimeOrdered<K>
where K: Eq
{
    fn cmp(&self, other: &Self) -> Ordering {
        self.since.cmp(&other.since).reverse()
    }
}

impl<K> PartialOrd for TimeOrdered<K>
where K: Eq
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// Items whose priority corresponds to their keys' priority.
/// I.e. items with higher key will have higher priority.
/// Example: KeyOrdered(3) > KeyOrdered(2)
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct KeyOrdered<K> {
    pub key: K,
    pub since: Instant,
}

impl<K> KeyOrdered<K> {
    pub fn new(key: K) -> Self {
        KeyOrdered {
            key,
            since: Instant::now(),
        }
    }
}

impl<K> HasKey<K> for KeyOrdered<K>
where K: Clone
{
    fn key(&self) -> K { self.key.clone() }
}

impl<K> Ord for KeyOrdered<K>
where K: Ord
{
    fn cmp(&self, other: &Self) -> Ordering { self.key.cmp(&other.key) }
}

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

/// Items whose priority is the reverse of their keys' priority.
/// I.e. items with lower key will have higher priority.
/// Example: KeyReverseOrdered(2) > KeyReverseOrdered(3)
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct KeyReverseOrdered<K> {
    pub key: K,
    pub since: Instant,
}

impl<K> KeyReverseOrdered<K> {
    pub fn new(key: K) -> Self {
        KeyReverseOrdered {
            key,
            since: Instant::now(),
        }
    }
}

impl<K> HasKey<K> for KeyReverseOrdered<K>
where K: Clone
{
    fn key(&self) -> K { self.key.clone() }
}

impl<K> Ord for KeyReverseOrdered<K>
where K: Ord
{
    fn cmp(&self, other: &Self) -> Ordering {
        self.key.cmp(&other.key).reverse()
    }
}

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