SparseMap

Struct SparseMap 

Source
pub struct SparseMap<T> { /* private fields */ }
Expand description

A sparse, generational map keyed by Key.

SparseMap provides stable keys with generation to prevent use-after-free bugs. Internally, it reuses vacant slots while incrementing a generation counter to invalidate stale keys.

§Guarantees

  • Insertion is O(1).
  • Removal is O(1).
  • Lookup is O(1).
  • Keys are invalidated once their value is removed.

Implementations§

Source§

impl<T> SparseMap<T>

Source

pub fn new() -> Self

Creates a new empty sparse map.

Source

pub fn insert(&mut self, value: T) -> Key

Inserts a value into the map and returns a unique Key.

Vacant slots are reused when possible. If a slot is reused, its generation counter is incremented to invalidate old keys.

Source

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

Removes a value associated with the given key.

Returns None if the key is invalid or already removed. The slot is marked for reuse.

Source

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

Returns an immutable reference to the value for the given key if present.

Source

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

Returns a mutable reference to the value for the given key if present.

Source

pub fn scope<F, R>(&mut self, key: &Key, f: F) -> Option<R>
where F: FnOnce(&mut Self, &mut T) -> R,

Temporarily takes ownership of the value associated with key, passes it to f, and then places it back into the map.

This enables safe, scoped mutation of a value while still allowing f to mutate the map itself (e.g. insert or remove other entries) without violating Rust’s aliasing rules.

Returns None if key does not currently refer to a live value.

§Guarantees
  • The value is removed from the map for the duration of f.
  • The value is restored to the same slot after f returns.
  • The key remains valid if and only if it was valid before the call.
§Example
use sparse_map::SparseMap;

let mut map = SparseMap::new();
let key = map.insert(42);

map.scope(&key, |map, value| {
    *value += 2;
    map.insert(24);
});

assert_eq!(map.get(&key), Some(&44));
Source

pub fn contains(&self, key: &Key) -> bool

Returns true if the key currently refers to a live value.

Source

pub fn len(&self) -> usize

Returns the number of live values stored in the map.

This is O(1) and does not require scanning the buffer.

Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no live values.

Trait Implementations§

Source§

impl<T: Debug> Debug for SparseMap<T>

Source§

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

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

impl<T> Default for SparseMap<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> Freeze for SparseMap<T>

§

impl<T> RefUnwindSafe for SparseMap<T>
where T: RefUnwindSafe,

§

impl<T> Send for SparseMap<T>
where T: Send,

§

impl<T> Sync for SparseMap<T>
where T: Sync,

§

impl<T> Unpin for SparseMap<T>
where T: Unpin,

§

impl<T> UnwindSafe for SparseMap<T>
where 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> 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, 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.