pub enum SmallMap<const N: usize, K, V, S = RandomState> {
Heap(HashMap<K, V, S>),
Inline(Inline<N, K, V, S>),
}Variants§
Implementations§
Source§impl<const N: usize, K, V, S: Default> SmallMap<N, K, V, S>
impl<const N: usize, K, V, S: Default> SmallMap<N, K, V, S>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty SmallMap with the specified capacity.
The hash map will be able to hold at least capacity elements without
reallocating. If capacity is smaller than N, the hash map will not allocate.
Source§impl<const N: usize, K, V, S> SmallMap<N, K, V, S>
impl<const N: usize, K, V, S> SmallMap<N, K, V, S>
Sourcepub const fn with_hasher(hash_builder: S) -> Self
pub const fn with_hasher(hash_builder: S) -> Self
Creates an empty SmallMap which will use the given hash builder to hash
keys. It will be allocated with the given allocator.
The hash map is initially created with a capacity of N, so it will not allocate until it its size bigger than inline size N.
§Examples
use core::hash::BuildHasherDefault;
use small_map::SmallMap;
let s = BuildHasherDefault::<ahash::AHasher>::default();
let mut map = SmallMap::<8, _, _, _>::with_hasher(s);
map.insert(1, 2);Sourcepub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self
Creates an empty SmallMap with the specified capacity, using hash_builder
to hash the keys.
The hash map will be able to hold at least capacity elements without
reallocating. If capacity is smaller than or eq to N, the hash map will not allocate.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the map can hold without reallocating.
This number is a lower bound; the SmallMap<N, K, V> might be able to hold
more, but is guaranteed to be able to hold at least this many.
§Examples
use small_map::SmallMap;
let map: SmallMap<8, i32, i32> = SmallMap::with_capacity(100);
assert_eq!(map.len(), 0);
assert!(map.capacity() >= 100);
let map: SmallMap<8, i32, i32> = SmallMap::with_capacity(2);
assert_eq!(map.len(), 0);
assert!(map.capacity() >= 8);Source§impl<const N: usize, K, V, S> SmallMap<N, K, V, S>
impl<const N: usize, K, V, S> SmallMap<N, K, V, S>
Sourcepub fn get<Q>(&self, k: &Q) -> Option<&V>
pub fn get<Q>(&self, k: &Q) -> Option<&V>
Returns a reference to the value corresponding to the key.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, &str> = SmallMap::new();
map.insert(1, "a");
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);Sourcepub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the key.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, i32> = SmallMap::new();
map.insert(1, 10);
if let Some(v) = map.get_mut(&1) {
*v = 20;
}
assert_eq!(map.get(&1), Some(&20));Sourcepub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)>
Returns the key-value pair corresponding to the supplied key.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, &str> = SmallMap::new();
map.insert(1, "a");
assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
assert_eq!(map.get_key_value(&2), None);Sourcepub fn contains_key<Q>(&self, k: &Q) -> bool
pub fn contains_key<Q>(&self, k: &Q) -> bool
Returns true if the map contains a value for the specified key.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, &str> = SmallMap::new();
map.insert(1, "a");
assert!(map.contains_key(&1));
assert!(!map.contains_key(&2));Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts a key-value pair into the map.
If the map did not have this key present, None is returned.
If the map did have this key present, the value is updated, and the old value is returned.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, &str> = SmallMap::new();
assert_eq!(map.insert(1, "a"), None);
assert_eq!(map.insert(1, "b"), Some("a"));
assert_eq!(map.get(&1), Some(&"b"));Sourcepub fn remove<Q>(&mut self, k: &Q) -> Option<V>
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
Removes a key from the map, returning the value at the key if the key was previously in the map.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, &str> = SmallMap::new();
map.insert(1, "a");
assert_eq!(map.remove(&1), Some("a"));
assert_eq!(map.remove(&1), None);Sourcepub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
pub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
Removes a key from the map, returning the stored key and value if the key was previously in the map.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, &str> = SmallMap::new();
map.insert(1, "a");
assert_eq!(map.remove_entry(&1), Some((1, "a")));
assert_eq!(map.remove_entry(&1), None);Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Retains only the elements specified by the predicate.
In other words, remove all pairs (k, v) for which f(&k, &mut v) returns false.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, i32> = SmallMap::new();
for i in 0..8 {
map.insert(i, i * 10);
}
map.retain(|&k, _| k % 2 == 0);
assert_eq!(map.len(), 4);Source§impl<const N: usize, K, V, S> SmallMap<N, K, V, S>where
S: Clone,
impl<const N: usize, K, V, S> SmallMap<N, K, V, S>where
S: Clone,
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the map.
This method clears the map as resets it to the Inline state.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, ()> = SmallMap::new();
for i in 0..16 {
map.insert(i, ());
}
assert!(!map.is_inline());
assert_eq!(map.len(), 16);
map.clear();
assert!(map.is_inline());
assert_eq!(map.len(), 0);Source§impl<const N: usize, K, V, S> SmallMap<N, K, V, S>
impl<const N: usize, K, V, S> SmallMap<N, K, V, S>
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the map.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, i32> = SmallMap::new();
assert_eq!(map.len(), 0);
map.insert(1, 10);
assert_eq!(map.len(), 1);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the map contains no elements.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, i32> = SmallMap::new();
assert!(map.is_empty());
map.insert(1, 10);
assert!(!map.is_empty());Sourcepub fn iter(&self) -> Iter<'_, N, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, N, K, V> ⓘ
An iterator visiting all key-value pairs in arbitrary order.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, &str, i32> = SmallMap::new();
map.insert("a", 1);
map.insert("b", 2);
for (key, val) in map.iter() {
println!("key: {key} val: {val}");
}Sourcepub fn keys(&self) -> Keys<'_, N, K, V> ⓘ
pub fn keys(&self) -> Keys<'_, N, K, V> ⓘ
An iterator visiting all keys in arbitrary order.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, &str, i32> = SmallMap::new();
map.insert("a", 1);
map.insert("b", 2);
for key in map.keys() {
println!("{key}");
}Sourcepub fn values(&self) -> Values<'_, N, K, V> ⓘ
pub fn values(&self) -> Values<'_, N, K, V> ⓘ
An iterator visiting all values in arbitrary order.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, &str, i32> = SmallMap::new();
map.insert("a", 1);
map.insert("b", 2);
for val in map.values() {
println!("{val}");
}Sourcepub fn into_keys(self) -> IntoKeys<N, K, V> ⓘ
pub fn into_keys(self) -> IntoKeys<N, K, V> ⓘ
Creates a consuming iterator visiting all the keys in arbitrary order.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, &str, i32> = SmallMap::new();
map.insert("a", 1);
map.insert("b", 2);
let keys: Vec<_> = map.into_keys().collect();Sourcepub fn into_values(self) -> IntoValues<N, K, V> ⓘ
pub fn into_values(self) -> IntoValues<N, K, V> ⓘ
Creates a consuming iterator visiting all the values in arbitrary order.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, &str, i32> = SmallMap::new();
map.insert("a", 1);
map.insert("b", 2);
let values: Vec<_> = map.into_values().collect();Trait Implementations§
Source§impl<const N: usize, K, V, S: Default> Default for SmallMap<N, K, V, S>
impl<const N: usize, K, V, S: Default> Default for SmallMap<N, K, V, S>
Source§fn default() -> Self
fn default() -> Self
Creates an empty SmallMap<N, K, V, S>, with the Default value for the hasher.
§Examples
use std::collections::hash_map::RandomState;
use small_map::SmallMap;
// You can specify all types of SmallMap, including N and hasher.
// Created map is empty and don't allocate memory
let map: SmallMap<8, u32, String> = SmallMap::default();
assert_eq!(map.capacity(), 8);
let map: SmallMap<8, u32, String, RandomState> = SmallMap::default();
assert_eq!(map.capacity(), 8);Source§impl<const N: usize, K, V, S> Extend<(K, V)> for SmallMap<N, K, V, S>
impl<const N: usize, K, V, S> Extend<(K, V)> for SmallMap<N, K, V, S>
Source§fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, V)>>(&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)