pub struct TpnTree<T, const N: usize> { /* private fields */ }
Implementations§
source§impl<T, const N: usize> TpnTree<T, N>
impl<T, const N: usize> TpnTree<T, N>
sourcepub fn iter_breadth_first(&self) -> BreadthFirstIterator<'_, T, N>
pub fn iter_breadth_first(&self) -> BreadthFirstIterator<'_, T, N>
Iterate the tree breadth first, starting with the root.
source§impl<T, const N: usize> TpnTree<T, N>
impl<T, const N: usize> TpnTree<T, N>
sourcepub fn iter_depth_first(&self) -> DepthFirstIterator<'_, T, N>
pub fn iter_depth_first(&self) -> DepthFirstIterator<'_, T, N>
Iterate the tree depth first, starting with the root.
source§impl<T: Coordinates<N>, const N: usize> TpnTree<Vec<T, Global>, N>
impl<T: Coordinates<N>, const N: usize> TpnTree<Vec<T, Global>, N>
sourcepub fn spans(&self, data: &T) -> bool
pub fn spans(&self, data: &T) -> bool
Checks if the tree spans over the coordinates of the provided data.
let tree = Tree3D::root(1.0);
assert_eq!(tree.spans(&[0.5,0.5,0.5]), true);
assert_eq!(tree.spans(&[1.5,0.5,0.5]), false);
sourcepub fn insert_by_coordinates(
&mut self,
data: T,
division_condition: &dyn Fn(&Self) -> bool
) -> Result<(), TpnTreeError>
pub fn insert_by_coordinates(
&mut self,
data: T,
division_condition: &dyn Fn(&Self) -> bool
) -> Result<(), TpnTreeError>
Inserts data in the tree with its center closest to the data given the constrains of the division_condition
.
The division condition
determines when a tree divides and inserts its data into its children.
Errors if the tree does not span the data.
let mut tree = Tree3D::root(1.0);
assert!(tree.insert_by_coordinates([1.0, 0.0, -1.0], &|_| false).is_ok());
sourcepub fn find_by_coordinates(&self, data: &T) -> Result<&Self, TpnTreeError>
pub fn find_by_coordinates(&self, data: &T) -> Result<&Self, TpnTreeError>
Return the tree closest to the given data coordinates.
Errors if the tree does not span the data.
let mut tree = Tree3D::root(1.0);
tree.insert_by_coordinates([1.0, 0.0, -1.0], &|_| false).expect("Couldn't insert.");
assert!(tree
.find_by_coordinates(&[0.0, 0.0, 0.0])
.ok()
.and_then(|tree| tree.data().map(|vec| vec.contains(&[1.0, 0.0, -1.0])))
.unwrap());
source§impl<T, const N: usize> TpnTree<T, N>
impl<T, const N: usize> TpnTree<T, N>
sourcepub fn new(coordinates: [f64; N], span: [f64; N], level: usize) -> Self
pub fn new(coordinates: [f64; N], span: [f64; N], level: usize) -> Self
Creates a new TpnTree.
Use this function if you need explicit control over the initial coordinates or a span with various length along different axis.
Usually the TpnTree::root
function should suffice and is simpler to use.
Examples
Here we create a one dimensional tree, i.e. with one axis sitting on the center [0.0]
with a span of 1.0 in each direction [1.0]
.
This is equal to a line segment spanning from -1.0 to 1.0 with its midpoint at 0.0.
The level
of the root is usually zero.
let root = TpnTree::<(), 1>::new([0.0], [1.0], 0);
Now lets create a two dimensional tree. The tree root corresponds to a square with its center sitting at the coordinates (1.0/1.0) with edges one edge being 4.0 the other being 1.0 units long. The edges equate to twice the span as it originates from the midpoint.
let root = TpnTree::<(), 2>::new([1.0, 1.0], [2.0, 0.5], 0);
sourcepub fn root(span: f64) -> Self
pub fn root(span: f64) -> Self
Creates a new TpnTree with equal span in all dimension at the center of the space at level zero.
Use this function if you want your TpnTree to represenst a centered N-dimensional hypercube.
Examples
Here we create a three dimensional TpnTree with a span of 1.0 in ervery dimension. That is equal to a cube with edges of length 2.0.
let root = TpnTree::<(), 3>::root(1.0);
sourcepub fn divide(&mut self) -> Result<(), TpnTreeError>
pub fn divide(&mut self) -> Result<(), TpnTreeError>
Divides the TpnTree into subregions creating new TpnTrees as children.
Returns true if division happened, returns false when already divided.
Each created child has its center moved by half the parents span up or down along the axis. Every child is equal to one unique combination of such half span moves.
Examples
Dividing in the 2D case is creating four smaller squares.
// +---+ +-+-+
// | | => +-+-+
// +---+ +-+-+
let mut root = TpnTree::<(), 2>::root(1.0);
assert!(root.divide().is_ok());
assert_eq!(root.child_count(), 4);
sourcepub fn get_child(&self, index: usize) -> Option<&Self>
pub fn get_child(&self, index: usize) -> Option<&Self>
Get a reference to a child TpnTree if it exists.
sourcepub fn get_child_mut(&mut self, index: usize) -> Option<&mut Self>
pub fn get_child_mut(&mut self, index: usize) -> Option<&mut Self>
Get a mutable reference to a child TpnTree if it exists.
sourcepub fn child_count(&self) -> usize
pub fn child_count(&self) -> usize
Returns the count of direct children.
sourcepub fn iter_children(&self) -> impl Iterator<Item = &Self>
pub fn iter_children(&self) -> impl Iterator<Item = &Self>
Iterates all direct children by reference.
sourcepub fn iter_children_mut(&mut self) -> impl Iterator<Item = &mut Self>
pub fn iter_children_mut(&mut self) -> impl Iterator<Item = &mut Self>
Iterates all direct children by mutable reference.
sourcepub fn coordinates(&self) -> [f64; N]
pub fn coordinates(&self) -> [f64; N]
Returns the coordinates of the center of the TpnTree.
sourcepub fn data_mut(&mut self) -> &mut Option<T>
pub fn data_mut(&mut self) -> &mut Option<T>
Returns the data by mutable reference of the TpnTree.
sourcepub fn adjacent_trees(&self) -> Vec<Self>
pub fn adjacent_trees(&self) -> Vec<Self>
Returns a list of adjacent trees along each dimension.
The trees appear in order of dimension and that first the tree above self, then the one below. Thereby there will be dimension times two TpnTrees returned.
Trait Implementations§
Auto Trait Implementations§
impl<T, const N: usize> RefUnwindSafe for TpnTree<T, N>where
T: RefUnwindSafe,
impl<T, const N: usize> Send for TpnTree<T, N>where
T: Send,
impl<T, const N: usize> Sync for TpnTree<T, N>where
T: Sync,
impl<T, const N: usize> Unpin for TpnTree<T, N>where
T: Unpin,
impl<T, const N: usize> UnwindSafe for TpnTree<T, N>where
T: UnwindSafe,
Blanket Implementations§
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
self
, then passes self.as_mut()
into the pipe
function.§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds.