pub struct IndexedMap<K, V> { /* private fields */ }Expand description
A simple hash map which assigns each key an index in insertion order.
Keys and values are stored in a Vec in insertion order. A separate hash table using linear
probing and FastHasher maps keys to their indexes. Keys cannot be removed.
The table is kept at most 25% full for performance. This can be faster than FastMap for
small maps with up to ~1000 entries.
§Examples
let mut map = IndexedMap::<&str, u32>::new();
let index = map.intern("x");
map[index] += 2;
assert_eq!(map.try_insert("x"), Err(0));
assert_eq!(map.try_insert_with("y", || 5), Ok(1));
assert_eq!(map.into_values().collect::<Vec<_>>(), [2, 5]);Implementations§
Source§impl<K: Hash + Eq, V> IndexedMap<K, V>
impl<K: Hash + Eq, V> IndexedMap<K, V>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty map with at least the specified capacity.
Sourcepub fn try_insert(&mut self, key: K) -> Result<usize, usize>where
V: Default,
pub fn try_insert(&mut self, key: K) -> Result<usize, usize>where
V: Default,
Inserts the key if it is not present, using the default value.
See try_insert_with.
Sourcepub fn insert(&mut self, key: K) -> boolwhere
V: Default,
pub fn insert(&mut self, key: K) -> boolwhere
V: Default,
Inserts the key if it is not present, returning whether it was inserted.
Sourcepub fn intern(&mut self, key: K) -> usizewhere
V: Default,
pub fn intern(&mut self, key: K) -> usizewhere
V: Default,
Returns the index of the key, inserting the key first if it is not present.
Sourcepub fn index_of(&self, key: &K) -> Option<usize>
pub fn index_of(&self, key: &K) -> Option<usize>
Returns the index of the provided key if it is present.
Sourcepub fn into_values(self) -> impl Iterator<Item = V>
pub fn into_values(self) -> impl Iterator<Item = V>
Returns an iterator over the values, ordered by index.