KdTrie

Struct KdTrie 

Source
pub struct KdTrie<V> {
    root: Node<V>,
    dimensions: usize,
}
Expand description

A K-dimensional trie data structure that efficiently stores values indexed by multi-dimensional coordinates.

The difference between KdTrie and MultiIndexed is that the latter has the dimension as part of the type, which allows for more type safety. The KdTrie itself may be useful in a situation where the dimension is not known at compile time.

The trie is structured as a tree where each level corresponds to one dimension of the coordinate space. For example, in a 3D trie, the first level corresponds to the x-coordinate, the second level to the y-coordinate, and the third level to the z-coordinate. This structure allows for efficient storage and retrieval of values in a sparse coordinate space.

§Thread Safety

KdTrie is designed to be thread-safe and wait-free, allowing concurrent insertions and retrievals from multiple threads. This is achieved through the use of atomic operations and the thread-safe properties of the underlying TwoEndedGrove data structure.

§Memory Efficiency

The trie only allocates memory for coordinates that are actually used, making it memory-efficient for sparse data. Each node in the trie is either an inner node (which contains child nodes) or a leaf node (which contains values).

§Type Parameters

  • V: The type of values stored in the trie

Fields§

§root: Node<V>§dimensions: usize

Implementations§

Source§

impl<V> KdTrie<V>

Source

pub fn iter(&self) -> Iter<'_, V, Vec<i32>>

Source

pub fn iter_mut(&mut self) -> IterMut<'_, V, Vec<i32>>

Source§

impl<V> KdTrie<V>

Source

pub fn new(dimensions: usize) -> Self

Creates a new KdTrie with the specified number of dimensions.

§Parameters
  • dimensions: The number of dimensions for the trie (must be greater than 0)
§Panics

Panics if dimensions is 0.

Source

pub fn get(&self, coords: &[i32]) -> Option<&V>

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

§Parameters
  • coords: A slice of coordinates with length equal to self.dimensions
§Returns
  • Some(&V) if a value exists at the specified coordinates
  • None if no value exists at the specified coordinates
§Panics

Panics if the length of coords does not match the number of dimensions.

Source

pub fn insert(&self, coords: &[i32], value: V)

Inserts a value at the specified coordinates.

This method traverses the trie structure to find the appropriate location for the value, creating nodes as needed along the way.

§Parameters
  • coords: A slice of coordinates with length equal to self.dimensions
  • value: The value to insert at the specified coordinates
§Panics

Panics if the length of coords does not match the number of dimensions.

Source

pub fn try_insert(&self, coords: &[i32], value: V) -> Result<(), V>

Attempts to insert a value at the specified coordinates.

This method traverses the trie structure to find the appropriate location for the value, creating nodes as needed along the way.

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

§Parameters
  • coords: A slice of coordinates with length equal to self.dimensions
  • value: The value to insert at the specified coordinates
§Returns
  • Ok(()) if the value was successfully inserted
  • Err(value) if the coordinate was already occupied, returning the value that we tried to insert
§Panics

Panics if the length of coords does not match the number of dimensions.

§Examples
use once::multiindexed::KdTrie;

let trie = KdTrie::<i32>::new(2);

assert_eq!(trie.try_insert(&[-3, 1], 10), Ok(()));
assert_eq!(trie.try_insert(&[-3, 1], 10), Err(10)); // Coordinate already occupied
Source

pub fn get_mut(&mut self, coords: &[i32]) -> Option<&mut V>

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

This method can only be called if we have an exclusive reference to self. When you have exclusive access, there’s no possibility of concurrent access, so the atomic synchronization used by get is unnecessary.

§Parameters
  • coords: A slice of coordinates with length equal to self.dimensions
§Returns
  • Some(&mut V) if a value exists at the specified coordinates
  • None if no value exists at the specified coordinates
§Panics

Panics if the length of coords does not match the number of dimensions.

§Examples
use once::multiindexed::KdTrie;

let mut trie = KdTrie::<i32>::new(2);
trie.insert(&[1, 2], 42);

// Modify the value through a mutable reference
if let Some(value) = trie.get_mut(&[1, 2]) {
    *value = 100;
}

assert_eq!(trie.get(&[1, 2]), Some(&100));
assert_eq!(trie.get_mut(&[0, 0]), None);
Source

pub fn dimensions(&self) -> usize

Source

pub(super) fn root(&self) -> &Node<V>

Source

pub(super) fn root_mut(&mut self) -> &mut Node<V>

Trait Implementations§

Source§

impl<V: Clone> Clone for KdTrie<V>

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<V: Debug> Debug for KdTrie<V>

Source§

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

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

impl<V> Drop for KdTrie<V>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<V: Hash> Hash for KdTrie<V>

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<V> Index<&[i32]> for KdTrie<V>

Source§

type Output = V

The returned type after indexing.
Source§

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

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

impl<V> IndexMut<&[i32]> for KdTrie<V>

Source§

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

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

impl<'a, V> IntoIterator for &'a KdTrie<V>

Source§

type IntoIter = Iter<'a, V, Vec<i32>>

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

type Item = (Vec<i32>, &'a V)

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<'a, V> IntoIterator for &'a mut KdTrie<V>

Source§

type IntoIter = IterMut<'a, V, Vec<i32>>

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

type Item = (Vec<i32>, &'a mut V)

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<V: PartialEq> PartialEq for KdTrie<V>

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<V: Eq> Eq for KdTrie<V>

Auto Trait Implementations§

§

impl<V> !Freeze for KdTrie<V>

§

impl<V> RefUnwindSafe for KdTrie<V>

§

impl<V> Send for KdTrie<V>

§

impl<V> Sync for KdTrie<V>

§

impl<V> Unpin for KdTrie<V>

§

impl<V> !UnwindSafe for KdTrie<V>

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.