Deque

Struct Deque 

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

A double-ended queue (deque) backed by VecDeque.

Provides O(1) push and pop from both ends. Iteration order is front-to-back by default; use .iter().rev() or Deque::iter_back for back-to-front traversal.

§Differences from crate::Queue

crate::Queue is FIFO — elements enter at the back and leave at the front only. Deque relaxes that: you can push and pop from either end independently, making it suitable for sliding-window algorithms, palindrome checks, work-stealing schedulers, and undo/redo stacks.

§Examples

use graph_collections::Deque;

let mut deque: Deque<u32> = Deque::new();

deque.push_back(2);
deque.push_front(1);
deque.push_back(3);

// front → back: [1, 2, 3]
assert_eq!(deque.front(), Some(&1));
assert_eq!(deque.back(),  Some(&3));

assert_eq!(deque.pop_front(), Some(1));
assert_eq!(deque.pop_back(),  Some(3));
assert_eq!(deque.len(), 1);

Implementations§

Source§

impl<T> Deque<T>

Source

pub fn new() -> Deque<T>

Creates a new, empty deque.

§Examples
use graph_collections::Deque;

let deque: Deque<u32> = Deque::new();
assert!(deque.is_empty());
Source

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

Creates a new, empty deque with at least the given capacity pre-allocated, avoiding reallocations for the first capacity pushes.

§Examples
use graph_collections::Deque;

let deque: Deque<u32> = Deque::with_capacity(32);
assert!(deque.is_empty());
Source§

impl<T> Deque<T>

Source

pub fn is_empty(&self) -> bool

Returns true if the deque contains no elements.

§Examples
use graph_collections::Deque;

let mut deque: Deque<u32> = Deque::new();
assert!(deque.is_empty());
deque.push_back(1);
assert!(!deque.is_empty());
Source

pub fn len(&self) -> usize

Returns the number of elements in the deque.

§Examples
use graph_collections::Deque;

let mut deque: Deque<u32> = Deque::new();
assert_eq!(deque.len(), 0);
deque.push_back(1);
deque.push_front(0);
assert_eq!(deque.len(), 2);
Source§

impl<T> Deque<T>

Source

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

Returns a reference to the front element, or None if empty.

Does not remove the element.

§Examples
use graph_collections::Deque;

let mut deque: Deque<u32> = Deque::new();
assert_eq!(deque.front(), None);
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.front(), Some(&1));
assert_eq!(deque.len(), 2); // unchanged
Source

pub fn front_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the front element, or None if empty.

§Examples
use graph_collections::Deque;

let mut deque: Deque<u32> = Deque::new();
deque.push_back(1);
if let Some(v) = deque.front_mut() {
    *v = 99;
}
assert_eq!(deque.front(), Some(&99));
Source

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

Returns a reference to the back element, or None if empty.

Does not remove the element.

§Examples
use graph_collections::Deque;

let mut deque: Deque<u32> = Deque::new();
assert_eq!(deque.back(), None);
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.back(), Some(&2));
assert_eq!(deque.len(), 2); // unchanged
Source

pub fn back_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the back element, or None if empty.

§Examples
use graph_collections::Deque;

let mut deque: Deque<u32> = Deque::new();
deque.push_back(1);
if let Some(v) = deque.back_mut() {
    *v = 42;
}
assert_eq!(deque.back(), Some(&42));
Source

pub fn get(&self, index: usize) -> Option<&T>

Returns a reference to the element at index (front = 0), or None if index is out of bounds.

§Examples
use graph_collections::Deque;

let deque: Deque<u32> = (10..=14).collect();
assert_eq!(deque.get(0), Some(&10));
assert_eq!(deque.get(2), Some(&12));
assert_eq!(deque.get(9), None);
Source§

impl<T> Deque<T>

Source

pub fn push_back(&mut self, element: T)

Appends element to the back of the deque.

§Examples
use graph_collections::Deque;

let mut deque: Deque<u32> = Deque::new();
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.back(), Some(&2));
Source

pub fn push_front(&mut self, element: T)

Prepends element to the front of the deque.

§Examples
use graph_collections::Deque;

let mut deque: Deque<u32> = Deque::new();
deque.push_front(2);
deque.push_front(1);
assert_eq!(deque.front(), Some(&1));
Source§

impl<T> Deque<T>

Source

pub fn pop_front(&mut self) -> Option<T>

Removes and returns the front element, or None if empty.

§Examples
use graph_collections::Deque;

let mut deque: Deque<u32> = Deque::new();
assert_eq!(deque.pop_front(), None);
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.pop_front(), Some(1));
assert_eq!(deque.pop_front(), Some(2));
assert_eq!(deque.pop_front(), None);
Source

