pub struct MultiStash<T> { /* private fields */ }Expand description
A vector-like data structure that is able to reuse slots for new elements.
Specifically allows for (armortized) O(1) instructions for:
Implementations§
Source§impl<T> MultiStash<T>
impl<T> MultiStash<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Construct a new, empty MultiStash.
The MultiStash will not allocate until items are put into it.
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Constructs a new, empty MultiStash with at least the specified capacity.
The MultiStash will be able to hold at least capacity elements without reallocating.
This method is allowed to allocate for more elements than capacity.
If capacity is 0, the MultiStash will not allocate.
It is important to note that although the returned MultiStash has the minimum
capacity specified, the MultiStash will have a zero length.
For an explanation of the difference between length and capacity, see Capacity and reallocation.
If it is important to know the exact allocated capacity of a MultiStash,
always use the capacity method after construction.
§Panics
Panics if the new capacity exceeds isize::MAX bytes.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the total number of elements the MultiStash can hold without reallocating.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more elements to be inserted
in the given MultiStash. The collection may reserve more space to
speculatively avoid frequent reallocations. After calling reserve,
capacity will be greater than or equal to self.len() + additional.
Does nothing if capacity is already sufficient.
§Panics
Panics if the new capacity exceeds isize::MAX bytes.
Sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Reserves the minimum capacity for at least additional more elements to
be inserted in the given MultiStash. Unlike reserve, this will not
deliberately over-allocate to speculatively avoid frequent allocations.
After calling 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.
§Panics
Panics if the new capacity exceeds isize::MAX bytes.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the MultiStash.
§Note
A single element might store multiple items.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the MultiStash contains no elements.
Sourcepub fn get(&self, key: Key) -> Option<(usize, &T)>
pub fn get(&self, key: Key) -> Option<(usize, &T)>
Returns a reference to an element at the key if any.
Sourcepub fn get_mut(&mut self, key: Key) -> Option<(usize, &mut T)>
pub fn get_mut(&mut self, key: Key) -> Option<(usize, &mut T)>
Returns a mutable reference to an element at the key if any.
Sourcepub fn put(&mut self, amount: NonZeroUsize, item: T) -> Key
pub fn put(&mut self, amount: NonZeroUsize, item: T) -> Key
Puts an amount of item into the MultiStash.
§Panics
Panics if the new capacity exceeds isize::MAX bytes.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the MultiStash, removing all elements.
Note that this method has no effect on the allocated capacity of the vector.
Sourcepub fn take_all(&mut self, key: Key) -> Option<(usize, T)>
pub fn take_all(&mut self, key: Key) -> Option<(usize, T)>
Removes and returns the element at key and its amount of remaining items.
Returns None if key refers to a vacant entry or is out of bounds.
Sourcepub fn bump(&mut self, key: Key, amount: usize) -> Option<usize>
pub fn bump(&mut self, key: Key, amount: usize) -> Option<usize>
Bumps the amount of items of the element at key if any.
Returns None if not element is found at the key.
§Panics
Panics if amount of the element at key overflows.
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Returns an iterator over the elements of the MultiStash.
The iterator yields all elements, their keys and remaining items from start to end.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
Returns an iterator over the elements of the MultiStash.
The iterator yields mutable references to all elements, their keys and remaining items from start to end.
Source§impl<T: Clone> MultiStash<T>
impl<T: Clone> MultiStash<T>
Trait Implementations§
Source§impl<T: Clone> Clone for MultiStash<T>
impl<T: Clone> Clone for MultiStash<T>
Source§fn clone(&self) -> MultiStash<T>
fn clone(&self) -> MultiStash<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug> Debug for MultiStash<T>
impl<T: Debug> Debug for MultiStash<T>
Source§impl<T> Default for MultiStash<T>
impl<T> Default for MultiStash<T>
Source§impl<T> Extend<(NonZero<usize>, T)> for MultiStash<T>
impl<T> Extend<(NonZero<usize>, T)> for MultiStash<T>
Source§fn extend<I: IntoIterator<Item = (NonZeroUsize, T)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (NonZeroUsize, T)>>(&mut self, iter: I)
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)