Struct more_collections::small_map::SmallMap
source · pub struct SmallMap<K, V, const C: usize, S = RandomState> { /* private fields */ }
Expand description
A map-like container that can store a specified number of elements inline.
SmallMap
shares most of its API with, and behaves like
IndexMap
. It can store a limited amount of data
inline, backed by SmallVec
. If the data exceeds the limit C
,
SmallMap
will move all its data over to the heap in the form of an
IndexMap
. For performance reasons, transitions between heap and inline
storage should generally be avoided.
The SmallMap
datastructure is meant for situations where the data does not
exceed C
most of the time but it still needs to support cases where the
data does exceed C
.
§Example
use more_collections::SmallMap;
let mut map = SmallMap::<usize, String, 3>::new();
// The map can hold up to three items inline
map.insert(0, "zero".to_string());
map.insert(1, "one".to_string());
map.insert(2, "two".to_string());
assert_eq!(3, map.len());
assert!(map.is_inline());
// Adding the fourth item will move the map to the heap
map.insert(3, "three".to_string());
assert_eq!(4, map.len());
assert!(!map.is_inline());
Implementations§
source§impl<K, V, const C: usize, S> SmallMap<K, V, C, S>
impl<K, V, const C: usize, S> SmallMap<K, V, C, S>
sourcepub const fn inline_capacity(&self) -> usize
pub const fn inline_capacity(&self) -> usize
The memory capacity that will be allocated inline. If the nubmer of values exceeds the inline capacity, the map will move to the heap.
sourcepub const fn is_inline(&self) -> bool
pub const fn is_inline(&self) -> bool
Is the data contained by this map stored inline (true
) or on the heap
(false
).
sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, K, V> ⓘ
Returns an iterator over the key-values in insertion order.
sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
Returns an iterator over the key-values in insertion order.
pub fn keys(&self) -> Keys<'_, K, V> ⓘ
source§impl<K, V, const C: usize, S> SmallMap<K, V, C, S>
impl<K, V, const C: usize, S> SmallMap<K, V, C, S>
sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
Return a reference to the value stored for key
, if it is present,
else None
.
Computational complexity:
- inline: O(n)
- heap: O(1)
sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Return a mutable reference to the value stored for key
, if it is
present, else None
.
Computational complexity:
- inline: O(n)
- heap: O(1)
sourcepub fn get_index(&self, index: usize) -> Option<(&K, &V)>
pub fn get_index(&self, index: usize) -> Option<(&K, &V)>
Get a key-value pair by index, if it is present, else None
.
Computational complexity: O(1)
sourcepub fn get_index_mut(&mut self, index: usize) -> Option<(&K, &mut V)>
pub fn get_index_mut(&mut self, index: usize) -> Option<(&K, &mut V)>
Get a mutable key-value pair by index, if it is present, else None
.
Computational complexity: O(1)
sourcepub fn get_index_of<Q>(&self, key: &Q) -> Option<usize>
pub fn get_index_of<Q>(&self, key: &Q) -> Option<usize>
Return the item index, if it exists in the map, else None
.
Computational complexity:
- inline: O(n)
- heap: O(1)
sourcepub fn entry(&mut self, key: K) -> Entry<'_, K, V, C, S>
pub fn entry(&mut self, key: K) -> Entry<'_, K, V, C, S>
Get the given key’s corresponding entry in the map for insertion and/or in-place manipulation.
Computational complexity:
- inline: O(n)
- heap: O(1)
sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Return true
if an equivalent to key
exists in the map.
Computational complexity:
- inline: O(n)
- heap: O(1)
sourcepub fn from_map(map: IndexMap<K, V, S>) -> Self
pub fn from_map(map: IndexMap<K, V, S>) -> Self
Convert the specified map and turn it into a SmallMap
.
If the map len is smaller or equal the inline capacity, the data will be moved inline.
sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<V>
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
Remove the key-value pair equivalent to key
and return its value.
If key
is not present None
is returned.
If an existing key is removed that causes the size of the SmallMap
to
be equal to or below the inline capacity, all remaining data after
removal of the specified key-value pair is moved to the heap.
The behavior of this method is equivalent to .swap_remove(key)
on
HashMap
s and Vec
s, order is not preserved.
Computational complexity:
- inline: O(n)
- heap: O(1)
sourcepub fn swap_remove_full<Q>(&mut self, key: &Q) -> Option<(usize, K, V)>
pub fn swap_remove_full<Q>(&mut self, key: &Q) -> Option<(usize, K, V)>
Remove the key-value pair equivalent to key
and return its index, key,
and value.
If key
is not present None
is returned.
If an existing key is removed that causes the size of the SmallMap
to
be equal to or below the inline capacity, all remaining data after
removal of the specified key-value pair is moved to the heap.
The behavior of this method is equivalent to .swap_remove(key)
on
HashMap
s and Vec
s, order is not preserved.
Computational complexity:
- inline: O(n)
- heap: O(1)
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 map 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.
§Errors
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.
source§impl<K, V, const C: usize, S> SmallMap<K, V, C, S>
impl<K, V, const C: usize, S> SmallMap<K, V, C, S>
sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts the specified key-value pair into this map.
If a value for the specified key
already exists, the new value will
overwrite the existing value. The iteration order of the key-value pair
will remain in the original position.
If a new key is added that causes the size of the SmallMap
to exceed
the inline capacity, all existing data and the new key-value pair is
moved to the heap.
Computational complexity:
- inline: O(n)
- heap: O(1)
sourcepub fn insert_full(&mut self, key: K, value: V) -> (usize, Option<V>)
pub fn insert_full(&mut self, key: K, value: V) -> (usize, Option<V>)
Inserts the specified key-value pair into this map, and get their index.
If a value for the specified key
already exists, the new value will
overwrite the existing value. The iteration order of the key-value pair
will remain in the original position.
If a new key is added that causes the size of the SmallMap
to exceed
the inline capacity, all existing data and the new key-value pair is
moved to the heap.
Computational complexity:
- inline: O(n)
- heap: O(1)
Trait Implementations§
source§impl<'a, K, V, const C: usize, S> IntoIterator for &'a SmallMap<K, V, C, S>
impl<'a, K, V, const C: usize, S> IntoIterator for &'a SmallMap<K, V, C, S>
source§impl<'a, K, V, const C: usize, S> IntoIterator for &'a mut SmallMap<K, V, C, S>
impl<'a, K, V, const C: usize, S> IntoIterator for &'a mut SmallMap<K, V, C, S>
source§impl<K, V, const C: usize, S> IntoIterator for SmallMap<K, V, C, S>
impl<K, V, const C: usize, S> IntoIterator for SmallMap<K, V, C, S>
source§impl<K, V, const C: usize, S> PartialEq for SmallMap<K, V, C, S>
impl<K, V, const C: usize, S> PartialEq for SmallMap<K, V, C, S>
impl<K, V, const C: usize, S> Eq for SmallMap<K, V, C, S>
Auto Trait Implementations§
impl<K, V, const C: usize, S> Freeze for SmallMap<K, V, C, S>
impl<K, V, const C: usize, S> RefUnwindSafe for SmallMap<K, V, C, S>
impl<K, V, const C: usize, S> Send for SmallMap<K, V, C, S>
impl<K, V, const C: usize, S> Sync for SmallMap<K, V, C, S>
impl<K, V, const C: usize, S> Unpin for SmallMap<K, V, C, S>
impl<K, V, const C: usize, S> UnwindSafe for SmallMap<K, V, C, S>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.