pub struct SecondaryMap<T, K: Key = DefaultKey> { /* private fields */ }Expand description
A secondary map that associates data with keys from a DeferredMap.
SecondaryMap allows you to store additional information for each key in a DeferredMap.
It is separate from the primary map and does not affect the primary map’s memory layout.
Key Features:
- Sparse Storage: Efficiently handles cases where not all keys in the primary map have associated data.
- Generation Checking: Automatically validates compatibility with the primary map’s keys. Stale keys (from older generations) will be ignored or overwritten as appropriate.
- Automatic Expansion: The map automatically grows to accommodate keys with larger indices.
一个辅助映射(SecondaryMap),用于将数据与 DeferredMap 的 Key 关联。
SecondaryMap 允许你为 DeferredMap 中的每个 Key 存储额外信息。
它与主映射分离,不影响主映射的内存布局。
主要特性:
- 稀疏存储:高效处理主映射中并非所有 Key 都有关联数据的情况。
- 代数检查:自动验证与主映射 Key 的兼容性。过期的 Key(来自旧代数)将被忽略或覆盖。
- 自动扩展:映射会自动增长以适应具有更大索引的 Key。
§Examples (示例)
use deferred_map::{DeferredMap, SecondaryMap};
let mut map = DeferredMap::new();
let handle = map.allocate_handle();
let key = handle.key();
map.insert(handle, "Primary Value");
let mut sec_map = SecondaryMap::new();
sec_map.insert(key, 100);
assert_eq!(sec_map.get(key), Some(&100));Implementations§
Source§impl<T, K: Key> SecondaryMap<T, K>
impl<T, K: Key> SecondaryMap<T, K>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a SecondaryMap with specified capacity
创建一个指定容量的 SecondaryMap
Sourcepub fn insert(&mut self, key: K, value: T) -> Option<T>
pub fn insert(&mut self, key: K, value: T) -> Option<T>
Insert a value for a specific key
If the key belongs to an older generation than what is currently stored, the insertion is ignored. If the key is newer, it overwrites the existing value.
为特定 Key 插入值
如果 Key 的代数(generation)老于当前存储的代数,插入将被忽略。 如果 Key 是新的,它将覆盖现有值。
§Returns
Some(old_value)if a value existed for the EXACT same key (same index and generation).Noneotherwise.
Sourcepub fn remove(&mut self, key: K) -> Option<T>
pub fn remove(&mut self, key: K) -> Option<T>
Remove value by key
Only removes if both index and generation match.
通过 Key 移除值
仅当索引和代数都匹配时才移除。
Sourcepub fn get_mut(&mut self, key: K) -> Option<&mut T>
pub fn get_mut(&mut self, key: K) -> Option<&mut T>
Get mutable reference to value by key
通过 Key 获取值的可变引用
Sourcepub fn contains_key(&self, key: K) -> bool
pub fn contains_key(&self, key: K) -> bool
Check if key exists
检查 Key 是否存在
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear all elements
Does not deallocate memory, but clears validity.
清空所有元素 不会释放内存,但会清除有效性。
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Retains only the elements specified by the predicate.
只保留满足谓词的元素。
Trait Implementations§
Source§impl<T: Clone, K: Clone + Key> Clone for SecondaryMap<T, K>
impl<T: Clone, K: Clone + Key> Clone for SecondaryMap<T, K>
Source§fn clone(&self) -> SecondaryMap<T, K>
fn clone(&self) -> SecondaryMap<T, K>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more