pub struct TopKHeap<T, F>{ /* private fields */ }Expand description
A bounded heap for streaming top-K selection
This maintains the K smallest (or largest) elements seen so far, without storing all N elements.
§Complexity
- Push: O(log K) when heap is full, O(log K) insertion
- Drain: O(K log K) to produce sorted output
- Space: O(K)
Implementations§
Source§impl<T, F> TopKHeap<T, F>
impl<T, F> TopKHeap<T, F>
Sourcepub fn new(k: usize, comparator: F, want_smallest: bool) -> Self
pub fn new(k: usize, comparator: F, want_smallest: bool) -> Self
Create a new top-K heap
k: Number of elements to keepcomparator: Defines the desired output order (Less = should come first)want_smallest: If true, keep the K elements that compare as smallest
Sourcepub fn threshold(&self) -> Option<&T>
pub fn threshold(&self) -> Option<&T>
Get the current boundary value
This is the value that new elements must beat to be included.
Sourcepub fn into_sorted_vec(self) -> Vec<T>
pub fn into_sorted_vec(self) -> Vec<T>
Drain the heap into a sorted vector
Complexity: O(K log K)
Auto Trait Implementations§
impl<T, F> Freeze for TopKHeap<T, F>where
F: Freeze,
impl<T, F> RefUnwindSafe for TopKHeap<T, F>where
F: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, F> Send for TopKHeap<T, F>
impl<T, F> Sync for TopKHeap<T, F>
impl<T, F> Unpin for TopKHeap<T, F>
impl<T, F> UnsafeUnpin for TopKHeap<T, F>where
F: UnsafeUnpin,
impl<T, F> UnwindSafe for TopKHeap<T, F>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more