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
//! # Kiddo
//!
//! A high-performance k-d tree library for exact and approximate nearest-neighbour
//! queries in low-dimensional spaces.
//!
//! Built with an aggressive focus on query performance, including cache-aware
//! layouts and optional SIMD-accelerated code paths. See the companion
//! benchmarking site to compare Kiddo against other k-d tree implementations
//! across a range of workloads.
//!
//! Kiddo v6 provides a single generic [`KdTree`](crate::kd_tree::KdTree) that supports floating-point
//! (`f64`, `f32`, `f16`), selected fixed-point (via the `fixed` crate), and
//! unsigned-integer (`u8`, `u16`, `u32`) types as coordinates, along with both mutable
//! and immutable usage patterns.
//!
//! Kiddo is designed for low-dimensional (< ~10D) search problems, especially 2D, 3D,
//! and 4D workloads. Typical use cases include point-cloud analysis,
//! astronomical catalogue crossmatching, colour quantization and palette
//! lookup, local neighbourhood queries in simulations, and other
//! nearest-neighbour and radius-search tasks. Kiddo has been used for diverse
//! geographical and scientific workloads including geocoding, astronomy,
//! cosmology, computer-aided drug discovery, crystallography, and
//! computational neuroscience.
//!
//! Kiddo supports the following query types:
//!
//! - **Exact Nearest Neighbour**: Useful for tasks like finding the nearest airport to a given
//! location, or finding the nearest catalogued star to a sky position.
//! `tree.query(&point).nearest_one().execute()`
//! - **k-nearest-neighbour (k-NN)** search, finding the `k` nearest items to a query point:
//! - ordered by distance: `tree.query(&point).nearest_n(5).execute()`.
//! Useful for finding the nearest weather stations or sensors to a location,
//! or generating candidate correspondences for point-cloud registration.
//! - `k` items within a max radius: `tree.query(&point).nearest_n(5).within(max_dist).execute()`.
//! Useful when you want the closest local neighbours inside a meaningful
//! cutoff, such as the nearest shops within 5 miles, or nearby atoms within
//! an interaction radius.
//! - All items within a radius: `tree.query(&point).within(max_dist).execute()`. Finds all
//! items within a specified radius of a query point, ordered by distance. Useful for radial
//! catalogue searches in astronomy, or collision and proximity queries where the full
//! neighbourhood is needed in sorted order.
//! - Unsorted, e.g. `tree.query(&point).nearest_n(5).within(max_dist).unsorted().execute()`:
//! This is often faster than the sorted radius-query form when result order does not
//! matter, such as finding all customers within 5 miles of a store, or
//! collecting point-cloud neighbourhoods for clustering or normal estimation.
//! - **Approximate nearest-neighbour** (ANN) search: `tree.query(&point).nearest_one().approx().execute()`
//! Returns a good approximate nearest item. Generally much faster than exact nearest-neighbour
//! search. Useful for latency-sensitive workloads like interactive point-cloud picking, or
//! mapping image pixels to a palette colour during colour quantization.
//! - **"best" `n` items**: `tree.query(&point).best_n(5, max_dist).execute()`. Finds the "best" n
//! items within a specified distance of a query point, for some definition of "best". For
//! example, "give me the 5 largest settlements within 50km of a given point, ordered by
//! descending population", or "the 5 brightest stars within a degree of a point on the sky,
//! ordered brightest first". This only makes semantic sense when your item type has meaningful
//! ordering; for points-only trees with `T = ()`, the query is allowed but not useful.
//! - **[Periodic Boundary Conditions](https://en.wikipedia.org/wiki/Periodic_boundary_conditions) (PBC)**,
//! whereby the points in the tree are considered to represent a single subunit that repeats
//! across space. Useful primarily for simulations, such as within cosmology or molecular dynamics
//! simulations:
//! `tree.query(&point).periodic_boundary_condition(box_size).within(max_dist).execute()`
//! - **Exclusive Boundary Queries**, where the query radius filter is an exclusive boundary
//! (< max_dist), rather than the default inclusive boundary (<= max_dist):
//! `tree.query(&point).within(max_dist).exclusive_boundaries().execute()`
//!
//! If your points are known up front and the tree will be built once and then
//! queried, start with [`ImmutableKdTree`]. It offers the best query
//! performance and pairs well with `rkyv` for zero-copy loading of prebuilt
//! trees from disk; when used with memory-mapped files, loading can be
//! effectively instant.
//!
//! If you need to add or remove points after construction, start with
//! [`MutableKdTree`]. Mutable trees remain a good fit for many dynamic
//! workloads, but they do not currently perform dynamic rebalancing, so
//! workloads with substantial growth or heavy churn may benefit from periodic
//! rebuilds.
//!
//! [`ImmutableKdTree`] and [`MutableKdTree`] are convenience aliases for
//! [`KdTree`](crate::kd_tree::KdTree) with sensible defaults for these common read-heavy and mutable
//! workloads.
//!
//! Kiddo is not intended as a library for high-dimensional vector search or
//! feature matching over hundreds or thousands of dimensions, where
//! k-d trees are usually the wrong data structure and other approaches are more
//! appropriate. The API does not impose a hard dimensional limit, but Kiddo is
//! primarily intended for low-dimensional workloads.
//!
//! ## Installation
//!
//! Add `kiddo` to `Cargo.toml`
//! ```toml
//! [dependencies]
//! kiddo = "6.0.0-alpha.1"
//! ```
//!
//! ## Usage
//! ```rust
//! use std::num::NonZero;
//!
//! use kiddo::leaf_strategies::VecOfArrays;
//! use kiddo::SquaredEuclidean;
//! use kiddo::QueryResultItem;
//! use kiddo::{Eytzinger, ImmutableKdTree};
//!
//! let entries = vec![
//! [0f64, 0f64],
//! [1f64, 1f64],
//! [2f64, 2f64],
//! [3f64, 3f64]
//! ];
//!
//! let kdtree = ImmutableKdTree::new_from_slice(&entries).unwrap();
//!
//! // How many items are in tree?
//! assert_eq!(kdtree.size(), 4);
//!
//! // find the nearest item to [0f64, 0f64].
//! let nearest = kdtree
//! .query(&[0f64, 0f64])
//! .nearest_one::<SquaredEuclidean<f64>>()
//! .execute();
//! assert_eq!(nearest.distance, 0f64);
//! assert_eq!(nearest.item, 0);
//!
//! // find the nearest 3 items to [0f64, 0f64], and collect into a `Vec`
//! assert_eq!(
//! kdtree
//! .query(&[0f64, 0f64])
//! .nearest_n::<SquaredEuclidean<f64>>(NonZero::new(3usize).unwrap())
//! .execute(),
//! vec![
//! QueryResultItem { point: (), distance: 0f64, item: 0 },
//! QueryResultItem { point: (), distance: 2f64, item: 1 },
//! QueryResultItem { point: (), distance: 8f64, item: 2 }
//! ]
//! );
//! ```
//!
//! See the [examples documentation](https://github.com/sdd/kiddo/tree/master/examples) for some more in-depth examples.
//!
//! ## Optional Features
//!
//! Kiddo exposes a number of optional crate features:
//!
//! - `fixed` enables support for fixed-point coordinate
//! types from the [`fixed`](https://docs.rs/fixed/latest/fixed) crate.
//!
//! - `f16` enables support for half-precision floating-point coordinates via
//! the [`half`](https://docs.rs/half/latest/half) crate.
//!
//! - `serde` enables serialization and deserialization via
//! [`Serde`](https://docs.rs/serde/latest/serde/).
//!
//! - `rkyv_08` enables zero-copy serialization and deserialization via
//! [`rkyv`](https://docs.rs/rkyv/latest/rkyv/) 0.8.x. This is particularly
//! useful for prebuilt immutable trees that you want to load very quickly,
//! especially in conjunction with memory-mapped files.
//!
//! - `simd` **(NIGHTLY)** enables handwritten SIMD and prefetch intrinsics for
//! additional performance where available. This requires a nightly Rust
//! toolchain.
//!
//! - `huge_pages` enables Linux-specific huge-page advice helpers for owned and
//! archived tree storage.
//!
//! - `leaf_nta_prefetch` enables additional non-temporal leaf prefetch hints in
//! some query paths. This is an advanced tuning feature and is only useful in
//! specific workloads.
//!
//! Kiddo also contains a number of additional feature flags used for internal
//! experimentation, benchmarking, simulation, and specialized tuning. Most
//! users will not need them.
//!
//! ## MSRV
//!
//! Kiddo v6's current minimum supported Rust version (MSRV) is **1.89.0**
//! (**1.85.0** for `v5.x.x`).
//!
//! Kiddo will aim to support at least **N-4** stable Rust releases, which is
//! roughly six months of stable compiler history, when used with the default
//! crate features.
//!
//! Kiddo will also endeavour to increase MSRV only when doing so would provide
//! a material improvement for users, rather than simply for the sake of using
//! a newer compiler.
//!
//! Optional features may require a newer toolchain than the default-feature
//! MSRV if their dependency stack requires it. The `simd` feature is
//! nightly-only and is outside the stable MSRV policy.
//!
//! **NOTE**: Support for rkyv 0.7 was removed in Kiddo v6.
pub type KdTree<A, T, SS, LS, const K: usize, const B: usize> = KdTree;
/// Distance metrics
pub type Chebyshev<R> = crateChebyshev;
pub type DotProduct<R> = crateDotProduct;
pub type Manhattan<R> = crateManhattan;
pub type Minkowski<const P: u32, R> = crateMinkowski;
pub type SquaredEuclidean<R> = crateSquaredEuclidean;
/// Stem ordering strategies for the kd-tree
pub use stem_strategies as stem_strategy;
pub type Donnelly<const BH: usize> = Donnelly;
pub type DonnellyNoPf<const BH: usize> = DonnellyNoPf;
pub type DonnellySimdDescent<const BH: usize> = DonnellySimdDescent;
pub type DonnellySimdFull<const BH: usize> = DonnellySimdFull;
pub type DonnellyUnrolled<const BH: usize> = DonnellyUnrolled;
pub type DonnellyUnrolledBlockDim<const BH: usize> =
DonnellyUnrolledBlockDim;
pub type Eytzinger = Eytzinger;
pub type EytzingerFlexPf<const PF1: isize = 0, const PF2: isize = 1> =
EytzingerFlexPf;
pub type EytzingerNoPf = EytzingerNoPf;
/// Leaf storage strategies for the kd-tree
pub use leaf_strategies as leaf_strategy;
pub type FlatVec<A, T, const K: usize, const B: usize> = FlatVec;
pub type VecOfArenas<A, T, const K: usize, const B: usize> =
VecOfArenas;
pub type VecOfArrays<A, T, const K: usize, const B: usize> =
VecOfArrays;
/// Convenience type alias for recommended default params for an immutable KdTree
pub type ImmutableKdTree<AX, const K: usize> =
;
/// Convenience type alias for recommended default params for a mutable KdTree
pub type MutableKdTree<AX, const K: usize> =
;
/// Leaf view abstraction for accessing leaf data
/// Chunked Leaf view abstraction for accessing leaf data
pub
/// Structs that are returned as query results
pub use ;
pub use ;