Expand description
A top N set of items.
This set contains no more than N items. When this limit is reached, the smallest (according to the specified comparison) is thrown.
Comparing two elements is done by a duel, resolved by a provided closure:
if true is returned, the first item wins, if false the second.
By the way, using PartialOrd::gt
will select the top elements and PartialOrd::lt
will select the lowest.
Of course, any closure could be used but it should satisfy the transitivity.
In other words, if a beats b and b beats c then a should beat c too.
If it is not the case, the results are unpredictable.
Implementations§
source§impl<X, C> TopSet<X, C>where
C: FnMut(&X, &X) -> bool,
impl<X, C> TopSet<X, C>where
C: FnMut(&X, &X) -> bool,
sourcepub fn with_init<I: IntoIterator<Item = X>>(n: usize, init: I, beat: C) -> Self
pub fn with_init<I: IntoIterator<Item = X>>(n: usize, init: I, beat: C) -> Self
Creates a new top set with a selecting closure and an initial set of items.
If the initial set contains more than n elements, only the n greatest ones
(according to beat selector) are stored.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the number of stored items.
It never exceeds the predefined capacity.
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Get the capacity of this top set
This capacity could only change by calling [resize].
sourcepub fn peek(&self) -> Option<&X>
pub fn peek(&self) -> Option<&X>
Read access to the lowest item of the top set
Notice that it actually returned the lowest one and so all the others are better (or equal) this one.
sourcepub fn iter(&self) -> impl Iterator<Item = &X>
pub fn iter(&self) -> impl Iterator<Item = &X>
Iterate over all the top selected items.
The iterator is not sorted. A sorted iteration
could be obtained by iterative call to Self::pop
or by using Self::into_iter_sorted.
sourcepub fn into_vec(self) -> Vec<X>
pub fn into_vec(self) -> Vec<X>
Gets all the top set elements in a vector.
This vector is not sorted.
See Self::into_sorted_vec if a sorted result is expected.
sourcepub fn insert(&mut self, x: X) -> Option<X>
pub fn insert(&mut self, x: X) -> Option<X>
Insert a new item.
If the top set is not filled, the item is simply added and None is returned.
If there is no more room, then one item should be rejected:
- if the new item is better than some already stored ones, it is added and the removed item is returned
- if the new item is worse than all the stored ones, it is returned
sourcepub fn into_iter_sorted(self) -> IntoIterSorted<X, C> ⓘ
pub fn into_iter_sorted(self) -> IntoIterSorted<X, C> ⓘ
Converts this topset into a sorted iterator
Notice that the lowest item of the top set is the first one. The greatest item is the last one.
sourcepub fn into_sorted_vec(self) -> Vec<X>where
X: PartialEq,
pub fn into_sorted_vec(self) -> Vec<X>where
X: PartialEq,
Returns the topset in a sorted vector.
The first element of the vector is the lowest item of the top set and the last one is the greatest one.
Trait Implementations§
source§impl<X, C> Extend<X> for TopSet<X, C>where
C: FnMut(&X, &X) -> bool,
impl<X, C> Extend<X> for TopSet<X, C>where
C: FnMut(&X, &X) -> bool,
source§fn extend<T: IntoIterator<Item = X>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = X>>(&mut self, iter: T)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)