Struct arraydeque::ArrayDeque
[−]
[src]
pub struct ArrayDeque<A: Array> { /* fields omitted */ }
A fixed capacity ring buffer.
It can be stored directly on the stack if needed.
The "default" usage of this type as a queue is to use push_back
to add to
the queue, and pop_front
to remove from the queue. extend
and append
push onto the back in this manner, and iterating over ArrayDeque
goes front
to back.
Capacity
Note that the capacity()
is always backed_array.len() - 1
.
Read more
Methods
impl<A: Array> ArrayDeque<A>
[src]
fn new() -> ArrayDeque<A>
Creates an empty ArrayDeque
.
Examples
use arraydeque::ArrayDeque; let vector: ArrayDeque<[usize; 3]> = ArrayDeque::new();
fn get(&self, index: usize) -> Option<&A::Item>
Retrieves an element in the ArrayDeque
by index.
Element at index 0 is the front of the queue.
Examples
use arraydeque::ArrayDeque; let mut buf: ArrayDeque<[_; 4]> = ArrayDeque::new(); buf.push_back(3); buf.push_back(4); buf.push_back(5); assert_eq!(buf.get(1), Some(&4));
fn get_mut(&mut self, index: usize) -> Option<&mut A::Item>
Retrieves an element in the ArrayDeque
mutably by index.
Element at index 0 is the front of the queue.
Examples
use arraydeque::ArrayDeque; let mut buf: ArrayDeque<[_; 4]> = ArrayDeque::new(); buf.push_back(3); buf.push_back(4); buf.push_back(5); if let Some(elem) = buf.get_mut(1) { *elem = 7; } assert_eq!(buf[1], 7);
fn swap(&mut self, i: usize, j: usize)
Swaps elements at indices i
and j
.
i
and j
may be equal.
Fails if there is no element with either index.
Element at index 0 is the front of the queue.
Examples
use arraydeque::ArrayDeque; let mut buf: ArrayDeque<[_; 4]> = ArrayDeque::new(); buf.push_back(3); buf.push_back(4); buf.push_back(5); buf.swap(0, 2); assert_eq!(buf[0], 5); assert_eq!(buf[2], 3);
fn capacity(&self) -> usize
Return the capacity of the ArrayDeque
.
Capacity
Note that the capacity()
is always backed_array.len() - 1
.
Read more
Examples
use arraydeque::ArrayDeque; let mut buf: ArrayDeque<[usize; 4]> = ArrayDeque::new(); assert_eq!(buf.capacity(), 3);
fn iter(&self) -> Iter<A::Item>
Returns a front-to-back iterator.
Examples
use arraydeque::ArrayDeque; let mut buf: ArrayDeque<[_; 4]> = ArrayDeque::new(); buf.push_back(5); buf.push_back(3); buf.push_back(4); let b: &[_] = &[&5, &3, &4]; let c: Vec<&i32> = buf.iter().collect(); assert_eq!(&c[..], b);
fn iter_mut(&mut self) -> IterMut<A::Item>
Returns a front-to-back iterator that returns mutable references.
Examples
use arraydeque::ArrayDeque; let mut buf: ArrayDeque<[_; 4]> = ArrayDeque::new(); buf.push_back(5); buf.push_back(3); buf.push_back(4); for num in buf.iter_mut() { *num = *num - 2; } let b: &[_] = &[&mut 3, &mut 1, &mut 2]; assert_eq!(&buf.iter_mut().collect::<Vec<&mut i32>>()[..], b);
fn as_slices(&self) -> (&[A::Item], &[A::Item])
Returns a pair of slices which contain, in order, the contents of the
ArrayDeque
.
Examples
use arraydeque::ArrayDeque; let mut vector: ArrayDeque<[_; 6]> = ArrayDeque::new(); vector.push_back(0); vector.push_back(1); vector.push_back(2); assert_eq!(vector.as_slices(), (&[0, 1, 2][..], &[][..])); vector.push_front(10); vector.push_front(9); assert_eq!(vector.as_slices(), (&[9, 10][..], &[0, 1, 2][..]));
fn as_mut_slices(&mut self) -> (&mut [A::Item], &mut [A::Item])
Returns a pair of slices which contain, in order, the contents of the
ArrayDeque
.
Examples
use arraydeque::ArrayDeque; let mut vector: ArrayDeque<[_; 5]> = ArrayDeque::new(); vector.push_back(0); vector.push_back(1); vector.push_front(10); vector.push_front(9); vector.as_mut_slices().0[0] = 42; vector.as_mut_slices().1[0] = 24; assert_eq!(vector.as_slices(), (&[42, 10][..], &[24, 1][..]));
fn len(&self) -> usize
Returns the number of elements in the ArrayDeque
.
Examples
use arraydeque::ArrayDeque; let mut v: ArrayDeque<[_; 4]> = ArrayDeque::new(); assert_eq!(v.len(), 0); v.push_back(1); assert_eq!(v.len(), 1);
fn is_empty(&self) -> bool
Returns true if the buffer contains no elements
Examples
use arraydeque::ArrayDeque; let mut v: ArrayDeque<[_; 4]> = ArrayDeque::new(); assert!(v.is_empty()); v.push_front(1); assert!(!v.is_empty());
fn drain<R>(&mut self, range: R) -> Drain<A> where
R: RangeArgument<usize>,
R: RangeArgument<usize>,
Create a draining iterator that removes the specified range in the
ArrayDeque
and yields the removed items.
Note 1: The element range is removed even if the iterator is not consumed until the end.
Note 2: It is unspecified how many elements are removed from the deque,
if the Drain
value is not dropped, but the borrow it holds expires
(eg. due to mem::forget).
Panics
Panics if the starting point is greater than the end point or if the end point is greater than the length of the vector.
Examples
use arraydeque::ArrayDeque; let mut v: ArrayDeque<[_; 4]> = vec![1, 2, 3].into_iter().collect(); assert_eq!(vec![3].into_iter().collect::<ArrayDeque<[_; 4]>>(), v.drain(2..).collect()); assert_eq!(vec![1, 2].into_iter().collect::<ArrayDeque<[_; 4]>>(), v); // A full range clears all contents v.drain(..); assert!(v.is_empty());
fn clear(&mut self)
Clears the buffer, removing all values.
Examples
use arraydeque::ArrayDeque; let mut v: ArrayDeque<[_; 4]> = ArrayDeque::new(); v.push_back(1); v.clear(); assert!(v.is_empty());
fn contains(&self, x: &A::Item) -> bool where
A::Item: PartialEq<A::Item>,
A::Item: PartialEq<A::Item>,
Returns true
if the ArrayDeque
contains an element equal to the
given value.
Examples
use arraydeque::ArrayDeque; let mut vector: ArrayDeque<[_; 3]> = ArrayDeque::new(); vector.push_back(0); vector.push_back(1); assert_eq!(vector.contains(&1), true); assert_eq!(vector.contains(&10), false);
fn front(&self) -> Option<&A::Item>
Provides a reference to the front element, or None
if the sequence is
empty.
Examples
use arraydeque::ArrayDeque; let mut d: ArrayDeque<[_; 3]> = ArrayDeque::new(); assert_eq!(d.front(), None); d.push_back(1); d.push_back(2); assert_eq!(d.front(), Some(&1));
fn front_mut(&mut self) -> Option<&mut A::Item>
Provides a mutable reference to the front element, or None
if the
sequence is empty.
Examples
use arraydeque::ArrayDeque; let mut d: ArrayDeque<[_; 3]> = ArrayDeque::new(); assert_eq!(d.front_mut(), None); d.push_back(1); d.push_back(2); match d.front_mut() { Some(x) => *x = 9, None => (), } assert_eq!(d.front(), Some(&9));
fn back(&self) -> Option<&A::Item>
Provides a reference to the back element, or None
if the sequence is
empty.
Examples
use arraydeque::ArrayDeque; let mut d: ArrayDeque<[_; 3]> = ArrayDeque::new(); assert_eq!(d.back(), None); d.push_back(1); d.push_back(2); assert_eq!(d.back(), Some(&2));
fn back_mut(&mut self) -> Option<&mut A::Item>
Provides a mutable reference to the back element, or None
if the
sequence is empty.
Examples
use arraydeque::ArrayDeque; let mut d: ArrayDeque<[_; 3]> = ArrayDeque::new(); assert_eq!(d.back(), None); d.push_back(1); d.push_back(2); match d.back_mut() { Some(x) => *x = 9, None => (), } assert_eq!(d.back(), Some(&9));
fn pop_front(&mut self) -> Option<A::Item>
Removes the first element and returns it, or None
if the sequence is
empty.
Examples
use arraydeque::ArrayDeque; let mut d: ArrayDeque<[_; 3]> = ArrayDeque::new(); d.push_back(1); d.push_back(2); assert_eq!(d.pop_front(), Some(1)); assert_eq!(d.pop_front(), Some(2)); assert_eq!(d.pop_front(), None);
fn push_front(&mut self, element: A::Item) -> Option<A::Item>
Inserts an element first in the sequence.
Return None
if the push succeeds, or and return Some(
element )
if the vector is full.
Examples
use arraydeque::ArrayDeque; let mut d: ArrayDeque<[_; 3]> = ArrayDeque::new(); d.push_front(1); d.push_front(2); let overflow = d.push_front(3); assert_eq!(d.front(), Some(&2)); assert_eq!(overflow, Some(3));
fn push_back(&mut self, element: A::Item) -> Option<A::Item>
Appends an element to the back of a buffer
Return None
if the push succeeds, or and return Some(
element )
if the vector is full.
Examples
use arraydeque::ArrayDeque; let mut buf: ArrayDeque<[_; 3]> = ArrayDeque::new(); buf.push_back(1); buf.push_back(3); let overflow = buf.push_back(5); assert_eq!(3, *buf.back().unwrap()); assert_eq!(overflow, Some(5));
fn pop_back(&mut self) -> Option<A::Item>
Removes the last element from a buffer and returns it, or None
if
it is empty.
Examples
use arraydeque::ArrayDeque; let mut buf: ArrayDeque<[_; 3]> = ArrayDeque::new(); assert_eq!(buf.pop_back(), None); buf.push_back(1); buf.push_back(3); assert_eq!(buf.pop_back(), Some(3));
fn swap_remove_back(&mut self, index: usize) -> Option<A::Item>
Removes an element from anywhere in the ArrayDeque
and returns it, replacing it with the
last element.
This does not preserve ordering, but is O(1).
Returns None
if index
is out of bounds.
Element at index 0 is the front of the queue.
Examples
use arraydeque::ArrayDeque; let mut buf: ArrayDeque<[_; 4]> = ArrayDeque::new(); assert_eq!(buf.swap_remove_back(0), None); buf.push_back(1); buf.push_back(2); buf.push_back(3); assert_eq!(buf.swap_remove_back(0), Some(1)); assert_eq!(buf.len(), 2); assert_eq!(buf[0], 3); assert_eq!(buf[1], 2);
fn swap_remove_front(&mut self, index: usize) -> Option<A::Item>
Removes an element from anywhere in the ArrayDeque
and returns it,
replacing it with the first element.
This does not preserve ordering, but is O(1).
Returns None
if index
is out of bounds.
Element at index 0 is the front of the queue.
Examples
use arraydeque::ArrayDeque; let mut buf: ArrayDeque<[_; 4]> = ArrayDeque::new(); assert_eq!(buf.swap_remove_front(0), None); buf.push_back(1); buf.push_back(2); buf.push_back(3); assert_eq!(buf.swap_remove_front(2), Some(3)); assert_eq!(buf.len(), 2); assert_eq!(buf[0], 2); assert_eq!(buf[1], 1);
fn insert(&mut self, index: usize, element: A::Item) -> Option<A::Item>
Inserts an element at index
within the ArrayDeque
. Whichever
end is closer to the insertion point will be moved to make room,
and all the affected elements will be moved to new positions.
Return None
if the push succeeds, or and return Some(
element )
if the vector is full.
Element at index 0 is the front of the queue.
Panics
Panics if index
is greater than ArrayDeque
's length
Examples
use arraydeque::ArrayDeque; let mut buf: ArrayDeque<[_; 4]> = ArrayDeque::new(); buf.push_back(10); buf.push_back(12); buf.insert(1, 11); let overflow = buf.insert(0, 9); assert_eq!(Some(&11), buf.get(1)); assert_eq!(overflow, Some(9));
fn remove(&mut self, index: usize) -> Option<A::Item>
Removes and returns the element at index
from the ArrayDeque
.
Whichever end is closer to the removal point will be moved to make
room, and all the affected elements will be moved to new positions.
Returns None
if index
is out of bounds.
Element at index 0 is the front of the queue.
Examples
use arraydeque::ArrayDeque; let mut buf: ArrayDeque<[_; 4]> = ArrayDeque::new(); buf.push_back(1); buf.push_back(2); buf.push_back(3); assert_eq!(buf.remove(1), Some(2)); assert_eq!(buf.get(1), Some(&3));
fn split_off(&mut self, at: usize) -> Self
Splits the collection into two at the given index.
Returns a newly allocated Self
. self
contains elements [0, at)
,
and the returned Self
contains elements [at, len)
.
Element at index 0 is the front of the queue.
Panics
Panics if at > len
Examples
use arraydeque::ArrayDeque; let mut buf: ArrayDeque<[_; 4]> = vec![1,2,3].into_iter().collect(); let buf2 = buf.split_off(1); // buf = [1], buf2 = [2, 3] assert_eq!(buf.len(), 1); assert_eq!(buf2.len(), 2);
fn append(&mut self, other: &mut Self)
Moves all the elements of other
into Self
, leaving other
empty.
Does not extract more items than there is space for. No error occurs if there are more iterator elements.
Examples
use arraydeque::ArrayDeque; let mut buf: ArrayDeque<[_; 8]> = vec![1, 2, 3].into_iter().collect(); // buf2: ArrayDeque<[_; 8]> can be infer let mut buf2 = vec![4, 5, 6].into_iter().collect(); let mut buf3 = vec![7, 8, 9].into_iter().collect(); buf.append(&mut buf2); assert_eq!(buf.len(), 6); assert_eq!(buf2.len(), 0); // max capacity reached buf.append(&mut buf3); assert_eq!(buf.len(), 7); assert_eq!(buf3.len(), 0);
fn retain<F>(&mut self, f: F) where
F: FnMut(&A::Item) -> bool,
F: FnMut(&A::Item) -> bool,
Retains only the elements specified by the predicate.
In other words, remove all elements e
such that f(&e)
returns false.
This method operates in place and preserves the order of the retained
elements.
Examples
use arraydeque::ArrayDeque; let mut buf: ArrayDeque<[_; 5]> = ArrayDeque::new(); buf.extend(1..5); buf.retain(|&x| x%2 == 0); let v: Vec<_> = buf.into_iter().collect(); assert_eq!(&v[..], &[2, 4]);
Trait Implementations
impl<A: Array> Clone for ArrayDeque<A> where
A::Item: Clone,
[src]
A::Item: Clone,
fn clone(&self) -> ArrayDeque<A>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<A: Array> Drop for ArrayDeque<A>
[src]
impl<A: Array> Default for ArrayDeque<A>
[src]
fn default() -> ArrayDeque<A>
Returns the "default value" for a type. Read more
impl<A: Array> PartialEq for ArrayDeque<A> where
A::Item: PartialEq,
[src]
A::Item: PartialEq,
fn eq(&self, other: &ArrayDeque<A>) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl<A: Array> Eq for ArrayDeque<A> where
A::Item: Eq,
[src]
A::Item: Eq,
impl<A: Array> PartialOrd for ArrayDeque<A> where
A::Item: PartialOrd,
[src]
A::Item: PartialOrd,
fn partial_cmp(&self, other: &ArrayDeque<A>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<A: Array> Ord for ArrayDeque<A> where
A::Item: Ord,
[src]
A::Item: Ord,
fn cmp(&self, other: &ArrayDeque<A>) -> Ordering
This method returns an Ordering
between self
and other
. Read more
impl<A: Array> Hash for ArrayDeque<A> where
A::Item: Hash,
[src]
A::Item: Hash,
fn hash<H: Hasher>(&self, state: &mut H)
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<A: Array> Index<usize> for ArrayDeque<A>
[src]
type Output = A::Item
The returned type after indexing
fn index(&self, index: usize) -> &A::Item
The method for the indexing (container[index]
) operation
impl<A: Array> IndexMut<usize> for ArrayDeque<A>
[src]
fn index_mut(&mut self, index: usize) -> &mut A::Item
The method for the mutable indexing (container[index]
) operation
impl<A: Array> FromIterator<A::Item> for ArrayDeque<A>
[src]
fn from_iter<T: IntoIterator<Item = A::Item>>(iter: T) -> Self
Creates a value from an iterator. Read more
impl<A: Array> IntoIterator for ArrayDeque<A>
[src]
type Item = A::Item
The type of the elements being iterated over.
type IntoIter = IntoIter<A>
Which kind of iterator are we turning this into?
fn into_iter(self) -> IntoIter<A>
Creates an iterator from a value. Read more
impl<'a, A: Array> IntoIterator for &'a ArrayDeque<A>
[src]
type Item = &'a A::Item
The type of the elements being iterated over.
type IntoIter = Iter<'a, A::Item>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Iter<'a, A::Item>
Creates an iterator from a value. Read more
impl<'a, A: Array> IntoIterator for &'a mut ArrayDeque<A>
[src]
type Item = &'a mut A::Item
The type of the elements being iterated over.
type IntoIter = IterMut<'a, A::Item>
Which kind of iterator are we turning this into?
fn into_iter(self) -> IterMut<'a, A::Item>
Creates an iterator from a value. Read more
impl<A: Array> Extend<A::Item> for ArrayDeque<A>
[src]
Extend the ArrayDeque
with an iterator.
Does not extract more items than there is space for. No error occurs if there are more iterator elements.
fn extend<T: IntoIterator<Item = A::Item>>(&mut self, iter: T)
Extends a collection with the contents of an iterator. Read more
impl<A: Array> Debug for ArrayDeque<A> where
A::Item: Debug,
[src]
A::Item: Debug,