pub struct LshIndex {
pub vectors: Vec<Vec<f64>>,
pub buckets: Vec<LshBucket>,
pub hashers: Vec<LshHasher>,
pub dim: usize,
pub num_tables: usize,
pub num_hashes: usize,
}Expand description
Approximate nearest-neighbour index using Locality-Sensitive Hashing.
Fields§
§vectors: Vec<Vec<f64>>All inserted vectors (indexed by position in this Vec).
buckets: Vec<LshBucket>One bucket table per LSH table.
hashers: Vec<LshHasher>One hasher per LSH table.
dim: usizeDimensionality of the vectors.
num_tables: usizeNumber of hash tables.
num_hashes: usizeNumber of hash bits per table.
Implementations§
Source§impl LshIndex
impl LshIndex
Sourcepub fn new(dim: usize, num_tables: usize, num_hashes: usize, seed: u64) -> Self
pub fn new(dim: usize, num_tables: usize, num_hashes: usize, seed: u64) -> Self
Create a new LSH index.
dim— vector dimensionalitynum_tables— number of independent hash tables (more → higher recall)num_hashes— number of hash bits per table (more → higher precision)seed— XorShift-64 seed for reproducibility
Sourcepub fn insert(&mut self, id: usize, vector: &[f64])
pub fn insert(&mut self, id: usize, vector: &[f64])
Insert a vector with the given id into all hash tables.
Sourcepub fn cosine_similarity(a: &[f64], b: &[f64]) -> f64
pub fn cosine_similarity(a: &[f64], b: &[f64]) -> f64
Compute cosine similarity between two vectors.
Returns 0.0 if either vector has zero magnitude.
Sourcepub fn search(&self, query: &[f64], k: usize) -> Vec<(usize, f64)>
pub fn search(&self, query: &[f64], k: usize) -> Vec<(usize, f64)>
Search for the top-k approximate nearest neighbours of query.
Collects candidates from all tables, deduplicates them, computes cosine similarity, and returns the top-k sorted by descending similarity. If the query itself was indexed (same vector), it may appear in results.
Auto Trait Implementations§
impl Freeze for LshIndex
impl RefUnwindSafe for LshIndex
impl Send for LshIndex
impl Sync for LshIndex
impl Unpin for LshIndex
impl UnsafeUnpin for LshIndex
impl UnwindSafe for LshIndex
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.