Struct truck_topology::Wire[][src]

pub struct Wire<P, C> { /* fields omitted */ }

Wire, a path or cycle which consists some edges.

The entity of this struct is VecDeque<Edge> and almost methods are inherited from VecDeque<Edge> by Deref and DerefMut traits.

Implementations

impl<P, C> Wire<P, C>[src]

pub fn new() -> Wire<P, C>[src]

Creates the empty wire.

pub fn with_capacity(capacity: usize) -> Wire<P, C>[src]

Creates the empty wire with space for at least capacity edges.

pub fn edge_iter(&self) -> EdgeIter<'_, P, C>[src]

Returns an iterator over the edges. Practically, an alias of iter().

pub fn edge_iter_mut(&mut self) -> EdgeIterMut<'_, P, C>[src]

Returns a mutable iterator over the edges. Practically, an alias of iter_mut().

pub fn edge_into_iter(self) -> EdgeIntoIter<P, C>[src]

Creates a consuming iterator. Practically, an alias of into_iter().

pub fn vertex_iter(&self) -> VertexIter<'_, P, C>

Notable traits for VertexIter<'a, P, C>

impl<'a, P, C> Iterator for VertexIter<'a, P, C> type Item = Vertex<P>;
[src]

Returns an iterator over the vertices.

pub fn front_edge(&self) -> Option<&Edge<P, C>>[src]

Returns the front edge. If self is empty wire, returns None.
Practically, an alias of the inherited method VecDeque::front().

pub fn front_vertex(&self) -> Option<&Vertex<P>>[src]

Returns the front vertex. If self is empty wire, returns None.

Examples

let v = Vertex::news(&[(), (), ()]);
let mut wire = Wire::new();
assert_eq!(wire.front_vertex(), None);
wire.push_back(Edge::new(&v[1], &v[2], ()));
wire.push_front(Edge::new(&v[0], &v[1], ()));
assert_eq!(wire.front_vertex(), Some(&v[0]));

pub fn back_edge(&self) -> Option<&Edge<P, C>>[src]

Returns the back edge. If self is empty wire, returns None.
Practically, an alias of the inherited method VecDeque::back()

pub fn back_vertex(&self) -> Option<&Vertex<P>>[src]

Returns the back edge. If self is empty wire, returns None.

Examples

let v = Vertex::news(&[(), (), ()]);
let mut wire = Wire::new();
assert_eq!(wire.back_vertex(), None);
wire.push_back(Edge::new(&v[1], &v[2], ()));
wire.push_front(Edge::new(&v[0], &v[1], ()));
assert_eq!(wire.back_vertex(), Some(&v[2]));

pub fn ends_vertices(&self) -> Option<(&Vertex<P>, &Vertex<P>)>[src]

Returns vertices at both ends.

use truck_topology::*;
let v = Vertex::news(&[(); 3]);
let mut wire = Wire::new();
assert_eq!(wire.back_vertex(), None);
wire.push_back(Edge::new(&v[1], &v[2], ()));
wire.push_front(Edge::new(&v[0], &v[1], ()));
assert_eq!(wire.ends_vertices(), Some((&v[0], &v[2])));

pub fn append(&mut self, other: &mut Wire<P, C>)[src]

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

pub fn split_off(&mut self, at: usize) -> Wire<P, C>[src]

Splits the Wire into two at the given index.

Examples

use truck_topology::*;
let v = Vertex::news(&[(); 7]);
let mut wire = Wire::new();
for i in 0..6 {
   wire.push_back(Edge::new(&v[i], &v[i + 1], ()));
}
let original_wire = wire.clone();
let mut wire1 = wire.split_off(4);
assert_eq!(wire.len(), 4);
assert_eq!(wire1.len(), 2);
wire.append(&mut wire1);
assert_eq!(original_wire, wire);

Panics

Panics if at > self.len()

pub fn invert(&mut self) -> &mut Self[src]

Inverts the wire.

Examples

