Only sort in CoverTree::find function if there are more than k points

Sorting only needs to be done if the list of KNN candidates is greater
than length k.
This commit is contained in:
Kiran Eiden
2022-01-04 14:50:47 -08:00
parent f93286ffbd
commit 389b0e8e67
+3 -1
View File
@@ -180,7 +180,9 @@ impl<T: Debug + PartialEq, F: RealNumber, D: Distance<T, F>> CoverTree<T, F, D>
}
}
neighbors.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
if neighbors.len() > k {
neighbors.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
}
Ok(neighbors.into_iter().take(k).collect())
}