Grove

Struct Grove 

Source
pub struct Grove<T> {
    blocks: [Block<T>; 32],
    max: AtomicUsize,
}
Expand description

A sparse vector with pinned elements and geometrically growing capacity.

Grove (a pun on “grow vec”) is a specialized data structure that provides efficient storage for data with the following key features:

  • Thread Safety: Safe for concurrent reads and writes from multiple threads
  • Memory Efficiency: Uses a block-based allocation strategy that grows geometrically
  • Pinned Elements: Once inserted, elements never move in memory
  • Sparse Storage: Efficiently handles sparse data with large gaps between indices

This data structure is primarily used as the backing store for other collections in this crate, such as OnceVec and MultiIndexed.

§Implementation Details

Grove uses a series of fixed-size blocks to store elements. Each block’s size is a power of 2, and the block number determines its size. This approach allows for efficient memory usage while still supporting large indices without allocating memory for all intermediate elements. We use distinct lazily-allocated blocks to avoid any reallocation, which would invalidate pointers held by other threads.

§Examples

use once::Grove;

let grove = Grove::<i32>::new();

// Insert elements at arbitrary indices
grove.insert(0, 10);
grove.insert(100, 20);
grove.insert(1000, 30);

// Retrieve elements
assert_eq!(grove.get(0), Some(&10));
assert_eq!(grove.get(100), Some(&20));
assert_eq!(grove.get(1000), Some(&30));
assert_eq!(grove.get(50), None); // No element at this index

Fields§

§blocks: [Block<T>; 32]§max: AtomicUsize

The maximum index that has been inserted into the Grove (exclusive).

Implementations§

Source§

impl<T> Grove<T>

Source

pub fn new() -> Self

Creates a new empty Grove.

§Examples
use once::Grove;

let grove = Grove::<i32>::new();
Source

fn locate(&self, index: usize) -> (usize, usize)

Finds the block and offset within the block for the given index.

This is an internal method used to determine which block contains the element at the specified index and the offset within that block.

§Parameters
  • index: The index to locate
§Returns

A tuple containing the block number and the offset within that block.

Source

fn ensure_init(&self, block_num: usize)

Ensures that the specified block is initialized.

This is an internal method that initializes a block if it hasn’t been initialized yet. It is called before any operation that needs to access a block.

§Parameters
  • block_num: The block number to initialize
Source

pub fn insert(&self, index: usize, value: T)

Inserts a value at the specified index.

This operation is thread-safe and can be called from multiple threads. However, this method panics if a value already exists at the specified index. Therefore, it should only be called at most once for any given index.

§Parameters
  • index: The index at which to insert the value
  • value: The value to insert
§Examples
use once::Grove;

let grove = Grove::<i32>::new();
grove.insert(0, 10);
grove.insert(100, 20);
Source

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

Attempts to insert a value at the specified index.

This method will only insert the value if the index is not already occupied. If the index is already occupied, the value is returned in the Err variant.

§Parameters
  • index: The index at which to insert the value
  • value: The value to insert
§Returns
  • Ok(()) if the value was successfully inserted
  • Err(value) if the index was already occupied, returning the value that we tried to insert
§Examples
use once::Grove;

let grove = Grove::<i32>::new();
assert_eq!(grove.try_insert(0, 10), Ok(()));
assert_eq!(grove.try_insert(0, 20), Err(20)); // Index already occupied
Source

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

Retrieves a reference to the value at the specified index, if it exists.

§Parameters
  • index: The index of the value to retrieve
§Returns
  • Some(&T) if a value exists at the specified index
  • None if no value exists at the specified index
§Examples
use once::Grove;

let grove = Grove::<i32>::new();
grove.insert(0, 10);

assert_eq!(grove.get(0), Some(&10));
assert_eq!(grove.get(1), None);
Source

pub fn get_mut(&mut self, index: usize) -> Option<&mut T>

Retrieves a mutable reference to the value at the specified index, if it exists.

This method can only be called if we have an exclusive reference to self, which may not be very common in practice. However, it is useful for Drop implementations.