pub fn pop_back(&mut self) -> Option<T>

Removes and returns the back element, or None if empty.

§Examples
use graph_collections::Deque;

let mut deque: Deque<u32> = Deque::new();
assert_eq!(deque.pop_back(), None);
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.pop_back(), Some(2));
assert_eq!(deque.pop_back(), Some(1));
assert_eq!(deque.pop_back(), None);
Source

pub fn clear(&mut self)

Removes all elements from the deque.

§Examples
use graph_collections::Deque;

let mut deque: Deque<u32> = (1..=5).collect();
assert_eq!(deque.len(), 5);
deque.clear();
assert!(deque.is_empty());
Source§

impl<T> Deque<T>

Source

pub fn rotate_left(&mut self, n: usize)

Rotates the deque n steps to the left: moves the front n elements to the back.

Equivalent to calling push_back(pop_front()) n times, but O(n) not O(n²).

§Panics

Panics if n > self.len().

§Examples
use graph_collections::Deque;

let mut deque: Deque<u32> = (1..=5).collect();
deque.rotate_left(2);
// was [1,2,3,4,5], now [3,4,5,1,2]
assert_eq!(deque.front(), Some(&3));
assert_eq!(deque.back(),  Some(&2));
Source

pub fn rotate_right(&mut self, n: usize)

Rotates the deque n steps to the right: moves the back n elements to the front.

Equivalent to calling push_front(pop_back()) n times, but O(n) not O(n²).

§Panics

Panics if n > self.len().

§Examples
use graph_collections::Deque;

let mut deque: Deque<u32> = (1..=5).collect();
deque.rotate_right(2);
// was [1,2,3,4,5], now [4,5,1,2,3]
assert_eq!(deque.front(), Some(&4));
assert_eq!(deque.back(),  Some(&3));
Source§

impl<T> Deque<T>

Source

pub fn iter(&self) -> Iter<'_, T>

Returns a front-to-back iterator over references to the elements.

The deque is not modified.

§Examples
use graph_collections::Deque;

let deque: Deque<u32> = (1..=4).collect();
let values: Vec<u32> = deque.iter().copied().collect();
assert_eq!(values, [1, 2, 3, 4]);
Source

pub fn iter_back(&self) -> Rev<Iter<'_, T>>

Returns a back-to-front iterator over references to the elements.

Convenience wrapper around self.iter().rev(). Because std::collections::vec_deque::Iter implements DoubleEndedIterator, reversing is O(1) setup with O(1) per element — no allocation.

§Examples
use graph_collections::Deque;

let deque: Deque<u32> = (1..=4).collect();
let values: Vec<u32> = deque.iter_back().copied().collect();
assert_eq!(values, [4, 3, 2, 1]);

Trait Implementations§

Source§

impl<T> Clone for Deque<T>
where T: Clone,

Source§

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

Source§

fn default() -> Deque<T>

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

impl<T> Extend<T> for Deque<T>

Source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = T>,

Pushes all elements produced by the iterator to the back of the deque.

§Examples
use graph_collections::Deque;

let mut deque: Deque<u32> = Deque::new();
deque.push_back(1);
deque.extend([2, 3, 4]);
assert_eq!(deque.len(),  4);
assert_eq!(deque.back(), Some(&4));
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 Deque<T>

Source§

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

Builds a Deque from any iterator. Elements are pushed to the back in iteration order — front of the iterator becomes front of the deque.

§Examples
use graph_collections::Deque;

let deque: Deque<u32> = (1..=4).collect();
assert_eq!(deque.front(), Some(&1));
assert_eq!(deque.back(),  Some(&4));
assert_eq!(deque.len(),   4);
Source§

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

Borrowing iterator: for x in &deque

Source§

fn into_iter(self) -> <&'a Deque<T> as IntoIterator>::IntoIter

Iterates over references to elements front-to-back without consuming the deque.

§Examples
use graph_collections::Deque;

let deque: Deque<u32> = (1..=3).collect();
let sum: u32 = (&deque).into_iter().sum();
assert_eq!(sum, 6);
assert_eq!(deque.len(), 3); // deque 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 Deque<T>

Consuming iterator: for x in deque

Source§

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

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

§Examples
use graph_collections::Deque;

let deque: Deque<u32> = (1..=3).collect();
let values: Vec<u32> = deque.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 Deque<T>
where T: PartialEq,

Source§

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

Source§

impl<T> StructuralPartialEq for Deque<T>

Auto Trait Implementations§

§

impl<T> Freeze for Deque<T>

§

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

§

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

§

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

§

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

§

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