forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathranking.rs
More file actions
146 lines (124 loc) · 3.47 KB
/
ranking.rs
File metadata and controls
146 lines (124 loc) · 3.47 KB
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
use std::cmp::Reverse;
use std::collections::BinaryHeap;
struct TopKItem<T> {
score: f32,
ordinal: usize,
value: T,
}
impl<T> PartialEq for TopKItem<T> {
fn eq(&self, other: &Self) -> bool {
self.score.to_bits() == other.score.to_bits() && self.ordinal == other.ordinal
}
}
impl<T> Eq for TopKItem<T> {}
impl<T> PartialOrd for TopKItem<T> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<T> Ord for TopKItem<T> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.score
.total_cmp(&other.score)
.then_with(|| self.ordinal.cmp(&other.ordinal))
}
}
pub(super) fn top_k_by_score<T, I>(items: I, limit: usize) -> Vec<(T, f32)>
where
I: IntoIterator<Item = (T, f32)>,
{
if limit == 0 {
return Vec::new();
}
let mut heap: BinaryHeap<Reverse<TopKItem<T>>> = BinaryHeap::new();
for (ordinal, (value, score)) in items.into_iter().enumerate() {
let candidate = Reverse(TopKItem {
score,
ordinal,
value,
});
if heap.len() < limit {
heap.push(candidate);
continue;
}
let replace = heap
.peek()
.map(|smallest| score > smallest.0.score)
.unwrap_or(false);
if replace {
heap.pop();
heap.push(candidate);
}
}
let mut results: Vec<_> = heap
.into_iter()
.map(|Reverse(item)| (item.value, item.score, item.ordinal))
.collect();
results.sort_by(|a, b| b.1.total_cmp(&a.1).then_with(|| a.2.cmp(&b.2)));
results
.into_iter()
.map(|(value, score, _)| (value, score))
.collect()
}
#[derive(Debug)]
struct TopKOrdItem<T, K> {
key: K,
ordinal: usize,
value: T,
}
impl<T, K: Ord> PartialEq for TopKOrdItem<T, K> {
fn eq(&self, other: &Self) -> bool {
self.key == other.key && self.ordinal == other.ordinal
}
}
impl<T, K: Ord> Eq for TopKOrdItem<T, K> {}
impl<T, K: Ord> PartialOrd for TopKOrdItem<T, K> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<T, K: Ord> Ord for TopKOrdItem<T, K> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.key
.cmp(&other.key)
.then_with(|| self.ordinal.cmp(&other.ordinal))
}
}
pub(super) fn top_k_by_ord<T, K, I>(items: I, limit: usize) -> Vec<(T, K)>
where
I: IntoIterator<Item = (T, K)>,
K: Ord,
{
if limit == 0 {
return Vec::new();
}
let mut heap: BinaryHeap<Reverse<TopKOrdItem<T, K>>> = BinaryHeap::new();
for (ordinal, (value, key)) in items.into_iter().enumerate() {
let candidate = Reverse(TopKOrdItem {
key,
ordinal,
value,
});
if heap.len() < limit {
heap.push(candidate);
continue;
}
let replace = heap
.peek()
.map(|smallest| candidate.0.key > smallest.0.key)
.unwrap_or(false);
if replace {
heap.pop();
heap.push(candidate);
}
}
let mut results: Vec<_> = heap
.into_iter()
.map(|Reverse(item)| (item.value, item.key, item.ordinal))
.collect();
results.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.2.cmp(&b.2)));
results
.into_iter()
.map(|(value, key, _)| (value, key))
.collect()
}