Crates.io | n_best |
lib.rs | n_best |
version | 0.1.0 |
source | src |
created_at | 2023-06-14 09:34:12.713541 |
updated_at | 2023-06-14 09:34:12.713541 |
description | Convenient collection to gather the N highest elements, and discard the others |
homepage | |
repository | https://github.com/luketpeterson/n_best/ |
max_upload_size | |
id | 889953 |
size | 9,403 |
Convenient collection to gather the N highest elements, and discard the others
This can be useful in the implementation of an algorithm like k-nearest-neighbour, where you can build an [NBest] using [Iterator::collect]
use n_best::NBest;
let numbers = vec![9, 2, 4, 6, 8, 1, 3, 5, 7, 0];
let n_best = NBest::with_cmp_fn_and_iter(4, |a, b| b.cmp(a), numbers);
assert_eq!(n_best.into_sorted_vec(), vec![0, 1, 2, 3]);
Future Work: This implementation uses [BinaryHeap], but an internal implementation would be more efficient because it currently needs to store a copy of the compare function for every retained element.
Future Work: Explore making the N value a constant parameter. It might be more efficient, and it would allow the [FromIterator] trait to be implemented. On the other hand type parameters are uglier to work with than arguments passed at runtime.