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: usizeImplementations§
Source§impl<V> KdTrie<V>
impl<V> KdTrie<V>
Sourcepub fn get(&self, coords: &[i32]) -> Option<&V>
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 toself.dimensions
§Returns
Some(&V)if a value exists at the specified coordinatesNoneif no value exists at the specified coordinates
§Panics
Panics if the length of coords does not match the number of dimensions.
Sourcepub fn insert(&self, coords: &[i32], value: V)
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 toself.dimensionsvalue: The value to insert at the specified coordinates
§Panics
Panics if the length of coords does not match the number of dimensions.
Sourcepub fn try_insert(&self, coords: &[i32], value: V) -> Result<(), V>
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 toself.dimensionsvalue: The value to insert at the specified coordinates
§Returns
Ok(())if the value was successfully insertedErr(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 occupiedSourcepub fn get_mut(&mut self, coords: &[i32]) -> Option<&mut V>
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 toself.dimensions
§Returns
Some(&mut V)if a value exists at the specified coordinatesNoneif 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);pub fn dimensions(&self) -> usize
pub(super) fn root(&self) -> &Node<V>
pub(super) fn root_mut(&mut self) -> &mut Node<V>
Trait Implementations§
Source§impl<'a, V> IntoIterator for &'a KdTrie<V>
impl<'a, V> IntoIterator for &'a KdTrie<V>
Source§impl<'a, V> IntoIterator for &'a mut KdTrie<V>
impl<'a, V> IntoIterator for &'a mut KdTrie<V>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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