Struct cranelift_entity::SparseMap
source · pub struct SparseMap<K, V>where
K: EntityRef,
V: SparseMapValue<K>,{ /* private fields */ }
Expand description
A sparse mapping of entity references.
A SparseMap<K, V>
map provides:
- Memory usage equivalent to
SecondaryMap<K, u32>
+Vec<V>
, so much smaller thanSecondaryMap<K, V>
for sparse mappings of largerV
types. - Constant time lookup, slightly slower than
SecondaryMap
. - A very fast, constant time
clear()
operation. - Fast insert and erase operations.
- Stable iteration that is as fast as a
Vec<V>
.
Compared to SecondaryMap
When should we use a SparseMap
instead of a secondary SecondaryMap
? First of all,
SparseMap
does not provide the functionality of a PrimaryMap
which can allocate and assign
entity references to objects as they are pushed onto the map. It is only the secondary entity
maps that can be replaced with a SparseMap
.
- A secondary entity map assigns a default mapping to all keys. It doesn’t distinguish between
an unmapped key and one that maps to the default value.
SparseMap
does not requireDefault
values, and it tracks accurately if a key has been mapped or not. - Iterating over the contents of an
SecondaryMap
is linear in the size of the key space, while iterating over aSparseMap
is linear in the number of elements in the mapping. This is an advantage precisely when the mapping is sparse. SparseMap::clear()
is constant time and super-fast.SecondaryMap::clear()
is linear in the size of the key space. (Or, rather the requiredresize()
call following theclear()
is).SparseMap
requires the values to implementSparseMapValue<K>
which means that they must contain their own key.
Implementations§
source§impl<K, V> SparseMap<K, V>where
K: EntityRef,
V: SparseMapValue<K>,
impl<K, V> SparseMap<K, V>where
K: EntityRef,
V: SparseMapValue<K>,
sourcepub fn get(&self, key: K) -> Option<&V>
pub fn get(&self, key: K) -> Option<&V>
Returns a reference to the value corresponding to the key.
sourcepub fn get_mut(&mut self, key: K) -> Option<&mut V>
pub fn get_mut(&mut self, key: K) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the key.
Note that the returned value must not be mutated in a way that would change its key. This would invalidate the sparse set data structure.
Examples found in repository?
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
pub fn insert(&mut self, value: V) -> Option<V> {
let key = value.key();
// Replace the existing entry for `key` if there is one.
if let Some(entry) = self.get_mut(key) {
return Some(mem::replace(entry, value));
}
// There was no previous entry for `key`. Add it to the end of `dense`.
let idx = self.dense.len();
debug_assert!(idx <= u32::MAX as usize, "SparseMap overflow");
self.dense.push(value);
self.sparse[key] = idx as u32;
None
}
sourcepub fn contains_key(&self, key: K) -> bool
pub fn contains_key(&self, key: K) -> bool
Return true
if the map contains a value corresponding to key
.
sourcepub fn insert(&mut self, value: V) -> Option<V>
pub fn insert(&mut self, value: V) -> Option<V>
Insert a value into the map.
If the map did not have this key present, None
is returned.
If the map did have this key present, the value is updated, and the old value is returned.
It is not necessary to provide a key since the value knows its own key already.
Trait Implementations§
source§impl<'a, K, V> IntoIterator for &'a SparseMap<K, V>where
K: EntityRef,
V: SparseMapValue<K>,
impl<'a, K, V> IntoIterator for &'a SparseMap<K, V>where
K: EntityRef,
V: SparseMapValue<K>,
Iterating over the elements of a set.