pub struct SymbolMap<V> { /* private fields */ }
Expand description
Provides a efficient lookup map using Symbol
as key.
This map is optimized as we know for sure, that the key is an i32
. Also, we will use this
map for objects within Node
and therefore know that most commonly there will be only a few
keys (most probably < 8 and almost always < 32).
Therefore we simply use a Vec
of tuples to store the keys and their value. We also keep
this vector sorted by the key so that a lookup can be done via binary_search
.
Using this approach easily beats HashMap
in both, performance and memory consumption.
Implementations§
source§impl<V: Default> SymbolMap<V>
impl<V: Default> SymbolMap<V>
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new and empty map.
§Examples
let mut map = SymbolMap::<i32>::new();
assert_eq!(map.len(), 0);
sourcepub fn get(&self, key: Symbol) -> Option<&V>
pub fn get(&self, key: Symbol) -> Option<&V>
Retrieves the value for the given key.
§Examples
let mut map = SymbolMap::<i32>::new();
map.put(42, 1);
assert_eq!(map.get(42).unwrap(), &1);
assert_eq!(map.get(99), None);
sourcepub fn get_or_insert(
&mut self,
key: Symbol,
factory: impl FnMut() -> V
) -> &mut V
pub fn get_or_insert( &mut self, key: Symbol, factory: impl FnMut() -> V ) -> &mut V
Retrieves the value for the given key or creates a new one in none exists.
If a new value is created, Default::default is invoked and a mutable reference is returned.
§Examples
let mut map = SymbolMap::<i32>::new();
map.put(42, 1);
assert_eq!(map.get_or_insert(42, || 1), &1);
assert_eq!(map.get_or_insert(23, || 0), &0);
sourcepub fn get_mut(&mut self, key: Symbol) -> Option<&mut V>
pub fn get_mut(&mut self, key: Symbol) -> Option<&mut V>
Retrieves the value for the given key as mutable reference.
§Examples
let mut map = SymbolMap::<String>::new();
map.put(42, "Hello".to_owned());
map.get_mut(42).unwrap().push_str(" World");
assert_eq!(map.get(42).unwrap().as_str(), "Hello World");
sourcepub fn put(&mut self, key: Symbol, value: V)
pub fn put(&mut self, key: Symbol, value: V)
Puts a value in the for the given key.
§Examples
let mut map = SymbolMap::<String>::new();
map.put(42, "Hello".to_owned());
assert_eq!(map.get(42).unwrap().as_str(), "Hello");
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Counts the number of entries in the map.
§Examples
let mut map = SymbolMap::<String>::new();
// If an entry is put in the map, its size is incremented...
map.put(42, "Hello".to_owned());
assert_eq!(map.len(), 1);
// If the entry is overwritten, the size remains the same...
map.put(42, "Hello".to_owned());
assert_eq!(map.len(), 1);
// If another entry is added, its size grows once again...
map.put(99, "World".to_owned());
assert_eq!(map.len(), 2);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Determines if this map is empty.
§Examples
let mut map = SymbolMap::<String>::new();
assert_eq!(map.is_empty(), true);
map.put(42, "Hello".to_owned());
assert_eq!(map.is_empty(), false);
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Reports the capacity currently reserved.
§Examples
let mut map = SymbolMap::<String>::new();
let initial_capacity = map.capacity();
for i in 1..=initial_capacity+1 {
map.put(i as i32, "Hello".to_owned());
}
assert_eq!(map.capacity() > initial_capacity, true);
sourcepub fn entries(&self) -> Iter<'_, (Symbol, V)>
pub fn entries(&self) -> Iter<'_, (Symbol, V)>
Provides an iterator over all entries in the map.
§Examples
let mut map = SymbolMap::<String>::new();
map.put(42, "Hello".to_owned());
map.put(99, "World".to_owned());
let string = map.entries().map(|(key, value)| format!("{}: {}", key, value)).join(", ");
assert_eq!(string, "42: Hello, 99: World");
Trait Implementations§
source§impl<V: PartialEq> PartialEq for SymbolMap<V>
impl<V: PartialEq> PartialEq for SymbolMap<V>
impl<V: Eq> Eq for SymbolMap<V>
impl<V> StructuralPartialEq for SymbolMap<V>
Auto Trait Implementations§
impl<V> Freeze for SymbolMap<V>
impl<V> RefUnwindSafe for SymbolMap<V>where
V: RefUnwindSafe,
impl<V> Send for SymbolMap<V>where
V: Send,
impl<V> Sync for SymbolMap<V>where
V: Sync,
impl<V> Unpin for SymbolMap<V>where
V: Unpin,
impl<V> UnwindSafe for SymbolMap<V>where
V: UnwindSafe,
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.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more