diff options
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/coords.rs | 2 | ||||
-rw-r--r-- | src/cos.rs | 6 | ||||
-rw-r--r-- | src/euclid.rs | 7 | ||||
-rw-r--r-- | src/exhaustive.rs | 6 | ||||
-rw-r--r-- | src/kd.rs | 7 | ||||
-rw-r--r-- | src/knn.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 4 | ||||
-rw-r--r-- | src/util.rs | 2 | ||||
-rw-r--r-- | src/vp.rs | 9 |
10 files changed, 34 insertions, 15 deletions
@@ -11,7 +11,7 @@ keywords = ["ann", "knn", "nearest-neighbors"] categories = ["algorithms", "data-structures"] [dependencies] -num-traits = "0.2.14" +num-traits = { version = "0.2.14", default-features = false, features = ["libm"] } [dev-dependencies] criterion = "0.3.5" diff --git a/src/coords.rs b/src/coords.rs index cee89ee..c0adaed 100644 --- a/src/coords.rs +++ b/src/coords.rs @@ -2,6 +2,8 @@ use crate::distance::Value; +use alloc::vec::Vec; + /// A coordinate space. pub trait Coordinates { /// The type of individual coordinates. @@ -6,7 +6,7 @@ use crate::distance::{Distance, Metric, Proximity, Value}; use num_traits::real::Real; use num_traits::{one, zero}; -use std::cmp::Ordering; +use core::cmp::Ordering; /// Compute the [cosine *similarity*] between two points. /// @@ -436,7 +436,7 @@ macro_rules! impl_distance { #[inline] fn try_from(value: $f) -> Result<Self, Self::Error> { - if value >= 0.0 && value <= std::$f::consts::PI { + if value >= 0.0 && value <= core::$f::consts::PI { Ok(Self(value.cos())) } else { Err(InvalidAngleError) @@ -492,7 +492,7 @@ impl_distance!(f64); mod tests { use super::*; - use std::f64::consts::{FRAC_PI_2, FRAC_PI_4, PI, SQRT_2}; + use core::f64::consts::{FRAC_PI_2, FRAC_PI_4, PI, SQRT_2}; #[test] fn test_cosine() { diff --git a/src/euclid.rs b/src/euclid.rs index 2268a0c..bfe9ad0 100644 --- a/src/euclid.rs +++ b/src/euclid.rs @@ -4,9 +4,10 @@ use crate::coords::Coordinates; use crate::distance::{Distance, Metric, Proximity, Value}; use crate::lp::Minkowski; +use num_traits::real::Real; use num_traits::zero; -use std::cmp::Ordering; +use core::cmp::Ordering; /// A point in Euclidean space. /// @@ -200,7 +201,7 @@ macro_rules! float_distance { impl From<EuclideanDistance<$f>> for $f { #[inline] fn from(value: EuclideanDistance<$f>) -> $f { - value.0.sqrt() + Real::sqrt(value.0) } } @@ -268,7 +269,7 @@ macro_rules! int_distance { impl From<EuclideanDistance<$i>> for $f { #[inline] fn from(value: EuclideanDistance<$i>) -> Self { - (value.0 as $ff).sqrt() as $f + Real::sqrt(value.0 as $ff) as $f } } diff --git a/src/exhaustive.rs b/src/exhaustive.rs index 7b63ef7..bfddeb1 100644 --- a/src/exhaustive.rs +++ b/src/exhaustive.rs @@ -3,6 +3,8 @@ use crate::distance::Proximity; use crate::knn::{ExactNeighbors, NearestNeighbors, Neighborhood}; +use alloc::vec::Vec; + /// A [`NearestNeighbors`] implementation that does exhaustive search. #[derive(Clone, Debug)] pub struct ExhaustiveSearch<T>(Vec<T>); @@ -48,7 +50,7 @@ impl<T> FromIterator<T> for ExhaustiveSearch<T> { /// An iterator that moves values out of an exhaustive index. #[derive(Debug)] -pub struct IntoIter<T>(std::vec::IntoIter<T>); +pub struct IntoIter<T>(alloc::vec::IntoIter<T>); impl<T> Iterator for IntoIter<T> { type Item = T; @@ -69,7 +71,7 @@ impl<T> IntoIterator for ExhaustiveSearch<T> { /// An iterator over the values in an exhaustive index. #[derive(Debug)] -pub struct Iter<'a, T>(std::slice::Iter<'a, T>); +pub struct Iter<'a, T>(core::slice::Iter<'a, T>); impl<'a, T> Iterator for Iter<'a, T> { type Item = &'a T; @@ -8,6 +8,9 @@ use crate::util::Ordered; use num_traits::Signed; +use alloc::boxed::Box; +use alloc::vec::Vec; + /// A node in a k-d tree. #[derive(Debug)] struct KdNode<T> { @@ -467,7 +470,7 @@ impl<T: Coordinates> FromIterator<T> for FlatKdTree<T> { /// An iterator that moves values out of a flat k-d tree. #[derive(Debug)] -pub struct FlatIntoIter<T>(std::vec::IntoIter<FlatKdNode<T>>); +pub struct FlatIntoIter<T>(alloc::vec::IntoIter<FlatKdNode<T>>); impl<T> Iterator for FlatIntoIter<T> { type Item = T; @@ -488,7 +491,7 @@ impl<T> IntoIterator for FlatKdTree<T> { /// An iterator over the values in a flat k-d tree. #[derive(Debug)] -pub struct FlatIter<'a, T>(std::slice::Iter<'a, FlatKdNode<T>>); +pub struct FlatIter<'a, T>(core::slice::Iter<'a, FlatKdNode<T>>); impl<'a, T> Iterator for FlatIter<'a, T> { type Item = &'a T; @@ -2,6 +2,8 @@ use crate::distance::{Distance, Proximity}; +use alloc::vec::Vec; + /// A nearest neighbor. #[derive(Clone, Copy, Debug)] pub struct Neighbor<V, D> { @@ -351,6 +353,8 @@ pub mod tests { use rand::prelude::*; + use alloc::vec; + type Point = Euclidean<[f32; 3]>; /// Test an [ExactNeighbors] implementation. @@ -124,6 +124,10 @@ #![warn(rust_2018_idioms)] +#![no_std] + +extern crate alloc; + pub mod chebyshev; pub mod coords; pub mod cos; diff --git a/src/util.rs b/src/util.rs index 6a969de..662fbc0 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,6 +1,6 @@ //! Internal utilities. -use std::cmp::Ordering; +use core::cmp::Ordering; /// A wrapper that converts a partial ordering into a total one by panicking. #[derive(Clone, Copy, Debug, PartialOrd)] @@ -4,9 +4,12 @@ use crate::distance::{Distance, DistanceValue, Metric, Proximity}; use crate::knn::{ExactNeighbors, NearestNeighbors, Neighborhood}; use crate::util::Ordered; +use alloc::boxed::Box; +use alloc::vec::Vec; + use num_traits::zero; -use std::fmt::{self, Debug, Formatter}; +use core::fmt::{self, Debug, Formatter}; /// A node in a VP tree. #[derive(Debug)] @@ -527,7 +530,7 @@ impl<T: Proximity> FromIterator<T> for FlatVpTree<T> { } /// An iterator that moves values out of a flat VP tree. -pub struct FlatIntoIter<T: Proximity>(std::vec::IntoIter<FlatVpNode<T>>); +pub struct FlatIntoIter<T: Proximity>(alloc::vec::IntoIter<FlatVpNode<T>>); impl<T> Debug for FlatIntoIter<T> where @@ -559,7 +562,7 @@ impl<T: Proximity> IntoIterator for FlatVpTree<T> { } /// An iterator over the values in a flat VP tree. -pub struct FlatIter<'a, T: Proximity>(std::slice::Iter<'a, FlatVpNode<T>>); +pub struct FlatIter<'a, T: Proximity>(core::slice::Iter<'a, FlatVpNode<T>>); impl<'a, T> Debug for FlatIter<'a, T> where |