Struct medianheap::MedianHeap
source · pub struct MedianHeap<T: Ord> { /* private fields */ }
Expand description
A median heap implemented with two binary heaps.
Implementations§
source§impl<T: Ord> MedianHeap<T>
impl<T: Ord> MedianHeap<T>
sourcepub fn with_max_size(max_size: usize) -> Self
pub fn with_max_size(max_size: usize) -> Self
sourcepub fn max_size(&self) -> Option<usize>
pub fn max_size(&self) -> Option<usize>
Returns the maximum size the median heap can grow to.
Examples
Basic usage:
let heap = MedianHeap::<i32>::with_max_size(42);
assert_eq!(heap.max_size(), Some(42));
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the heap.
Examples
Basic usage:
let mut heap = MedianHeap::new();
assert_eq!(heap.len(), 0);
heap.push(1);
assert_eq!(heap.len(), 1);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if there are no elements on the heap.
Examples
Basic usage:
let mut heap = MedianHeap::<i32>::new();
assert_eq!(heap.is_empty(), true);
sourcepub fn median(&self) -> Option<Median<&T>>
pub fn median(&self) -> Option<Median<&T>>
This either returns
Some(Median::Single(T))
containing the single median value if there are an odd number of elements,Some(Median::Pair(T, T))
containing the two middlemost values if there are an even number of elements, orNone
if the heap is empty.
Examples
Basic usage:
let mut heap = MedianHeap::new();
assert_eq!(heap.median(), None);
heap.push(1);
assert_eq!(heap.median(), Some(Median::Single(&1)));
heap.push(3);
assert_eq!(heap.median(), Some(Median::Pair(&1, &3)));
sourcepub fn push(&mut self, item: T)
pub fn push(&mut self, item: T)
Pushes an item onto the median heap.
When max_size
is set and the heap is full, this will remove
- the smallest item if the pushed item is greater than (
>
) the current median, - the largest item, if the pushed item is less than (
<
) the current median, or - both the smallest and the largest item, if the pushed item is equal (
==
) to the current median.
Examples
Basic usage:
let mut heap = MedianHeap::new();
heap.push(1);
heap.push(2);
heap.push(3);
assert_eq!(heap.len(), 3);
Usage with max_size
:
let mut heap = MedianHeap::with_max_size(2);
heap.push(1);
heap.push(1);
assert_eq!(heap.len(), 2);
When we now push another 1
it will be inserted in the middle, which leads to
both the smallest and largest item being removed in order not to overflow the max_size
.
heap.push(1);
assert_eq!(heap.len(), 1);
source§impl<T: Ord + Clone> MedianHeap<T>
impl<T: Ord + Clone> MedianHeap<T>
sourcepub fn median_with(&self, f: impl FnOnce(&T, &T) -> T) -> Option<T>
pub fn median_with(&self, f: impl FnOnce(&T, &T) -> T) -> Option<T>
This either returns
Some(T)
containing the median value if there are an odd number of elements,Some(T)
containing the arithmetic mean of the two middlemost values if there are an even number of elements, orNone
if the heap is empty.
Examples
Basic usage:
let mut heap = MedianHeap::new();
heap.push(1);
heap.push(3);
assert_eq!(heap.median_with(|l, r| (l + r) / 2), Some(2));
Trait Implementations§
source§impl<T: Clone + Ord> Clone for MedianHeap<T>
impl<T: Clone + Ord> Clone for MedianHeap<T>
source§fn clone(&self) -> MedianHeap<T>
fn clone(&self) -> MedianHeap<T>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl<T> RefUnwindSafe for MedianHeap<T>where
T: RefUnwindSafe,
impl<T> Send for MedianHeap<T>where
T: Send,
impl<T> Sync for MedianHeap<T>where
T: Sync,
impl<T> Unpin for MedianHeap<T>where
T: Unpin,
impl<T> UnwindSafe for MedianHeap<T>where
T: UnwindSafe,
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