Skip to main content

ConcurrentSparseSet

Struct ConcurrentSparseSet 

Source
pub struct ConcurrentSparseSet<K: Key, const PAGE: usize = DEFAULT_PAGE, D: SharedDirectory = Flat> { /* private fields */ }
Available on crate feature 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>

Source

pub const PAGE_SIZE: usize = PAGE

Number of slots per sparse page and keys per dense page.

Source

pub fn new() -> Self

Creates an empty set without allocating.

§Panics

Panics if PAGE is zero or page storage exceeds isize::MAX bytes.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn len(&self) -> usize

Length of the committed prefix; can lag while an earlier append runs.

Source

pub fn is_empty(&self) -> bool

Whether the committed prefix is empty, subject to the lag of len.

Source

pub fn iter(&self) -> DenseIter<'_, K, PAGE> ⓘ

Iterates over a snapshot of the committed keys in dense order.

Source

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.

Source

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.

Source

pub fn clear(&mut self)

Removes every key and clears readiness, retaining allocated pages. Takes O(len) time.

Source

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

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.

Source

pub fn snapshot(&self) -> PagedSparseSet<K, PAGE, D>

Copies the committed prefix into a plain set in O(prefix length). Like len and iter, this can omit completed inserts beyond an earlier unpublished reservation. The keys form a valid plain set.

Source

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.

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<K: Key + Debug, const PAGE: usize, D: SharedDirectory> Debug for ConcurrentSparseSet<K, PAGE, D>

Source§

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

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

impl<K: Key, const PAGE: usize, D: SharedDirectory> Default for ConcurrentSparseSet<K, PAGE, D>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
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.

Source§

fn deserialize<De: Deserializer<'de>>( deserializer: De, ) -> Result<Self, De::Error>

Deserialize this value from the given Serde deserializer. Read more
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.

Source§

fn extend<I: IntoIterator<Item = K>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

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

Converts to this type from the input type.
Source§

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

Creates a value from an iterator. Read more
Source§

impl<'a, K: Key, const PAGE: usize, D: SharedDirectory> IntoIterator for &'a ConcurrentSparseSet<K, PAGE, D>

Source§

type Item = &'a K

The type of the elements being iterated over.
Source§

type IntoIter = DenseIter<'a, K, PAGE>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

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.

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

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>
where Index<PAGE, D>: Unpin, AppendLog<K, PAGE>: Unpin,

§

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> 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.