rust_dsa

Struct IntervalHeap

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

An interval heap implementation for use as a double-ended priority queue.

§Example

use rust_dsa::IntervalHeap;

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

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

// We can peek at the min and max values.
assert_eq!(heap.peek_min(), Some(&1));
assert_eq!(heap.peek_max(), Some(&6));

// And pop them off from both ends.
assert_eq!(heap.pop_min(), Some(1));
assert_eq!(heap.pop_min(), Some(3));
assert_eq!(heap.pop_max(), Some(6));
assert_eq!(heap.pop_min(), Some(4));
assert_eq!(heap.pop_min(), None);

// We can also create heaps from arrays.
let mut heap = IntervalHeap::from([2, 6, 2]);

// And heaps can contain duplicate items.
assert_eq!(heap.pop_min(), Some(2));
assert_eq!(heap.pop_min(), Some(2));

assert_eq!(heap.len(), 1);

§Runtime complexity

Implementations§

Source§

impl<T> IntervalHeap<T>

Source

pub fn new() -> IntervalHeap<T>

Creates an empty heap.

Source

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

Inserts a value into the heap.

§Example
use rust_dsa::IntervalHeap;

let mut heap = IntervalHeap::new();

heap.insert('h');
heap.insert('n');

assert_eq!(heap.peek_min(), Some(&'h'));
assert_eq!(heap.peek_max(), Some(&'n'));
Source

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

Removes and returns the smallest value in the heap, or None if the heap is empty.

§Example
use rust_dsa::IntervalHeap;

let mut heap = IntervalHeap::from([4, 3, 6]);

assert_eq!(heap.pop_min(), Some(3));
assert_eq!(heap.pop_min(), Some(4));
assert_eq!(heap.pop_min(), Some(6));
assert_eq!(heap.pop_min(), None);
Source

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

Removes and returns the largest value in the heap, or None if the heap is empty.

§Example
use rust_dsa::IntervalHeap;

let mut heap = IntervalHeap::from([4, 3, 6]);

assert_eq!(heap.pop_max(), Some(6));
assert_eq!(heap.pop_max(), Some(4));
assert_eq!(heap.pop_max(), Some(3));
assert_eq!(heap.pop_max(), None);
Source

pub fn peek_min(&self) -> Option<&T>

Returns a reference to the smallest value in the heap, or None if the heap is empty.

§Example
use rust_dsa::IntervalHeap;

let mut heap = IntervalHeap::from(['a', 'b', 'c']);

assert_eq!(heap.peek_min(), Some(&'a'));

heap.clear();

assert_eq!(heap.peek_min(), None);
Source

pub fn peek_max(&self) -> Option<&T>

Returns a reference to the largest value in the heap, or None if the heap is empty.

§Example
use rust_dsa::IntervalHeap;

let mut heap = IntervalHeap::from(['a', 'b', 'c']);

assert_eq!(heap.peek_max(), Some(&'c'));

heap.clear();

assert_eq!(heap.peek_min(), None);
Source

pub fn len(&self) -> usize

Returns the number of elements in the heap.

§Example
use rust_dsa::IntervalHeap;

let mut heap: IntervalHeap<_> = (0..42).collect();

assert_eq!(heap.len(), 42);

heap.pop_max();

assert_eq!(heap.len(), 41);

heap.clear();

assert_eq!(heap.len(), 0);
Source

pub fn is_empty(&self) -> bool

Returns true if the heap is empty.

§Example
use rust_dsa::IntervalHeap;

let mut heap: IntervalHeap<_> = ('a'..='z').collect();

assert!(!heap.is_empty());

heap.clear();

assert!(heap.is_empty());
Source

pub fn clear(&mut self)

Clears the heap.

§Example
use rust_dsa::IntervalHeap;

let mut heap: IntervalHeap<_> = ('a'..='z').collect();

assert!(!heap.is_empty());

heap.clear();

assert!(heap.is_empty());

Trait Implementations§

Source§

impl<T: Clone> Clone for IntervalHeap<T>

Source§

fn clone(&self) -> IntervalHeap<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 IntervalHeap<T>

Source§

fn default() -> IntervalHeap<T>

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

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

Source§

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

use rust_dsa::IntervalHeap;

let random: Vec<i32> = (0..10_000).map(|_| rand::random()).collect();

let mut sorted = random.clone();
sorted.sort();
let mut iter = sorted.into_iter();

let mut heap: IntervalHeap<_> = random.into_iter().collect();

for _ in 0..10_001 {
    if rand::random() {
        assert_eq!(heap.pop_min(), iter.next());
    } else {
        assert_eq!(heap.pop_max(), iter.next_back());
    }
}
Source§

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

Source§

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

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<T> Freeze for IntervalHeap<T>

§

impl<T> RefUnwindSafe for IntervalHeap<T>
where T: RefUnwindSafe,

§

impl<T> Send for IntervalHeap<T>
where T: Send,

§

impl<T> Sync for IntervalHeap<T>
where T: Sync,

§

impl<T> Unpin for IntervalHeap<T>
where T: Unpin,

§

impl<T> UnwindSafe for IntervalHeap<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.