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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::collections::HashSet;
use std::hash::Hash;
use std::fmt::Debug;
use rand::Rng;
use rayon::prelude::{IntoParallelRefMutIterator, ParallelIterator, IntoParallelRefIterator, IntoParallelIterator};
use crate::hyperindex::HyperIndex;
pub struct DistanceNode<K: Eq+Hash> {
pub key: K,
pub distance: f32
}
impl<K:Eq+Hash> Eq for DistanceNode<K>
{
}
impl<K:Eq+Hash> PartialEq for DistanceNode<K> {
fn eq(&self, other: &Self) -> bool {
self.key == other.key
}
}
impl<K:Eq+Hash> Hash for DistanceNode<K> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.key.hash(state);
}
}
pub struct MultiIndex<K:Send+Sync> {
indices: Vec<HyperIndex<K>>
}
impl<K:Clone+Eq+Hash+Debug+Send+Sync> MultiIndex<K> {
pub fn new<R : Rng + Sized>(dimension: usize, index_count: u8, hyperplane_count: u8, mut rng: &mut R) -> MultiIndex<K> {
MultiIndex {
indices: (0..index_count).map(|_| HyperIndex::new(dimension, hyperplane_count, &mut rng)).collect()
}
}
pub fn nearest<F>(&self, point: &Vec<f32>, count: usize, get_dist: F) -> Vec<DistanceNode<K>>
where F : Fn(&Vec<f32>, &K) -> f32 + Send + Sync
{
let mut results: Vec<_> = self.indices.par_iter()
.flat_map(|i| i.group(&i.key(&point)))
.flat_map(|a| a)
.map(|a| a.clone())
.collect::<HashSet<K>>()
.into_par_iter()
.map(|a| DistanceNode { distance: get_dist(point, &a), key: a })
.collect::<Vec<_>>();
results.sort_by(|a, b| a.distance.partial_cmp(&b.distance).unwrap_or(std::cmp::Ordering::Equal));
results.truncate(count);
return results;
}
pub fn add(&mut self, key: K, vector: &Vec<f32>)
{
let mut work:Vec<_> = self.indices.iter_mut()
.map(|idx| (idx, key.clone(), vector))
.collect();
work.par_iter_mut().for_each(|work| {
work.0.add(work.1.clone(), work.2);
});
}
pub fn dimensions(&self) -> usize {
self.indices[0].dimensions()
}
pub fn planes_len(&self) -> usize {
self.indices[0].planes_len()
}
pub fn indices_len(&self) -> usize {
self.indices.len()
}
}
#[cfg(test)]
mod tests
{
use rand::prelude::*;
use std::collections::HashSet;
extern crate time;
use time::Instant;
use crate::multiindex::MultiIndex;
use crate::vector::{ random_unit_vector, euclidean_distance };
#[test]
fn new_creates_index() {
let a = MultiIndex::<usize>::new(300, 15, 10, &mut thread_rng());
assert_eq!(300, a.dimensions());
assert_eq!(10, a.planes_len());
assert_eq!(15, a.indices_len());
}
#[test]
fn multiindex_compare() {
let mut a = MultiIndex::new(300, 30, 5, &mut thread_rng());
let mut vectors = Vec::new();
let mut rng = thread_rng();
for key in 0..10000usize {
let v = random_unit_vector(300, &mut rng);
a.add(key, &v);
vectors.push((key, v));
}
let start_linear = Instant::now();
println!();
println!("Linear results:");
let query_point = vectors[0].clone();
let mut nearest_linear: Vec<(f32, &(usize, Vec<f32>))> = vectors.iter().map(|item| (euclidean_distance(&item.1, &query_point.1), item)).collect();
nearest_linear.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
let end_linear = Instant::now();
println!("{:?} seconds for linear", end_linear - start_linear);
for i in 0..20 {
println!("idx:{:?}\t\tdist:{:?}", (nearest_linear[i].1).0, nearest_linear[i].0);
}
let start_indexed = Instant::now();
println!();
println!("Index results:");
let near = a.nearest(&query_point.1, 100, |p, k| {
euclidean_distance(p, &vectors[*k].1)
});
let end_indexed = Instant::now();
println!("{:?} seconds for index", end_indexed - start_indexed);
for i in 0.. near.len().min(20) {
println!("idx:{:?}\t\tdist:{:?}", near[i].key, near[i].distance);
}
let linear_set: HashSet<_> = nearest_linear.iter().map(|a| (a.1).0).take(20).collect();
let near_set: HashSet<_> = near.iter().map(|a| a.key).take(20).collect();
let overlap: Vec<_> = linear_set.intersection(&near_set).collect();
println!();
println!("Overlap:{:?}/20", overlap.len())
}
}