[][src]Struct atone::Vc

pub struct Vc<T> { /* fields omitted */ }

A VecDeque (and Vec) variant that spreads resize load across pushes.

See the crate-level documentation for details.

Implementations

impl<T> Vc<T>[src]

pub fn new() -> Self[src]

Creates an empty Vc.

Examples

use atone::Vc;

let vector: Vc<u32> = Vc::new();

pub fn with_capacity(capacity: usize) -> Self[src]

Creates an empty Vc with space for at least capacity elements.

Examples

use atone::Vc;

let vector: Vc<u32> = Vc::with_capacity(10);

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

Provides a reference to the element at the given index.

Element at index 0 is the front of the queue.

Examples

use atone::Vc;

let mut buf = Vc::new();
buf.push_back(3);
buf.push_back(4);
buf.push_back(5);
assert_eq!(buf.get(1), Some(&4));

pub fn get_mut(&mut self, index: usize) -> Option<&mut T>[src]

Provides a mutable reference to the element at the given index.

Element at index 0 is the front of the queue.

Examples

use atone::Vc;

let mut buf = Vc::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);

pub fn swap(&mut self, i: usize, j: usize)[src]

Swaps elements at indices i and j.

i and j may be equal.

Element at index 0 is the front of the queue.

Panics

Panics if either index is out of bounds.

Examples

use atone::Vc;

let mut buf = Vc::new();
buf.push_back(3);
buf.push_back(4);
buf.push_back(5);
assert_eq!(buf, vec![3, 4, 5]);
buf.swap(0, 2);
assert_eq!(buf, vec![5, 4, 3]);

pub fn reverse(&mut self)[src]

Reverses the order of elements in the Vc, in place.

Examples

use atone::Vc;
use std::iter::FromIterator;

let mut v: Vc<_> = (1..=3).collect();
v.reverse();
assert_eq!(v, vec![3, 2, 1]);

pub fn capacity(&self) -> usize[src]

Returns the number of elements the Vc can hold without reallocating.

Examples

use atone::Vc;

let buf: Vc<i32> = Vc::with_capacity(10);
assert!(buf.capacity() >= 10);

pub fn reserve_exact(&mut self, additional: usize)[src]

Reserves the minimum capacity for exactly additional more elements to be inserted in the given Vc. Does nothing if the capacity is already sufficient.

Note that the allocator may give the collection more space than it requests. Therefore capacity can not be relied upon to be precisely minimal. Prefer [reserve] if future insertions are expected.

While we try to make this incremental where possible, it may require all-at-once resizing.

Panics

Panics if the new capacity overflows usize.

Examples

use atone::Vc;

let mut buf: Vc<i32> = vec![1].into_iter().collect();
buf.reserve_exact(10);
assert!(buf.capacity() >= 11);

pub fn reserve(&mut self, additional: usize)[src]

Reserves capacity for at least additional more elements to be inserted in the given Vc. The collection may reserve more space to avoid frequent reallocations.

While we try to make this incremental where possible, it may require all-at-once resizing.

Panics

Panics if the new capacity overflows usize.

Examples

use atone::Vc;

let mut buf: Vc<i32> = vec![1].into_iter().collect();
buf.reserve(10);
assert!(buf.capacity() >= 11);

pub fn shrink_to_fit(&mut self)[src]

Shrinks the capacity of the Vc as much as possible.

It will drop down as close as possible to the length but the allocator may still inform the Vc that there is space for a few more elements.

Examples

use atone::Vc;

let mut buf = Vc::with_capacity(15);
buf.extend(0..4);
assert_eq!(buf.capacity(), 15);
buf.shrink_to_fit();
assert!(buf.capacity() >= 4);
assert!(buf.capacity() < 15);

pub fn truncate(&mut self, len: usize)[src]

Shortens the Vc, keeping the first len elements and dropping the rest.

If len is greater than the Vc's current length, this has no effect.

Examples

use atone::Vc;

let mut buf = Vc::new();
buf.push_back(5);
buf.push_back(10);
buf.push_back(15);
assert_eq!(buf, vec![5, 10, 15]);
buf.truncate(1);
assert_eq!(buf, vec![5]);

pub fn iter(&self) -> Iter<T>[src]

Returns a front-to-back iterator.

Examples

use atone::Vc;

