rust_dsa

Struct MinQueue

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

A FIFO queue that supports O(1) push, pop and find-minimum.

Keith Schwarz explains how this works here.

§Example

use rust_dsa::MinQueue;

// First, we create a new queue.
let mut queue = MinQueue::new();

// We can push elements.
queue.push(1);
queue.push(6);
queue.push(2);
queue.push(3);

// We can get the minimum element.
assert_eq!(queue.get_min(), Some(&1));

// We can peek and poll as usual.
assert_eq!(queue.peek(), Some(&1));
assert_eq!(queue.poll(), Some(1));

// The min element reflects the queue's new state.
assert_eq!(queue.get_min(), Some(&2));

// We can iterate over the queue.
for x in queue {
    // Prints 6, 2 and 3.
    println!("{x}");
}

// We can also create queues from arrays.
let a = MinQueue::from(['q', 'u', 'e', 'u', 'e']);

// And iterators.
let b: MinQueue<_> = "queue".chars().collect();

assert!(a == b);

Implementations§

Source§

impl<T> MinQueue<T>

Source

pub fn new() -> MinQueue<T>

Creates an empty queue.

Source

pub fn with_capacity(capacity: usize) -> MinQueue<T>

Creates an empty queue with the specified capacity.

Source

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

Pushes an element into the queue.

§Example
use rust_dsa::MinQueue;

let mut queue = MinQueue::new();
queue.push(5);

assert_eq!(queue.poll(), Some(5));
assert_eq!(queue.poll(), None);
Source

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

Removes the next element in the queue and returns it or None if the queue is empty.

§Example
use rust_dsa::MinQueue;

let mut queue = MinQueue::from([5]);

assert_eq!(queue.poll(), Some(5));
assert_eq!(queue.poll(), None);
Source

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

Returns a reference the next element in the queue or None if the queue is queue.

§Example
use rust_dsa::MinQueue;

let mut queue = MinQueue::from(['a']);

assert_eq!(queue.peek(), Some(&'a'));

queue.poll();

assert_eq!(queue.peek(), None);
Source

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

Returns the smallest element in the queue or None if the queue is empty.

§Example
use rust_dsa::MinQueue;

let mut queue = MinQueue::from([1, 5, 3, 4, 8, 2, 6]);

assert_eq!(queue.get_min(), Some(&1));

queue.poll();

assert_eq!(queue.get_min(), Some(&2));

queue.clear();

assert_eq!(queue.get_min(), None);
Source

pub fn len(&self) -> usize

Returns the number of elements in the queue.

§Example
use rust_dsa::MinQueue;

let mut queue = MinQueue::from([1, 5, 3, 4, 8, 2, 6]);

assert_eq!(queue.len(), 7);

queue.clear();

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

pub fn is_empty(&self) -> bool

Returns true if the queue is empty.

§Example
use rust_dsa::MinQueue;

let mut queue = MinQueue::from([1, 5, 3, 4, 8, 2, 6]);

assert!(!queue.is_empty());

queue.clear();

assert!(queue.is_empty());
Source

pub fn clear(&mut self)

Clears the queue.

§Example
use rust_dsa::MinQueue;

let mut queue = MinQueue::from([5, 3, 4, 8, 2, 6, 1]);

assert!(!queue.is_empty());

queue.clear();

assert!(queue.is_empty());

Trait Implementations§

Source§

impl<T: Clone> Clone for MinQueue<T>

Source§

fn clone(&self) -> MinQueue<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> Debug for MinQueue<T>
where T: Debug,

Source§

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

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

impl<T> Default for MinQueue<T>

Source§

fn default() -> MinQueue<T>

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

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Creates a value from an iterator. Read more
Source§

impl<'a, T> IntoIterator for &'a MinQueue<T>

Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for MinQueue<T>

Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

type Item = T

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> PartialEq for MinQueue<T>
where T: PartialEq,

Source§

fn eq(&self, other: &MinQueue<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq> Eq for MinQueue<T>

Auto Trait Implementations§

§

impl<T> Freeze for MinQueue<T>

§

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

§

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

§

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

§

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

§

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