pub struct ImmutableKdTree<A: Copy + Default, T: Copy + Default, const K: usize, const B: usize> { /* private fields */ }
Expand description

Immutable floating point k-d tree

Offers less memory utilisation, smaller size when serialized, and faster more consistent query performance. This comes at the expense of not being able to modify the contents of the tree after its initial construction, and longer construction times. As with the vanilla tree, f64 or f32 are supported currently for co-ordinate values, or f16 if the f16 feature is enabled

A convenient type alias exists for ImmutableKdTree with some sensible defaults set: kiddo::ImmutableKdTree.

Implementations§

source§

impl<A, T, const K: usize, const B: usize> ImmutableKdTree<A, T, K, B>
where A: Axis + BestFromDists<T, B>, T: Content, usize: Cast<T>,

source

pub fn new_from_slice(source: &[[A; K]]) -> Self
where usize: Cast<T>,

Creates an ImmutableKdTree, balanced and optimized, populated with items from source.

ImmutableKdTree instances are optimally balanced and tuned, but are not modifiable after construction.

§Examples
use kiddo::immutable::float::kdtree::ImmutableKdTree;

let points: Vec<[f64; 3]> = vec!([1.0f64, 2.0f64, 3.0f64]);
let tree: ImmutableKdTree<f64, u32, 3, 32> = ImmutableKdTree::new_from_slice(&points);

assert_eq!(tree.size(), 1);
source

pub fn size(&self) -> usize

Returns the current number of elements stored in the tree

§Examples
use kiddo::immutable::float::kdtree::ImmutableKdTree;

let points: Vec<[f64; 3]> = vec!([1.0f64, 2.0f64, 3.0f64]);
let tree: ImmutableKdTree<f64, u32, 3, 32> = ImmutableKdTree::new_from_slice(&points);

assert_eq!(tree.size(), 1);
source

pub fn capacity(&self) -> usize

Returns the theoretical max capacity of this tree

source

pub fn generate_stats(&self) -> TreeStats

Generates a TreeStats object, describing some statistics of a particular instance of an ImmutableTree

source

pub fn iter(&self) -> impl Iterator<Item = (T, [A; K])> + '_

Iterate over all (index, point) tuples in arbitrary order.

use kiddo::immutable::float::kdtree::ImmutableKdTree;

let points: Vec<[f64; 3]> = vec!([1.0f64, 2.0f64, 3.0f64]);
let tree: ImmutableKdTree<f64, u32, 3, 32> = ImmutableKdTree::new_from_slice(&points);

let mut pairs: Vec<_> = tree.iter().collect();
assert_eq!(pairs.pop().unwrap(), (0, [1.0, 2.0, 3.0]));
source§

impl<A: Axis, T: Content, const K: usize, const B: usize> ImmutableKdTree<A, T, K, B>

source

pub fn approx_nearest_one<D>(&self, query: &[A; K]) -> NearestNeighbour<A, T>
where A: BestFromDists<T, B>, D: DistanceMetric<A, K>, usize: Cast<T>,

Queries the tree to find the approximate nearest element to query, using the specified distance metric function.

Faster than querying for nearest_one(point) due to not recursing up the tree to find potentially closer points in other branches.

§Examples
    use kiddo::ImmutableKdTree;
    use kiddo::SquaredEuclidean;

    let content: Vec<[f64; 3]> = vec!(
            [1.0, 2.0, 5.0],
            [2.0, 3.0, 6.0]
        );

        let tree: ImmutableKdTree<f64, 3> = ImmutableKdTree::new_from_slice(&content);

    let nearest = tree.approx_nearest_one::<SquaredEuclidean>(&[1.0, 2.0, 5.1]);

    assert!((nearest.distance - 0.01f64).abs() < f64::EPSILON);
    assert_eq!(nearest.item, 0);
source§

impl<A: Axis, T: Content, const K: usize, const B: usize> ImmutableKdTree<A, T, K, B>

source

pub fn best_n_within<D>( &self, query: &[A; K], dist: A, max_qty: usize ) -> impl Iterator<Item = BestNeighbour<A, T>>
where A: BestFromDists<T, B>, usize: Cast<T>, D: DistanceMetric<A, K>,

Finds the “best” n elements within dist of query.

