pub(super) union Node<V> {
inner: ManuallyDrop<TwoEndedGrove<Self>>,
leaf: ManuallyDrop<TwoEndedGrove<V>>,
}Expand description
A node in the K-dimensional trie structure.
This is an internal implementation detail of KdTrie. It uses a union to represent either an
inner node, which contains child nodes, or a leaf node, which contains values. This allows us to
store values in contiguous memory instead of creating a separate node for each value.
§Node Types
There are two types of nodes in the trie:
-
Inner Nodes: These nodes contain child nodes and are used for all dimensions except the last one. Each inner node is a
TwoEndedGroveof otherNodeinstances, allowing for efficient storage of sparse child nodes. -
Leaf Nodes: These nodes contain the actual values and are used for the last dimension. Each leaf node is a
TwoEndedGroveof values of typeV.
§Thread Safety
The Node struct 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 in
the underlying TwoEndedGrove data structure.
§Memory Management
The Node struct uses a Rust union to efficiently represent either an inner node or a leaf node
without the overhead of an enum discriminant. This approach saves memory but requires careful
manual memory management.
§Safety
It is the responsibility of the caller to know which variant is contained in the union. We only
use this in KdTrie, where we know which variant is contained based on the number of
dimensions.
The inner and leaf variants are wrapped in ManuallyDrop, because union fields can’t have
drop glue. Unions don’t know what they contain, and so can’t know which drop implementation to
run. We delegate the responsibility of dropping the node to the KdTrie, using drop_level.
Fields§
§inner: ManuallyDrop<TwoEndedGrove<Self>>§leaf: ManuallyDrop<TwoEndedGrove<V>>Implementations§
Source§impl<V> Node<V>
impl<V> Node<V>
Sourcepub(super) fn new_inner() -> Self
pub(super) fn new_inner() -> Self
Creates a new inner node.
An inner node contains child nodes and is used for all dimensions except the last one.
Sourcepub(super) fn new_leaf() -> Self
pub(super) fn new_leaf() -> Self
Creates a new leaf node.
A leaf node contains values and is used for the last dimension.
Sourcepub(super) unsafe fn ensure_child(&self, idx: i32, to_insert: Self) -> &Self
pub(super) unsafe fn ensure_child(&self, idx: i32, to_insert: Self) -> &Self
Ensures that a child node exists at the specified index, creating it if necessary.
§Parameters
idx: The index at which to ensure a child node existsto_insert: The node to insert if no child exists at the specified index
§Returns
A reference to the child node at the specified index
§Safety
Can only be called on an inner node.
Sourcepub(super) unsafe fn get_child_mut(&mut self, idx: i32) -> Option<&mut Self>
pub(super) unsafe fn get_child_mut(&mut self, idx: i32) -> Option<&mut Self>
Retrieves a mutable reference to the child node at the specified index, if it exists.
§Parameters
idx: The index of the child node to retrieve
§Returns
Some(&mut Self)if a child node exists at the specified indexNoneif no child node exists at the specified index
§Safety
Can only be called on an inner node.
Sourcepub(super) unsafe fn get_value_mut(&mut self, idx: i32) -> Option<&mut V>
pub(super) unsafe fn get_value_mut(&mut self, idx: i32) -> Option<&mut V>
Sourcepub(super) unsafe fn try_set_value(&self, idx: i32, value: V) -> Result<(), V>
pub(super) unsafe fn try_set_value(&self, idx: i32, value: V) -> Result<(), V>
Attempts to set the value at the specified index.
§Parameters
idx: The index at which to set the valuevalue: The value to set
§Returns
Ok(())if the value was successfully setErr(value)if the index was already occupied, returning the value that we tried to insert
§Safety
Can only be called on a leaf node.
Sourcepub(super) unsafe fn inner(&self) -> &TwoEndedGrove<Self>
pub(super) unsafe fn inner(&self) -> &TwoEndedGrove<Self>
Sourcepub(super) unsafe fn leaf(&self) -> &TwoEndedGrove<V>
pub(super) unsafe fn leaf(&self) -> &TwoEndedGrove<V>
Sourcepub(super) fn drop_level(&mut self, dimensions: usize, level: usize)
pub(super) fn drop_level(&mut self, dimensions: usize, level: usize)
Recursively drops the node and all its children.
This method is called by the Drop implementation of KdTrie to ensure
proper cleanup of all allocated memory.
§Parameters
dimensions: The total number of dimensions in the trielevel: The current level in the trie (the root node has level 0)
Trait Implementations§
Auto Trait Implementations§
impl<V> !Freeze for Node<V>
impl<V> RefUnwindSafe for Node<V>
impl<V> Send for Node<V>
impl<V> Sync for Node<V>
impl<V> Unpin for Node<V>
impl<V> !UnwindSafe for Node<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> 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