Struct topset::TopSet

source ·
pub struct TopSet<X, C>where
    C: FnMut(&X, &X) -> bool,
{ /* private fields */ }
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§

Creates a new top set with a selecting closure.

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.

Check if the top set is empty

Get the number of stored items.

It never exceeds the predefined capacity.

Get the capacity of this top set

This capacity could only change by calling [resize].

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.

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.

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.

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

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.

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.

Resize the top set

If the size decreases, then the lowest items are removed. If the size increases, nothing else happens but there is still more room for next insertions.

Pop the lowest item of the top set

Remove and return the lowest item of the top set. After this call, there is one more room for a item.

This method is the only way to get the top elements in a sorted way (from the lowest to the best).

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Converts to this type from the input type.
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.