|
25 | 25 | //! assert!(!neighbors3d.is_empty()); |
26 | 26 | //! ``` |
27 | 27 |
|
28 | | -use crate::errors::SpartError; |
29 | | -use crate::geometry::DistanceMetric; |
| 28 | +use std::{cmp::Ordering, collections::BinaryHeap}; |
| 29 | + |
30 | 30 | use ordered_float::OrderedFloat; |
31 | 31 | #[cfg(feature = "serde")] |
32 | 32 | use serde::{Deserialize, Serialize}; |
33 | | -use std::cmp::Ordering; |
34 | | -use std::collections::BinaryHeap; |
35 | 33 | use tracing::info; |
36 | 34 |
|
| 35 | +use crate::{errors::SpartError, geometry::DistanceMetric}; |
| 36 | + |
37 | 37 | /// Trait representing a point that can be stored in the Kd‑tree implementation. |
38 | 38 | /// |
39 | 39 | /// A type implementing `KdPoint` must provide the number of dimensions, |
@@ -272,11 +272,24 @@ impl<P: KdPoint> KdTree<P> { |
272 | 272 | }); |
273 | 273 | } |
274 | 274 | } |
275 | | - // Pass k explicitly to avoid unwraps inside recursion |
| 275 | + |
| 276 | + if self.root.is_some() { |
| 277 | + let mut existing = Vec::new(); |
| 278 | + Self::collect_points(&self.root, &mut existing); |
| 279 | + points.extend(existing); |
| 280 | + } |
276 | 281 | self.root = Self::insert_bulk_rec(&mut points[..], 0, k); |
277 | 282 | Ok(()) |
278 | 283 | } |
279 | 284 |
|
| 285 | + fn collect_points(node: &Option<Box<KdNode<P>>>, result: &mut Vec<P>) { |
| 286 | + if let Some(n) = node { |
| 287 | + result.push(n.point.clone()); |
| 288 | + Self::collect_points(&n.left, result); |
| 289 | + Self::collect_points(&n.right, result); |
| 290 | + } |
| 291 | + } |
| 292 | + |
280 | 293 | fn insert_bulk_rec(points: &mut [P], depth: usize, k: usize) -> Option<Box<KdNode<P>>> { |
281 | 294 | if points.is_empty() { |
282 | 295 | return None; |
|
0 commit comments