Flat KNN
Simple and efficient library for k-nearest neighbors search in Rust.
Library takes advantages of the SIMD capabilities of modern CPUs to perform fast k-NN search. It features a generic API that supports multiple data layouts, numeric types (f32, f16), and distance metrics without any runtime overhead.
Installation
Add the following to your Cargo.toml. Currently, the library can be installed directly from the GitHub repository:
[]
= { = "https://github.com/simonplhak/flat-knn" }
Usage
use ;
use f16;
Extensibility
The flat-knn crate relies strictly on static trait bounds, avoiding slow dynamic dispatch or abstraction overhead.
Extending Distance Metrics
You can swap L2 or Dot with your own distance calculations by implementing the DistanceMetric<T> trait on a custom struct.
This trait requires you to define how a calculation builds standard (distance, index) metadata into a sorting item for the generic max-heap/min-heap structure:
use DistanceMetric;
use OrderedFloat;
;
Extending Data Types
By default, the crate implements the Indexable<T> trait for 1D vectors partitioned by stride (Vec<T>, usize), nested Vec<Vec<T>>, and flat slices &[T].
If you have a custom continuous memory structure like ndarray or matrices, you can easily provide a custom target struct implementing the Indexable<T> trait, defining get, len, and is_empty.