All Projects → kornelski → vpsearch

kornelski / vpsearch

Licence: other
C library for finding nearest (most similar) element in a set

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to vpsearch

version-compare
↔️ Rust library to easily compare version strings. Mirror from https://gitlab.com/timvisee/version-compare
Stars: ✭ 32 (+18.52%)
Mutual labels:  rust-library
font8x8-rs
8x8 monochrome bitmap fonts for rendering. Implemented in Rust.
Stars: ✭ 15 (-44.44%)
Mutual labels:  rust-library
telnet-rs
A simple implementation of Telnet in Rust.
Stars: ✭ 35 (+29.63%)
Mutual labels:  rust-library
ArmorLib
Easily scan files for threats to security and privacy. A Rust library and command line tool. WIP.
Stars: ✭ 20 (-25.93%)
Mutual labels:  rust-library
thread-priority
A simple thread schedule and priority library for rust
Stars: ✭ 48 (+77.78%)
Mutual labels:  rust-library
enumset
A library for compact bit sets containing enums.
Stars: ✭ 60 (+122.22%)
Mutual labels:  rust-library
inline-c-rs
Write and execute C code inside Rust.
Stars: ✭ 121 (+348.15%)
Mutual labels:  rust-library
rust-rgb
struct RGB for sharing pixels between crates
Stars: ✭ 70 (+159.26%)
Mutual labels:  rust-library
requests-rs
Rust HTTP client library styled after awesome Python requests
Stars: ✭ 37 (+37.04%)
Mutual labels:  rust-library
rust-pkcs11
Rust PKCS#11 Library
Stars: ✭ 70 (+159.26%)
Mutual labels:  rust-library
Batch-First
A JIT compiled chess engine which traverses the search tree in batches in a best-first manner, allowing for neural network batching, asynchronous GPU use, and vectorized CPU computations.
Stars: ✭ 27 (+0%)
Mutual labels:  tree-search
unicode-linebreak
󠁼💔 Implementation of the Unicode Line Breaking Algorithm in Rust
Stars: ✭ 14 (-48.15%)
Mutual labels:  rust-library
simple redis
Simple and resilient redis client for rust.
Stars: ✭ 21 (-22.22%)
Mutual labels:  rust-library
daemonize-me
Rust library to ease the task of creating daemons
Stars: ✭ 34 (+25.93%)
Mutual labels:  rust-library
blinkt
A Rust library for the Pimoroni Blinkt!, and any similar APA102 or SK9822 LED strips or boards, on a Raspberry Pi.
Stars: ✭ 18 (-33.33%)
Mutual labels:  rust-library
kul
A unique textual notation that can be used as both a data format and a markup language and that has powerful extensibility of both lexical syntax and semantics, and a Rust library for parsing it.
Stars: ✭ 12 (-55.56%)
Mutual labels:  rust-library
soap-rs
SOAP client for Rust programming language
Stars: ✭ 37 (+37.04%)
Mutual labels:  rust-library
rust-lp-modeler
Lp modeler written in Rust
Stars: ✭ 75 (+177.78%)
Mutual labels:  rust-library
harsh
Hashids implementation in Rust
Stars: ✭ 48 (+77.78%)
Mutual labels:  rust-library
Curio
A Blazing Fast HTTP Client
Stars: ✭ 35 (+29.63%)
Mutual labels:  rust-library

VP-tree nearest neighbor search

A relatively simple and readable Rust implementation of Vantage Point tree search algorithm.

The VP tree algorithm doesn't need to know coordinates of items, only distances between them. It can efficiently search multi-dimensional spaces and abstract things as long as you can define similarity between them (e.g. points, colors, and even images).

Please see the API reference or examples for details.

This algorithm does not work with squared distances. When implementing Euclidean distance, you MUST use sqrt(). Vantage Point trees require metric spaces.

#[derive(Copy, Clone)]
struct Point {
    x: f32, y: f32,
}

/// `MetricSpace` makes items comparable. It's a bit like Rust's `PartialOrd`.
impl vpsearch::MetricSpace for Point {
    type UserData = ();
    type Distance = f32;

    fn distance(&self, other: &Self, _: &Self::UserData) -> Self::Distance {
        let dx = self.x - other.x;
        let dy = self.y - other.y;

        // You MUST use sqrt here! The algorithm will give wrong results for squared distances.
        (dx*dx + dy*dy).sqrt()
    }
}

fn main() {
    let points = vec![Point{x:2.0,y:3.0}, Point{x:0.0,y:1.0}, Point{x:4.0,y:5.0}];

    let vp = vpsearch::Tree::new(&points);
    let (index, _) = vp.find_nearest(&Point{x:1.0,y:2.0});

    println!("The nearest point is at ({}, {})", points[index].x, points[index].y);
}

Implementing MetricSpace for Rust built-in types

This library includes a workaround for orphan rules. You need to add your crate's type when implementing MetricSpace:

struct MyImpl; // it can be any type, as long as it's yours
impl vpsearch::MetricSpace<MyImpl> for Vec<u8> {
    // continue as usual
}

Memory efficiency tip

Tree clones all the items and puts them in the tree. If the items are too big to clone and you'd rather keep the items elsewhere, you can!

Instead of storing the items, make the tree store indices into your items storage, and pass actual items as user_data to your MetricSpace::distance() function.

let items = /* your actual items are here */;
let indices: Vec<_> = (0...items.len() as u32).collect();
let tree = Tree::new_with_user_data_ref(&items, &indices);
let res = tree.find_nearest(&needle, &items);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].