use std::borrow::Borrow;
use std::cmp::Ordering;
use std::collections::BinaryHeap;
use crate::document::{Document, Metric};
use crate::kernels;
#[derive(Debug, Clone, PartialEq)]
pub struct Hit {
pub id: String,
pub distance: f32,
pub attrs: Option<serde_json::Value>,
}
pub fn distance(metric: Metric, a: &[f32], b: &[f32]) -> f32 {
let fast = match metric {
Metric::L2 => kernels::l2(a, b),
Metric::Dot => -kernels::dot(a, b),
Metric::Cosine => {
let (na, nb) = (kernels::norm2(a), kernels::norm2(b));
let trusted = |n: f32| n.is_finite() && n >= kernels::NORM_FLOOR;
if !(trusted(na) && trusted(nb)) {
return reference(metric, a, b);
}
let cosine = 1.0 - kernels::dot(a, b) / (na.sqrt() * nb.sqrt());
if cosine < kernels::COSINE_RECHECK {
return reference(metric, a, b);
}
cosine
}
};
if fast.is_finite() {
fast
} else {
reference(metric, a, b)
}
}
fn reference(metric: Metric, a: &[f32], b: &[f32]) -> f32 {
match metric {
Metric::L2 => a
.iter()
.zip(b)
.map(|(x, y)| {
let d = f64::from(*x) - f64::from(*y);
d * d
})
.sum::<f64>() as f32,
Metric::Dot => -dot_f64(a, b) as f32,
Metric::Cosine => {
let na = dot_f64(a, a);
let nb = dot_f64(b, b);
if na == 0.0 || nb == 0.0 {
return 1.0;
}
(1.0 - dot_f64(a, b) / (na.sqrt() * nb.sqrt())) as f32
}
}
}
fn dot_f64(a: &[f32], b: &[f32]) -> f64 {
a.iter()
.zip(b)
.map(|(x, y)| f64::from(*x) * f64::from(*y))
.sum()
}
struct Candidate<D> {
distance: f32,
doc: D,
}
impl<D: Borrow<Document>> PartialEq for Candidate<D> {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl<D: Borrow<Document>> Eq for Candidate<D> {}
impl<D: Borrow<Document>> PartialOrd for Candidate<D> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<D: Borrow<Document>> Ord for Candidate<D> {
fn cmp(&self, other: &Self) -> Ordering {
self.distance
.total_cmp(&other.distance)
.then_with(|| self.doc.borrow().id.cmp(&other.doc.borrow().id))
}
}
pub fn flat_top_k(
metric: Metric,
query: &[f32],
docs: impl IntoIterator<Item = Document>,
k: usize,
) -> Vec<Hit> {
let candidates = docs.into_iter().map(|doc| Candidate {
distance: distance(metric, query, &doc.vector),
doc,
});
select(candidates, k)
.into_iter()
.map(|c| Hit {
id: c.doc.id,
distance: c.distance,
attrs: c.doc.attrs,
})
.collect()
}
fn select<D: Borrow<Document>>(
candidates: impl Iterator<Item = Candidate<D>>,
k: usize,
) -> Vec<Candidate<D>> {
if k == 0 {
return Vec::new();
}
let mut heap: BinaryHeap<Candidate<D>> = BinaryHeap::with_capacity(k.min(1024) + 1);
for candidate in candidates {
if heap.len() < k {
heap.push(candidate);
} else if heap.peek().is_some_and(|worst| candidate < *worst) {
heap.pop();
heap.push(candidate);
}
}
heap.into_sorted_vec()
}
#[cfg(test)]
mod tests {
use super::*;
fn doc(id: &str, v: &[f32]) -> Document {
Document {
id: id.into(),
vector: v.to_vec(),
attrs: None,
sparse: None,
}
}
#[test]
fn l2_is_squared_euclidean() {
assert_eq!(distance(Metric::L2, &[0.0, 0.0], &[3.0, 4.0]), 25.0);
}
#[test]
fn dot_is_negated() {
assert_eq!(distance(Metric::Dot, &[1.0, 2.0], &[3.0, 4.0]), -11.0);
}
#[test]
fn cosine_identical_is_zero_orthogonal_is_one_opposite_is_two() {
assert!(distance(Metric::Cosine, &[1.0, 0.0], &[2.0, 0.0]).abs() < 1e-6);
assert!((distance(Metric::Cosine, &[1.0, 0.0], &[0.0, 5.0]) - 1.0).abs() < 1e-6);
assert!((distance(Metric::Cosine, &[1.0, 0.0], &[-1.0, 0.0]) - 2.0).abs() < 1e-6);
}
#[test]
fn cosine_with_zero_norm_stored_vector_is_one() {
assert_eq!(distance(Metric::Cosine, &[1.0, 0.0], &[0.0, 0.0]), 1.0);
}
#[test]
fn top_k_returns_closest_ascending() {
let docs = vec![
doc("far", &[10.0, 0.0]),
doc("near", &[1.0, 0.0]),
doc("mid", &[4.0, 0.0]),
];
let hits = flat_top_k(Metric::L2, &[0.0, 0.0], docs, 2);
let ids: Vec<_> = hits.iter().map(|h| h.id.as_str()).collect();
assert_eq!(ids, ["near", "mid"]);
assert!(hits[0].distance < hits[1].distance);
}
#[test]
fn ties_break_by_id() {
let docs = vec![doc("b", &[1.0]), doc("a", &[1.0]), doc("c", &[1.0])];
let hits = flat_top_k(Metric::L2, &[0.0], docs, 2);
let ids: Vec<_> = hits.iter().map(|h| h.id.as_str()).collect();
assert_eq!(ids, ["a", "b"]);
}
#[test]
fn k_larger_than_corpus_returns_all_and_zero_returns_none() {
let docs = vec![doc("a", &[1.0]), doc("b", &[2.0])];
assert_eq!(flat_top_k(Metric::L2, &[0.0], docs.clone(), 10).len(), 2);
assert!(flat_top_k(Metric::L2, &[0.0], docs, 0).is_empty());
}
fn reference_f64(metric: Metric, a: &[f32], b: &[f32]) -> f64 {
let dot = |x: &[f32], y: &[f32]| -> f64 {
x.iter()
.zip(y)
.map(|(p, q)| f64::from(*p) * f64::from(*q))
.sum()
};
match metric {
Metric::L2 => a
.iter()
.zip(b)
.map(|(x, y)| (f64::from(*x) - f64::from(*y)).powi(2))
.sum(),
Metric::Dot => -dot(a, b),
Metric::Cosine => {
let (na, nb) = (dot(a, a), dot(b, b));
if na == 0.0 || nb == 0.0 {
return 1.0;
}
1.0 - dot(a, b) / (na.sqrt() * nb.sqrt())
}
}
}
#[test]
fn tiny_vectors_keep_their_cosine() {
let a = [1e-25f32; 4];
assert!(distance(Metric::Cosine, &a, &a).abs() < 1e-6);
let b = [1e-25f32, -1e-25, 1e-25, -1e-25];
assert!((distance(Metric::Cosine, &a, &b) - 1.0).abs() < 1e-6);
}
#[test]
fn subnormal_scale_vectors_keep_their_cosine() {
for s in [1e-21f32, 1e-22, 3e-23, 1e-23] {
let (a, b) = ([3.0 * s, s], [s, 3.0 * s]);
let got = distance(Metric::Cosine, &a, &b);
assert!((got - 0.4).abs() < 1e-6, "s={s}: {got}");
}
}
#[test]
fn cosine_near_zero_keeps_its_resolution() {
let a: Vec<f32> = (0..128)
.map(|i| ((i * 7) % 13) as f32 / 13.0 + 0.1)
.collect();
let self_distance = distance(Metric::Cosine, &a, &a);
assert!(self_distance.abs() < 1e-12, "self distance {self_distance}");
let mut b = a.clone();
b[0] += 1e-4;
let near = distance(Metric::Cosine, &a, &b);
let want = reference_f64(Metric::Cosine, &a, &b);
assert!(near > 0.0, "a moved vector must be farther than itself");
assert!(
(f64::from(near) - want).abs() <= 1e-3 * want,
"{near} vs {want}"
);
}
#[test]
fn huge_components_give_the_reference_value() {
let (a, b) = ([1e20f32, 0.0], [1e20f32, 1e20]);
let want = reference_f64(Metric::Cosine, &a, &b) as f32;
assert!((distance(Metric::Cosine, &a, &b) - want).abs() < 1e-6);
assert!((want - 0.292_893_2).abs() < 1e-6);
let (a, b) = ([1e20f32, 1e20], [1e20f32, -1e20]);
assert_eq!(distance(Metric::Dot, &a, &b), 0.0);
}
#[test]
fn distance_matches_the_f64_reference_at_every_dimension() {
let mut seed = 9u64;
let mut next = || {
seed ^= seed << 13;
seed ^= seed >> 7;
seed ^= seed << 17;
(seed >> 40) as f32 / (1u32 << 23) as f32 - 1.0
};
for dim in [1, 3, 7, 8, 9, 25, 128, 960] {
let a: Vec<f32> = (0..dim).map(|_| next()).collect();
let b: Vec<f32> = (0..dim).map(|_| next()).collect();
for metric in [Metric::L2, Metric::Dot, Metric::Cosine] {
let want = reference_f64(metric, &a, &b);
let got = distance(metric, &a, &b);
assert!(
(f64::from(got) - want).abs() <= 1e-4 * (1.0 + want.abs()),
"{metric:?} at {dim}: {got} vs {want}"
);
}
}
}
#[test]
fn flat_top_k_agrees_with_a_full_sort() {
let docs: Vec<Document> = (0..200)
.map(|i| {
doc(
&format!("d{i:03}"),
&[((i * 37) % 101) as f32, (i % 7) as f32],
)
})
.collect();
let query = [50.0f32, 3.0];
let mut all: Vec<(f32, String)> = docs
.iter()
.map(|d| (distance(Metric::L2, &query, &d.vector), d.id.clone()))
.collect();
all.sort_by(|a, b| a.0.total_cmp(&b.0).then_with(|| a.1.cmp(&b.1)));
for k in [1, 5, 50, 200, 300] {
let want: Vec<&str> = all.iter().take(k).map(|(_, id)| id.as_str()).collect();
let flat = flat_top_k(Metric::L2, &query, docs.clone(), k);
let flat: Vec<&str> = flat.iter().map(|h| h.id.as_str()).collect();
assert_eq!(flat, want, "flat_top_k k={k}");
}
}
#[test]
fn attrs_are_carried_onto_hits() {
let mut d = doc("a", &[1.0]);
d.attrs = Some(serde_json::json!({"tag": 1}));
let hits = flat_top_k(Metric::L2, &[0.0], vec![d], 1);
assert_eq!(hits[0].attrs, Some(serde_json::json!({"tag": 1})));
}
}