Trait trait_based_collection::queue::DequeCollection
source · [−]pub trait DequeCollection<'a, T>: Collection<'a, T>where
T: 'a,{
fn push_front(&mut self, value: T);
fn pop_back(&mut self) -> Option<T>;
fn push_back(&mut self, value: T) { ... }
fn pop_front(&mut self) -> Option<T> { ... }
fn peek_front(&'a self) -> Option<Self::ItemRef> { ... }
fn peek_front_mut(&'a mut self) -> Option<Self::ItemMut> { ... }
fn peek_back(&'a self) -> Option<Self::ItemRef> { ... }
fn peek_back_mut(&'a mut self) -> Option<Self::ItemMut> { ... }
}Expand description
Trait that encapsulates the common functionality of a double-ended queue. This means that the
queue can be used as both a LIFO and FIFO queue. This trait is a sub-trait of the
Collection trait, allowing to implement some default methods associated with a queue
collection (push_back, pop_front, peek_front, peek_front_mut, peek_back,
peek_back_mut).
Examples
A simple example of creating a DequeCollection by using a wrapper around the Vec data
structure with the minimum amount of code (by using the default implementation of the
different methods):
#[derive(All)]
struct MyCollection<T> {
data: Vec<T>,
}
impl<'a, T: 'a> DequeCollection<'a, T> for MyCollection<T> {
fn push_front(&mut self, value: T) {
self.data.insert(0, value);
}
fn pop_back(&mut self) -> Option<T> {
self.data.pop()
}
}Required Methods
sourcefn push_front(&mut self, value: T)
fn push_front(&mut self, value: T)
sourcefn pop_back(&mut self) -> Option<T>
fn pop_back(&mut self) -> Option<T>
Removes the element from the back of the queue.
Examples
Example using CircularDeque:
use trait_based_collection::{prelude::*, CircularDeque};
let mut deque: CircularDeque<u32> = CircularDeque::default();
deque.push_front(1);
deque.push_front(2);
assert_eq!(deque.pop_back(), Some(1));
assert_eq!(deque.pop_back(), Some(2));
assert_eq!(deque.pop_back(), None);Provided Methods
sourcefn push_back(&mut self, value: T)
fn push_back(&mut self, value: T)
Pushes a new element to the back of the queue. This is the same as add.
Examples
Example using CircularDeque:
use trait_based_collection::{prelude::*, CircularDeque};
let mut deque: CircularDeque<u32> = CircularDeque::default();
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.peek_front(), Some(&1));
assert_eq!(deque.peek_back(), Some(&2));sourcefn pop_front(&mut self) -> Option<T>
fn pop_front(&mut self) -> Option<T>
Removes the element from the front of the queue. This is the same as remove.
Examples
Example using Deque:
use trait_based_collection::{prelude::*, Deque};
let mut deque: Deque<u32> = Deque::default();
deque.push_front(1);
deque.push_front(2);
assert_eq!(deque.pop_front(), Some(2));
assert_eq!(deque.pop_front(), Some(1));
assert_eq!(deque.pop_front(), None);sourcefn peek_front(&'a self) -> Option<Self::ItemRef>
fn peek_front(&'a self) -> Option<Self::ItemRef>
Returns a reference to the element at the front of the queue. This is the same as peek.
Examples
Example using Deque:
use trait_based_collection::{prelude::*, Deque};
let mut deque: Deque<u32> = Deque::default();
assert_eq!(deque.peek_front(), None);
deque.push_front(1);
assert_eq!(deque.peek_front(), Some(&1));
deque.push_front(2);
assert_eq!(deque.peek_front(), Some(&2));sourcefn peek_front_mut(&'a mut self) -> Option<Self::ItemMut>
fn peek_front_mut(&'a mut self) -> Option<Self::ItemMut>
Returns a mutable reference to the element at the front of the queue. This is the same as
peek_mut.
Examples
Example using CircularDeque:
use trait_based_collection::{prelude::*, CircularDeque};
let mut deque: CircularDeque<u32> = CircularDeque::default();
assert_eq!(deque.peek_front_mut(), None);
deque.push_front(1);
assert_eq!(deque.peek_front_mut(), Some(&mut 1));
deque.push_front(2);
assert_eq!(deque.peek_front_mut(), Some(&mut 2));sourcefn peek_back(&'a self) -> Option<Self::ItemRef>
fn peek_back(&'a self) -> Option<Self::ItemRef>
Returns a reference to the element at the back of the queue. This is the same as get
with the index self.len() - 1.
Examples
Example using Deque:
use trait_based_collection::{prelude::*, Deque};
let mut deque: Deque<u32> = Deque::default();
assert_eq!(deque.peek_back(), None);
deque.push_front(1);
assert_eq!(deque.peek_back(), Some(&1));
deque.push_front(2);
assert_eq!(deque.peek_back(), Some(&1));sourcefn peek_back_mut(&'a mut self) -> Option<Self::ItemMut>
fn peek_back_mut(&'a mut self) -> Option<Self::ItemMut>
Returns a mutable reference to the element at the back of the queue. This is the same as
get_mut with the index self.len() - 1.
Examples
use trait_based_collection::{prelude::*, CircularDeque};
let mut deque: CircularDeque<u32> = CircularDeque::default();
assert_eq!(deque.peek_back_mut(), None);
deque.push_front(1);
assert_eq!(deque.peek_back_mut(), Some(&mut 1));
deque.push_front(2);
assert_eq!(deque.peek_back_mut(), Some(&mut 1));