Struct cranelift_entity::SecondaryMap
source · Expand description
A mapping K -> V
for densely indexed entity references.
The SecondaryMap
data structure uses the dense index space to implement a map with a vector.
Unlike PrimaryMap
, an SecondaryMap
can’t be used to allocate entity references. It is used
to associate secondary information with entities.
The map does not track if an entry for a key has been inserted or not. Instead it behaves as if all keys have a default entry from the beginning.
Implementations§
source§impl<K, V> SecondaryMap<K, V>where
K: EntityRef,
V: Clone,
impl<K, V> SecondaryMap<K, V>where
K: EntityRef,
V: Clone,
Shared SecondaryMap
implementation for all value types.
sourcepub fn new() -> Selfwhere
V: Default,
pub fn new() -> Selfwhere
V: Default,
Create a new empty map.
Examples found in repository?
More examples
sourcepub fn with_capacity(capacity: usize) -> Selfwhere
V: Default,
pub fn with_capacity(capacity: usize) -> Selfwhere
V: Default,
Create a new, empty map with the specified capacity.
The map will be able to hold exactly capacity
elements without reallocating.
sourcepub fn with_default(default: V) -> Self
pub fn with_default(default: V) -> Self
Create a new empty map with a specified default value.
This constructor does not require V to implement Default.
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the map can hold without reallocating.
sourcepub fn get(&self, k: K) -> Option<&V>
pub fn get(&self, k: K) -> Option<&V>
Get the element at k
if it exists.
Examples found in repository?
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
pub fn get(&self, key: K) -> Option<&V> {
if let Some(idx) = self.sparse.get(key).cloned() {
if let Some(entry) = self.dense.get(idx as usize) {
if entry.key() == key {
return Some(entry);
}
}
}
None
}
/// 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.
pub fn get_mut(&mut self, key: K) -> Option<&mut V> {
if let Some(idx) = self.sparse.get(key).cloned() {
if let Some(entry) = self.dense.get_mut(idx as usize) {
if entry.key() == key {
return Some(entry);
}
}
}
None
}
/// Return the index into `dense` of the value corresponding to `key`.
fn index(&self, key: K) -> Option<usize> {
if let Some(idx) = self.sparse.get(key).cloned() {
let idx = idx as usize;
if let Some(entry) = self.dense.get(idx) {
if entry.key() == key {
return Some(idx);
}
}
}
None
}
sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
Iterate over all the keys and values in this map, mutable edition.
sourcepub fn values_mut(&mut self) -> IterMut<'_, V>
pub fn values_mut(&mut self) -> IterMut<'_, V>
Iterate over all the values in this map, mutable edition.
Trait Implementations§
source§impl<K, V> Clone for SecondaryMap<K, V>where
K: EntityRef + Clone,
V: Clone + Clone,
impl<K, V> Clone for SecondaryMap<K, V>where
K: EntityRef + Clone,
V: Clone + Clone,
source§fn clone(&self) -> SecondaryMap<K, V>
fn clone(&self) -> SecondaryMap<K, V>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<K, V> Default for SecondaryMap<K, V>where
K: EntityRef,
V: Clone + Default,
impl<K, V> Default for SecondaryMap<K, V>where
K: EntityRef,
V: Clone + Default,
source§fn default() -> SecondaryMap<K, V>
fn default() -> SecondaryMap<K, V>
source§impl<K, V> Index<K> for SecondaryMap<K, V>where
K: EntityRef,
V: Clone,
impl<K, V> Index<K> for SecondaryMap<K, V>where
K: EntityRef,
V: Clone,
Immutable indexing into an SecondaryMap
.
All keys are permitted. Untouched entries have the default value.
source§impl<K, V> IndexMut<K> for SecondaryMap<K, V>where
K: EntityRef,
V: Clone,
impl<K, V> IndexMut<K> for SecondaryMap<K, V>where
K: EntityRef,
V: Clone,
Mutable indexing into an SecondaryMap
.
The map grows as needed to accommodate new keys.