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>

source

pub fn new() -> Self

Creates an empty MedianHeap.

Examples

Basic usage:

let mut heap = MedianHeap::new();
heap.push(4);
source

pub fn with_max_size(max_size: usize) -> Self

Creates an empty MedianHeap which can only grow to max_size.

Panics

Panics if max_size is zero.

Examples

Basic usage:

let mut heap = MedianHeap::with_max_size(42);
heap.push(4);
source

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));
source

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);
source

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);
source

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, or
  • None 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)));
source

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>

source

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, or
  • None 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>

source§

fn clone(&self) -> MedianHeap<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Ord + Debug> Debug for MedianHeap<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Ord> Default for MedianHeap<T>

source§

fn default() -> Self

Creates an empty MedianHeap.

Auto 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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.