use truck_topology::*;
let v = Vertex::news(&[(); 4]);
let mut wire = Wire::from(vec![
    Edge::new(&v[3], &v[2], ()),
    Edge::new(&v[2], &v[1], ()),
    Edge::new(&v[1], &v[0], ()),
]);
wire.invert();
for (i, vert) in wire.vertex_iter().enumerate() {
    assert_eq!(v[i], vert);
}

pub fn inverse(&self) -> Wire<P, C>[src]

Returns the inverse wire.

Examples

use truck_topology::*;
let v = Vertex::news(&[(); 4]);
let mut wire = Wire::from(vec![
    Edge::new(&v[3], &v[2], ()),
    Edge::new(&v[2], &v[1], ()),
    Edge::new(&v[1], &v[0], ()),
]);
let inverse = wire.inverse();
wire.invert();
for (edge0, edge1) in wire.edge_iter().zip(inverse.edge_iter()) {
    assert_eq!(edge0, edge1);
}

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

Returns whether all the adjacent pairs of edges have shared vertices or not.

Examples

use truck_topology::*;
let v = Vertex::news(&[(); 4]);
let mut wire = Wire::from(vec![
    Edge::new(&v[0], &v[1], ()),
    Edge::new(&v[2], &v[3], ()),
]);
assert!(!wire.is_continuous());
wire.insert(1, Edge::new(&v[1], &v[2], ()));
assert!(wire.is_continuous());
use truck_topology::*;
// The empty wire is continuous
assert!(Wire::<(), ()>::new().is_continuous());

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

Returns whether the front vertex of the wire is the same as the back one or not.

Examples

use truck_topology::*;
let v = Vertex::news(&[(); 4]);
let mut wire = Wire::from(vec![
    Edge::new(&v[0], &v[1], ()),
    Edge::new(&v[2], &v[3], ()),
]);
assert!(!wire.is_cyclic());
wire.push_back(Edge::new(&v[3], &v[0], ()));
assert!(wire.is_cyclic());
use truck_topology::*;
// The empty wire is cyclic.
assert!(Wire::<(), ()>::new().is_cyclic());

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

Returns whether the wire is closed or not. Here, "closed" means "continuous" and "cyclic".

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

Returns whether simple or not. Here, "simple" means all the vertices in the wire are shared from only two edges at most.

Examples

use std::iter::FromIterator;
use truck_topology::*;
let v = Vertex::news(&[(); 4]);
let edge0 = Edge::new(&v[0], &v[1], ());
let edge1 = Edge::new(&v[1], &v[2], ());
let edge2 = Edge::new(&v[2], &v[3], ());
let edge3 = Edge::new(&v[3], &v[1], ());
let edge4 = Edge::new(&v[3], &v[0], ());

let wire0 = Wire::from_iter(vec![&edge0, &edge1, &edge2, &edge3]);
let wire1 = Wire::from(vec![edge0, edge1, edge2, edge4]);

assert!(!wire0.is_simple());
assert!(wire1.is_simple());
use truck_topology::*;
// The empty wire is simple.
assert!(Wire::<(), ()>::new().is_simple());

pub fn disjoint_wires(wires: &Vec<Wire<P, C>>) -> bool[src]

Determines whether all the wires in wires has no same vertices.

impl<P: Tolerance, C: Curve<Point = P>> Wire<P, C>[src]

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

Returns the consistence of the geometry of end vertices and the geometry of edge.

Methods from Deref<Target = VecDeque<Edge<P, C>>>

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

Provides a reference to the element at the given index.

Element at index 0 is the front of the queue.

Examples

use std::collections::VecDeque;

let mut buf = VecDeque::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>1.0.0[src]

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

Element at index 0 is the front of the queue.

Examples

use std::collections::VecDeque;

