Vector

Struct Vector 

Source
pub struct Vector<T, const N: usize>{ /* private fields */ }
Expand description

A persistent vector.

Implementations§

Source§

impl<T, const N: usize> Vector<T, N>

Source

pub fn new() -> Self

Create a new, empty, vector.

Source

pub fn len(&self) -> usize

The number of elements in this vector.

Source

pub fn is_empty(&self) -> bool

Returns true if the length is zero.

Source§

impl<T: Clone, const N: usize> Vector<T, N>

Source

pub fn get(&self, idx: usize) -> Option<&T>

Gets an element at a given index, or None if idx is out-of-bounds.

Source

pub fn set(&mut self, idx: usize, elt: T)

Sets an element at a given index.

Panics if the index is out of bounds.

Source

pub fn push(&mut self, elt: T)

Adds an element to the end of this array.

Runs in time complexity O(log n) where n is the array length.

Source

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

Removes and returns the element at the end of this array, or None if we’re empty.

Runs in time complexity O(log self.len()).

Source

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

If len is less than our length, shortens this vector to length len.

If len is greater than or equal to our length, does nothing.

The running time is O(log self.len() + (len - self.len())). That is, it is linear in the number of elements to be discarded. Even if the elements to be discarded have trivial destructors, we still need to destroy (and potentially de-allocate) a linear number of internal nodes. However, the constant factor in this linear term should be quite small; you can expect that truncate is much faster than multiple calls to pop.

Source

pub fn iter(&self) -> Iter<'_, T, N>

Returns an iterator over all elements in this vector.

Iterator construction runs in O(log self.len()) time. Each step of the iteration runs in amortized constant time, worst-case O(log self.len()) time.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, T, N>

Returns a mutable iterator over all elements in this vector.

Iterator construction runs in O(log self.len()) time. Each step of the iteration runs in amortized constant time, worst-case O(log self.len()) time.

Source

pub fn iter_starting_at(&self, idx: usize) -> Iter<'_, T, N>

Returns an iterator over borrowed elements in this vector, starting at a specific index.

Iterator construction runs in O(log self.len()) time. Each step of the iteration runs in amortized constant time, worst-case O(log self.len()) time. In particular, if idx is large then this is much more efficient than calling iter and then advancing past idx elements.

Source

pub fn iter_mut_starting_at(&mut self, idx: usize) -> IterMut<'_, T, N>

Returns an iterator over mutable elements in this vector, starting at a specific index.

Iterator construction runs in O(log self.len()) time. Each step of the iteration runs in amortized constant time, worst-case O(log self.len()) time. In particular, if idx is large then this is much more efficient than calling iter and then advancing past idx elements.

Source

pub fn into_iter_starting_at(self, idx: usize) -> IntoIter<T, N>

Returns an iterator over borrowed elements in this vector, starting at a specific index.

Iterator construction runs in O(log self.len() + idx) time. Each step of the iteration runs in amortized constant time, worst-case O(log self.len()) time. Unlike the borrowed iter_starting_at, this is not asymptotically faster than calling into_iter and then advancing past idx elements, because we need to destruct and then potentially de-allocate a linear (in idx) number of things. However, this method should be faster in practice than iterating over idx elements.

Trait Implementations§

Source§

impl<T: Clone, const N: usize> Clone for Vector<T, N>

Source§

fn clone(&self) -> Vector<T, N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, const N: usize> Debug for Vector<T, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, const N: usize> Default for Vector<T, N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Clone, const N: usize> Extend<T> for Vector<T, N>

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

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

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: Clone, const N: usize> FromIterator<T> for Vector<T, N>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T: Hash, const N: usize> Hash for Vector<T, N>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

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

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Clone, const N: usize> Index<usize> for Vector<T, N>

Source§

type Output = T

The returned type after indexing.
Source§

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

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, T, const N: usize> IntoIterator for &'a Vector<T, N>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T, N>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T, const N: usize> IntoIterator for &'a mut Vector<T, N>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T, N>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: Clone, const N: usize> IntoIterator for Vector<T, N>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T, N>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq, const N: usize> PartialEq for Vector<T, N>

Source§

fn eq(&self, other: &Vector<T, N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq, const N: usize> Eq for Vector<T, N>

Source§

impl<T, const N: usize> StructuralPartialEq for Vector<T, N>

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for Vector<T, N>

§

impl<T, const N: usize> RefUnwindSafe for Vector<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> !Send for Vector<T, N>

§

impl<T, const N: usize> !Sync for Vector<T, N>

§

impl<T, const N: usize> Unpin for Vector<T, N>

§

impl<T, const N: usize> UnwindSafe for Vector<T, N>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.