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>
impl<T> MinQueue<T>
Sourcepub fn with_capacity(capacity: usize) -> MinQueue<T>
pub fn with_capacity(capacity: usize) -> MinQueue<T>
Creates an empty queue with the specified capacity.
Sourcepub fn push(&mut self, value: T)where
T: Ord,
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);Sourcepub fn poll(&mut self) -> Option<T>where
T: Ord,
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);Sourcepub fn peek(&self) -> Option<&T>
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);Sourcepub fn get_min(&mut self) -> Option<&T>where
T: Ord,
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);Sourcepub fn len(&self) -> usize
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);Trait Implementations§
Source§impl<T> FromIterator<T> for MinQueue<T>where
T: Ord,
impl<T> FromIterator<T> for MinQueue<T>where
T: Ord,
Source§impl<'a, T> IntoIterator for &'a MinQueue<T>
impl<'a, T> IntoIterator for &'a MinQueue<T>
Source§impl<T> IntoIterator for MinQueue<T>
impl<T> IntoIterator for MinQueue<T>
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> 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