let mut buf = Vc::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);

pub fn iter_mut(&mut self) -> IterMut<T>[src]

Returns a front-to-back iterator that returns mutable references.

Examples

use atone::Vc;

let mut buf = Vc::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);

pub fn as_single_slice(&self) -> Option<&[T]>[src]

Returns the single slice which contains, in order, the contents of the Vc, if possible.

You will likely want to call make_contiguous before calling this method to ensure that this method returns Some.

Examples

use atone::Vc;
let mut vector = Vc::new();

vector.push(0);
vector.push(1);
vector.push(2);

assert_eq!(vector.as_single_slice(), Some(&[0, 1, 2][..]));

// Push enough items, and the memory is no longer
// contiguous due to incremental resizing.
for i in 3..16 {
    vector.push(i);
}

assert_eq!(vector.as_single_slice(), None);

// TODO:
// With make_contiguous, we can bring it back.
// vector.make_contiguous();
// assert_eq!(vector.as_single_slice(), Some(&(0..16).collect::<Vec<_>>()[..]));

pub fn len(&self) -> usize[src]

Returns the number of elements in the Vc.

Examples

use atone::Vc;

let mut v = Vc::new();
assert_eq!(v.len(), 0);
v.push_back(1);
assert_eq!(v.len(), 1);

pub fn is_empty(&self) -> bool[src]

Returns true if the Vc is empty.

Examples

use atone::Vc;

let mut v = Vc::new();
assert!(v.is_empty());
v.push(1);
assert!(!v.is_empty());

pub fn range<R>(&self, range: R) -> impl ExactSizeIterator<Item = &T> where
    R: RangeBounds<usize>, 
[src]

Creates an iterator that covers the specified range in the VecDeque.

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 atone::Vc;

let v: Vc<_> = vec![1, 2, 3].into_iter().collect();
let range = v.range(2..).copied().collect::<Vc<_>>();
assert_eq!(range, vec![3]);

// A full range covers all contents
let all = v.range(..);
assert_eq!(all.len(), 3);

pub fn range_mut<R>(
    &mut self,
    range: R
) -> impl ExactSizeIterator<Item = &mut T> where
    R: RangeBounds<usize>, 
[src]

Creates an iterator that covers the specified mutable range in the VecDeque.

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 atone::Vc;

let mut v: Vc<_> = vec![1, 2, 3].into_iter().collect();
for v in v.range_mut(2..) {
  *v *= 2;
}
assert_eq!(v, vec![1, 2, 6]);

// A full range covers all contents
for v in v.range_mut(..) {
  *v *= 2;
}
assert_eq!(v, vec![2, 4, 12]);

pub fn drain<R>(&mut self, range: R) -> Drain<T> where
    R: RangeBounds<usize>, 
[src]

Creates a draining iterator that removes the specified range in the Vc 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 (e.g., 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 atone::Vc;

let mut v: Vc<_> = vec![1, 2, 3].into_iter().collect();
let drained = v.drain(2..).collect::<Vc<_>>();
assert_eq!(drained, vec![3]);
assert_eq!(v, vec![1, 2]);

// A full range clears all contents
v.drain(..);
assert!(v.is_empty());

pub fn clear(&mut self)[src]

Clears the Vc, removing all values.

Examples

use atone::Vc;

let mut v = Vc::new();
v.push_back(1);
v.clear();
assert!(v.is_empty());

pub fn contains(&self, x: &T) -> bool where
    T: PartialEq<T>, 
[src]

Returns true if the Vc contains an element equal to the given value.

Examples

use atone::Vc;

let mut vector: Vc<u32> = Vc::new();

vector.push_back(0);
vector.push_back(1);

assert_eq!(vector.contains(&1), true);
assert_eq!(vector.contains(&10), false);

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

Provides a reference to the front element, or None if the Vc is empty.

Examples

use atone::Vc;

let mut d = Vc::new();
assert_eq!(d.front(), None);

d.push_back(1);
d.push_back(2);
assert_eq!(d.front(), Some(&1));

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

Provides a mutable reference to the front element, or None if the Vc is empty.

Examples

use atone::Vc;

let mut d = Vc::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));

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

Provides a reference to the back element, or None if the Vc is empty.

Examples

use atone::Vc;

let mut d = Vc::new();
assert_eq!(d.back(), None);