Results are returned in arbitrary order. ‘Best’ is determined by performing a comparison of the elements using < (ie, std::cmp::Ordering::is_lt). Returns an iterator.

§Examples
    use kiddo::ImmutableKdTree;
    use kiddo::best_neighbour::BestNeighbour;
    use kiddo::SquaredEuclidean;

    let content: Vec<[f64; 3]> = vec!(
            [1.0, 2.0, 5.0],
            [2.0, 3.0, 6.0]
        );

        let tree: ImmutableKdTree<f64, 3> = ImmutableKdTree::new_from_slice(&content);

    let mut best_n_within = tree.best_n_within::<SquaredEuclidean>(&[1.0, 2.0, 5.0], 10f64, 1);
    let first = best_n_within.next().unwrap();

    assert_eq!(first, BestNeighbour { distance: 0.0, item: 0 });
source§

impl<A: Axis, T: Content, const K: usize, const B: usize> ImmutableKdTree<A, T, K, B>

source

pub fn nearest_n<D>( &self, query: &[A; K], qty: usize ) -> Vec<NearestNeighbour<A, T>>
where A: BestFromDists<T, B>, D: DistanceMetric<A, K>, usize: Cast<T>,

Finds the nearest qty elements to query, according the specified distance metric function.

§Examples
    use kiddo::ImmutableKdTree;
    use kiddo::SquaredEuclidean;

    let content: Vec<[f64; 3]> = vec!(
            [1.0, 2.0, 5.0],
            [2.0, 3.0, 6.0]
        );

        let tree: ImmutableKdTree<f64, 3> = ImmutableKdTree::new_from_slice(&content);

    let nearest: Vec<_> = tree.nearest_n::<SquaredEuclidean>(&[1.0, 2.0, 5.1], 1);

    assert_eq!(nearest.len(), 1);
    assert!((nearest[0].distance - 0.01f64).abs() < f64::EPSILON);
    assert_eq!(nearest[0].item, 0);
source§

impl<A, T, const K: usize, const B: usize> ImmutableKdTree<A, T, K, B>
where A: Axis + BestFromDists<T, B>, T: Content, usize: Cast<T>,

source

pub fn nearest_n_within<D>( &self, query: &[A; K], dist: A, max_items: usize, sorted: bool ) -> Vec<NearestNeighbour<A, T>>
where D: DistanceMetric<A, K>,

Finds up to n elements within dist of query, using the specified distance metric function.

§Examples
    use kiddo::ImmutableKdTree;
    use kiddo::SquaredEuclidean;
    let content: Vec<[f64; 3]> = vec!(
            [1.0, 2.0, 5.0],
            [2.0, 3.0, 6.0]
        );

        let tree: ImmutableKdTree<f64, 3> = ImmutableKdTree::new_from_slice(&content);

    let within = tree.nearest_n_within::<SquaredEuclidean>(&[1.0, 2.0, 5.0], 10f64, 2, true);

    assert_eq!(within.len(), 2);
source§

impl<A, T, const K: usize, const B: usize> ImmutableKdTree<A, T, K, B>
where A: Axis + BestFromDists<T, B>, T: Content, usize: Cast<T>,

source

pub fn nearest_one<D>(&self, query: &[A; K]) -> NearestNeighbour<A, T>
where D: DistanceMetric<A, K>,

Queries the tree to find the nearest item to the query point.

Faster than querying for nearest_n(point, 1, …) due to not needing to allocate memory or maintain sorted results.

§Examples
    use kiddo::ImmutableKdTree;
    use kiddo::SquaredEuclidean;

    let content: Vec<[f64; 3]> = vec!(
            [1.0, 2.0, 5.0],
            [2.0, 3.0, 6.0]
        );

        let tree: ImmutableKdTree<f64, 3> = ImmutableKdTree::new_from_slice(&content);

    let nearest = tree.nearest_one::<SquaredEuclidean>(&[1.0, 2.0, 5.1]);

    assert!((nearest.distance - 0.01f64).abs() < f64::EPSILON);
    assert_eq!(nearest.item, 0);
source§

impl<A: Axis, T: Content, const K: usize, const B: usize> ImmutableKdTree<A, T, K, B>

source

pub fn within<D>(&self, query: &[A; K], dist: A) -> Vec<NearestNeighbour<A, T>>
where A: BestFromDists<T, B>, D: DistanceMetric<A, K>, usize: Cast<T>,

