TwoEndedGrove

Struct TwoEndedGrove 

Source
pub struct TwoEndedGrove<T> {
    non_neg: Grove<T>,
    neg: Grove<T>,
    min: AtomicI32,
    max: AtomicI32,
}
Expand description

A bidirectional sparse vector that supports both positive and negative indices.

TwoEndedGrove extends the functionality of Grove by allowing elements to be indexed using both positive and negative integers. It maintains two separate Grove instances: one for non-negative indices and another for negative indices.

This data structure is primarily used as the backing store for the nodes in KdTrie.

§Examples

use once::TwoEndedGrove;

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

// Insert elements at both positive and negative indices
grove.insert(-5, 10);
grove.insert(0, 20);
grove.insert(5, 30);

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

// Get the range of valid indices
assert_eq!(grove.range(), -5..6);

Fields§

§non_neg: Grove<T>§neg: Grove<T>§min: AtomicI32§max: AtomicI32

Implementations§

Source§

impl<T> TwoEndedGrove<T>

Source

pub fn new() -> Self

Creates a new empty TwoEndedGrove.

§Examples
use once::TwoEndedGrove;

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

pub fn insert(&self, idx: i32, value: T)

Inserts a value at the specified index.

This operation is thread-safe and can be called from multiple threads. This method panics if a value already exists at the specified index.

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

let grove = TwoEndedGrove::<i32>::new();
grove.insert(-5, 10);
grove.insert(5, 20);
Source

pub fn try_insert(&self, idx: i32, 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
  • idx: 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::TwoEndedGrove;

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

pub fn get(&self, idx: i32) -> Option<&T>

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

§Parameters
  • idx: 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::TwoEndedGrove;

let grove = TwoEndedGrove::<i32>::new();
grove.insert(-5, 10);

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

pub fn get_mut(&mut self, idx: i32) -> 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. It is mainly used for the Drop implementation of the MultiIndexed, which allows to use non-atomic operations for performance.

§Parameters
  • idx: 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::TwoEndedGrove;

let mut grove = TwoEndedGrove::<i32>::new();
grove.insert(-5, 10);

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

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

pub fn range(&self) -> Range<i32>

Returns the range of indices that have values in the TwoEndedGrove.

We return both the minimum and maximum indices at the same time to optimize memory orderings.

§Returns

A Range representing the range of indices that have values in the TwoEndedGrove. It is not guaranteed that all indices in the range have values. However, it is guaranteed that only the indices in the range have values if the TwoEndedGrove is not being concurrently modified.

§Examples
use once::TwoEndedGrove;

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

assert_eq!(grove.range(), -5..6);
Source

pub fn iter(&self) -> impl Iterator<Item = (i32, &T)>

Source

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

Source

pub fn values(&self) -> impl Iterator<Item = &T>

Source

pub fn values_mut(&mut self) -> impl Iterator<Item = &mut T>

Source

pub fn is_set(&self, idx: i32) -> bool

Checks if a value exists at the specified index.

§Parameters
  • idx: The index to check
§Returns

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

§Examples
use once::TwoEndedGrove;

let grove = TwoEndedGrove::<i32>::new();
grove.insert(-5, 10);

assert!(grove.is_set(-5));
assert!(!grove.is_set(0));

Trait Implementations§

Source§

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

Source§

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

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

impl<T> Default for TwoEndedGrove<T>

Source§

fn default() -> Self

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

impl<T: Hash> Hash for TwoEndedGrove<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<i32> for TwoEndedGrove<T>

Source§

type Output = T

The returned type after indexing.
Source§

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

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

impl<T> IndexMut<i32> for TwoEndedGrove<T>

Source§

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

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

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

Auto Trait Implementations§

§

impl<T> !Freeze for TwoEndedGrove<T>

§

impl<T> RefUnwindSafe for TwoEndedGrove<T>

§

impl<T> Send for TwoEndedGrove<T>

§

impl<T> Sync for TwoEndedGrove<T>

§

impl<T> Unpin for TwoEndedGrove<T>

§

impl<T> !UnwindSafe for TwoEndedGrove<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.