- For completeness, but also for additional performance when there are extremely large numbers of results, use `par_sort_unstable_by()` for sorting the results. For most sane result sets this will not represent a significant speedup (for the Kubernetes benchmark it's around 1%) but as the set to be sorted grows the impact would be larger.
36 lines
797 B
Rust
36 lines
797 B
Rust
use super::matcher;
|
|
use rayon::prelude::*;
|
|
|
|
pub struct Match {
|
|
pub score: i64,
|
|
pub content: String,
|
|
}
|
|
|
|
pub struct Options {
|
|
pub pattern: String,
|
|
pub minimun_score: i64,
|
|
}
|
|
|
|
impl Options {
|
|
pub fn new(pattern: String) -> Self {
|
|
Self {
|
|
pattern,
|
|
minimun_score: 20,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn sort_strings(options: Options, strings: Vec<String>) -> Vec<Match> {
|
|
let matcher = matcher::Matcher::new(options.pattern);
|
|
|
|
let mut matches = strings
|
|
.into_par_iter()
|
|
.map(|candidate| Match {
|
|
score: matcher.score(candidate.as_str()),
|
|
content: candidate,
|
|
})
|
|
.filter(|m| m.score > 25)
|
|
.collect::<Vec<Match>>();
|
|
matches.par_sort_unstable_by(|a, b| a.score.cmp(&b.score));
|
|
matches
|
|
}
|