Struct space::LinearKnn[][src]

pub struct LinearKnn<I>(pub I);
Expand description

Performs a linear knn search by iterating over everything in the space and performing a binary search on running set of neighbors.

Example

use space::{Knn, LinearKnn, MetricPoint, Neighbor};

#[derive(PartialEq)]
struct Hamming(u8);

impl MetricPoint for Hamming {
    type Metric = u8;

    fn distance(&self, other: &Self) -> Self::Metric {
        (self.0 ^ other.0).count_ones() as u8
    }
}

let data = [
    Hamming(0b1010_1010),
    Hamming(0b1111_1111),
    Hamming(0b0000_0000),
    Hamming(0b1111_0000),
    Hamming(0b0000_1111),
];

let search = LinearKnn(data.iter());
assert_eq!(
    &search.knn(&Hamming(0b0101_0000), 3),
    &[
        Neighbor { index: 2, distance: 2 },
        Neighbor { index: 3, distance: 2 },
        Neighbor { index: 0, distance: 6 },
    ]
);

Trait Implementations

Get num nearest neighbors of target. Read more

Get the nearest neighbor of target. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Numeric cast from self to T.

Performs the conversion.

Numeric cast from T to Self.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.