pub struct NonEmptySmallVec<const N: usize, T> {
pub head: T,
pub tail: SmallVec<[T; N]>,
}Expand description
A Non-empty stack vector which can grow to the heap.
See crate::collections::NonEmptyVec for more inforamtion,
as it’s identical to it except that we make use of a SmallVec
instead of a Vec for tail storage.
Note that the total storage is N+1, as N is the size of the tail, but there’s also the head.
Fields§
§head: T§tail: SmallVec<[T; N]>Implementations§
Source§impl<const N: usize, T> NonEmptySmallVec<N, T>
impl<const N: usize, T> NonEmptySmallVec<N, T>
Sourcepub fn new(e: T) -> Self
pub fn new(e: T) -> Self
Alias for NonEmptySmallVec::singleton.
Sourcepub fn to_ref(&self) -> NonEmptySmallVec<N, &T>
pub fn to_ref(&self) -> NonEmptySmallVec<N, &T>
Converts from &NonEmptySmallVec<N, T> to NonEmptySmallVec<N, &T>,
allocating a new tail of borrows. Named to_ (not as_) because it is
not a free view.
Sourcepub fn collect<I>(iter: I) -> Option<Self>where
I: IntoIterator<Item = T>,
pub fn collect<I>(iter: I) -> Option<Self>where
I: IntoIterator<Item = T>,
Attempt to convert an iterator into a NonEmptySmallVec vector.
Returns None if the iterator was empty.
Sourcepub fn first_mut(&mut self) -> &mut T
pub fn first_mut(&mut self) -> &mut T
Get the mutable reference to the first element. Never fails.
Sourcepub fn insert(&mut self, index: usize, element: T)
pub fn insert(&mut self, index: usize, element: T)
Inserts an element at position index within the vector, shifting all elements after it to the right.
§Panics
Panics if index > len.
Sourcepub fn len_nonzero(&self) -> NonZeroUsize
pub fn len_nonzero(&self) -> NonZeroUsize
Gets the length of the list as a NonZeroUsize.
Sourcepub fn capacity(&self) -> NonZeroUsize
pub fn capacity(&self) -> NonZeroUsize
Get the capacity of the list.
Sourcepub fn contains(&self, x: &T) -> boolwhere
T: PartialEq,
pub fn contains(&self, x: &T) -> boolwhere
T: PartialEq,
Check whether an element is contained in the list.
Sourcepub fn truncate(&mut self, len: NonZeroUsize)
pub fn truncate(&mut self, len: NonZeroUsize)
Truncate the list to a certain size. Must be greater than 0.
pub fn iter(&self) -> NonEmptySmallVecIter<'_, T> ⓘ
pub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T> + '_
Sourcepub fn from_slice(slice: &[T]) -> Option<Self>where
T: Clone,
pub fn from_slice(slice: &[T]) -> Option<Self>where
T: Clone,
Often we have a Vec (or slice &[T]) but want to ensure that it is NonEmptySmallVec before
proceeding with a computation. Using from_slice will give us a proof
that we have a NonEmptySmallVec in the Some branch, otherwise it allows
the caller to handle the None case.
Sourcepub fn from_smallvec(vec: SmallVec<[T; N]>) -> Option<Self>
pub fn from_smallvec(vec: SmallVec<[T; N]>) -> Option<Self>
Often we have a Vec (or slice &[T]) but want to ensure that it is NonEmptySmallVec before
proceeding with a computation. Using from_smallvec will give us a proof
that we have a NonEmptySmallVec in the Some branch, otherwise it allows
the caller to handle the None case.
This version will consume the Vec you pass in. If you would rather pass the data as a
slice then use NonEmptySmallVec::from_slice.
Sourcepub fn split_first(&self) -> (&T, &[T])
pub fn split_first(&self) -> (&T, &[T])
Deconstruct a NonEmptySmallVec into its head and tail.
This operation never fails since we are guaranteed
to have a head element.
Sourcepub fn split(&self) -> (&T, &[T], Option<&T>)
pub fn split(&self) -> (&T, &[T], Option<&T>)
Deconstruct a NonEmptySmallVec into its first, last, and
middle elements, in that order.
If there is only one element then last is None.
Sourcepub fn append(&mut self, other: &mut SmallVec<[T; N]>)
pub fn append(&mut self, other: &mut SmallVec<[T; N]>)
Append a Vec to the tail of the NonEmptySmallVec.
Sourcepub fn map<U, F>(self, f: F) -> NonEmptySmallVec<N, U>where
F: FnMut(T) -> U,
pub fn map<U, F>(self, f: F) -> NonEmptySmallVec<N, U>where
F: FnMut(T) -> U,
A structure preserving map. This is useful for when
we wish to keep the NonEmptySmallVec structure guaranteeing
that there is at least one element. Otherwise, we can
use non_empty_smallvec.iter().map(f).
Sourcepub fn try_map<E, U, F>(self, f: F) -> Result<NonEmptySmallVec<N, U>, E>
pub fn try_map<E, U, F>(self, f: F) -> Result<NonEmptySmallVec<N, U>, E>
A structure preserving, fallible mapping function.
Sourcepub fn flat_map<U, F>(self, f: F) -> NonEmptySmallVec<N, U>where
F: FnMut(T) -> NonEmptySmallVec<N, U>,
pub fn flat_map<U, F>(self, f: F) -> NonEmptySmallVec<N, U>where
F: FnMut(T) -> NonEmptySmallVec<N, U>,
When we have a function that goes from some T to a NonEmptySmallVec<U>,
we may want to apply it to a NonEmptySmallVec<T> but keep the structure flat.
This is where flat_map shines.
Sourcepub fn flatten(full: NonEmptySmallVec<N, Self>) -> Self
pub fn flatten(full: NonEmptySmallVec<N, Self>) -> Self
Flatten nested NonEmptySmallVecs into a single one.
Sourcepub fn binary_search(&self, x: &T) -> Result<usize, usize>where
T: Ord,
pub fn binary_search(&self, x: &T) -> Result<usize, usize>where
T: Ord,
Binary searches this sorted non-empty vector 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.
Sourcepub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
Binary searches this sorted non-empty with a comparator function.
The comparator function should implement an order consistent with the sort order of the underlying slice, returning an order code that indicates whether its argument is Less, Equal or Greater 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.
Sourcepub fn binary_search_by_key<'a, B, F>(
&'a self,
b: &B,
f: F,
) -> Result<usize, usize>
pub fn binary_search_by_key<'a, B, F>( &'a self, b: &B, f: F, ) -> Result<usize, usize>
Binary searches this sorted non-empty vector with a key extraction function.
Assumes that the vector is sorted by the key.
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.
Sourcepub fn maximum(&self) -> &Twhere
T: Ord,
pub fn maximum(&self) -> &Twhere
T: Ord,
Returns the maximum element in the non-empty vector.
This will return the first item in the vector if the tail is empty.
Sourcepub fn minimum(&self) -> &Twhere
T: Ord,
pub fn minimum(&self) -> &Twhere
T: Ord,
Returns the minimum element in the non-empty vector.
This will return the first item in the vector if the tail is empty.
Sourcepub fn maximum_by<F>(&self, compare: F) -> &T
pub fn maximum_by<F>(&self, compare: F) -> &T
Returns the element that gives the maximum value with respect to the specified comparison function.
This will return the first item in the vector if the tail is empty.
Sourcepub fn minimum_by<F>(&self, compare: F) -> &T
pub fn minimum_by<F>(&self, compare: F) -> &T
Returns the element that gives the minimum value with respect to the specified comparison function.
This will return the first item in the vector if the tail is empty.
Sourcepub fn maximum_by_key<U, F>(&self, f: F) -> &T
pub fn maximum_by_key<U, F>(&self, f: F) -> &T
Returns the element that gives the maximum value with respect to the specified function.
This will return the first item in the vector if the tail is empty.
Sourcepub fn minimum_by_key<U, F>(&self, f: F) -> &T
pub fn minimum_by_key<U, F>(&self, f: F) -> &T
Returns the element that gives the minimum value with respect to the specified function.
This will return the first item in the vector if the tail is empty.
Sourcepub fn sort(&mut self)where
T: Ord,
pub fn sort(&mut self)where
T: Ord,
Sorts the NonEmptySmallVec.
The implementation uses slice::sort for the tail and then checks where the
head belongs. If the head is already the smallest element, this should be as fast as sorting a
slice. However, if the head needs to be inserted, then it incurs extra cost for removing
the new head from the tail and adding the old head at the correct index.
Sourcepub fn sort_by<F>(&mut self, compare: F)
pub fn sort_by<F>(&mut self, compare: F)
Sorts the NonEmptySmallVec with a comparator function.
The implementation uses slice::sort_by for the tail and then checks where
the head belongs. If the head is already the smallest element, this should be as fast as sorting
a slice. However, if the head needs to be inserted, then it incurs extra cost for removing the
new head from the tail and adding the old head at the correct index.
Sourcepub fn sort_by_key<K, F>(&mut self, f: F)
pub fn sort_by_key<K, F>(&mut self, f: F)
Sorts the NonEmptySmallVec with a key extraction function.
Sourcepub fn sort_by_cached_key<K, F>(&mut self, f: F)
pub fn sort_by_cached_key<K, F>(&mut self, f: F)
Sorts the NonEmptySmallVec with a key extraction function, caching the keys.
The implementation uses slice::sort_by_cached_key
for the tail and then determines where the head belongs using the cached head key.
Trait Implementations§
Source§impl<const N: usize, T: Clone> Clone for NonEmptySmallVec<N, T>
impl<const N: usize, T: Clone> Clone for NonEmptySmallVec<N, T>
Source§fn clone(&self) -> NonEmptySmallVec<N, T>
fn clone(&self) -> NonEmptySmallVec<N, T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<'de, const N: usize, T: Deserialize<'de>> Deserialize<'de> for NonEmptySmallVec<N, T>
impl<'de, const N: usize, T: Deserialize<'de>> Deserialize<'de> for NonEmptySmallVec<N, T>
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
impl<const N: usize, T: Eq> Eq for NonEmptySmallVec<N, T>
Source§impl<const N: usize, A> Extend<A> for NonEmptySmallVec<N, A>
impl<const N: usize, A> Extend<A> for NonEmptySmallVec<N, A>
Source§fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<const N: usize, T> From<NonEmptySmallVec<N, T>> for SmallVec<[T; N]>
impl<const N: usize, T> From<NonEmptySmallVec<N, T>> for SmallVec<[T; N]>
Source§fn from(non_empty_smallvec: NonEmptySmallVec<N, T>) -> Self
fn from(non_empty_smallvec: NonEmptySmallVec<N, T>) -> Self
Turns a non-empty list into a Vec.