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> SmallMap<K, V, C>

source

pub fn new() -> Self

Create a new map.

source§

impl<K, V, const C: usize, S> SmallMap<K, V, C, S>

source

pub fn len(&self) -> usize

The number of key-values stored in the map.

source

pub fn is_empty(&self) -> bool

Returns true if the map is empty.

source

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.

source

pub const fn is_inline(&self) -> bool

Is the data contained by this map stored inline (true) or on the heap (false).

source

pub fn iter(&self) -> Iter<'_, K, V>

Returns an iterator over the key-values in insertion order.

source

pub fn iter_mut(&mut self) -> IterMut<'_, K, V>

Returns an iterator over the key-values in insertion order.

source

pub fn keys(&self) -> Keys<'_, K, V>

source§

impl<K, V, const C: usize, S> SmallMap<K, V, C, S>
where K: Hash + Eq, S: BuildHasher,

source

pub fn get<Q>(&self, key: &Q) -> Option<&V>
where Q: Hash + Equivalent<K> + ?Sized,

Return a reference to the value stored for key, if it is present, else None.

Computational complexity:

  • inline: O(n)
  • heap: O(1)
source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where Q: Hash + Equivalent<K> + ?Sized,

Return a mutable reference to the value stored for key, if it is present, else None.

Computational complexity:

  • inline: O(n)
  • heap: O(1)
source

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)

source

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)

source

pub fn get_index_of<Q>(&self, key: &Q) -> Option<usize>
where Q: Hash + Equivalent<K> + ?Sized,

Return the item index, if it exists in the map, else None.

Computational complexity:

  • inline: O(n)
  • heap: O(1)
source

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)
source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where Q: Hash + Equivalent<K> + ?Sized,

Return true if an equivalent to key exists in the map.

Computational complexity:

  • inline: O(n)
  • heap: O(1)
source

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.

source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where Q: Hash + Equivalent<K> + ?Sized,

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 HashMaps and Vecs, order is not preserved.

Computational complexity:

  • inline: O(n)
  • heap: O(1)
source

pub fn swap_remove_full<Q>(&mut self, key: &Q) -> Option<(usize, K, V)>
where Q: Hash + Equivalent<K> + ?Sized,

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 HashMaps and Vecs, order is not preserved.

Computational complexity:

  • inline: O(n)
  • heap: O(1)
source

pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
where F: FnMut((&'a K, &'a V)) -> Ordering,

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>
where K: Hash + Eq, S: BuildHasher + Default,

source

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)
source

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<K: Clone, V: Clone, const C: usize, S: Clone> Clone for SmallMap<K, V, C, S>

source§

fn clone(&self) -> SmallMap<K, V, C, S>

Returns a copy 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<K, V, const C: usize, S> Debug for SmallMap<K, V, C, S>
where K: Debug, V: Debug,

source§

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

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

impl<K, V, const C: usize, S> Default for SmallMap<K, V, C, S>

source§

fn default() -> Self

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

impl<K, V, const C: usize, S> FromIterator<(K, V)> for SmallMap<K, V, C, S>
where K: Hash + Eq, S: BuildHasher + Default,

source§

fn from_iter<I: IntoIterator<Item = (K, V)>>(iterable: I) -> Self

Creates a value from an iterator. Read more
source§

impl<K, V, const C: usize, S> Hash for SmallMap<K, V, C, S>
where K: Hash + Eq, V: Hash + Eq,

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<K, V, Q, const C: usize, S> Index<&Q> for SmallMap<K, V, C, S>
where K: Eq + Hash, Q: Hash + Equivalent<K> + ?Sized, S: BuildHasher,

§

type Output = V

The returned type after indexing.
source§

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

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

impl<K, V, const C: usize, S> Index<usize> for SmallMap<K, V, C, S>
where K: Eq + Hash, S: BuildHasher,

§

type Output = V

The returned type after indexing.
source§

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

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

impl<K, V, Q, const C: usize, S> IndexMut<&Q> for SmallMap<K, V, C, S>
where K: Eq + Hash, Q: Hash + Equivalent<K> + ?Sized, S: BuildHasher,

source§

fn index_mut(&mut self, key: &Q) -> &mut Self::Output

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

impl<K, V, const C: usize, S> IndexMut<usize> for SmallMap<K, V, C, S>
where K: Eq + Hash, S: BuildHasher,

source§

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

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

impl<'a, K, V, const C: usize, S> IntoIterator for &'a SmallMap<K, V, C, S>

§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, K, V, const C: usize, S> IntoIterator for &'a mut SmallMap<K, V, C, S>

§

type IntoIter = IterMut<'a, K, V>

Which kind of iterator are we turning this into?
§

type Item = (&'a K, &'a mut V)

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<K, V, const C: usize, S> IntoIterator for SmallMap<K, V, C, S>

§

type Item = (K, V)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<K, V, C>

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<K, V, const C: usize, S> PartialEq for SmallMap<K, V, C, S>
where K: Hash + PartialEq, V: PartialEq,

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K, V, const C: usize, S> Eq for SmallMap<K, V, C, S>
where K: Hash + Eq, V: Eq,

Auto Trait Implementations§

§

impl<K, V, const C: usize, S> Freeze for SmallMap<K, V, C, S>
where S: Freeze, K: Freeze, V: Freeze,

§

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>
where S: Send, K: Send, V: Send,

§

impl<K, V, const C: usize, S> Sync for SmallMap<K, V, C, S>
where S: Sync, K: Sync, V: Sync,

§

impl<K, V, const C: usize, S> Unpin for SmallMap<K, V, C, S>
where S: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, const C: usize, S> UnwindSafe for SmallMap<K, V, C, S>

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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,

§

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>,

§

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>,

§

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.