d.push_back(1);
d.push_back(2);
assert_eq!(d.back(), Some(&2));

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

Provides a mutable reference to the back element, or None if the Vc is empty.

Examples

use atone::Vc;

let mut d = Vc::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));

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

Removes the first element and returns it, or None if the Vc is empty.

Examples

use atone::Vc;

let mut d = Vc::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);

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

Removes the last element from the Vc and returns it, or None if it is empty.

Examples

use atone::Vc;

let mut buf = Vc::new();
assert_eq!(buf.pop_back(), None);
buf.push_back(1);
buf.push_back(3);
assert_eq!(buf.pop_back(), Some(3));

pub fn pop(&mut self) -> Option<T>[src]

Removes the last element from the Vc and returns it, or None if it is empty.

Examples

let mut vec = vec![1, 2, 3];
assert_eq!(vec.pop(), Some(3));
assert_eq!(vec, [1, 2]);

pub fn push_back(&mut self, value: T)[src]

Appends an element to the back of the Vc.

Examples

use atone::Vc;

let mut buf = Vc::new();
buf.push_back(1);
buf.push_back(3);
assert_eq!(3, *buf.back().unwrap());

pub fn push(&mut self, value: T)[src]

Appends an element to the back of the Vc.

Panics

Panics if the number of elements in the vector overflows a usize.

Examples

let mut vec = vec![1, 2];
vec.push(3);
assert_eq!(vec, [1, 2, 3]);

pub fn swap_remove_front(&mut self, index: usize) -> Option<T>[src]

Removes an element from anywhere in the Vc 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 atone::Vc;

let mut buf = Vc::new();
assert_eq!(buf.swap_remove_front(0), None);
buf.push_back(1);
buf.push_back(2);
buf.push_back(3);
assert_eq!(buf, vec![1, 2, 3]);

assert_eq!(buf.swap_remove_front(2), Some(3));
assert_eq!(buf, vec![2, 1]);

pub fn swap_remove_back(&mut self, index: usize) -> Option<T>[src]

Removes an element from anywhere in the Vc 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 atone::Vc;

let mut buf = Vc::new();
assert_eq!(buf.swap_remove_back(0), None);
buf.push_back(1);
buf.push_back(2);
buf.push_back(3);
assert_eq!(buf, vec![1, 2, 3]);

assert_eq!(buf.swap_remove_back(0), Some(1));
assert_eq!(buf, vec![3, 2]);

pub fn swap_remove(&mut self, index: usize) -> T[src]

Removes an element from the vector and returns it.

The removed element is replaced by the last element of the vector.

This does not preserve ordering, but is O(1).

Panics

Panics if index is out of bounds.

Examples

let mut v = vec!["foo", "bar", "baz", "qux"];

assert_eq!(v.swap_remove(1), "bar");
assert_eq!(v, ["foo", "qux", "baz"]);

assert_eq!(v.swap_remove(0), "foo");
assert_eq!(v, ["baz", "qux"]);

pub fn insert(&mut self, index: usize, value: T)[src]

Inserts an element at index within the Vc, shifting all elements with indices greater than or equal to index towards the back.

Element at index 0 is the front of the queue.

Panics

Panics if index is greater than Vc's length

Examples

use atone::Vc;

let mut vec_deque = Vc::new();
vec_deque.push_back('a');
vec_deque.push_back('b');
vec_deque.push_back('c');
assert_eq!(vec_deque, vec!['a', 'b', 'c']);

vec_deque.insert(1, 'd');
assert_eq!(vec_deque, vec!['a', 'd', 'b', 'c']);

pub fn remove(&mut self, index: usize) -> T[src]

Removes and returns the element at index from the Vc. 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.

Panics

Panics if index is out of bounds.

Examples

use atone::Vc;

let mut buf = Vc::new();
buf.push_back(1);
buf.push_back(2);
buf.push_back(3);
assert_eq!(buf, vec![1, 2, 3]);

assert_eq!(buf.remove(1), 2);
assert_eq!(buf, vec![1, 3]);

pub fn append(&mut self, other: &mut Self)[src]

Moves all the elements of other into self, leaving other empty.

Panics

Panics if the new number of elements in self overflows a usize.

Examples

use atone::Vc;

