pub struct Map<K: PartialEq, V, const N: usize> { /* private fields */ }
Expand description
A faster alternative of std::collections::HashMap
.
For example, this is how you make a map, which is allocated on stack and is capable of storing up to eight key-values pairs:
let mut m : micromap::Map<u64, &str, 8> = micromap::Map::new();
m.insert(1, "Jeff Lebowski");
m.insert(2, "Walter Sobchak");
assert_eq!(2, m.len());
It is faster because it doesn’t use a hash function at all. It simply keeps
all pairs in an array and when it’s necessary to find a value, it goes through
all pairs comparing the needle with each pair available. Also it is faster
because it doesn’t use heap. When a Map
is being created, it allocates the necessary
space on stack. That’s why the maximum size of the map must be provided in
compile time.
It is also faster because it doesn’t grow in size. When a Map
is created,
its size is fixed on stack. If an attempt is made to insert too many keys
into it, it simply panics. Moreover, in the “release” mode it doesn’t panic,
but its behaviour is undefined. In the “release” mode all boundary checks
are disabled, for the sake of higher performance.
Implementations§
source§impl<K: PartialEq, V, const N: usize> Map<K, V, N>
impl<K: PartialEq, V, const N: usize> Map<K, V, N>
sourcepub fn contains_key<Q: PartialEq + ?Sized>(&self, k: &Q) -> boolwhere
K: Borrow<Q>,
pub fn contains_key<Q: PartialEq + ?Sized>(&self, k: &Q) -> boolwhere
K: Borrow<Q>,
Does the map contain this key?
sourcepub fn insert(&mut self, k: K, v: V)
pub fn insert(&mut self, k: K, v: V)
Insert a single pair into the map.
Panics
It may panic if there are too many pairs in the map already. Pay attention,
it panics only in the “debug” mode. In the “release” mode, you are going to get
undefined behavior. This is done for the sake of performance, in order to
avoid a repetitive check for the boundary condition on every insert()
.
sourcepub fn get<Q: PartialEq + ?Sized>(&self, k: &Q) -> Option<&V>where
K: Borrow<Q>,
pub fn get<Q: PartialEq + ?Sized>(&self, k: &Q) -> Option<&V>where
K: Borrow<Q>,
Get a reference to a single value.
sourcepub fn retain<F: Fn(&K, &V) -> bool>(&mut self, f: F)
pub fn retain<F: Fn(&K, &V) -> bool>(&mut self, f: F)
Retains only the elements specified by the predicate.
source§impl<K: PartialEq, V, const N: usize> Map<K, V, N>
impl<K: PartialEq, V, const N: usize> Map<K, V, N>
sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
An iterator visiting all values mutably in arbitrary order.
sourcepub fn into_values(self) -> IntoValues<K, V, N> ⓘ
pub fn into_values(self) -> IntoValues<K, V, N> ⓘ
Consuming iterator visiting all the values in arbitrary order.
Trait Implementations§
source§impl<K: PartialEq + Borrow<Q>, Q: PartialEq + ?Sized, V, const N: usize> Index<&Q> for Map<K, V, N>
impl<K: PartialEq + Borrow<Q>, Q: PartialEq + ?Sized, V, const N: usize> Index<&Q> for Map<K, V, N>
source§impl<K: PartialEq + Borrow<Q>, Q: PartialEq + ?Sized, V, const N: usize> IndexMut<&Q> for Map<K, V, N>
impl<K: PartialEq + Borrow<Q>, Q: PartialEq + ?Sized, V, const N: usize> IndexMut<&Q> for Map<K, V, N>
source§impl<K: PartialEq, V: PartialEq, const N: usize> PartialEq for Map<K, V, N>
impl<K: PartialEq, V: PartialEq, const N: usize> PartialEq for Map<K, V, N>
source§fn eq(&self, other: &Self) -> bool
fn eq(&self, other: &Self) -> bool
Two maps can be compared.
For example:
let mut m1: micromap::Map<u8, i32, 10> = micromap::Map::new();
let mut m2: micromap::Map<u8, i32, 10> = micromap::Map::new();
m1.insert(1, 42);
m2.insert(1, 42);
assert_eq!(m1, m2);
// two maps with different order of key-value pairs are still equal:
m1.insert(2, 1);
m1.insert(3, 16);
m2.insert(3, 16);
m2.insert(2, 1);
assert_eq!(m1, m2);