diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2020-05-28 14:47:10 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2020-06-24 10:02:23 -0400 |
commit | 0bc4df6a1ab14ecf55d68cc86134d14b26eca6e5 (patch) | |
tree | 9da22c96b160ece1f7efc463bbd6520aadafd85c /src/lib.rs | |
parent | 7677a551690458c4bc588955ea0d4b5db7f8942d (diff) | |
download | acap-0bc4df6a1ab14ecf55d68cc86134d14b26eca6e5.tar.xz |
exhaustive: Implement an exhaustive search index
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 32 |
1 files changed, 32 insertions, 0 deletions
@@ -5,6 +5,7 @@ pub mod coords; pub mod distance; pub mod euclid; +pub mod exhaustive; pub use coords::Coordinates; pub use distance::{Distance, Metric, Proximity}; @@ -303,6 +304,12 @@ pub trait ExactNeighbors<K: Proximity<V>, V = K>: NearestNeighbors<K, V> {} pub mod tests { use super::*; + use crate::exhaustive::ExhaustiveSearch; + + use rand::prelude::*; + + use std::iter::FromIterator; + type Point = Euclidean<[f32; 3]>; /// Test a [NearestNeighbors] implementation. @@ -313,6 +320,7 @@ pub mod tests { { test_empty(&from_iter); test_pythagorean(&from_iter); + test_random_points(&from_iter); } fn test_empty<T, F>(from_iter: &F) @@ -385,4 +393,28 @@ pub mod tests { ] ); } + + fn test_random_points<T, F>(from_iter: &F) + where + T: NearestNeighbors<Point>, + F: Fn(Vec<Point>) -> T, + { + let mut points = Vec::new(); + for _ in 0..256 { + points.push(Euclidean([random(), random(), random()])); + } + + let index = from_iter(points.clone()); + let eindex = ExhaustiveSearch::from_iter(points.clone()); + + let target = Euclidean([random(), random(), random()]); + + assert_eq!( + index.k_nearest(&target, 3), + eindex.k_nearest(&target, 3), + "target: {:?}, points: {:#?}", + target, + points, + ); + } } |