MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / distance

Function distance

nodedb-vector/src/distance/mod.rs:17–38  ·  view source on GitHub ↗
(a: &[f32], b: &[f32], metric: DistanceMetric)

Source from the content-addressed store, hash-verified

15/// falls back to scalar implementations on other architectures.
16#[inline]
17pub fn distance(a: &[f32], b: &[f32], metric: DistanceMetric) -> f32 {
18 assert_eq!(
19 a.len(),
20 b.len(),
21 "distance: length mismatch (a.len()={}, b.len()={})",
22 a.len(),
23 b.len()
24 );
25 let rt = simd::runtime();
26 match metric {
27 DistanceMetric::L2 => (rt.l2_squared)(a, b),
28 DistanceMetric::Cosine => (rt.cosine_distance)(a, b),
29 DistanceMetric::InnerProduct => (rt.neg_inner_product)(a, b),
30 DistanceMetric::Manhattan => manhattan(a, b),
31 DistanceMetric::Chebyshev => chebyshev(a, b),
32 DistanceMetric::Hamming => hamming_f32(a, b),
33 DistanceMetric::Jaccard => jaccard(a, b),
34 DistanceMetric::Pearson => pearson(a, b),
35 // Unknown future metric — fall back to L2.
36 _ => (rt.l2_squared)(a, b),
37 }
38}
39
40/// Batch distance: compute distances from `query` to each candidate.
41///

Callers 15

batch_distancesFunction · 0.70
distance_typedFunction · 0.70
f32_refFunction · 0.70
batch_distancesFunction · 0.50
is_diverse_batchedFunction · 0.50
adaptive_searchFunction · 0.50
search_with_metricMethod · 0.50
searchMethod · 0.50
matryoshka_searchFunction · 0.50

Calls 6

manhattanFunction · 0.85
chebyshevFunction · 0.85
hamming_f32Function · 0.85
jaccardFunction · 0.85
pearsonFunction · 0.85
runtimeFunction · 0.50