let mut buf: Vc<_> = vec![1, 2].into_iter().collect();
let mut buf2: Vc<_> = vec![3, 4].into_iter().collect();
buf.append(&mut buf2);
assert_eq!(buf, vec![1, 2, 3, 4]);
assert_eq!(buf2, vec![]);

pub fn retain<F>(&mut self, f: F) where
    F: FnMut(&T) -> bool
[src]

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, visiting each element exactly once in the original order, and preserves the order of the retained elements.

Examples

use atone::Vc;

let mut buf = Vc::new();
buf.extend(1..5);
buf.retain(|&x| x % 2 == 0);
assert_eq!(buf, vec![2, 4]);

The exact order may be useful for tracking external state, like an index.

use atone::Vc;

let mut buf = Vc::new();
buf.extend(1..6);

let keep = [false, true, true, false, true];
let mut i = 0;
buf.retain(|_| (keep[i], i += 1).0);
assert_eq!(buf, vec![2, 3, 5]);

pub fn resize_with(&mut self, new_len: usize, generator: impl FnMut() -> T)[src]

Modifies the Vc in-place so that len() is equal to new_len, either by removing excess elements from the back or by appending elements generated by calling generator to the back.

Examples

use atone::Vc;

let mut buf = Vc::new();
buf.push_back(5);
buf.push_back(10);
buf.push_back(15);
assert_eq!(buf, vec![5, 10, 15]);

buf.resize_with(5, Default::default);
assert_eq!(buf, vec![5, 10, 15, 0, 0]);

buf.resize_with(2, || unreachable!());
assert_eq!(buf, vec![5, 10]);

let mut state = 100;
buf.resize_with(5, || { state += 1; state });
assert_eq!(buf, vec![5, 10, 101, 102, 103]);

pub fn make_contiguous(&mut self) -> &mut [T][src]

Rearranges the internal storage so that all elements are in one contiguous slice, which is then returned.

This method does not allocate and does not change the order of the inserted elements. As it returns a mutable slice, this can be used to sort or binary search a deque.

This method will also move over leftover items from the last resize, if any.

Examples

Sorting the content of a deque.

use atone::Vc;

let mut buf = Vc::with_capacity(3);
for i in 1..=16 {
    buf.push(16 - i);
}

// The backing memory of buf is now split,
// since some items are left over after the resize.
// To sort the list, we make it contiguous, and then sort.
buf.make_contiguous().sort();
assert_eq!(buf, (0..16).collect::<Vec<_>>());

// Similarly, we can sort it in reverse order.
buf.make_contiguous().sort_by(|a, b| b.cmp(a));
assert_eq!(buf, (1..=16).map(|i| 16 - i).collect::<Vec<_>>());

Getting immutable access to the contiguous slice.

use atone::Vc;
let mut buf = Vc::new();
for i in 1..=3 {
    buf.push(i);
}

buf.make_contiguous();
if let Some(slice) = buf.as_single_slice() {
    // we can now be sure that `slice` contains all elements of the deque,
    // while still having immutable access to `buf`.
    assert_eq!(buf.len(), slice.len());
    assert_eq!(slice, &[1, 2, 3] as &[_]);
}

impl<T: Clone> Vc<T>[src]

pub fn resize(&mut self, new_len: usize, value: T)[src]

Modifies the Vc in-place so that len() is equal to new_len, either by removing excess elements from the back or by appending clones of value to the back.

Examples

use atone::Vc;

let mut buf = Vc::new();
buf.push_back(5);
buf.push_back(10);
buf.push_back(15);
assert_eq!(buf, vec![5, 10, 15]);

buf.resize(2, 0);
assert_eq!(buf, vec![5, 10]);

buf.resize(5, 20);
assert_eq!(buf, vec![5, 10, 20, 20, 20]);

Trait Implementations

impl<T: Clone> Clone for Vc<T>[src]

impl<T: Debug> Debug for Vc<T>[src]

impl<T> Default for Vc<T>[src]

fn default() -> Self[src]

Creates an empty Vc<T>.

impl<'de, T> Deserialize<'de> for Vc<T> where
    T: Deserialize<'de>, 
[src]

impl<A: Eq> Eq for Vc<A>[src]

impl<'a, T: 'a + Copy> Extend<&'a T> for Vc<T>[src]

impl<A> Extend<A> for Vc<A>[src]