Finds all elements within dist of query, using the specified distance metric function.

Results are returned sorted nearest-first

§Examples
    use kiddo::ImmutableKdTree;
    use kiddo::SquaredEuclidean;
    let content: Vec<[f64; 3]> = vec!(
            [1.0, 2.0, 5.0],
            [2.0, 3.0, 6.0]
        );

        let tree: ImmutableKdTree<f64, 3> = ImmutableKdTree::new_from_slice(&content);

    let within = tree.within::<SquaredEuclidean>(&[1.0, 2.0, 5.0], 10f64);

    assert_eq!(within.len(), 2);
source§

impl<A: Axis, T: Content, const K: usize, const B: usize> ImmutableKdTree<A, T, K, B>

source

pub fn within_unsorted<D>( &self, query: &[A; K], dist: A ) -> Vec<NearestNeighbour<A, T>>
where A: BestFromDists<T, B>, D: DistanceMetric<A, K>, usize: Cast<T>,

Finds all elements within dist of query, using the specified distance metric function.

Results are returned in arbitrary order. Faster than within.

§Examples
use kiddo::ImmutableKdTree;
use kiddo::SquaredEuclidean;
let content: Vec<[f64; 3]> = vec!(
            [1.0, 2.0, 5.0],
            [2.0, 3.0, 6.0]
        );

        let tree: ImmutableKdTree<f64, 3> = ImmutableKdTree::new_from_slice(&content);

let within = tree.within_unsorted::<SquaredEuclidean>(&[1.0, 2.0, 5.0], 10f64);

assert_eq!(within.len(), 2);

Trait Implementations§

source§

impl<A: Copy + Default, T: Copy + Default, const K: usize, const B: usize> Archive for ImmutableKdTree<A, T, K, B>
where Vec<LeafNode<A, T, K, B>>: Archive, Vec<A>: Archive, usize: Archive,

§

type Archived = ArchivedImmutableKdTree<A, T, K, B>

The archived representation of this type. Read more
§

type Resolver = ImmutableKdTreeResolver<A, T, K, B>

The resolver for this type. It must contain all the additional information from serializing needed to make the archived type from the normal type.
source§

unsafe fn resolve( &self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived )

Creates the archived version of this value at the given position and writes it to the given output. Read more
source§

impl<A: Clone + Copy + Default, T: Clone + Copy + Default, const K: usize, const B: usize> Clone for ImmutableKdTree<A, T, K, B>

source§

fn clone(&self) -> ImmutableKdTree<A, T, K, B>

Returns a copy 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<A: Debug + Copy + Default, T: Debug + Copy + Default, const K: usize, const B: usize> Debug for ImmutableKdTree<A, T, K, B>

source§

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

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

impl<'de, A, T, const K: usize, const B: usize> Deserialize<'de> for ImmutableKdTree<A, T, K, B>
where A: Deserialize<'de> + Copy + Default, T: Deserialize<'de> + Copy + Default,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<__D: Fallible + ?Sized, A: Copy + Default, T: Copy + Default, const K: usize, const B: usize> Deserialize<ImmutableKdTree<A, T, K, B>, __D> for Archived<ImmutableKdTree<A, T, K, B>>
where Vec<LeafNode<A, T, K, B>>: Archive, Archived<Vec<LeafNode<A, T, K, B>>>: Deserialize<Vec<LeafNode<A, T, K, B>>, __D>, Vec<A>: Archive, Archived<Vec<A>>: Deserialize<Vec<A>, __D>, usize: Archive, Archived<usize>: Deserialize<usize, __D>,

source§

fn deserialize( &self, deserializer: &mut __D ) -> Result<ImmutableKdTree<A, T, K, B>, __D::Error>

Deserializes using the given deserializer
source§

impl<A, T, const K: usize, const B: usize> From<&[[A; K]]> for ImmutableKdTree<A, T, K, B>
where A: Axis + BestFromDists<T, B>, T: Content, usize: Cast<T>,

source§

fn from(slice: &[[A; K]]) -> Self

Creates an ImmutableKdTree, balanced and optimized, populated with items from source.

ImmutableKdTree instances are optimally balanced and tuned, but are not modifiable after construction.

