feat: Improve the readability of the ranking function

This commit is contained in:
Kerollmops 2018-08-22 21:12:13 +02:00 committed by Clément Renault
parent 2e0193a39e
commit 0b02e31ce9

View File

@ -78,12 +78,6 @@ impl Pool {
}
}
fn invert_sorts<F>(a: &Document, b: &Document, sorts: &[F]) -> bool
where F: Fn(&Document, &Document) -> Ordering,
{
sorts.iter().rev().all(|sort| sort(a, b) == Ordering::Equal)
}
impl IntoIterator for Pool {
type Item = Document;
type IntoIter = vec::IntoIter<Self::Item>;
@ -98,16 +92,23 @@ impl IntoIterator for Pool {
exact,
];
for (i, sort) in sorts.iter().enumerate() {
{
let mut groups = vec![self.documents.as_mut_slice()];
for sort in sorts {
let mut temp = mem::replace(&mut groups, Vec::new());
let mut computed = 0;
for group in GroupByMut::new(&mut self.documents, |a, b| invert_sorts(a, b, &sorts[..i])) {
// TODO prefer using `sort_unstable_by_key` to allow reusing the key computation
// `number of words` needs to be reversed, we can use the `cmp::Reverse` struct to do that
for group in temp {
group.sort_unstable_by(sort);
for group in GroupByMut::new(group, |a, b| sort(a, b) == Ordering::Equal) {
computed += group.len();
groups.push(group);
if computed >= self.limit { break }
}
}
}
}
self.documents.truncate(self.limit);
self.documents.into_iter()