use crate::distance::Proximity;
use crate::knn::{ExactNeighbors, NearestNeighbors, Neighborhood};
use alloc::vec::Vec;
#[derive(Clone, Debug)]
pub struct ExhaustiveSearch<T>(Vec<T>);
impl<T> ExhaustiveSearch<T> {
pub fn new() -> Self {
Self(Vec::new())
}
pub fn iter(&self) -> Iter<'_, T> {
self.into_iter()
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn push(&mut self, item: T) {
self.0.push(item);
}
}
impl<T> Default for ExhaustiveSearch<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> FromIterator<T> for ExhaustiveSearch<T> {
fn from_iter<I: IntoIterator<Item = T>>(items: I) -> Self {
Self(items.into_iter().collect())
}
}
#[derive(Debug)]
pub struct IntoIter<T>(alloc::vec::IntoIter<T>);
impl<T> Iterator for IntoIter<T> {
type Item = T;
fn next(&mut self) -> Option<T> {
self.0.next()
}
}
impl<T> IntoIterator for ExhaustiveSearch<T> {
type Item = T;
type IntoIter = IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
IntoIter(self.0.into_iter())
}
}
#[derive(Debug)]
pub struct Iter<'a, T>(core::slice::Iter<'a, T>);
impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<&'a T> {
self.0.next()
}
}
impl<'a, T> IntoIterator for &'a ExhaustiveSearch<T> {
type Item = &'a T;
type IntoIter = Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
Iter(self.0.iter())
}
}
impl<T> Extend<T> for ExhaustiveSearch<T> {
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
for value in iter {
self.push(value);
}
}
}
impl<K: Proximity<V>, V> NearestNeighbors<K, V> for ExhaustiveSearch<V> {
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<K: Proximity<V>, V> ExactNeighbors<K, V> for ExhaustiveSearch<V> {}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::knn::tests::test_exact_neighbors;
#[test]
fn test_exhaustive_index() {
test_exact_neighbors(ExhaustiveSearch::from_iter);
}
}