pub mod biased_descent;
pub mod hnsw_index;
pub mod in_descent;
pub mod quantization;
pub mod quantized_hnsw;
pub mod simd;
pub use biased_descent::{apply_bias, BiasOptions, CentralityMap};
pub use in_descent::{search as in_descent_search, Adjacency, InDescentOptions, Positions};
pub use hnsw_index::{HnswIndex, HnswConfig, MultiMetricHnswIndex};
pub use quantization::{
ProductQuantizer, ProductQuantizerConfig, Codebook, QuantizedVector,
Encoder, Decoder, DistanceComputer, PqError, PqResult,
};
pub use quantized_hnsw::{QuantizedHnswIndex, QuantizedHnswConfig, MemoryStats};
use serde::{Serialize, Deserialize};
pub type Dimension = usize;
pub type Vector = Vec<f32>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DistanceMetric {
Cosine,
L2,
InnerProduct,
}
pub fn l2_distance(a: &[f32], b: &[f32]) -> f32 {
simd::l2_distance(a, b)
}
pub fn cosine_distance(a: &[f32], b: &[f32]) -> f32 {
simd::cosine_distance(a, b)
}
pub fn inner_product_distance(a: &[f32], b: &[f32]) -> f32 {
-simd::dot_product(a, b)
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn test_l2_distance() {
let a = vec![1.0, 0.0, 0.0];
let b = vec![0.0, 1.0, 0.0];
let dist = l2_distance(&a, &b);
assert!((dist - 1.414).abs() < 0.01);
}
#[test]
fn test_cosine_distance() {
let a = vec![1.0, 0.0];
let b = vec![0.0, 1.0];
let dist = cosine_distance(&a, &b);
assert!((dist - 1.0).abs() < 0.001); }
#[test]
fn test_inner_product() {
let a = vec![1.0, 2.0, 3.0];
let b = vec![4.0, 5.0, 6.0];
let dist = inner_product_distance(&a, &b);
assert_eq!(dist, -32.0); }
}