2 unstable releases

0.4.0 Aug 25, 2023
0.3.10 Dec 19, 2022
0.2.2 May 12, 2022
0.1.6 May 4, 2022

#1617 in Algorithms

Download history 71/week @ 2024-03-21 40/week @ 2024-03-28 103/week @ 2024-04-04 190/week @ 2024-04-11 9/week @ 2024-04-18 19/week @ 2024-04-25 25/week @ 2024-05-02 12/week @ 2024-05-09 83/week @ 2024-05-16 2/week @ 2024-05-23 18/week @ 2024-05-30 77/week @ 2024-06-06 12/week @ 2024-06-13 17/week @ 2024-06-20 17/week @ 2024-06-27 58/week @ 2024-07-04

110 downloads per month

MIT license

25KB
321 lines

Top N Set

Crates.io Crates.io License Docs

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

No runtime deps