#search #computer-vision #knn #ann

no-std space

A library providing abstractions for spatial datastructures and search

35 releases (17 breaking)

0.18.0 Aug 14, 2021
0.17.0 Jul 18, 2021
0.16.0 Jul 18, 2021
0.10.3 Mar 5, 2020
0.6.0 Nov 20, 2018

#350 in Data structures

Download history 2663/week @ 2023-12-14 1468/week @ 2023-12-21 1280/week @ 2023-12-28 3796/week @ 2024-01-04 3375/week @ 2024-01-11 3874/week @ 2024-01-18 2999/week @ 2024-01-25 3693/week @ 2024-02-01 3048/week @ 2024-02-08 2874/week @ 2024-02-15 3282/week @ 2024-02-22 4687/week @ 2024-02-29 4593/week @ 2024-03-07 4591/week @ 2024-03-14 3665/week @ 2024-03-21 3351/week @ 2024-03-28

17,247 downloads per month
Used in 11 crates (8 directly)

MIT license

18KB
155 lines

space

Discord Crates.io MIT/Apache docs.rs LoC Tests Lints no_std

A library providing abstractions for spatial datastructures and search

If you use a kNN datastructure library and would like to have the Knn trait implemented on its types natively, please raise an issue on that library. Similarly, crates which define datapoints with specific distance metrics, and not general linear algebra crates, can implement the MetricPoint trait.

See the bitarray crate for an implementation of MetricPoint using hamming distance (with optional, though unstable, 512-bit SIMD support, and always-on 64-bit popcnt instruction support).

Usage

use space::Metric;

struct Hamming;

impl Metric<u8> for Hamming {
    type Unit = u8;

    fn distance(&self, &a: &u8, &b: &u8) -> Self::Unit {
        (a ^ b).count_ones() as u8
    }
}
use space::{Knn, KnnFromBatch, LinearKnn, Metric, Neighbor};

#[derive(Default)]
struct Hamming;

impl Metric<u8> for Hamming {
    type Unit = u8;

    fn distance(&self, &a: &u8, &b: &u8) -> Self::Unit {
        (a ^ b).count_ones() as u8
    }
}

let data = vec![
    (0b1010_1010, 12),
    (0b1111_1111, 13),
    (0b0000_0000, 14),
    (0b1111_0000, 16),
    (0b0000_1111, 10),
];

let search: LinearKnn<Hamming, _> = KnnFromBatch::from_batch(data.iter());

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

Benchmarks

To run the benchmarks, use the following command:

cargo bench --all-features

If you do not pass --all-features, the benchmark wont run. Due to this issue, the SIMD feature must be enabled. Cargo offers no way to automatically bring the SIMD feature in for the benchmark, and thus it must be passed at the command line.

Dependencies

~105–340KB