n_best

Crates.ion_best
lib.rsn_best
version0.1.0
sourcesrc
created_at2023-06-14 09:34:12.713541
updated_at2023-06-14 09:34:12.713541
descriptionConvenient collection to gather the N highest elements, and discard the others
homepage
repositoryhttps://github.com/luketpeterson/n_best/
max_upload_size
id889953
size9,403
(luketpeterson)

documentation

README

NBest

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.

Commit count: 1

cargo fmt