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
mod candidates;
mod discrete_distance;
mod discrete_hnsw;
mod hnsw;
pub use self::hnsw::*;
pub(crate) use candidates::*;
pub use discrete_distance::*;
pub use discrete_hnsw::*;
pub use generic_array::{typenum, ArrayLength, GenericArray};
pub trait FloatingDistance {
fn floating_distance(lhs: &Self, rhs: &Self) -> f32;
}
#[derive(Copy, Clone, Debug)]
pub struct Params {
ef_construction: usize,
}
impl Params {
pub fn new() -> Self {
Default::default()
}
pub fn ef_construction(mut self, ef_construction: usize) -> Self {
self.ef_construction = ef_construction;
self
}
}
impl Default for Params {
fn default() -> Self {
Self {
ef_construction: 400,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Euclidean<T>(pub T);
impl FloatingDistance for Euclidean<&[f32]> {
fn floating_distance(&Euclidean(lhs): &Self, &Euclidean(rhs): &Self) -> f32 {
assert_eq!(lhs.len(), rhs.len());
lhs.iter()
.zip(rhs)
.map(|(a, b)| (a - b).powi(2))
.sum::<f32>()
}
}