[−][src]Enum im::vector::Vector

pub enum Vector<A> {
// some variants omitted
}

A persistent vector.

This is a sequence of elements in insertion order - if you need a list of things, any kind of list of things, this is what you're looking for.

It's implemented as an RRB vector with smart head/tail chunking. In performance terms, this means that practically every operation is O(log n), except push/pop on both sides, which will be O(1) amortised, and O(log n) in the worst case. In practice, the push/pop operations will be blindingly fast, nearly on par with the native VecDeque, and other operations will have decent, if not high, performance, but they all have more or less the same O(log n) complexity, so you don't need to keep their performance characteristics in mind - everything, even splitting and merging, is safe to use and never too slow.

Performance Notes

Because of the head/tail chunking technique, until you push a number of items above double the tree's branching factor (that's self.len() = 2 × k (where k = 64) = 128) on either side, the data structure is still just a handful of arrays, not yet an RRB tree, so you'll see performance and memory characteristics similar to Vec or VecDeque.

This means that the structure always preallocates four chunks of size k (k being the tree's branching factor), equivalent to a Vec with an initial capacity of 256. Beyond that, it will allocate tree nodes of capacity k as needed.

In addition, vectors start out as single chunks, and only expand into the full data structure once you go past the chunk size. This makes them perform identically to Vec at small sizes.

Methods

impl<A: Clone> Vector<A>[src]

#[must_use] pub fn new() -> Self[src]

Construct an empty vector.

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

Get the length of a vector.

Time: O(1)

Examples

assert_eq!(5, vector![1, 2, 3, 4, 5].len());

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

Test whether a vector is empty.

Time: O(1)

Examples

let vec = vector!["Joe", "Mike", "Robert"];
assert_eq!(false, vec.is_empty());
assert_eq!(true, Vector::<i32>::new().is_empty());

ⓘImportant traits for Iter<'a, A>Important traits for Iter<'a, A> impl<'a, A: Clone> Iterator for Iter<'a, A> type Item = &'a A;#[must_use] pub fn iter(&self) -> Iter<A>[src]

Get an iterator over a vector.

Time: O(1)

ⓘImportant traits for IterMut<'a, A>Important traits for IterMut<'a, A> impl<'a, A> Iterator for IterMut<'a, A> where    A: 'a + Clone,  type Item = &'a mut A;#[must_use] pub fn iter_mut(&mut self) -> IterMut<A>[src]

Get a mutable iterator over a vector.

Time: O(1)

ⓘImportant traits for Chunks<'a, A>Important traits for Chunks<'a, A> impl<'a, A: Clone> Iterator for Chunks<'a, A> type Item = &'a [A];#[must_use] pub fn chunks(&self) -> Chunks<A>[src]

Deprecated since 12.3.0:

renamed to leaves to avoid confusion with Vec::chunks

Get an iterator over the leaf nodes of a vector.

This method has been deprecated; use leaves instead.

Time: O(1)

ⓘImportant traits for ChunksMut<'a, A>Important traits for ChunksMut<'a, A> impl<'a, A: Clone> Iterator for ChunksMut<'a, A> type Item = &'a mut [A];#[must_use] pub fn chunks_mut(&mut self) -> ChunksMut<A>[src]

Deprecated since 12.3.0:

renamed to leaves_mut to avoid confusion with Vec::chunks

Get a mutable iterator over the leaf nodes of a vector.

This method has been deprecated; use leaves_mut instead.

Time: O(1)

ⓘImportant traits for Chunks<'a, A>Important traits for Chunks<'a, A> impl<'a, A: Clone> Iterator for Chunks<'a, A> type Item = &'a [A];#[must_use] pub fn leaves(&self) -> Chunks<A>[src]

Get an iterator over the leaf nodes of a vector.

This returns an iterator over the Chunks at the leaves of the RRB tree. These are useful for efficient parallelisation of work on the vector, but should not be used for basic iteration.

Time: O(1)

ⓘImportant traits for ChunksMut<'a, A>Important traits for ChunksMut<'a, A> impl<'a, A: Clone> Iterator for ChunksMut<'a, A> type Item = &'a mut [A];#[must_use] pub fn leaves_mut(&mut self) -> ChunksMut<A>[src]

Get a mutable iterator over the leaf nodes of a vector. This returns an iterator over the Chunks at the leaves of the RRB tree. These are useful for efficient parallelisation of work on the vector, but should not be used for basic iteration.

Time: O(1)

#[must_use] pub fn focus(&self) -> Focus<A>[src]

Construct a Focus for a vector.

Time: O(1)

#[must_use] pub fn focus_mut(&mut self) -> FocusMut<A>[src]

Construct a FocusMut for a vector.

Time: O(1)

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

Get a reference to the value at index index in a vector.

Returns None if the index is out of bounds.

Time: O(log n)

Examples

let vec = vector!["Joe", "Mike", "Robert"];
assert_eq!(Some(&"Robert"), vec.get(2));
assert_eq!(None, vec.get(5));

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

Get a mutable reference to the value at index index in a vector.

Returns None if the index is out of bounds.

Time: O(log n)

Examples

let mut vec = vector!["Joe", "Mike", "Robert"];
{
let robert = vec.get_mut(2).unwrap();
assert_eq!(&mut "Robert", robert);
*robert = "Bjarne";
}
assert_eq!(vector!["Joe", "Mike", "Bjarne"], vec);

#[must_use] pub fn front(&self) -> Option<&A>[src]

Get the first element of a vector.

If the vector is empty, None is returned.

Time: O(log n)

#[must_use] pub fn front_mut(&mut self) -> Option<&mut A>[src]

Get a mutable reference to the first element of a vector.

If the vector is empty, None is returned.

Time: O(log n)

#[must_use] pub fn head(&self) -> Option<&A>[src]

Get the first element of a vector.

If the vector is empty, None is returned.

This is an alias for the front method.

Time: O(log n)

#[must_use] pub fn back(&self) -> Option<&A>[src]

Get the last element of a vector.

If the vector is empty, None is returned.

Time: O(log n)

#[must_use] pub fn back_mut(&mut self) -> Option<&mut A>[src]

Get a mutable reference to the last element of a vector.

If the vector is empty, None is returned.

Time: O(log n)

#[must_use] pub fn last(&self) -> Option<&A>[src]

Get the last element of a vector.

If the vector is empty, None is returned.

This is an alias for the back method.

Time: O(log n)

#[must_use] pub fn index_of(&self, value: &A) -> Option<usize> where    A: PartialEq, [src]

Get the index of a given element in the vector.

Searches the vector for the first occurrence of a given value, and returns the index of the value if it's there. Otherwise, it returns None.

Time: O(n)

Examples

let mut vec = vector![1, 2, 3, 4, 5];
assert_eq!(Some(2), vec.index_of(&3));
assert_eq!(None, vec.index_of(&31337));

#[must_use] pub fn contains(&self, value: &A) -> bool where    A: PartialEq, [src]

Test if a given element is in the vector.

Searches the vector for the first occurrence of a given value, and returns true if it's there. If it's nowhere to be found in the vector, it returnsfalse`.

Time: O(n)

Examples

let mut vec = vector![1, 2, 3, 4, 5];
assert_eq!(true, vec.contains(&3));
assert_eq!(false, vec.contains(&31337));

pub fn clear(&mut self)[src]

Discard all elements from the vector.

This leaves you with an empty vector, and all elements that were previously inside it are dropped.

Time: O(n)

#[must_use] pub fn binary_search_by<F>(&self, f: F) -> Result<usize, usize> where    F: FnMut(&A) -> Ordering, [src]

Binary search a sorted vector for a given element using a comparator function.

Assumes the vector has already been sorted using the same comparator function, eg. by using sort_by.

If the value is found, it returns Ok(index) where index is the index of the element. If the value isn't found, it returns Err(index) where index is the index at which the element would need to be inserted to maintain sorted order.

Time: O(log n)

Binary search a sorted vector for a given element.

If the value is found, it returns Ok(index) where index is the index of the element. If the value isn't found, it returns Err(index) where index is the index at which the element would need to be inserted to maintain sorted order.

Time: O(log n)

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

Binary search a sorted vector for a given element with a key extract function.

Assumes the vector has already been sorted using the same key extract function, eg. by using sort_by_key.

If the value is found, it returns Ok(index) where index is the index of the element. If the value isn't found, it returns Err(index) where index is the index at which the element would need to be inserted to maintain sorted order.

Time: O(log n)

impl<A: Clone> Vector<A>[src]

#[must_use] pub fn singleton(a: A) -> Self[src]

Deprecated since 12.3.0:

renamed to unit for consistency

Construct a vector with a single value.

This method has been deprecated; use unit instead.

#[must_use] pub fn unit(a: A) -> Self[src]

Construct a vector with a single value.

Examples

let vec = Vector::unit(1337);
assert_eq!(1, vec.len());
assert_eq!(
vec.get(0),
Some(&1337)
);

#[must_use] pub fn update(&self, index: usize, value: A) -> Self[src]

Create a new vector with the value at index index updated.

Panics if the index is out of bounds.

Time: O(log n)

Examples

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

pub fn set(&mut self, index: usize, value: A) -> A[src]

Update the value at index index in a vector.

Returns the previous value at the index.

Panics if the index is out of bounds.

Time: O(log n)

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

Swap the elements at indices i and j.

Time: O(log n)

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

Push a value to the front of a vector.

Time: O(1)*

Examples

let mut vec = vector![5, 6, 7];
vec.push_front(4);
assert_eq!(vector![4, 5, 6, 7], vec);

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

Push a value to the back of a vector.

Time: O(1)*

Examples

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

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

Remove the first element from a vector and return it.

Time: O(1)*

Examples

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

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

Remove the last element from a vector and return it.

Time: O(1)*

Examples

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

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

Append the vector other to the end of the current vector.

Time: O(log n)

Examples

let mut vec = vector![1, 2, 3];
vec.append(vector![7, 8, 9]);
assert_eq!(vector![1, 2, 3, 7, 8, 9], vec);

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

Retain only the elements specified by the predicate.

Remove all elements for which the provided function f returns false from the vector.

Time: O(n)

pub fn split_at(self, index: usize) -> (Self, Self)[src]

Split a vector at a given index.

Split a vector at a given index, consuming the vector and returning a pair of the left hand side and the right hand side of the split.

Time: O(log n)

Examples

let mut vec = vector![1, 2, 3, 7, 8, 9];
let (left, right) = vec.split_at(3);
assert_eq!(vector![1, 2, 3], left);
assert_eq!(vector![7, 8, 9], right);

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

Split a vector at a given index.

Split a vector at a given index, leaving the left hand side in the current vector and returning a new vector containing the right hand side.

Time: O(log n)

Examples

let mut left = vector![1, 2, 3, 7, 8, 9];
let right = left.split_off(3);
assert_eq!(vector![1, 2, 3], left);
assert_eq!(vector![7, 8, 9], right);

#[must_use] pub fn skip(&self, count: usize) -> Self[src]

Construct a vector with count elements removed from the start of the current vector.

Time: O(log n)

#[must_use] pub fn take(&self, count: usize) -> Self[src]

Construct a vector of the first count elements from the current vector.

Time: O(log n)

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

Truncate a vector to the given size.

Discards all elements in the vector beyond the given length.

Panics if the new length is greater than the current length.

Time: O(log n)

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

Extract a slice from a vector.

Remove the elements from start_index until end_index in the current vector and return the removed slice as a new vector.

Time: O(log n)

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

Insert an element into a vector.

Insert an element at position index, shifting all elements after it to the right.

Performance Note

While push_front and push_back are heavily optimised operations, insert in the middle of a vector requires a split, a push, and an append. Thus, if you want to insert many elements at the same location, instead of inserting them one by one, you should rather create a new vector containing the elements to insert, split the vector at the insertion point, and append the left hand, the new vector and the right hand in order.

Time: O(log n)

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

Remove an element from a vector.

Remove the element from position 'index', shifting all elements after it to the left, and return the removec element.

Performance Note

While pop_front and pop_back are heavily optimised operations, remove in the middle of a vector requires a split, a pop, and an append. Thus, if you want to remove many elements from the same location, instead of removeing them one by one, it is much better to use slice.

Time: O(log n)

pub fn insert_ord(&mut self, item: A) where    A: Ord, [src]

Insert an element into a sorted vector.

Insert an element into a vector in sorted order, assuming the vector is already in sorted order.

Time: O(log n)

Examples

let mut vec = vector![1, 2, 3, 7, 8, 9];
vec.insert_ord(5);
assert_eq!(vector![1, 2, 3, 5, 7, 8, 9], vec);

Sort a vector.

Time: O(n log n)

Examples

let mut vec = vector![3, 2, 5, 4, 1];
vec.sort();
assert_eq!(vector![1, 2, 3, 4, 5], vec);

pub fn sort_by<F>(&mut self, cmp: F) where    F: Fn(&A, &A) -> Ordering, [src]

Sort a vector using a comparator function.

Time: O(n log n)

Examples

let mut vec = vector![3, 2, 5, 4, 1];
vec.sort_by(|left, right| left.cmp(right));
assert_eq!(vector![1, 2, 3, 4, 5], vec);

Trait Implementations

impl<A: Clone> Clone for Vector<A>[src]

default fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<A: Clone + Ord> Ord for Vector<A>[src]

default fn max(self, other: Self) -> Self1.21.0[src]

Compares and returns the maximum of two values. Read more

default fn min(self, other: Self) -> Self1.21.0[src]

Compares and returns the minimum of two values. Read more

default fn clamp(self, min: Self, max: Self) -> Self[src]

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

Restrict a value to a certain interval. Read more

impl<A: Clone> Extend<A> for Vector<A>[src]

fn extend<I>(&mut self, iter: I) where    I: IntoIterator<Item = A>, [src]

Add values to the end of a vector by consuming an iterator.

Time: O(n)

impl<A: Clone + PartialOrd> PartialOrd<Vector<A>> for Vector<A>[src]

#[must_use] default fn lt(&self, other: &Rhs) -> bool1.0.0[src]

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

#[must_use] default fn le(&self, other: &Rhs) -> bool1.0.0[src]

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

#[must_use] default fn gt(&self, other: &Rhs) -> bool1.0.0[src]

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

#[must_use] default fn ge(&self, other: &Rhs) -> bool1.0.0[src]

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

impl<A: Clone + PartialEq> PartialEq<Vector<A>> for Vector<A>[src]

#[must_use] default fn ne(&self, other: &Rhs) -> bool1.0.0[src]

This method tests for !=.

impl<A: Clone + Eq> PartialEq<Vector<A>> for Vector<A>[src]

#[must_use] default fn ne(&self, other: &Rhs) -> bool1.0.0[src]

This method tests for !=.

impl<A: Clone> From<Vec<A>> for Vector<A>[src]

fn from(vec: Vec<A>) -> Self[src]

Create a vector from a std::vec::Vec.

Time: O(n)

impl<'a, A: Clone> From<&'a Vec<A>> for Vector<A>[src]

fn from(vec: &Vec<A>) -> Self[src]

Create a vector from a std::vec::Vec.

Time: O(n)

impl<'a, A: Clone> IntoIterator for &'a Vector<A>[src]

type Item = &'a A

The type of the elements being iterated over.

type IntoIter = Iter<'a, A>

Which kind of iterator are we turning this into?

impl<A: Clone> IntoIterator for Vector<A>[src]

type Item = A

The type of the elements being iterated over.

type IntoIter = ConsumingIter<A>

Which kind of iterator are we turning this into?

impl<A: Clone + Hash> Hash for Vector<A>[src]

default fn hash_slice<H>(data: &[Self], state: &mut H) where    H: Hasher, 1.3.0[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl<A: Clone> Add<Vector<A>> for Vector<A>[src]

type Output = Vector<A>

The resulting type after applying the + operator.

fn add(self, other: Self) -> Self::Output[src]

Concatenate two vectors.

Time: O(log n)

impl<'a, A: Clone> Add<&'a Vector<A>> for &'a Vector<A>[src]

type Output = Vector<A>

The resulting type after applying the + operator.

fn add(self, other: Self) -> Self::Output[src]

Concatenate two vectors.

Time: O(log n)

impl<A: Clone> Index<usize> for Vector<A>[src]

type Output = A

The returned type after indexing.

fn index(&self, index: usize) -> &Self::Output[src]

Get a reference to the value at index index in the vector.

Time: O(log n)

impl<A: Clone> IndexMut<usize> for Vector<A>[src]

fn index_mut(&mut self, index: usize) -> &mut Self::Output[src]

Get a mutable reference to the value at index index in the vector.

Time: O(log n)

impl<A: Clone> FromIterator<A> for Vector<A>[src]

fn from_iter<I>(iter: I) -> Self where    I: IntoIterator<Item = A>, [src]

Create a vector from an iterator.

Time: O(n)

Blanket Implementations

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, U> TryFrom for T where    U: Into<T>, [src]

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.

impl<T> Same for T[src]

type Output = T

Should always be Self