forked from xai-org/x-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselector.rs
More file actions
45 lines (39 loc) · 1.26 KB
/
selector.rs
File metadata and controls
45 lines (39 loc) · 1.26 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
use crate::util;
use std::any::type_name_of_val;
pub trait Selector<Q, C>: Send + Sync
where
Q: Clone + Send + Sync + 'static,
C: Clone + Send + Sync + 'static,
{
/// Default selection: sort and truncate based on provided configs
fn select(&self, _query: &Q, candidates: Vec<C>) -> Vec<C> {
let mut sorted = self.sort(candidates);
if let Some(limit) = self.size() {
sorted.truncate(limit);
}
sorted
}
/// Decide if this selector should run for the given query
fn enable(&self, _query: &Q) -> bool {
true
}
/// Extract the score from a candidate to use for sorting.
fn score(&self, candidate: &C) -> f64;
/// Sort candidates by their scores in descending order.
fn sort(&self, candidates: Vec<C>) -> Vec<C> {
let mut sorted = candidates;
sorted.sort_by(|a, b| {
self.score(b)
.partial_cmp(&self.score(a))
.unwrap_or(std::cmp::Ordering::Equal)
});
sorted
}
/// Optionally provide a size to select. Defaults to no truncation if not overridden.
fn size(&self) -> Option<usize> {
None
}
fn name(&self) -> &'static str {
util::short_type_name(type_name_of_val(self))
}
}