1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! The [`Distance`] trait — one entry point per metric.
//!
//! Each metric in the crate ([`crate::Cosine`], [`crate::DotProduct`],
//! [`crate::Euclidean`], [`crate::Manhattan`], [`crate::Hamming`]) is a
//! zero-sized type that implements this trait. The associated functions
//! (`compute`, `compute_batch`) are dispatched at the type level — there is
//! no virtual call, no `dyn` indirection, and no allocation on the hot
//! path.
use Result;
/// Compute a distance between two `&[f32]` slices.
///
/// The associated functions take no receiver — every metric type in this
/// crate is zero-sized and is used as a tag, not a value. Mismatched or
/// empty inputs are returned as
/// [`iqdb_types::IqdbError`](iqdb_types::IqdbError) and the library never
/// panics.
///
/// # Examples
///
/// ```
/// use iqdb_distance::{Distance, Euclidean};
///
/// let a = [0.0_f32, 0.0, 0.0];
/// let b = [3.0_f32, 4.0, 0.0];
///
/// let d = Euclidean::compute(&a, &b).expect("non-empty, same length");
/// assert!((d - 5.0).abs() < 1e-6);
/// ```