§Examples
use kiddo::immutable::float::kdtree::ImmutableKdTree;

let points: Vec<[f64; 3]> = vec!([1.0f64, 2.0f64, 3.0f64]);
let tree: ImmutableKdTree<f64, u32, 3, 32> = (&*points).into();

assert_eq!(tree.size(), 1);
source§

impl<A: PartialEq + Copy + Default, T: PartialEq + Copy + Default, const K: usize, const B: usize> PartialEq for ImmutableKdTree<A, T, K, B>

source§

fn eq(&self, other: &ImmutableKdTree<A, T, K, B>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<__S: Fallible + ?Sized, A: Copy + Default, T: Copy + Default, const K: usize, const B: usize> Serialize<__S> for ImmutableKdTree<A, T, K, B>
where Vec<LeafNode<A, T, K, B>>: Serialize<__S>, Vec<A>: Serialize<__S>, usize: Serialize<__S>,

source§

fn serialize(&self, serializer: &mut __S) -> Result<Self::Resolver, __S::Error>

Writes the dependencies for the object and returns a resolver that can create the archived type.
source§

impl<A, T, const K: usize, const B: usize> Serialize for ImmutableKdTree<A, T, K, B>
where A: Serialize + Copy + Default, T: Serialize + Copy + Default,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<A: Copy + Default, T: Copy + Default, const K: usize, const B: usize> StructuralPartialEq for ImmutableKdTree<A, T, K, B>

Auto Trait Implementations§

§

impl<A, T, const K: usize, const B: usize> RefUnwindSafe for ImmutableKdTree<A, T, K, B>

§

impl<A, T, const K: usize, const B: usize> Send for ImmutableKdTree<A, T, K, B>
where A: Send, T: Send,

§

impl<A, T, const K: usize, const B: usize> Sync for ImmutableKdTree<A, T, K, B>
where A: Sync, T: Sync,

§

impl<A, T, const K: usize, const B: usize> Unpin for ImmutableKdTree<A, T, K, B>
where A: Unpin, T: Unpin,

§

impl<A, T, const K: usize, const B: usize> UnwindSafe for ImmutableKdTree<A, T, K, B>
where A: UnwindSafe, T: UnwindSafe,

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> ArchivePointee for T

§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
source§

impl<T> ArchiveUnsized for T
where T: Archive,

§

type Archived = <T as Archive>::Archived

The archived counterpart of this type. Unlike Archive, it may be unsized. Read more
§

type MetadataResolver = ()

The resolver for the metadata of this type. Read more
source§

unsafe fn resolve_metadata( &self, _: usize, _: <T as ArchiveUnsized>::MetadataResolver, _: *mut <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata )

Creates the archived version of the metadata for this value at the given position and writes it to the given output. Read more
source§

unsafe fn resolve_unsized( &self, from: usize, to: usize, resolver: Self::MetadataResolver, out: *mut RelPtr<Self::Archived, <isize as Archive>::Archived> )

Resolves a relative pointer to this value with the given from and to and writes it to the given output. Read more
source§

impl<T> Az for T

source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

source§

fn cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> CheckedAs for T

source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
source§

impl<F, W, T, D> Deserialize<With<T, W>, D> for F
where W: DeserializeWith<F, T, D>, D: Fallible + ?Sized, F: ?Sized,

source§

fn deserialize( &self, deserializer: &mut D ) -> Result<With<T, W>, <D as Fallible>::Error>

Deserializes using the given deserializer
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

source§

fn lossy_into(self) -> Dst

Performs the conversion.
source§

impl<T> OverflowingAs for T

source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
§

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
§

impl<T> Pointee for T

§

type Metadata = ()

The type for metadata in pointers and references to Self.
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> SaturatingAs for T

source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T, S> SerializeUnsized<S> for T
where T: Serialize<S>, S: Serializer + ?Sized,

source§

fn serialize_unsized( &self, serializer: &mut S ) -> Result<usize, <S as Fallible>::Error>

Writes the object and returns the position of the archived type.
source§

fn serialize_metadata(&self, _: &mut S) -> Result<(), <S as Fallible>::Error>

Serializes the metadata for the given type.
source§

impl<T> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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.
source§

impl<T> UnwrappedAs for T

source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> WrappingAs for T

source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,