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>
pub fn get<Q>(&self, k: &Q) -> Option<&V>
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.
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.
pub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
pub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
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);