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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
use crate::{distance::*, Node, Tree, utils::{FnntwResult, check_point}};
use ordered_float::NotNan;
impl<'t, const D: usize> Tree<'t, D> {
pub fn query_nearest(
&'t self,
query: &[f64; D]
) -> FnntwResult<(f64, u64, &'t [NotNan<f64>; D])> {
let query: &[NotNan<f64>; D] = check_point(query)?;
if let Some(ref boxsize) = self.boxsize {
Ok(self.query_nearest_periodic(query, boxsize))
} else {
Ok(self.query_nearest_nonperiodic(query))
}
}
fn query_nearest_nonperiodic(
&'t self,
query: &[NotNan<f64>; D]
) -> (f64, u64, &'t [NotNan<f64>; D]) {
let current_node: &Node<'t, D> = &self.root_node;
let mut points_to_check: Vec<(&usize, &[NotNan<f64>; D], f64)> =
Vec::with_capacity(self.height_hint);
let mut current_best_dist_sq = std::f64::MAX;
let mut current_best_neighbor: &'t [NotNan<f64>; D] = current_node
.stem_position();
self.check_stem(
query,
current_node,
&mut current_best_dist_sq,
&mut current_best_neighbor,
&mut points_to_check,
);
let best_idx = self.data_index[current_best_neighbor];
(current_best_dist_sq, best_idx, current_best_neighbor)
}
fn query_nearest_periodic(
&'t self,
query: &[NotNan<f64>; D],
boxsize: &[NotNan<f64>; D],
) -> (f64, u64, &'t [NotNan<f64>; D]) {
let (mut best_dist2, mut best_idx, mut best_nn) = self.query_nearest_nonperiodic(query);
let mut closest_side_dist2 = [0.0_f64; D];
for side in 0..D {
let query_component = unsafe { query.get_unchecked(side) };
let upper = unsafe { boxsize.get_unchecked(side) } - query_component;
debug_assert!(!upper.is_sign_negative());
debug_assert!(!query_component.is_sign_negative());
closest_side_dist2[side] = upper.min(*query_component).powi(2);
}
let mut images_to_check = Vec::with_capacity(2_usize.pow(D as u32)-1);
for image in 1..2_usize.pow(D as u32) {
let closest_image = (0..D)
.map(|idx| ((image / 2_usize.pow(idx as u32)) % 2) == 1);
let dist_to_side_edge_or_other: f64 = closest_image
.clone()
.enumerate()
.flat_map(|(side, flag)| if flag {
Some( unsafe { closest_side_dist2.get_unchecked(side) } )
} else { None })
.fold(0.0, |acc, x| acc + x);
if dist_to_side_edge_or_other < best_dist2 {
let mut image_to_check = query.clone();
for (idx, flag) in closest_image.enumerate() {
if flag {
let query_component: &NotNan<f64> = unsafe { query.get_unchecked(idx) };
let boxsize_component = unsafe { boxsize.get_unchecked(idx) };
unsafe {
if *query_component < boxsize_component / 2.0 {
*image_to_check.get_unchecked_mut(idx) = query_component + boxsize_component
} else {
*image_to_check.get_unchecked_mut(idx) = query_component - boxsize_component
}
}
}
}
images_to_check.push(image_to_check);
}
}
for image in &images_to_check {
let (image_best_dist2, image_best_idx, image_nn) = self.query_nearest_nonperiodic(
unsafe {
std::mem::transmute(image)
}
);
if image_best_dist2 < best_dist2 {
best_dist2 = image_best_dist2;
best_idx = image_best_idx;
best_nn = image_nn;
}
}
(best_dist2, best_idx, best_nn)
}
#[inline(always)]
fn check_child<'i, 'o>(
&'i self,
query: &[NotNan<f64>; D],
sibling: &usize,
stem_position: &'i [NotNan<f64>; D],
current_best_dist_sq: &'o mut f64,
current_best_neighbor: &'o mut &'i [NotNan<f64>; D],
points_to_check: &'o mut Vec<(&'i usize, &'i [NotNan<f64>; D], f64)>,
) where
'i: 'o,
't: 'i,
{
let sibling = unsafe { self.nodes.get_unchecked(*sibling) };
match sibling {
Node::Leaf { points, .. } => {
self.check_parent(
query,
stem_position,
current_best_dist_sq,
current_best_neighbor,
);
self.check_leaf(query, points, current_best_dist_sq, current_best_neighbor)
}
Node::Stem { .. } => {
self.check_parent(
query,
stem_position,
current_best_dist_sq,
current_best_neighbor,
);
self.check_stem(
query,
sibling,
current_best_dist_sq,
current_best_neighbor,
points_to_check,
)
}
}
}
#[inline(always)]
fn check_leaf<'a, 'b>(
&self,
query: &'b [NotNan<f64>; D],
leaf_points: impl IntoIterator<Item = &'b &'a [NotNan<f64>; D]>,
current_best_dist_sq: &mut f64,
current_best_neighbor: &mut &'a [NotNan<f64>; D],
) where
'a: 'b,
{
for candidate in leaf_points.into_iter() {
new_best(
query,
candidate,
current_best_dist_sq,
current_best_neighbor,
);
}
}
#[inline(always)]
fn check_stem<'i, 'o>(
&'i self,
query: &[NotNan<f64>; D],
stem: &'i Node<D>,
current_best_dist_sq: &'o mut f64,
current_best_neighbor: &'o mut &'i [NotNan<f64>; D],
points_to_check: &'o mut Vec<(&'i usize, &'i [NotNan<f64>; D], f64)>,
) where
'i: 'o,
't: 'i,
{
let mut current_node = stem;
while current_node.is_stem() {
let next_leafnode = match current_node {
Node::Stem {
ref split_dim,
point,
left,
ref right,
..
} => {
if unsafe { query.get_unchecked(*split_dim) > point.get_unchecked(*split_dim) }
{
let (sibling_lower, sibling_upper) =
unsafe { self.nodes.get_unchecked(*left) }.get_bounds();
let dist_sq_to_space =
calc_dist_sq_to_space(query, sibling_lower, sibling_upper);
points_to_check.push((left, point, dist_sq_to_space));
right
} else {
let (sibling_lower, sibling_upper) =
unsafe { self.nodes.get_unchecked(*right) }.get_bounds();
let dist_sq_to_space =
calc_dist_sq_to_space(query, sibling_lower, sibling_upper);
points_to_check.push((right, point, dist_sq_to_space));
left
}
}
_ => unreachable!("we are traversing though stems"),
};
current_node = unsafe { self.nodes.get_unchecked(*next_leafnode) };
}
self.check_leaf(
query,
current_node.iter(),
current_best_dist_sq,
current_best_neighbor,
);
while let Some((sibling, parent, dist_sq_to_space)) = points_to_check.pop() {
if dist_sq_to_space < *current_best_dist_sq {
self.check_child(
query,
sibling,
parent,
current_best_dist_sq,
current_best_neighbor,
points_to_check,
);
}
}
}
#[inline(always)]
fn check_parent<'i, 'o>(
&self,
query: &[NotNan<f64>; D],
stem_position: &'i [NotNan<f64>; D],
current_best_dist_sq: &'o mut f64,
current_best_neighbor: &'o mut &'i [NotNan<f64>; D],
) where
'i: 'o,
't: 'i,
{
new_best(
query,
stem_position,
current_best_dist_sq,
current_best_neighbor,
);
}
}