Crates.io | topset |
lib.rs | topset |
version | 0.4.0 |
source | src |
created_at | 2022-05-04 08:57:36.02747 |
updated_at | 2023-08-25 13:33:17.124484 |
description | Top N selector |
homepage | |
repository | https://github.com/XopheD/topset |
max_upload_size | |
id | 580294 |
size | 27,058 |
This crate provides a topset which selects a given number of greatest items. The criterium used to sort the items could be specified as a closure. It is based internally on a binary heap with a fixed size.
The struct TopSet
could be used directly or through the trait TopSetReducing
which automatically extend the iterator trait.
Note: the returned items are unsorted.
use topset::TopIter;
fn main()
{
let items = vec![4, 5, 8, 3, 2, 1, 4, 7, 9, 8];
// getting the four greatest integers (repeating allowed)
items.iter().cloned()
.topset(4, i32::gt)
.into_iter()
.for_each(|x| eprintln!("in the top 4: {}", x));
// getting the four smallest integers
// (we just need to reverse the comparison function)
items.topset(4, i32::lt)
.into_iter()
.for_each(|x| eprintln!("in the last 4: {}", x));
}
will produce (possibly in a different order):
in the top 4: 7
in the top 4: 8
in the top 4: 9
in the top 4: 8
in the last 4: 4
in the last 4: 3
in the last 4: 1
in the last 4: 2