pub struct Queue<T> { /* private fields */ }Expand description
A FIFO queue backed by VecDeque.
Provides O(1) enqueue and dequeue operations. Iteration order is front-to-back (i.e. the next element to be dequeued comes first).
§Examples
use graph_collections::Queue;
let mut queue: Queue<u32> = Queue::new();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
assert_eq!(queue.front(), Some(&1));
assert_eq!(queue.dequeue(), Some(1));
assert_eq!(queue.len(), 2);Implementations§
Source§impl<T> Queue<T>
impl<T> Queue<T>
Sourcepub fn new() -> Queue<T>
pub fn new() -> Queue<T>
Creates a new, empty queue.
§Examples
use graph_collections::Queue;
let queue: Queue<u32> = Queue::new();
assert!(queue.is_empty());Sourcepub fn with_capacity(capacity: usize) -> Queue<T>
pub fn with_capacity(capacity: usize) -> Queue<T>
Creates a new, empty queue with at least the given capacity
pre-allocated, avoiding reallocations for the first capacity pushes.
§Examples
use graph_collections::Queue;
let mut queue: Queue<u32> = Queue::with_capacity(16);
assert!(queue.is_empty());Source§impl<T> Queue<T>
impl<T> Queue<T>
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the queue contains no elements.
§Examples
use graph_collections::Queue;
let mut queue: Queue<u32> = Queue::new();
assert!(queue.is_empty());
queue.enqueue(1);
assert!(!queue.is_empty());Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the queue.
§Examples
use graph_collections::Queue;
let mut queue: Queue<u32> = Queue::new();
assert_eq!(queue.len(), 0);
queue.enqueue(42);
assert_eq!(queue.len(), 1);Sourcepub fn front(&self) -> Option<&T>
pub fn front(&self) -> Option<&T>
Returns a reference to the element at the front of the queue
(the next one to be dequeued), or None if the queue is empty.
§Examples
use graph_collections::Queue;
let mut queue: Queue<u32> = Queue::new();
assert_eq!(queue.front(), None);
queue.enqueue(1);
queue.enqueue(2);
assert_eq!(queue.front(), Some(&1));Sourcepub fn back(&self) -> Option<&T>
pub fn back(&self) -> Option<&T>
Returns a reference to the element at the back of the queue
(the most recently enqueued one), or None if the queue is empty.
§Examples
use graph_collections::Queue;
let mut queue: Queue<u32> = Queue::new();
assert_eq!(queue.back(), None);
queue.enqueue(1);
queue.enqueue(2);
assert_eq!(queue.back(), Some(&2));Sourcepub fn enqueue(&mut self, element: T)
pub fn enqueue(&mut self, element: T)
Adds element to the back of the queue.
§Examples
use graph_collections::Queue;
let mut queue: Queue<u32> = Queue::new();
queue.enqueue(1);
queue.enqueue(2);
assert_eq!(queue.front(), Some(&1));
assert_eq!(queue.back(), Some(&2));Sourcepub fn dequeue(&mut self) -> Option<T>
pub fn dequeue(&mut self) -> Option<T>
Removes and returns the element at the front of the queue,
or None if the queue is empty.
§Examples
use graph_collections::Queue;
let mut queue: Queue<u32> = Queue::new();
assert_eq!(queue.dequeue(), None);
queue.enqueue(1);
queue.enqueue(2);
assert_eq!(queue.dequeue(), Some(1));
assert_eq!(queue.dequeue(), Some(2));
assert_eq!(queue.dequeue(), None);Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all elements from the queue.
§Examples
use graph_collections::Queue;
let mut queue: Queue<u32> = Queue::new();
queue.enqueue(1);
queue.enqueue(2);
queue.clear();
assert!(queue.is_empty());Sourcepub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
Returns a front-to-back iterator over references to the elements.
The queue is not modified.
§Examples
use graph_collections::Queue;
let mut queue: Queue<u32> = Queue::new();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
let values: Vec<u32> = queue.iter().copied().collect();
assert_eq!(values, [1, 2, 3]);Trait Implementations§
Source§impl<T> Extend<T> for Queue<T>
impl<T> Extend<T> for Queue<T>
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
Enqueues all elements produced by the iterator.
§Examples
use graph_collections::Queue;
let mut queue: Queue<u32> = Queue::new();
queue.extend([1, 2, 3]);
assert_eq!(queue.len(), 3);Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<T> FromIterator<T> for Queue<T>
impl<T> FromIterator<T> for Queue<T>
Source§impl<'a, T> IntoIterator for &'a Queue<T>
Borrowing iterator: for x in &queue
impl<'a, T> IntoIterator for &'a Queue<T>
Borrowing iterator: for x in &queue
Source§fn into_iter(self) -> <&'a Queue<T> as IntoIterator>::IntoIter
fn into_iter(self) -> <&'a Queue<T> as IntoIterator>::IntoIter
Iterates over references to elements front-to-back without consuming the queue.
§Examples
use graph_collections::Queue;
let queue: Queue<u32> = (1..=3).collect();
let sum: u32 = (&queue).into_iter().sum();
assert_eq!(sum, 6);
assert_eq!(queue.len(), 3); // queue is still aliveSource§impl<T> IntoIterator for Queue<T>
Consuming iterator: for x in queue
impl<T> IntoIterator for Queue<T>
Consuming iterator: for x in queue