pub struct ConcurrentSparseMap<K: Key, V, const PAGE: usize = DEFAULT_PAGE, D: SharedDirectory = Flat> { /* private fields */ }concurrent only.Expand description
A paged sparse map with insert-only shared access and exclusive editing.
Slots, prefix snapshots and directory selection follow
ConcurrentSparseSet. An occupied slot refuses a new entry, including
another generation. Existing values are never replaced while shared, so
lookups return stable &V references without guards. Shared insertion can
interleave dense order; exclusive removal uses swap-remove.
Same-slot insertions wait for the winning writer, including its factory.
Factories must not recursively insert into their own reserved slot, and
factories on two threads that each insert the other’s pending key
deadlock (each waits on the other’s reservation). Sync
requires both K and V to be Send + Sync. Uses core and alloc only.
With Flat, memory follows the highest page and lookups cost O(1);
its finite index range can panic. Radix accepts any
usize index, uses directory memory proportional to installed pages, and
looks up pages in O(depth).
use paged_collections::{concurrent::ConcurrentSparseMap, page::Radix};
let map = ConcurrentSparseMap::<usize, _, 4, Radix>::new();
assert_eq!(map.try_insert(usize::MAX - 1, "wide"), Ok(&"wide"));
assert_eq!(map.get(&(usize::MAX - 1)), Some(&"wide"));Implementations§
Source§impl<K: Key, V, const PAGE: usize, D: SharedDirectory> ConcurrentSparseMap<K, V, PAGE, D>
impl<K: Key, V, const PAGE: usize, D: SharedDirectory> ConcurrentSparseMap<K, V, PAGE, D>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty map without allocating.
§Panics
Panics if PAGE is zero or page storage exceeds isize::MAX bytes.
Sourcepub fn try_insert(&self, key: K, value: V) -> Result<&V, (K, V)>
pub fn try_insert(&self, key: K, value: V) -> Result<&V, (K, V)>
Adds an entry if its slot is vacant, returning a shared value reference.
Returns Err((key, value)) for a duplicate or another generation.
Waits for a writer already reserving that slot to publish or roll back.
§Panics
Panics at u32::MAX - 1 entries, for indices outside the Flat spine
range, or on allocation capacity overflow. Allocation can also fail.
Sourcepub fn try_insert_many(
&self,
entries: impl IntoIterator<Item = (K, V)>,
) -> usize
pub fn try_insert_many( &self, entries: impl IntoIterator<Item = (K, V)>, ) -> usize
Adds every entry whose slot is vacant, dropping the others, and returns
how many were added. Existing entries and earlier duplicates win.
Batches share one dense reservation, which scales across threads far
better than repeated try_insert.
Each batch is read and its keys validated before any slots are reserved. If the iterator panics, earlier batches stay inserted.
§Panics
As try_insert.
Sourcepub fn get(&self, key: &K) -> Option<&V>
pub fn get(&self, key: &K) -> Option<&V>
The value for this key, including completed appends beyond len.
Does not wait for in-flight insertions. With Flat, panics for an out-of-range index.
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Whether this exact key is present, without waiting for insertions.
With Flat, panics for an out-of-range key index.
Sourcepub fn get_or_insert_with(&self, key: K, make: impl FnOnce() -> V) -> Option<&V>
pub fn get_or_insert_with(&self, key: K, make: impl FnOnce() -> V) -> Option<&V>
Gets this key’s value, or inserts make() if its slot is vacant.
Returns None only if another generation occupies the slot.
Only the RESERVED winner calls make, at most once per call. If it
panics, a guard restores the slot to empty and other writers may retry.
The factory must not insert recursively into this same slot: it would
wait on its own reservation. Nor may two threads’ factories insert
each other’s pending keys: each would wait on the other’s
reservation. Factories run before dense reservation.
§Panics
As try_insert, or if make panics.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Length of the committed prefix; can lag while an earlier append runs.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the committed prefix is empty, subject to the lag of len.
Sourcepub fn iter(&self) -> SparseMapIter<'_, K, V, PAGE> ⓘ
pub fn iter(&self) -> SparseMapIter<'_, K, V, PAGE> ⓘ
Iterates over a snapshot of committed (&key, &value) pairs.
Sourcepub fn get_mut(&mut self, key: &K) -> Option<&mut V>
pub fn get_mut(&mut self, key: &K) -> Option<&mut V>
Mutable access to a matching key’s value under exclusive access.
With Flat, panics for an out-of-range key index.
Sourcepub fn remove(&mut self, key: &K) -> Option<V>
pub fn remove(&mut self, key: &K) -> Option<V>
Removes a matching entry and returns its value, moving the last entry
into its position. With Flat, panics for an out-of-range key index.
Sourcepub fn iter_mut(&mut self) -> SparseMapIterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> SparseMapIterMut<'_, K, V> ⓘ
Iterates over (&key, &mut value) pairs under exclusive access.
Keys cannot be changed, preserving the sparse index.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Drops every entry and clears readiness, retaining pages. Takes O(len). If a destructor panics, that entry is gone; remaining entries keep their sparse positions and may still be used.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Frees index pages with no entry and dense pages beyond the last entry, keeping directory chunks. Stored entries keep their addresses and positions. Shared inserts can allocate the released pages again.
Sourcepub fn drain(&mut self) -> IntoIter<(K, V)> ⓘ
pub fn drain(&mut self) -> IntoIter<(K, V)> ⓘ
Removes all entries and returns them as (key, value) pairs in dense order.
Keeps allocated pages. Takes O(len) time and temporary storage.
Sourcepub fn snapshot(&self) -> PagedSparseMap<K, V, PAGE, D>where
V: Clone,
pub fn snapshot(&self) -> PagedSparseMap<K, V, PAGE, D>where
V: Clone,
Sourcepub fn into_inner(self) -> PagedSparseMap<K, V, PAGE, D>
pub fn into_inner(self) -> PagedSparseMap<K, V, PAGE, D>
Moves entries into a plain map with the same directory in dense order, in O(len). Values move without cloning; their addresses can change.
Trait Implementations§
Source§impl<K: Key, V: Clone, const PAGE: usize, D: SharedDirectory> Clone for ConcurrentSparseMap<K, V, PAGE, D>
Clones the committed prefix via ConcurrentSparseMap::snapshot,
which can omit completed inserts beyond an unpublished reservation.
impl<K: Key, V: Clone, const PAGE: usize, D: SharedDirectory> Clone for ConcurrentSparseMap<K, V, PAGE, D>
Clones the committed prefix via ConcurrentSparseMap::snapshot,
which can omit completed inserts beyond an unpublished reservation.
Source§impl<K: Key + Debug, V: Debug, const PAGE: usize, D: SharedDirectory> Debug for ConcurrentSparseMap<K, V, PAGE, D>
impl<K: Key + Debug, V: Debug, const PAGE: usize, D: SharedDirectory> Debug for ConcurrentSparseMap<K, V, PAGE, D>
Source§impl<K: Key, V, const PAGE: usize, D: SharedDirectory> Default for ConcurrentSparseMap<K, V, PAGE, D>
impl<K: Key, V, const PAGE: usize, D: SharedDirectory> Default for ConcurrentSparseMap<K, V, PAGE, D>
Source§impl<'de, K: Key + Deserialize<'de>, V: Deserialize<'de>, const PAGE: usize, D: SharedDirectory> Deserialize<'de> for ConcurrentSparseMap<K, V, PAGE, D>
Reads the plain PagedSparseMap format, rejecting keys and lengths
outside the concurrent map’s capacity. Later entries replace earlier
entries at the same index in place, as in the plain map.
impl<'de, K: Key + Deserialize<'de>, V: Deserialize<'de>, const PAGE: usize, D: SharedDirectory> Deserialize<'de> for ConcurrentSparseMap<K, V, PAGE, D>
Reads the plain PagedSparseMap format, rejecting keys and lengths
outside the concurrent map’s capacity. Later entries replace earlier
entries at the same index in place, as in the plain map.
Source§fn deserialize<De: Deserializer<'de>>(
deserializer: De,
) -> Result<Self, De::Error>
fn deserialize<De: Deserializer<'de>>( deserializer: De, ) -> Result<Self, De::Error>
Source§impl<K: Key, V, const PAGE: usize, D: SharedDirectory> Extend<(K, V)> for ConcurrentSparseMap<K, V, PAGE, D>
Inserts entries in input order, keeping the first occupant of each slot.
impl<K: Key, V, const PAGE: usize, D: SharedDirectory> Extend<(K, V)> for ConcurrentSparseMap<K, V, PAGE, D>
Inserts entries in input order, keeping the first occupant of each slot.
Source§fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: T)
fn extend_one(&mut self, item: T)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K: Key, V, const PAGE: usize, D: SharedDirectory> From<PagedSparseMap<K, V, PAGE, D>> for ConcurrentSparseMap<K, V, PAGE, D>
impl<K: Key, V, const PAGE: usize, D: SharedDirectory> From<PagedSparseMap<K, V, PAGE, D>> for ConcurrentSparseMap<K, V, PAGE, D>
Source§fn from(inner: PagedSparseMap<K, V, PAGE, D>) -> Self
fn from(inner: PagedSparseMap<K, V, PAGE, D>) -> Self
Source§impl<K: Key, V, const PAGE: usize, D: SharedDirectory> FromIterator<(K, V)> for ConcurrentSparseMap<K, V, PAGE, D>
Collects entries in input order, keeping the first occupant of each slot.
impl<K: Key, V, const PAGE: usize, D: SharedDirectory> FromIterator<(K, V)> for ConcurrentSparseMap<K, V, PAGE, D>
Collects entries in input order, keeping the first occupant of each slot.
Source§impl<'a, K: Key, V, const PAGE: usize, D: SharedDirectory> IntoIterator for &'a ConcurrentSparseMap<K, V, PAGE, D>
impl<'a, K: Key, V, const PAGE: usize, D: SharedDirectory> IntoIterator for &'a ConcurrentSparseMap<K, V, PAGE, D>
Source§impl<'a, K: Key, V, const PAGE: usize, D: SharedDirectory> IntoIterator for &'a mut ConcurrentSparseMap<K, V, PAGE, D>
impl<'a, K: Key, V, const PAGE: usize, D: SharedDirectory> IntoIterator for &'a mut ConcurrentSparseMap<K, V, PAGE, D>
impl<K: Key + Send, V: Send, const PAGE: usize, D: SharedDirectory> Send for ConcurrentSparseMap<K, V, PAGE, D>
Source§impl<K: Key + Serialize, V: Serialize, const PAGE: usize, D: SharedDirectory> Serialize for ConcurrentSparseMap<K, V, PAGE, D>
Serializes the committed entries in dense order as a valid plain map
in O(prefix length). The prefix can omit completed inserts beyond an
earlier unpublished reservation, as in ConcurrentSparseMap::iter.
impl<K: Key + Serialize, V: Serialize, const PAGE: usize, D: SharedDirectory> Serialize for ConcurrentSparseMap<K, V, PAGE, D>
Serializes the committed entries in dense order as a valid plain map
in O(prefix length). The prefix can omit completed inserts beyond an
earlier unpublished reservation, as in ConcurrentSparseMap::iter.
impl<K: Key + Send + Sync, V: Send + Sync, const PAGE: usize, D: SharedDirectory> Sync for ConcurrentSparseMap<K, V, PAGE, D>
Auto Trait Implementations§
impl<K, V, const PAGE: usize = DEFAULT_PAGE, D = Flat> !Freeze for ConcurrentSparseMap<K, V, PAGE, D>
impl<K, V, const PAGE: usize = DEFAULT_PAGE, D = Flat> !RefUnwindSafe for ConcurrentSparseMap<K, V, PAGE, D>
impl<K, V, const PAGE: usize, D> Forget for ConcurrentSparseMap<K, V, PAGE, D>
impl<K, V, const PAGE: usize, D> Move for ConcurrentSparseMap<K, V, PAGE, D>
impl<K, V, const PAGE: usize, D> Unpin for ConcurrentSparseMap<K, V, PAGE, D>
impl<K, V, const PAGE: usize, D> UnsafeUnpin for ConcurrentSparseMap<K, V, PAGE, D>
impl<K, V, const PAGE: usize, D> UnwindSafe for ConcurrentSparseMap<K, V, PAGE, D>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more