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
#[doc(hidden)]
#[macro_export]
macro_rules! generate_within_unsorted_iter {
($comments:tt) => {
#[doc = concat!$comments]
#[inline]
pub fn within_unsorted_iter<D>(
&'a self,
query: &'query [A; K],
dist: A,
) -> WithinUnsortedIter<'a, A, T>
where
D: DistanceMetric<A, K>,
{
self.within_unsorted_iter_exclusive::<D>(query, dist, true)
}
#[doc = concat!$comments]
#[inline]
pub fn within_unsorted_iter_exclusive<D>(
&'a self,
query: &'query [A; K],
dist: A,
inclusive: bool,
) -> WithinUnsortedIter<'a, A, T>
where
D: DistanceMetric<A, K>,
{
// Like [`within_unsorted_iter`] but allows controlling boundary inclusiveness.
//
// When `inclusive` is true, points at exactly the maximum distance are included.
// When false, only points strictly less than the maximum distance are included.
let mut off = [A::zero(); K];
let root_index: IDX = *transform(&self.root_index);
let query = query.clone();
let gen = Gn::new_scoped(move |gen_scope| {
let query_ref = &query;
unsafe {
self.within_unsorted_iter_recurse::<D>(
query_ref,
dist,
root_index,
0,
gen_scope,
&mut off,
A::zero(),
inclusive,
);
}
done!();
});
WithinUnsortedIter::new(gen)
}
#[allow(clippy::too_many_arguments)]
unsafe fn within_unsorted_iter_recurse<'scope, D>(
&'a self,
query: &[A; K],
radius: A,
curr_node_idx: IDX,
split_dim: usize,
mut gen_scope: Scope<'scope, 'a, (), NearestNeighbour<A, T>>,
off: &mut [A; K],
rd: A,
inclusive: bool,
) -> Scope<'scope, 'a, (), NearestNeighbour<A, T>>
where
D: DistanceMetric<A, K>,
{
if is_stem_index(curr_node_idx) {
let node = self.stems.get_unchecked(curr_node_idx.az::<usize>());
let split_val: A = *transform(&node.split_val);
let node_left: IDX = *transform(&node.left);
let node_right: IDX = *transform(&node.right);
let mut rd = rd;
let old_off = off[split_dim];
let new_off = query[split_dim].saturating_dist(split_val);
let [closer_node_idx, further_node_idx] =
if *query.get_unchecked(split_dim) < split_val {
[node_left, node_right]
} else {
[node_right, node_left]
};
let next_split_dim = (split_dim + 1).rem(K);
gen_scope = self.within_unsorted_iter_recurse::<D>(
query,
radius,
closer_node_idx,
next_split_dim,
gen_scope,
off,
rd,
inclusive,
);
rd = D::accumulate(rd, D::dist1(new_off, old_off));
if if inclusive { rd <= radius } else { rd < radius } {
off[split_dim] = new_off;
gen_scope = self.within_unsorted_iter_recurse::<D>(
query,
radius,
further_node_idx,
next_split_dim,
gen_scope,
off,
rd,
inclusive,
);
off[split_dim] = old_off;
}
} else {
let leaf_node = self
.leaves
.get_unchecked((curr_node_idx - IDX::leaf_offset()).az::<usize>());
let size: IDX = *transform(&leaf_node.size);
leaf_node
.content_points
.iter()
.enumerate()
.take(size.az::<usize>())
.for_each(|(idx, entry)| {
let distance = D::dist(query, transform(entry));
if if inclusive { distance <= radius } else { distance < radius } {
let item = unsafe { leaf_node.content_items.get_unchecked(idx) };
let item = *transform(item);
gen_scope.yield_with(NearestNeighbour {
distance,
item,
});
}
});
}
gen_scope
}
};
}