Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with no-std #6

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

[dev-dependencies]
criterion = "0.3.5"
Expand Down
1 change: 1 addition & 0 deletions src/coords.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! [Coordinate spaces](https://en.wikipedia.org/wiki/Cartesian_coordinate_system).

use alloc::vec::Vec;
use crate::distance::Value;

/// A coordinate space.
Expand Down
6 changes: 3 additions & 3 deletions src/cos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion src/euclid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
use crate::distance::{Distance, Metric, Proximity, Value};
use crate::lp::Minkowski;

use num_traits::real::Real;

Check warning on line 7 in src/euclid.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `num_traits::real::Real`

Check warning on line 7 in src/euclid.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `num_traits::real::Real`
use num_traits::zero;

use std::cmp::Ordering;
use core::cmp::Ordering;

/// A point in Euclidean space.
///
Expand Down
5 changes: 3 additions & 2 deletions src/exhaustive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::distance::Proximity;
use crate::knn::{ExactNeighbors, NearestNeighbors, Neighborhood};
use alloc::vec::Vec;

/// A [`NearestNeighbors`] implementation that does exhaustive search.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -48,7 +49,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;
Expand All @@ -69,7 +70,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;
Expand Down
8 changes: 6 additions & 2 deletions src/kd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use crate::lp::Minkowski;
use crate::knn::{ExactNeighbors, NearestNeighbors, Neighborhood};
use crate::util::Ordered;

use alloc::vec;
use alloc::vec::Vec;
use alloc::boxed::Box;

use num_traits::Signed;

/// A node in a k-d tree.
Expand Down Expand Up @@ -467,7 +471,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>(vec::IntoIter<FlatKdNode<T>>);

impl<T> Iterator for FlatIntoIter<T> {
type Item = T;
Expand All @@ -488,7 +492,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;
Expand Down
1 change: 1 addition & 0 deletions src/knn.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! [Nearest neighbor search](https://en.wikipedia.org/wiki/Nearest_neighbor_search) interfaces.

use alloc::vec::Vec;
use crate::distance::{Distance, Proximity};

/// A nearest neighbor.
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@
//! [`k_nearest_within()`]: knn::NearestNeighbors#method.k_nearest_within
//! [`ExactNeighbors`]: knn::ExactNeighbors

#![warn(rust_2018_idioms)]
#![no_std]

#[macro_use]

Check warning on line 127 in src/lib.rs

View workflow job for this annotation

GitHub Actions / build

unused `#[macro_use]` import
extern crate alloc;

pub mod chebyshev;
pub mod coords;
Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
10 changes: 7 additions & 3 deletions src/vp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ use crate::distance::{Distance, DistanceValue, Metric, Proximity};
use crate::knn::{ExactNeighbors, NearestNeighbors, Neighborhood};
use crate::util::Ordered;

use alloc::boxed::Box;
use alloc::vec;
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)]
Expand Down Expand Up @@ -527,7 +531,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>(vec::IntoIter<FlatVpNode<T>>);

impl<T> Debug for FlatIntoIter<T>
where
Expand Down Expand Up @@ -559,7 +563,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
Expand Down
Loading