let mut buf = VecDeque::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)1.0.0[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 std::collections::VecDeque;

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

pub fn capacity(&self) -> usize1.0.0[src]

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

Examples

use std::collections::VecDeque;

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

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

Reserves the minimum capacity for exactly additional more elements to be inserted in the given VecDeque. 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.

Panics

Panics if the new capacity overflows usize.

Examples

use std::collections::VecDeque;

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

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

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

Panics

Panics if the new capacity overflows usize.

Examples

use std::collections::VecDeque;

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

pub fn try_reserve_exact(
    &mut self,
    additional: usize
) -> Result<(), TryReserveError>
[src]

🔬 This is a nightly-only experimental API. (try_reserve)

new API

Tries to reserve the minimum capacity for exactly additional more elements to be inserted in the given VecDeque<T>. After calling try_reserve_exact, capacity will be greater than or equal to self.len() + additional. 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.

Errors

If the capacity overflows usize, or the allocator reports a failure, then an error is returned.

Examples

#![feature(try_reserve)]
use std::collections::TryReserveError;
use std::collections::VecDeque;

fn process_data(data: &[u32]) -> Result<VecDeque<u32>, TryReserveError> {
    let mut output = VecDeque::new();

    // Pre-reserve the memory, exiting if we can't
    output.try_reserve_exact(data.len())?;

    // Now we know this can't OOM(Out-Of-Memory) in the middle of our complex work
    output.extend(data.iter().map(|&val| {
        val * 2 + 5 // very complicated
    }));

    Ok(output)
}

pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>[src]

🔬 This is a nightly-only experimental API. (try_reserve)

new API

Tries to reserve capacity for at least additional more elements to be inserted in the given VecDeque<T>. The collection may reserve more space to avoid frequent reallocations. After calling try_reserve, capacity will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient.

Errors

If the capacity overflows usize, or the allocator reports a failure, then an error is returned.

Examples

#![feature(try_reserve)]
use std::collections::TryReserveError;
use std::collections::VecDeque;

fn process_data(data: &[u32]) -> Result<VecDeque<u32>, TryReserveError> {
    let mut output = VecDeque::new();

    // Pre-reserve the memory, exiting if we can't
    output.try_reserve(data.len())?;

    // Now we know this can't OOM in the middle of our complex work
    output.extend(data.iter().map(|&val| {
        val * 2 + 5 // very complicated
    }));

    Ok(output)
}

pub fn shrink_to_fit(&mut self)1.5.0[src]

Shrinks the capacity of the VecDeque as much as possible.

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

Examples

use std::collections::VecDeque;

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

pub fn shrink_to(&mut self, min_capacity: usize)[src]

🔬 This is a nightly-only experimental API. (shrink_to)

new API

Shrinks the capacity of the VecDeque with a lower bound.

The capacity will remain at least as large as both the length and the supplied value.

If the current capacity is less than the lower limit, this is a no-op.

Examples

#![feature(shrink_to)]
use std::collections::VecDeque;

let mut buf = VecDeque::with_capacity(15);
buf.extend(0..4);
assert_eq!(buf.capacity(), 15);
buf.shrink_to(6);
assert!(buf.capacity() >= 6);
buf.shrink_to(0);
assert!(buf.capacity() >= 4);

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

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

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

Examples

use std::collections::VecDeque;

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

pub fn iter(&self) -> Iter<'_, T>1.0.0[src]

Returns a front-to-back iterator.

Examples

use std::collections::VecDeque;

let mut buf = VecDeque::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>1.0.0[src]

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

Examples

use std::collections::VecDeque;

let mut buf = VecDeque::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_slices(&self) -> (&[T], &[T])1.5.0[src]

Returns a pair of slices which contain, in order, the contents of the VecDeque.

If make_contiguous was previously called, all elements of the VecDeque will be in the first slice and the second slice will be empty.

Examples

use std::collections::VecDeque;

let mut vector = VecDeque::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][..]));

pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T])1.5.0[src]

Returns a pair of slices which contain, in order, the contents of the VecDeque.

If make_contiguous was previously called, all elements of the VecDeque will be in the first slice and the second slice will be empty.

Examples

use std::collections::VecDeque;

let mut vector = VecDeque::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][..]));

pub fn len(&self) -> usize1.0.0[src]

Returns the number of elements in the VecDeque.

Examples

use std::collections::VecDeque;

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

pub fn is_empty(&self) -> bool1.0.0[src]

Returns true if the VecDeque is empty.

Examples