§Parameters
  • index: The index of the value to retrieve
§Returns
  • Some(&mut T) if a value exists at the specified index
  • None if no value exists at the specified index
§Examples
use once::Grove;

let mut grove = Grove::<i32>::new();
grove.insert(0, 10);

if let Some(value) = grove.get_mut(0) {
    *value = 20;
}

assert_eq!(grove.get(0), Some(&20));
Source

pub unsafe fn get_unchecked(&self, index: usize) -> &T

Retrieves a reference to the value at the specified index without checking that a value exists.

§Safety

A value must have been previously inserted at the specified index.

Source

pub fn is_set(&self, index: usize) -> bool

Checks if a value exists at the specified index.

§Parameters
  • index: The index to check
§Returns

true if a value exists at the specified index, false otherwise.

§Examples
use once::Grove;

let grove = Grove::<i32>::new();
grove.insert(0, 10);

assert!(grove.is_set(0));
assert!(!grove.is_set(1));
Source

pub fn len(&self) -> usize

Returns the length of the Grove.

The length is defined as the maximum index that has been inserted into the Grove plus one. Note that this does not necessarily reflect the number of elements in the Grove, as there may be gaps. Also, since this is a wait-free data structure, the return value may not be accurate, but it will always be a lower bound for the true length from the perspective of any thread.

§Returns

The length of the Grove.

§Examples
use once::Grove;

let grove = Grove::<i32>::new();
grove.insert(0, 10);
grove.insert(5, 20);

assert_eq!(grove.len(), 6); // 5 + 1
Source

pub fn is_empty(&self) -> bool

Source

pub fn from_vec(v: Vec<T>) -> Self

Creates a Grove from a Vec.

The elements in the vector will be inserted into the Grove at their respective indices.

§Parameters
  • v: The vector to convert
§Returns

A new Grove containing the elements from the vector.

§Examples
use once::Grove;

let vec = vec![10, 20, 30];
let grove = Grove::from_vec(vec);

assert_eq!(grove.get(0), Some(&10));
assert_eq!(grove.get(1), Some(&20));
assert_eq!(grove.get(2), Some(&30));
Source

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator over the index-value pairs in the Grove.

The iterator yields pairs in order of their indices, from 0 to len() - 1. Indices that don’t have a value are skipped.

§Returns

An iterator over (usize, &T) pairs in the Grove.

§Examples
use once::Grove;

let grove = Grove::<i32>::new();
grove.insert(0, 10);
grove.insert(2, 30);

let entries: Vec<_> = grove.iter().collect();
assert_eq!(entries, vec![(0, &10), (2, &30)]);
Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = (usize, &mut T)>

Source

pub fn values(&self) -> Values<'_, T>

Returns an iterator over the values in the Grove.

The iterator yields values in order of their indices, from 0 to len() - 1. Indices that don’t have a value are skipped.

§Returns

An iterator over the values in the Grove.

§Examples
use once::Grove;

let grove = Grove::<i32>::new();
grove.insert(0, 10);
grove.insert(2, 30);

let values: Vec<_> = grove.values().collect();
assert_eq!(values, vec![&10, &30]);

Trait Implementations§

Source§

impl<T: Clone> Clone for Grove<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: Debug> Debug for Grove<T>

Source§

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

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

impl<T> Default for Grove<T>

Source§

fn default() -> Self

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

impl<T: Hash> Hash for Grove<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> Index<usize> for Grove<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<usize> for Grove<T>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, T> IntoIterator for &'a Grove<T>

Source§

type IntoIter = Iter<'a, T>

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

type Item = (usize, &'a T)

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq> PartialEq for Grove<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: Eq> Eq for Grove<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Grove<T>

§

impl<T> RefUnwindSafe for Grove<T>

§

impl<T> Send for Grove<T>

§

impl<T> Sync for Grove<T>

§

impl<T> Unpin for Grove<T>

§

impl<T> !UnwindSafe for Grove<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> 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

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 = 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.