pub struct ConcurrentSparseSet<K: Key, const PAGE: usize = DEFAULT_PAGE, D: SharedDirectory = Flat> { /* private fields */ }concurrent only.Expand description
A paged sparse set with shared insertion and stable shared references.
At most one key occupies each Key::index. An occupied slot refuses
both duplicates and other generations. Same-slot insertions wait for the
winning insertion; lookups do not wait. Removing or clearing requires
exclusive access and can move the last dense key into a removed position.
Iteration and pages snapshot the hole-free committed prefix. Its length can lag behind completed insertions while an earlier append is in flight; lookups see every completed insertion. With no insertion in flight the prefix contains every entry.
The default Flat directory has O(1) page lookups and memory following
the highest page; indices outside its finite spine range panic.
Radix accepts any usize index, uses directory
memory proportional to installed pages, and looks up pages in O(depth). At most
u32::MAX - 1 entries fit because one encoding is reserved for writers.
Pages are retained until destruction. Uses only core and alloc.
use paged_collections::{concurrent::ConcurrentSparseSet, page::Radix};
let set = ConcurrentSparseSet::<usize, 4, Radix>::new();
assert!(set.insert(usize::MAX - 1));
assert!(set.contains(usize::MAX - 1));Implementations§
Source§impl<K: Key, const PAGE: usize, D: SharedDirectory> ConcurrentSparseSet<K, PAGE, D>
impl<K: Key, const PAGE: usize, D: SharedDirectory> ConcurrentSparseSet<K, PAGE, D>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty set without allocating.
§Panics
Panics if PAGE is zero or page storage exceeds isize::MAX bytes.
Sourcepub fn insert(&self, key: K) -> bool
pub fn insert(&self, key: K) -> bool
Adds key, returning false if any key already holds its slot.
Waits for an insertion already reserving the same slot to finish.
§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 insert_many(&self, keys: impl IntoIterator<Item = K>) -> usize
pub fn insert_many(&self, keys: impl IntoIterator<Item = K>) -> usize
Adds every key whose slot is vacant, returning how many were added.
Earlier duplicates win. Batches share one dense reservation, which
scales across threads far better than repeated insert.
Positions need not follow input order.
Each batch is read and its keys validated before any slots are reserved. If the iterator panics, earlier batches stay inserted.
§Panics
As insert.
Sourcepub fn position(&self, key: K) -> Option<usize>
pub fn position(&self, key: K) -> Option<usize>
Dense position of this key, including a completed insertion beyond
the committed prefix. Positions can change under exclusive removal.
With Flat, panics if the key index exceeds the sparse spine range.
Sourcepub fn contains(&self, key: K) -> bool
pub fn contains(&self, key: K) -> bool
Whether this key is present. Does not wait for an in-flight insertion.
With Flat, panics if the key index exceeds the sparse spine range.
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) -> DenseIter<'_, K, PAGE> ⓘ
pub fn iter(&self) -> DenseIter<'_, K, PAGE> ⓘ
Iterates over a snapshot of the committed keys in dense order.
Sourcepub fn pages(
&self,
) -> impl DoubleEndedIterator<Item = &[K]> + ExactSizeIterator + FusedIterator
pub fn pages( &self, ) -> impl DoubleEndedIterator<Item = &[K]> + ExactSizeIterator + FusedIterator
Nonempty page slices of a snapshot of the committed dense prefix. Every page but the last is full.
Sourcepub fn remove(&mut self, key: K) -> bool
pub fn remove(&mut self, key: K) -> bool
Removes a matching key, moving the last key into its position.
Requires exclusive access. With Flat, panics for an out-of-range key index.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes every key and clears readiness, retaining allocated pages. Takes O(len) time.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Frees index pages with no key and dense pages beyond the last key, keeping directory chunks. Stored keys keep their addresses and positions. Shared inserts can allocate the released pages again.
§Examples
use paged_collections::concurrent::ConcurrentSparseSet;
let mut set: ConcurrentSparseSet<usize, 4> = (0..64).collect();
for key in 2..64 {
set.remove(key);
}
set.shrink_to_fit();
assert_eq!(set.pages().count(), 1);
assert!(set.insert(40));Sourcepub fn drain(&mut self) -> IntoIter<K> ⓘ
pub fn drain(&mut self) -> IntoIter<K> ⓘ
Removes all keys and returns them in dense order. Keeps allocated pages. Takes O(len) time and temporary storage.
Sourcepub fn snapshot(&self) -> PagedSparseSet<K, PAGE, D>
pub fn snapshot(&self) -> PagedSparseSet<K, PAGE, D>
Sourcepub fn into_inner(self) -> PagedSparseSet<K, PAGE, D>
pub fn into_inner(self) -> PagedSparseSet<K, PAGE, D>
Moves keys into the plain set with the same directory in dense order, in O(len).
Trait Implementations§
Source§impl<K: Key, const PAGE: usize, D: SharedDirectory> Clone for ConcurrentSparseSet<K, PAGE, D>
Copies the committed prefix via ConcurrentSparseSet::snapshot,
which can omit completed inserts beyond an unpublished reservation.
impl<K: Key, const PAGE: usize, D: SharedDirectory> Clone for ConcurrentSparseSet<K, PAGE, D>
Copies the committed prefix via ConcurrentSparseSet::snapshot,
which can omit completed inserts beyond an unpublished reservation.
Source§impl<K: Key + Debug, const PAGE: usize, D: SharedDirectory> Debug for ConcurrentSparseSet<K, PAGE, D>
impl<K: Key + Debug, const PAGE: usize, D: SharedDirectory> Debug for ConcurrentSparseSet<K, PAGE, D>
Source§impl<K: Key, const PAGE: usize, D: SharedDirectory> Default for ConcurrentSparseSet<K, PAGE, D>
impl<K: Key, const PAGE: usize, D: SharedDirectory> Default for ConcurrentSparseSet<K, PAGE, D>
Source§impl<'de, K: Key + Deserialize<'de>, const PAGE: usize, D: SharedDirectory> Deserialize<'de> for ConcurrentSparseSet<K, PAGE, D>
Reads the plain PagedSparseSet format, rejecting keys and lengths
outside the concurrent set’s capacity.
impl<'de, K: Key + Deserialize<'de>, const PAGE: usize, D: SharedDirectory> Deserialize<'de> for ConcurrentSparseSet<K, PAGE, D>
Reads the plain PagedSparseSet format, rejecting keys and lengths
outside the concurrent set’s capacity.
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, const PAGE: usize, D: SharedDirectory> Extend<K> for ConcurrentSparseSet<K, PAGE, D>
Inserts keys in input order, keeping the first occupant of each slot.
impl<K: Key, const PAGE: usize, D: SharedDirectory> Extend<K> for ConcurrentSparseSet<K, PAGE, D>
Inserts keys in input order, keeping the first occupant of each slot.
Source§fn extend<I: IntoIterator<Item = K>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = K>>(&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, const PAGE: usize, D: SharedDirectory> From<PagedSparseSet<K, PAGE, D>> for ConcurrentSparseSet<K, PAGE, D>
impl<K: Key, const PAGE: usize, D: SharedDirectory> From<PagedSparseSet<K, PAGE, D>> for ConcurrentSparseSet<K, PAGE, D>
Source§fn from(inner: PagedSparseSet<K, PAGE, D>) -> Self
fn from(inner: PagedSparseSet<K, PAGE, D>) -> Self
Source§impl<K: Key, const PAGE: usize, D: SharedDirectory> FromIterator<K> for ConcurrentSparseSet<K, PAGE, D>
impl<K: Key, const PAGE: usize, D: SharedDirectory> FromIterator<K> for ConcurrentSparseSet<K, PAGE, D>
Source§fn from_iter<I: IntoIterator<Item = K>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = K>>(iter: I) -> Self
Source§impl<'a, K: Key, const PAGE: usize, D: SharedDirectory> IntoIterator for &'a ConcurrentSparseSet<K, PAGE, D>
impl<'a, K: Key, const PAGE: usize, D: SharedDirectory> IntoIterator for &'a ConcurrentSparseSet<K, PAGE, D>
impl<K: Key + Send, const PAGE: usize, D: SharedDirectory> Send for ConcurrentSparseSet<K, PAGE, D>
Source§impl<K: Key + Serialize, const PAGE: usize, D: SharedDirectory> Serialize for ConcurrentSparseSet<K, PAGE, D>
Serializes the committed keys in dense order as a valid plain set
sequence in O(prefix length). The prefix can omit completed inserts
beyond an earlier unpublished reservation, as in ConcurrentSparseSet::iter.
impl<K: Key + Serialize, const PAGE: usize, D: SharedDirectory> Serialize for ConcurrentSparseSet<K, PAGE, D>
Serializes the committed keys in dense order as a valid plain set
sequence in O(prefix length). The prefix can omit completed inserts
beyond an earlier unpublished reservation, as in ConcurrentSparseSet::iter.
impl<K: Key + Send + Sync, const PAGE: usize, D: SharedDirectory> Sync for ConcurrentSparseSet<K, PAGE, D>
Auto Trait Implementations§
impl<K, const PAGE: usize = DEFAULT_PAGE, D = Flat> !Freeze for ConcurrentSparseSet<K, PAGE, D>
impl<K, const PAGE: usize = DEFAULT_PAGE, D = Flat> !RefUnwindSafe for ConcurrentSparseSet<K, PAGE, D>
impl<K, const PAGE: usize, D> Forget for ConcurrentSparseSet<K, PAGE, D>
impl<K, const PAGE: usize, D> Move for ConcurrentSparseSet<K, PAGE, D>
impl<K, const PAGE: usize, D> Unpin for ConcurrentSparseSet<K, PAGE, D>
impl<K, const PAGE: usize, D> UnsafeUnpin for ConcurrentSparseSet<K, PAGE, D>where
Index<PAGE, D>: UnsafeUnpin,
AppendLog<K, PAGE>: UnsafeUnpin,
impl<K, const PAGE: usize, D> UnwindSafe for ConcurrentSparseSet<K, PAGE, D>where
Index<PAGE, D>: UnwindSafe,
AppendLog<K, PAGE>: UnwindSafe,
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