use std::collections::VecDeque;

let mut v = VecDeque::new();
assert!(v.is_empty());
v.push_front(1);
assert!(!v.is_empty());

pub fn range<R>(&self, range: R) -> Iter<'_, T> where
    R: RangeBounds<usize>, 
1.51.0[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 std::collections::VecDeque;

let v: VecDeque<_> = vec![1, 2, 3].into_iter().collect();
let range = v.range(2..).copied().collect::<VecDeque<_>>();
assert_eq!(range, [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) -> IterMut<'_, T> where
    R: RangeBounds<usize>, 
1.51.0[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 std::collections::VecDeque;

let mut v: VecDeque<_> = 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>, 
1.6.0[src]

Creates a draining iterator that removes the specified range in the VecDeque 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 std::collections::VecDeque;

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

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

pub fn clear(&mut self)1.0.0[src]

Clears the VecDeque, removing all values.

Examples

use std::collections::VecDeque;

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

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

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

Examples

use std::collections::VecDeque;

let mut vector: VecDeque<u32> = VecDeque::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>1.0.0[src]

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

Examples

use std::collections::VecDeque;

let mut d = VecDeque::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>1.0.0[src]

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

Examples

use std::collections::VecDeque;

let mut d = VecDeque::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>1.0.0[src]

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

Examples

use std::collections::VecDeque;

let mut d = VecDeque::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>1.0.0[src]

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

Examples

use std::collections::VecDeque;

let mut d = VecDeque::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>1.0.0[src]

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

Examples

use std::collections::VecDeque;

let mut d = VecDeque::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>1.0.0[src]

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

Examples

use std::collections::VecDeque;

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

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

Prepends an element to the VecDeque.

Examples

use std::collections::VecDeque;

let mut d = VecDeque::new();
d.push_front(1);
d.push_front(2);
assert_eq!(d.front(), Some(&2));

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

Appends an element to the back of the VecDeque.

Examples

use std::collections::VecDeque;

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

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

Removes an element from anywhere in the VecDeque 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 std::collections::VecDeque;

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

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

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

Removes an element from anywhere in the VecDeque 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 std::collections::VecDeque;

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

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

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

Inserts an element at index within the VecDeque, 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 VecDeque's length

Examples

use std::collections::VecDeque;

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

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

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

Removes and returns the element at index from the VecDeque. 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 std::collections::VecDeque;

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

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

#[must_use = "use `.truncate()` if you don't need the other half"]pub fn split_off(&mut self, at: usize) -> VecDeque<T>1.4.0[src]

Splits the VecDeque into two at the given index.

Returns a newly allocated VecDeque. self contains elements [0, at), and the returned VecDeque contains elements [at, len).

Note that the capacity of self does not change.

Element at index 0 is the front of the queue.

Panics

Panics if at > len.

Examples

use std::collections::VecDeque;

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

pub fn append(&mut self, other: &mut VecDeque<T>)1.4.0[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 std::collections::VecDeque;

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

pub fn retain<F>(&mut self, f: F) where
    F: FnMut(&T) -> bool
1.4.0[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 std::collections::VecDeque;

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

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

use std::collections::VecDeque;

let mut buf = VecDeque::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, [2, 3, 5]);

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

Modifies the VecDeque 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 std::collections::VecDeque;

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

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

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

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

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

Notable traits for &'_ mut [u8]

impl<'_> Write for &'_ mut [u8]impl<'_> Read for &'_ [u8]
1.48.0[src]

Rearranges the internal storage of this deque so it is 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 a deque.

Once the internal storage is contiguous, the as_slices and as_mut_slices methods will return the entire contents of the VecDeque in a single slice.

Examples

Sorting the content of a deque.

use std::collections::VecDeque;

let mut buf = VecDeque::with_capacity(15);

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

// sorting the deque
buf.make_contiguous().sort();
assert_eq!(buf.as_slices(), (&[1, 2, 3] as &[_], &[] as &[_]));

// sorting it in reverse order
buf.make_contiguous().sort_by(|a, b| b.cmp(a));
assert_eq!(buf.as_slices(), (&[3, 2, 1] as &[_], &[] as &[_]));

Getting immutable access to the contiguous slice.

use std::collections::VecDeque;

let mut buf = VecDeque::new();

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

buf.make_contiguous();
if let (slice, &[]) = buf.as_slices() {
    // 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, &[3, 2, 1] as &[_]);
}

pub fn rotate_left(&mut self, mid: usize)1.36.0[src]

Rotates the double-ended queue mid places to the left.

Equivalently,

  • Rotates item mid into the first position.
  • Pops the first mid items and pushes them to the end.
  • Rotates len() - mid places to the right.

Panics

If mid is greater than len(). Note that mid == len() does not panic and is a no-op rotation.

Complexity

Takes *O*(min(mid, len() - mid)) time and no extra space.

Examples

use std::collections::VecDeque;

let mut buf: VecDeque<_> = (0..10).collect();

buf.rotate_left(3);
assert_eq!(buf, [3, 4, 5, 6, 7, 8, 9, 0, 1, 2]);

for i in 1..10 {
    assert_eq!(i * 3 % 10, buf[0]);
    buf.rotate_left(3);
}
assert_eq!(buf, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);

pub fn rotate_right(&mut self, k: usize)1.36.0[src]

Rotates the double-ended queue k places to the right.

Equivalently,

  • Rotates the first item into position k.
  • Pops the last k items and pushes them to the front.
  • Rotates len() - k places to the left.

Panics

If k is greater than len(). Note that k == len() does not panic and is a no-op rotation.

Complexity

Takes *O*(min(k, len() - k)) time and no extra space.

Examples

use std::collections::VecDeque;

let mut buf: VecDeque<_> = (0..10).collect();

buf.rotate_right(3);
assert_eq!(buf, [7, 8, 9, 0, 1, 2, 3, 4, 5, 6]);

for i in 1..10 {
    assert_eq!(0, buf[i * 3 % 10]);
    buf.rotate_right(3);
}
assert_eq!(buf, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
🔬 This is a nightly-only experimental API. (vecdeque_binary_search)

Binary searches this sorted VecDeque for a given element.

If the value is found then Result::Ok is returned, containing the index of the matching element. If there are multiple matches, then any one of the matches could be returned. If the value is not found then Result::Err is returned, containing the index where a matching element could be inserted while maintaining sorted order.

Examples

Looks up a series of four elements. The first is found, with a uniquely determined position; the second and third are not found; the fourth could match any position in [1, 4].

#![feature(vecdeque_binary_search)]
use std::collections::VecDeque;

let deque: VecDeque<_> = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();

assert_eq!(deque.binary_search(&13),  Ok(9));
assert_eq!(deque.binary_search(&4),   Err(7));
assert_eq!(deque.binary_search(&100), Err(13));
let r = deque.binary_search(&1);
assert!(matches!(r, Ok(1..=4)));

If you want to insert an item to a sorted VecDeque, while maintaining sort order:

#![feature(vecdeque_binary_search)]
use std::collections::VecDeque;

let mut deque: VecDeque<_> = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
let num = 42;
let idx = deque.binary_search(&num).unwrap_or_else(|x| x);
deque.insert(idx, num);
assert_eq!(deque, &[0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);

pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize> where
    F: FnMut(&'a T) -> Ordering
[src]

🔬 This is a nightly-only experimental API. (vecdeque_binary_search)

Binary searches this sorted VecDeque with a comparator function.

The comparator function should implement an order consistent with the sort order of the underlying VecDeque, returning an order code that indicates whether its argument is Less, Equal or Greater than the desired target.

If the value is found then Result::Ok is returned, containing the index of the matching element. If there are multiple matches, then any one of the matches could be returned. If the value is not found then Result::Err is returned, containing the index where a matching element could be inserted while maintaining sorted order.

Examples

Looks up a series of four elements. The first is found, with a uniquely determined position; the second and third are not found; the fourth could match any position in [1, 4].

#![feature(vecdeque_binary_search)]
use std::collections::VecDeque;

let deque: VecDeque<_> = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();

assert_eq!(deque.binary_search_by(|x| x.cmp(&13)),  Ok(9));
assert_eq!(deque.binary_search_by(|x| x.cmp(&4)),   Err(7));
assert_eq!(deque.binary_search_by(|x| x.cmp(&100)), Err(13));
let r = deque.binary_search_by(|x| x.cmp(&1));
assert!(matches!(r, Ok(1..=4)));

pub fn binary_search_by_key<'a, B, F>(
    &'a self,
    b: &B,
    f: F
) -> Result<usize, usize> where
    F: FnMut(&'a T) -> B,
    B: Ord
[src]

🔬 This is a nightly-only experimental API. (vecdeque_binary_search)

Binary searches this sorted VecDeque with a key extraction function.

Assumes that the VecDeque is sorted by the key, for instance with make_contiguous().sort_by_key() using the same key extraction function.

If the value is found then Result::Ok is returned, containing the index of the matching element. If there are multiple matches, then any one of the matches could be returned. If the value is not found then Result::Err is returned, containing the index where a matching element could be inserted while maintaining sorted order.

Examples

Looks up a series of four elements in a slice of pairs sorted by their second elements. The first is found, with a uniquely determined position; the second and third are not found; the fourth could match any position in [1, 4].

#![feature(vecdeque_binary_search)]
use std::collections::VecDeque;

let deque: VecDeque<_> = vec![(0, 0), (2, 1), (4, 1), (5, 1),
         (3, 1), (1, 2), (2, 3), (4, 5), (5, 8), (3, 13),
         (1, 21), (2, 34), (4, 55)].into();

assert_eq!(deque.binary_search_by_key(&13, |&(a, b)| b),  Ok(9));
assert_eq!(deque.binary_search_by_key(&4, |&(a, b)| b),   Err(7));
assert_eq!(deque.binary_search_by_key(&100, |&(a, b)| b), Err(13));
let r = deque.binary_search_by_key(&1, |&(a, b)| b);
assert!(matches!(r, Ok(1..=4)));

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

Modifies the VecDeque 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 std::collections::VecDeque;

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

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

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

Trait Implementations

impl<P, C> Clone for Wire<P, C>[src]

impl<P: Debug, C: Debug> Debug for Wire<P, C>[src]

impl<P, C> Deref for Wire<P, C>[src]

type Target = VecDeque<Edge<P, C>>

The resulting type after dereferencing.

impl<P, C> DerefMut for Wire<P, C>[src]

impl<P, C> Eq for Wire<P, C>[src]

impl<P, C> Extend<Edge<P, C>> for Wire<P, C>[src]

impl<T, P, C> From<T> for Wire<P, C> where
    T: Into<VecDeque<Edge<P, C>>>, 
[src]

impl<'a, P, C> FromIterator<&'a Edge<P, C>> for Wire<P, C>[src]

impl<P, C> FromIterator<Edge<P, C>> for Wire<P, C>[src]

impl<P: Hash, C: Hash> Hash for Wire<P, C>[src]

impl<P, C> IntoIterator for Wire<P, C>[src]

type Item = Edge<P, C>

The type of the elements being iterated over.

type IntoIter = IntoIter<Edge<P, C>>

Which kind of iterator are we turning this into?

impl<'a, P, C> IntoIterator for &'a Wire<P, C>[src]

type Item = &'a Edge<P, C>

The type of the elements being iterated over.

type IntoIter = Iter<'a, Edge<P, C>>

Which kind of iterator are we turning this into?

impl<P, C> PartialEq<Wire<P, C>> for Wire<P, C>[src]

Auto Trait Implementations

impl<P, C> RefUnwindSafe for Wire<P, C>[src]

impl<P, C> Send for Wire<P, C> where
    C: Send,
    P: Send
[src]

impl<P, C> Sync for Wire<P, C> where
    C: Send,
    P: Send
[src]

impl<P, C> Unpin for Wire<P, C>[src]

impl<P, C> UnwindSafe for Wire<P, C>[src]

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> From<T> for T[src]

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

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.