add lsh hashing support

This commit is contained in:
2026-03-18 19:18:48 +01:00
parent 3c0111f2cb
commit 8644adfb07
3 changed files with 746 additions and 0 deletions

View File

@@ -217,6 +217,78 @@ let keys = adaptive_probe(
);
```
## LSH Index
For efficient similarity search using Locality-Sensitive Hashing:
```rust
use redoal::{Point, normalize, resample, spectral_signature, lsh::LSHIndex};
// Create LSH index with default parameters (4 tables, 8 dimensions, 12 projections)
let mut index = LSHIndex::new(4, 8, 12);
// Process gesture and extract spectral embedding
let gesture = vec![
Point::new(0.0, 0.0),
Point::new(1.0, 0.0),
Point::new(0.5, 1.0),
];
let norm = normalize(&gesture);
let resamp = resample(&norm, 64);
let spectral: Vec<f32> = spectral_signature(&resamp, 8)
.iter()
.map(|x| *x as f32)
.collect();
// Insert gesture into index
index.insert(spectral);
// Query for similar gestures
let query = vec![
Point::new(0.1, 0.1),
Point::new(1.1, 0.1),
Point::new(0.6, 1.1),
];
let norm_query = normalize(&query);
let resamp_query = resample(&norm_query, 64);
let spectral_query: Vec<f32> = spectral_signature(&resamp_query, 8)
.iter()
.map(|x| *x as f32)
.collect();
let results = index.query_knn(&spectral_query, 3);
for result in results {
println!("Found similar gesture: {:?}", result);
}
```
### LSH Features
- **Deterministic Initialization**: Fixed seeds for reproducible results
- **Multi-table Indexing**: 4 tables by default for improved recall
- **Projection-based Hashing**: ≤64 projections (u64 bit limit)
- **L2 Distance Ranking**: Always rank results by distance
- **Multi-probe Search**: Query neighboring buckets for better results
- **Vector Normalization**: Built-in normalization support
### LSH Usage Example with Multi-probe
```rust
let mut index = LSHIndex::new(4, 8, 12);
// Insert gestures
let gesture1 = vec![1.0, 0.5, 0.3, 0.2, 0.1, 0.05, 0.03, 0.02];
let gesture2 = vec![1.1, 0.4, 0.35, 0.15, 0.12, 0.04, 0.02, 0.01];
index.insert(gesture1);
index.insert(gesture2);
// Query with multi-probe (checks neighboring buckets)
let query = vec![1.05, 0.45, 0.32, 0.18, 0.11, 0.045, 0.025, 0.015];
let results = index.query_knn_multi_probe(&query, 3, 2); // k=3, probe 2 bits
```
## HNSW Index
For fast local similarity search on peers: