Struct arraydeque::ArrayDeque [] [src]

pub struct ArrayDeque<A: Array> { /* fields omitted */ }

ArrayDeque is 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]

Creates an empty ArrayDeque.

Examples

use arraydeque::ArrayDeque;

let vector: ArrayDeque<[usize; 3]> = ArrayDeque::new();

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

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

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

Return the capacity of the ArrayVec.

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

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

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

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][..]));

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][..]));

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<A: Array> Drop for ArrayDeque<A>
[src]

A method called when the value goes out of scope. Read more

impl<A: Array> Default for ArrayDeque<A>
[src]

Returns the "default value" for a type. Read more

impl<A: Array> PartialEq for ArrayDeque<A> where A::Item: PartialEq
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<A: Array> Eq for ArrayDeque<A> where A::Item: Eq
[src]

impl<A: Array> PartialOrd for ArrayDeque<A> where A::Item: PartialOrd
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

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]

This method returns an Ordering between self and other. Read more

impl<A: Array> Hash for ArrayDeque<A> where A::Item: Hash
[src]

Feeds this value into the state given, updating the hasher as necessary.

Feeds a slice of this type into the state provided.

impl<A: Array> Index<usize> for ArrayDeque<A>
[src]

The returned type after indexing

The method for the indexing (container[index]) operation

impl<A: Array> IndexMut<usize> for ArrayDeque<A>
[src]

The method for the mutable indexing (container[index]) operation

impl<A: Array> FromIterator<A::Item> for ArrayDeque<A>
[src]

Creates a value from an iterator. Read more

impl<A: Array> IntoIterator for ArrayDeque<A>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

impl<'a, A: Array> IntoIterator for &'a ArrayDeque<A>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

impl<'a, A: Array> IntoIterator for &'a mut ArrayDeque<A>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

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.

Extends a collection with the contents of an iterator. Read more

impl<A: Array> Debug for ArrayDeque<A> where A::Item: Debug
[src]

Formats the value using the given formatter.