Struct SeqArray

Source
pub struct SeqArray<T: Copy + Clone + Send + Sync + 'static> { /* private fields */ }
Expand description

A thread-safe, concurrent, lock-free, and resizable array that uses interior mutability. Can be used as a basic building block for more complex concurrent data structures.

use seqmap::SeqArray;

let arr = SeqArray::<u32>::with_capacity(4);

arr.set(0, 1);
arr.set(1, 2);

std::thread::scope(|s| {
    s.spawn(|| {
        assert_eq!(arr.get(0), Ok(1));
        assert_eq!(arr.get(1), Ok(2));
        arr.set(2, 3);
    });
    s.spawn(|| {
        assert_eq!(arr.get(0), Ok(1));
        assert_eq!(arr.get(1), Ok(2));
        arr.set(3, 4);
    });
});

assert_eq!(arr.get(0), Ok(1));
assert_eq!(arr.get(1), Ok(2));
assert!(
    (arr.get(2) == Ok(3) && arr.get(3) == Ok(4)) ||
    (arr.get(2) == Ok(4) && arr.get(3) == Ok(3))
);

Implementations§

Source§

impl<T: Copy + Clone + Send + Sync + 'static> SeqArray<T>

Source

pub fn new() -> Self

Creates a new empty SeqArray with a default initial capacity of 10.

Source

pub fn with_capacity(cap: usize) -> Self

Creates a new SeqArray with the specified initial capacity.

Source

pub fn capacity(&self) -> usize

Returns the current capacity of the array.

Source

pub fn len(&self) -> usize

Returns the approximate current length of the array (eventually consistent).

Source

pub fn is_empty(&self) -> bool

Returns true if the array is empty, false otherwise.

Source

pub fn resizing(&self) -> bool

Source

pub fn cloning(&self) -> bool

Source

pub fn get_without_resize(&self, snapshot_cap: usize, index: usize) -> Result<T>

Tries to get the value at index, returning if the array capacity has changed from snapshot_cap to something else.

Immediately returns if self.capacity() != snapshot_cap.

Analogue of Self::get.

Source

pub fn get(&self, index: usize) -> Result<T>

Gets the value at index, if present and within bounds.

During a resize, this will wait for the resize to complete before checking the value. For a version that does not wait for resize, see Self::get_without_resize.

Source

pub fn unset(&self, index: usize) -> Result<T>

Unsets the value at index, returning the old value if it was set.

Because of the underlying data model, it is possible to have a sparse array without having to use Option<T> since the flag byte is used to track initialization safely.

Source

pub fn unset_without_resize( &self, snapshot_cap: usize, index: usize, ) -> Result<T>

Analogue of Self::set that does not wait for resize and will instead return early if a resize is detected, based on whether the capacity has changed from snapshot_cap.

Source

pub fn set(&self, index: usize, value: T) -> Result<Option<T>>

Sets the value at index, returning an error if the index is out of bounds.

This will wait for any ongoing resize to complete before setting the value. For a version that does not wait for resize, see Self::set_without_resize.

Source

pub fn set_without_resize( &self, snapshot_cap: usize, index: usize, value: T, ) -> Result<Option<T>>

Analogue of Self::set that does not wait for resize and will instead return early if a resize is detected, based on whether the capacity has changed from snapshot_cap.

Source

pub fn cas_set(&self, index: usize, value: T) -> Result<()>

Atomically sets the slot at index to value only if it is currently unset.

This will wait for any ongoing resize to complete before setting the value. For a version that does not wait for resize, see Self::cas_set_without_resize.

Source

pub fn cas_set_without_resize( &self, snapshot_cap: usize, index: usize, value: T, ) -> Result<()>

Analogue of Self::cas_set that does not wait for resize and will instead return early if a resize is detected, based on whether the capacity has changed from snapshot_cap.

Source

pub fn reserve(&self, new_cap: usize)

Increase the capacity to at least new_cap. No-op if already large enough.

Source

pub fn scale_up(&self)

Bumps up the capacity of the array by multiplying the current capacity by the golden ratio.

This is used internally to increase the capacity when needed.

Trait Implementations§

Source§

impl<T: Copy + Clone + Send + Sync + 'static> Clone for SeqArray<T>

Source§

fn clone(&self) -> Self

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: Copy + Clone + Send + Sync + 'static> Default for SeqArray<T>

Source§

fn default() -> Self

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

impl<T: Copy + Clone + Send + Sync + 'static> Drop for SeqArray<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Copy + Clone + Send + Sync + 'static> IntoIterator for &SeqArray<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

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<T: Copy + Clone + Send + Sync + 'static + PartialEq> PartialEq for SeqArray<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Copy + Clone + Send + Sync + 'static + Eq> Eq for SeqArray<T>

Auto Trait Implementations§

§

impl<T> !Freeze for SeqArray<T>

§

impl<T> !RefUnwindSafe for SeqArray<T>

§

impl<T> Send for SeqArray<T>

§

impl<T> Sync for SeqArray<T>

§

impl<T> Unpin for SeqArray<T>

§

impl<T> !UnwindSafe for SeqArray<T>

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.