SecondaryMap

Struct SecondaryMap 

Source
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>

Source

pub fn new() -> Self

Create a new empty SecondaryMap

创建一个新的空 SecondaryMap

Source

pub fn with_capacity(capacity: usize) -> Self

Create a SecondaryMap with specified capacity

创建一个指定容量的 SecondaryMap

Source

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).
  • None otherwise.
Source

pub fn remove(&mut self, key: K) -> Option<T>

Remove value by key

Only removes if both index and generation match.

通过 Key 移除值

仅当索引和代数都匹配时才移除。

Source

pub fn get(&self, key: K) -> Option<&T>

Get reference to value by key

通过 Key 获取值的引用

Source

pub fn get_mut(&mut self, key: K) -> Option<&mut T>

Get mutable reference to value by key

通过 Key 获取值的可变引用

Source

pub fn contains_key(&self, key: K) -> bool

Check if key exists

检查 Key 是否存在

Source

pub fn len(&self) -> usize

Return the number of elements

返回元素数量

Source

pub fn is_empty(&self) -> bool

Check if empty

检查是否为空

Source

pub fn capacity(&self) -> usize

Capacity of the underlying vector

底层 vector 的容量

Source

pub fn clear(&mut self)

Clear all elements

Does not deallocate memory, but clears validity.

清空所有元素 不会释放内存,但会清除有效性。

Source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(K, &mut T) -> bool,

Retains only the elements specified by the predicate.

只保留满足谓词的元素。

Source

pub fn iter(&self) -> impl Iterator<Item = (K, &T)>

Iterator over all (key, value) pairs

遍历所有 (key, value) 对的迭代器

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = (K, &mut T)>

Mutable iterator over all (key, value) pairs

遍历所有 (key, value) 对的可变迭代器

Trait Implementations§

Source§

impl<T: Clone, K: Clone + Key> Clone for SecondaryMap<T, K>

Source§

fn clone(&self) -> SecondaryMap<T, K>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for SecondaryMap<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, K: Key> Default for SecondaryMap<T, K>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T, K> Freeze for SecondaryMap<T, K>

§

impl<T, K> RefUnwindSafe for SecondaryMap<T, K>

§

impl<T, K> Send for SecondaryMap<T, K>
where K: Send, T: Send,

§

impl<T, K> Sync for SecondaryMap<T, K>
where K: Sync, T: Sync,

§

impl<T, K> Unpin for SecondaryMap<T, K>
where K: Unpin, T: Unpin,

§

impl<T, K> UnwindSafe for SecondaryMap<T, K>
where K: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.