From 0bc4df6a1ab14ecf55d68cc86134d14b26eca6e5 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 28 May 2020 14:47:10 -0400 Subject: exhaustive: Implement an exhaustive search index --- src/exhaustive.rs | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/exhaustive.rs (limited to 'src/exhaustive.rs') diff --git a/src/exhaustive.rs b/src/exhaustive.rs new file mode 100644 index 0000000..909edda --- /dev/null +++ b/src/exhaustive.rs @@ -0,0 +1,89 @@ +//! Exhaustive nearest neighbor search. + +use crate::distance::Proximity; +use crate::{ExactNeighbors, NearestNeighbors, Neighborhood}; + +use std::iter::FromIterator; + +/// A [NearestNeighbors] implementation that does exhaustive search. +#[derive(Debug)] +pub struct ExhaustiveSearch(Vec); + +impl ExhaustiveSearch { + /// Create an empty ExhaustiveSearch index. + pub fn new() -> Self { + Self(Vec::new()) + } + + /// Add a new item to the index. + pub fn push(&mut self, item: T) { + self.0.push(item); + } + + /// Get the size of this index. + pub fn len(&self) -> usize { + self.0.len() + } + + /// Check if this index is empty. + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } +} + +impl Default for ExhaustiveSearch { + fn default() -> Self { + Self::new() + } +} + +impl FromIterator for ExhaustiveSearch { + fn from_iter>(items: I) -> Self { + Self(items.into_iter().collect()) + } +} + +impl IntoIterator for ExhaustiveSearch { + type Item = T; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} + +impl Extend for ExhaustiveSearch { + fn extend>(&mut self, iter: I) { + for value in iter { + self.push(value); + } + } +} + +impl, V> NearestNeighbors for ExhaustiveSearch { + fn search<'k, 'v, N>(&'v self, mut neighborhood: N) -> N + where + K: 'k, + V: 'v, + N: Neighborhood<&'k K, &'v V>, + { + for e in &self.0 { + neighborhood.consider(e); + } + neighborhood + } +} + +impl, V> ExactNeighbors for ExhaustiveSearch {} + +#[cfg(test)] +pub mod tests { + use super::*; + + use crate::tests::test_nearest_neighbors; + + #[test] + fn test_exhaustive_index() { + test_nearest_neighbors(ExhaustiveSearch::from_iter); + } +} -- cgit v1.2.3