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
// src/nearest/exact.rs
//! Exact nearest-neighbor ranking, under the `exact-nearest` feature.
//!
//! The certified bounds of the parent module return a candidate set that
//! holds the true k nearest, but they cannot always establish which of two
//! candidates is nearer. Two entries whose bound intervals overlap are both
//! returned, unranked. This module ranks them exactly. The bounds and the
//! search remain in `situs`, and the one exact operation, comparing the
//! distances from the query point to two points, is
//! `arithmetica::compare_distance`.
//!
//! # The method
//!
//! 1. **Candidates:** `C` is the candidate set of
//! [`Index::nearest_candidates`], unchanged.
//! 2. **Clamp:** For each candidate `e`, `q(e)` is the query point clamped to
//! `e`'s box. Each coordinate is the query's own when it lies inside the
//! box's interval on that axis, and otherwise the interval's nearer end,
//! chosen by exact comparisons of stored values. `q(e)` is therefore a
//! selection of input values rather than a computed one, and
//! `|p − q(e)|` is exactly the Euclidean distance from `p` to the box.
//! 3. **Sort**: `C` is sorted by `compare_distance(p, q(a), q(b))`, then by id,
//! then by internal position. `compare_distance` is exact for every
//! finite input, so the first key is a total preorder by true distance,
//! which the two tie-breaks refine into a total order.
//! 4. **Cut:** The first `k` are kept, then every following candidate whose
//! distance equals the k-th's exactly.
//!
//! # Why the result is the exact answer over all entries
//!
//! Let `d²ₖ` be the true k-th smallest squared distance over every stored
//! entry. The candidate set contains every entry with `d² ≤ d²ₖ`, which is
//! the true k nearest and every entry exactly tied with the k-th (the
//! argument is on [`Index::nearest_candidates`]). An entry outside the set
//! has `lower_sq > τ ≥ d²ₖ`, so its true distance is strictly greater than
//! the k-th's, and it can be neither among the k nearest nor tied with the
//! k-th. Sorting the set exactly therefore orders every entry that can
//! appear in the answer, and the cut at the k-th, with its exact ties,
//! recovers the exact answer.
//!
//! # Why the comparator cannot fail
//!
//! `compare_distance` returns an error only for a non-finite coordinate.
//! The query point is checked before the search starts, and every stored
//! box is finite (a [`Rect`] cannot be built otherwise), so every clamped
//! point is finite too. The comparator therefore treats an error as
//! unreachable. A `debug_assert!` states it, and a release build, which
//! cannot reach it, orders the pair as equal rather than panicking.
use Vec;
use Ordering;
use ;
use crateRect;
use crateDynamicIndex;
use crate;
use crateIndex;
// ===========================================================================
// The ranking, shared by both trees
// ===========================================================================
/// The point of `rect` nearest to `(x, y)`, the query clamped to the box.
///
/// Exact comparisons select stored values, and nothing is computed.
/// The exact comparison of `a`'s and `b`'s distances from `p`, `Less` when
/// `a` is nearer.
/// `nearest_exact` over either tree (module doc).
// ===========================================================================
// The public methods
// ===========================================================================