Queue

Struct Queue 

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

Source

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());
Source

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>

Source

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());
Source

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);
Source

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));
Source

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));
Source

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));
Source

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);
Source

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());
Source

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

Source§

fn clone(&self) -> Queue<T>

Returns a duplicate 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 Queue<T>
where T: Debug,

Source§

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

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

impl<T> Default for Queue<T>

Source§

fn default() -> Queue<T>

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

impl<T> Extend<T> for Queue<T>

Source§

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)

🔬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> FromIterator<T> for Queue<T>

Source§

fn from_iter<I>(iter: I) -> Queue<T>
where I: IntoIterator<Item = T>,

Builds a Queue from any iterator. Elements are enqueued in iteration order.

§Examples
use graph_collections::Queue;

let queue: Queue<u32> = (1..=3).collect();
assert_eq!(queue.front(), Some(&1));
assert_eq!(queue.len(),   3);
Source§

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

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 alive
Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

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

impl<T> IntoIterator for Queue<T>

Consuming iterator: for x in queue

Source§

fn into_iter(self) -> <Queue<T> as IntoIterator>::IntoIter

Consumes the queue and yields elements front-to-back.

§Examples
use graph_collections::Queue;

let queue: Queue<u32> = (1..=3).collect();
let values: Vec<u32> = queue.into_iter().collect();
assert_eq!(values, [1, 2, 3]);
Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

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

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

Source§

fn eq(&self, other: &Queue<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 for Queue<T>
where T: Eq,

Source§

impl<T> StructuralPartialEq for Queue<T>

Auto Trait Implementations§

§

impl<T> Freeze for Queue<T>

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Queue<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, dest: *mut u8)

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