impl<T> From<Vc<T>> for Vec<T>[src]

impl<T> From<Vc<T>> for VecDeque<T>[src]

impl<T> From<Vec<T>> for Vc<T>[src]

impl<T> From<VecDeque<T>> for Vc<T>[src]

impl<A> FromIterator<A> for Vc<A>[src]

impl<T> FromParallelIterator<T> for Vc<T> where
    T: Send
[src]

impl<A: Hash> Hash for Vc<A>[src]

impl<A> Index<usize> for Vc<A>[src]

type Output = A

The returned type after indexing.

impl<A> IndexMut<usize> for Vc<A>[src]

impl<T> IntoIterator for Vc<T>[src]

type Item = T

The type of the elements being iterated over.

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?

impl<'a, T> IntoIterator for &'a Vc<T>[src]

type Item = &'a T

The type of the elements being iterated over.

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?

impl<'a, T> IntoIterator for &'a mut Vc<T>[src]

type Item = &'a mut T

The type of the elements being iterated over.

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?

impl<T: Send> IntoParallelIterator for Vc<T>[src]

type Item = T

The type of item that the parallel iterator will produce.

type Iter = IntoIter<T>

The parallel iterator type that will be created.

impl<'a, T: Sync> IntoParallelIterator for &'a Vc<T>[src]

type Item = &'a T

The type of item that the parallel iterator will produce.

type Iter = Iter<'a, T>

The parallel iterator type that will be created.

impl<'a, T: Send> IntoParallelIterator for &'a mut Vc<T>[src]

type Item = &'a mut T

The type of item that the parallel iterator will produce.

type Iter = IterMut<'a, T>

The parallel iterator type that will be created.

impl<A: Ord> Ord for Vc<A>[src]

impl<'a, T> ParallelExtend<&'a T> for Vc<T> where
    T: 'a + Copy + Send + Sync
[src]

impl<T> ParallelExtend<T> for Vc<T> where
    T: Send
[src]

impl<'_, A, B> PartialEq<&'_ [B]> for Vc<A> where
    A: PartialEq<B>, 
[src]

impl<'_, A, B> PartialEq<&'_ mut [B]> for Vc<A> where
    A: PartialEq<B>, 
[src]

impl<A: PartialEq> PartialEq<Vc<A>> for Vc<A>[src]

impl<A, B> PartialEq<Vc<A>> for Vec<B> where
    A: PartialEq<B>, 
[src]

impl<'_, A, B> PartialEq<Vc<A>> for &'_ [B] where
    A: PartialEq<B>, 
[src]

impl<'_, A, B> PartialEq<Vc<A>> for &'_ mut [B] where
    A: PartialEq<B>, 
[src]

impl<A, B> PartialEq<Vec<B>> for Vc<A> where
    A: PartialEq<B>, 
[src]

impl<A: PartialOrd> PartialOrd<Vc<A>> for Vc<A>[src]

impl<T> Serialize for Vc<T> where
    T: Serialize
[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for Vc<T> where
    T: RefUnwindSafe

impl<T> Send for Vc<T> where
    T: Send

impl<T> Sync for Vc<T> where
    T: Sync

impl<T> Unpin for Vc<T> where
    T: Unpin

impl<T> UnwindSafe for Vc<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T> IntoParallelIterator for T where
    T: ParallelIterator
[src]

type Iter = T

The parallel iterator type that will be created.

type Item = <T as ParallelIterator>::Item

The type of item that the parallel iterator will produce.

impl<'data, I> IntoParallelRefIterator<'data> for I where
    I: 'data + ?Sized,
    &'data I: IntoParallelIterator
[src]

type Iter = <&'data I as IntoParallelIterator>::Iter

The type of the parallel iterator that will be returned.

type Item = <&'data I as IntoParallelIterator>::Item

The type of item that the parallel iterator will produce. This will typically be an &'data T reference type. Read more

impl<'data, I> IntoParallelRefMutIterator<'data> for I where
    I: 'data + ?Sized,
    &'data mut I: IntoParallelIterator
[src]

type Iter = <&'data mut I as IntoParallelIterator>::Iter

The type of iterator that will be created.

type Item = <&'data mut I as IntoParallelIterator>::Item

The type of item that will be produced; this is typically an &'data mut T reference. Read more

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.