Struct elsa::index_map::FrozenIndexMap
source · pub struct FrozenIndexMap<K, V, S = RandomState> { /* private fields */ }
Expand description
Append-only version of indexmap::IndexMap
where
insertion does not require mutable access
Implementations§
source§impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S>
impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S>
sourcepub fn insert(&self, k: K, v: V) -> &V::Target
pub fn insert(&self, k: K, v: V) -> &V::Target
If the key exists in the map, returns a reference to the corresponding value, otherwise inserts a new entry in the map for that key and returns a reference to the generated value.
Existing values are never overwritten.
The key may be any borrowed form of the map’s key type, but
Hash
and Eq
on the borrowed form must match those for
the key type.
Example
use elsa::index_map::FrozenIndexMap;
let map = FrozenIndexMap::new();
assert_eq!(map.insert(1, Box::new("a")), &"a");
assert_eq!(map.insert(1, Box::new("b")), &"a");
sourcepub fn insert_full(&self, k: K, v: V) -> (usize, &V::Target)
pub fn insert_full(&self, k: K, v: V) -> (usize, &V::Target)
If the key exists in the map, returns a reference to the corresponding value and its index, otherwise inserts a new entry in the map for that key and returns a reference to the generated value and its index.
Existing values are never overwritten.
The key may be any borrowed form of the map’s key type, but
Hash
and Eq
on the borrowed form must match those for
the key type.
Example
use elsa::index_map::FrozenIndexMap;
let map = FrozenIndexMap::new();
assert_eq!(map.insert_full(12, Box::new("a")), (0, &"a"));
assert_eq!(map.insert_full(12, Box::new("b")), (0, &"a"));
sourcepub fn get<Q>(&self, k: &Q) -> Option<&V::Target>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn get<Q>(&self, k: &Q) -> Option<&V::Target>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,
Returns a reference to the value corresponding to the key.
The key may be any borrowed form of the map’s key type, but
Hash
and Eq
on the borrowed form must match those for
the key type.
Examples
use elsa::FrozenIndexMap;
let map = FrozenIndexMap::new();
map.insert(1, Box::new("a"));
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);
sourcepub fn get_index(&self, index: usize) -> Option<(&K::Target, &V::Target)>where
K: StableDeref,
pub fn get_index(&self, index: usize) -> Option<(&K::Target, &V::Target)>where K: StableDeref,
Returns a reference to the key-value mapping corresponding to an index.
The key may be any borrowed form of the map’s key type, but
Hash
and Eq
on the borrowed form must match those for
the key type.
Examples
use elsa::index_map::FrozenIndexMap;
let map = FrozenIndexMap::new();
let (idx, _ref) = map.insert_full(Box::new("foo"), Box::new("a"));
assert_eq!(idx, 0);
assert_eq!(map.get_index(idx), Some((&"foo", &"a")));
assert_eq!(map.get_index(idx + 1), None);
sourcepub fn map_get<Q, T, F>(&self, k: &Q, f: F) -> Option<T>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
F: FnOnce(&V) -> T,
pub fn map_get<Q, T, F>(&self, k: &Q, f: F) -> Option<T>where K: Borrow<Q>, Q: Hash + Eq + ?Sized, F: FnOnce(&V) -> T,
Applies a function to the owner of the value corresponding to the key (if any).
The key may be any borrowed form of the map’s key type, but
Hash
and Eq
on the borrowed form must match those for
the key type.
Examples
use elsa::FrozenIndexMap;
let map = FrozenIndexMap::new();
map.insert(1, Box::new("a"));
assert_eq!(map.map_get(&1, Clone::clone), Some(Box::new("a")));
assert_eq!(map.map_get(&2, Clone::clone), None);
source§impl<K, V, S> FrozenIndexMap<K, V, S>
impl<K, V, S> FrozenIndexMap<K, V, S>
sourcepub fn into_tuple_vec(self) -> Vec<(K, V)>
pub fn into_tuple_vec(self) -> Vec<(K, V)>
Collects the contents of this map into a vector of tuples.
The order of the entries is as if iterating an IndexMap
.
Examples
use elsa::FrozenIndexMap;
let map = FrozenIndexMap::new();
map.insert(1, Box::new("a"));
map.insert(2, Box::new("b"));
let tuple_vec = map.into_tuple_vec();
assert_eq!(tuple_vec, vec![(1, Box::new("a")), (2, Box::new("b"))]);