rust_dsa

Struct MedianHeap

Source
pub struct MedianHeap<T> { /* private fields */ }
Expand description

A data structure for efficiently calculating a running median.

§Example

use rust_dsa::{MedianHeap, Median};

// First, we create a new heap.
let mut heap = MedianHeap::new();

// Then we can add values in any order.
heap.insert(4);
heap.insert(1);
heap.insert(3);
heap.insert(6);
heap.insert(2);

// We can get the median value.
assert_eq!(heap.median(), Some(Median::One(&3)));

heap.insert(9);

// If the heap size is even, there are two medians.
assert_eq!(heap.median(), Some(Median::Two(&3, &4)));

// We can pop the median value.
assert_eq!(heap.pop(), Some(3));
assert_eq!(heap.pop(), Some(4));

// And we can add values from an iterator.
heap.extend(3..8);

assert_eq!(heap.median(), Some(Median::One(&5)));
assert_eq!(heap.len(), 9);

§Runtime complexity

OperationRuntime Complexity
MedianHeap::insertO(log n)
MedianHeap::medianO(1)
MedianHeap::popO(log n)

Implementations§

Source§

impl<T> MedianHeap<T>

Source

pub fn new() -> Self
where T: Ord,

Source

pub fn insert(&mut self, value: T)
where T: Ord,

Inserts a new value into the heap.

§Example
use rust_dsa::{Median, MedianHeap};

let mut heap = MedianHeap::new();

heap.insert(5);
heap.insert(2);

assert_eq!(heap.median(), Some(Median::Two(&2, &5)));

heap.insert(3);

assert_eq!(heap.median(), Some(Median::One(&3)));
Source

pub fn pop(&mut self) -> Option<T>
where T: Ord,

Pops the median element from the heap.

If there are two median values (i.e., MedianHeap::median would return Median::Two), then this function returns the smaller of the two values.

§Example
use rust_dsa::MedianHeap;

let mut heap: MedianHeap<_> = "acb".chars().collect();

assert_eq!(heap.pop(), Some('b'));
assert_eq!(heap.pop(), Some('a'));
assert_eq!(heap.pop(), Some('c'));
assert_eq!(heap.pop(), None);
Source

pub fn median(&self) -> Option<Median<'_, T>>

Returns one or two median values in the heap, or None if the heap is empty.

§Example
use rust_dsa::{Median, MedianHeap};

let mut heap = MedianHeap::from([13, 8, 5, 3, 2, 1, 1]);
assert_eq!(heap.median(), Some(Median::One(&3)));

heap.pop();
assert_eq!(heap.median(), Some(Median::Two(&2, &5)));

heap.clear();
assert_eq!(heap.median(), None);
Source

pub fn len(&self) -> usize

Returns the number of elements in the heap.

§Example
use rust_dsa::MedianHeap;

let mut heap: MedianHeap<_> = "abcdefgh".chars().collect();
assert_eq!(heap.len(), 8);

heap.extend("xyz".chars());
assert_eq!(heap.len(), 11);
Source

pub fn is_empty(&self) -> bool

Returns true if the heap is empty.

§Example
use rust_dsa::MedianHeap;

let mut heap = MedianHeap::from([1, 2, 3]);
assert!(!heap.is_empty());

heap.clear();
assert!(heap.is_empty());
Source

pub fn clear(&mut self)

Clears the heap.

§Example
use rust_dsa::MedianHeap;

let mut heap = MedianHeap::from(["foo", "bar", "baz"]);
assert!(!heap.is_empty());

heap.clear();
assert!(heap.is_empty());

Trait Implementations§

Source§

impl<T: Clone> 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> Default for MedianHeap<T>
where T: Ord,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Extend<T> for MedianHeap<T>
where T: Ord,

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T, const N: usize> From<[T; N]> for MedianHeap<T>
where T: Ord,

Source§

fn from(array: [T; N]) -> Self

Converts to this type from the input type.
Source§

impl<T> FromIterator<T> for MedianHeap<T>
where T: Ord,

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<T> Freeze for MedianHeap<T>
where T: Freeze,

§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

Source§

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>,

Source§

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>,

Source§

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.