manifold-knn 0.7.3

Dynamic-programming k-nearest-neighbor queries over birth-ordered point clouds, with optional 3D Delaunay construction.
Documentation
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
//! Dynamic-programming k-nearest-neighbor queries for birth-ordered point clouds.
//!
//! This crate implements the query-side data structure described by Wang et al.,
//! *Manifold k-NN: Accelerated k-NN Queries for Manifold Point Clouds*.
//!
//! The paper separates the method into two concerns:
//!
//! 1. Maintain a **successor table**. When a point `j` is inserted, append `j` to
//!    every earlier point whose Voronoi cell is pruned by the insertion. In a
//!    Delaunay implementation this means appending `j` to the Delaunay neighbors
//!    of `j` in the prefix triangulation.
//! 2. Answer k-NN queries by first following the dynamic-programming 1-NN path,
//!    then expanding only the successor lists of the best candidates.
//!
//! This crate implements item 2, plus safe helpers for constructing and updating
//! successor tables. With the optional `delaunay-3d` feature it can also build
//! insertion-time successor tables directly from the `delaunay` crate's 3D
//! incremental triangulation API.
//!
//! # Example
//!
//! ```
//! use manifold_knn::ManifoldKnn;
//!
//! let points = vec![
//!     [0.0, 0.0, 0.0],
//!     [1.0, 0.0, 0.0],
//!     [0.0, 1.0, 0.0],
//!     [0.0, 0.0, 1.0],
//! ];
//!
//! // Exact but quadratic fallback: every later point is a successor of every
//! // earlier point. This is useful for tests and small point sets.
//! let index = ManifoldKnn::<3>::from_complete_successors(points)?;
//! let nn = index.knn(&[0.2, 0.1, 0.0], 2)?;
//!
//! assert_eq!(nn.len(), 2);
//! assert_eq!(nn[0].index, 0);
//! # Ok::<(), manifold_knn::Error>(())
//! ```

#![cfg_attr(feature = "simd", feature(portable_simd))]
#![deny(unsafe_code)]
#![warn(missing_docs)]

mod bounded;
mod error;
mod table;

#[cfg(feature = "delaunay-3d")]
pub mod delaunay3d;

#[cfg(feature = "delaunay-3d")]
pub use delaunay3d::{Delaunay3dKernel, DelaunayManifoldKnn3, DelaunayTriangulation3};

pub use error::Error;
pub use table::SuccessorTable;

use bounded::{BoundedNeighbors, neighbor_cmp};

/// A nearest-neighbor result.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Neighbor {
    /// Index of the point in the birth-time-ordered point array.
    pub index: usize,
    /// Squared Euclidean distance from the query to the point.
    pub squared_distance: f64,
}

impl Neighbor {
    /// Returns the Euclidean distance from the query to this neighbor.
    #[must_use]
    pub fn distance(self) -> f64 {
        self.squared_distance.sqrt()
    }
}

/// Summary returned by deletion/update operations.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct DeleteReport {
    /// Number of references to the deleted point that were removed from lists.
    pub removed_references: usize,
    /// Number of new successor edges inserted.
    pub inserted_successors: usize,
    /// Number of requested successor edges that were already present.
    pub already_present: usize,
}

/// Dynamic-programming k-NN index over a fixed-dimensional Euclidean point set.
///
/// `D` is the ambient dimension. For the paper's common point-cloud case use
/// `ManifoldKnn<3>`.
#[derive(Clone, Debug)]
pub struct ManifoldKnn<const D: usize> {
    points: Vec<[f64; D]>,
    successors: SuccessorTable,
    active: Vec<bool>,
}

impl<const D: usize> ManifoldKnn<D> {
    /// Creates an index from points and a successor table.
    ///
    /// The successor table must have one list per point; every entry in list `i`
    /// must be a strictly later birth index `j > i`. Lists must be sorted and
    /// duplicate-free. Use [`SuccessorTable::from_lists_normalized`] when input
    /// lists may be unsorted.
    pub fn new(points: Vec<[f64; D]>, successors: SuccessorTable) -> Result<Self, Error> {
        validate_points(&points)?;
        successors.validate_for_len(points.len())?;
        let active = vec![true; points.len()];
        Ok(Self {
            points,
            successors,
            active,
        })
    }

    /// Builds an exact but quadratic successor table.
    ///
    /// This stores every pair `i < j` as a successor edge `i -> j`. It is useful
    /// as a correctness oracle, for small inputs, and for tests. It does **not**
    /// provide the acceleration of the manifold/Delaunay successor table.
    pub fn from_complete_successors(points: Vec<[f64; D]>) -> Result<Self, Error> {
        let n = points.len();
        Self::new(points, SuccessorTable::complete(n))
    }

    /// Builds an index from insertion-time neighbor lists.
    ///
    /// `neighbors_at_insertion[j]` must contain earlier birth indices `i < j`
    /// that were adjacent to `j` when `j` was inserted. In the paper, these are
    /// the Delaunay neighbors of `p_j` in the prefix triangulation. For every
    /// such neighbor, this constructor appends `j` to the successor list of `i`.
    pub fn from_insertion_neighbors(
        points: Vec<[f64; D]>,
        neighbors_at_insertion: Vec<Vec<usize>>,
    ) -> Result<Self, Error> {
        let successors =
            SuccessorTable::from_insertion_neighbors(points.len(), neighbors_at_insertion)?;
        Self::new(points, successors)
    }

    /// Returns all points in birth order.
    #[must_use]
    pub fn points(&self) -> &[[f64; D]] {
        &self.points
    }

    /// Returns the successor table.
    #[must_use]
    pub fn successors(&self) -> &SuccessorTable {
        &self.successors
    }

    /// Returns a mutable successor table.
    ///
    /// This is exposed for integration with external Delaunay maintenance code.
    /// Call [`SuccessorTable::validate_for_len`] after substantial edits.
    #[must_use]
    pub fn successors_mut(&mut self) -> &mut SuccessorTable {
        &mut self.successors
    }

    /// Number of stored points, including inactive points left by deletion APIs.
    #[must_use]
    #[inline]
    pub fn len(&self) -> usize {
        self.points.len()
    }

    /// Returns `true` if the index contains no stored points.
    #[must_use]
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.points.is_empty()
    }

    /// Number of active points.
    #[must_use]
    #[inline]
    pub fn active_len(&self) -> usize {
        self.active.iter().filter(|&&is_active| is_active).count()
    }

    /// Returns whether the point at `index` is active.
    #[must_use]
    #[inline]
    pub fn is_active(&self, index: usize) -> bool {
        self.active.get(index).copied().unwrap_or(false)
    }

    /// Iterator over active point indices in birth order.
    #[inline]
    pub fn active_indices(&self) -> impl Iterator<Item = usize> + '_ {
        self.active
            .iter()
            .enumerate()
            .filter_map(|(index, &is_active)| is_active.then_some(index))
    }

    /// Inserts a new point and updates successor lists from externally supplied
    /// insertion-time neighbors.
    ///
    /// `prior_neighbors` must contain active earlier indices that are adjacent to
    /// the new point at insertion time. In a faithful implementation of the
    /// paper, these come from incremental Delaunay construction.
    ///
    /// Returns the birth index assigned to the inserted point.
    #[inline]
    pub fn insert_with_neighbors<I>(
        &mut self,
        point: [f64; D],
        prior_neighbors: I,
    ) -> Result<usize, Error>
    where
        I: IntoIterator<Item = usize>,
    {
        validate_point(self.points.len(), &point)?;

        let new_index = self.points.len();
        let mut neighbors: Vec<usize> = prior_neighbors.into_iter().collect();
        neighbors.sort_unstable();
        neighbors.dedup();

        for &neighbor in &neighbors {
            if neighbor >= new_index {
                return Err(Error::InvalidInsertionNeighbor {
                    inserted: new_index,
                    neighbor,
                });
            }
            if !self.active[neighbor] {
                return Err(Error::InactivePoint { index: neighbor });
            }
        }

        self.points.push(point);
        self.active.push(true);
        self.successors.push_empty_list();

        for neighbor in neighbors {
            self.successors.insert_successor(neighbor, new_index)?;
        }

        Ok(new_index)
    }

    /// Returns the nearest active point in the full index.
    #[inline]
    pub fn nearest(&self, query: &[f64; D]) -> Result<Option<Neighbor>, Error> {
        WORKSPACE.with(|ws| {
            let mut workspace = ws.borrow_mut();
            self.nearest_with_workspace(query, &mut workspace)
        })
    }

    /// Returns the nearest active point in the full index using a workspace to avoid allocations.
    #[inline]
    pub fn nearest_with_workspace(
        &self,
        query: &[f64; D],
        workspace: &mut QueryWorkspace,
    ) -> Result<Option<Neighbor>, Error> {
        let slice = self.knn_prefix_with_workspace(query, 1, self.points.len(), workspace)?;
        Ok(slice.first().copied())
    }

    /// Returns up to `k` nearest active points in the full index.
    ///
    /// Results are sorted by squared distance, with birth index used as a stable
    /// tie-breaker.
    #[inline]
    pub fn knn(&self, query: &[f64; D], k: usize) -> Result<Vec<Neighbor>, Error> {
        self.knn_prefix(query, k, self.points.len())
    }

    /// Returns up to `k` nearest active points in the full index using a workspace.
    #[inline]
    pub fn knn_with_workspace<'a>(
        &self,
        query: &[f64; D],
        k: usize,
        workspace: &'a mut QueryWorkspace,
    ) -> Result<&'a [Neighbor], Error> {
        self.knn_prefix_with_workspace(query, k, self.points.len(), workspace)
    }

    /// Returns up to `k` nearest active points whose birth index is `< prefix_len`.
    ///
    /// This is the paper's zero-overhead prefix-subset query. The same successor
    /// table is reused; candidates outside the prefix are ignored.
    #[inline]
    pub fn knn_prefix(
        &self,
        query: &[f64; D],
        k: usize,
        prefix_len: usize,
    ) -> Result<Vec<Neighbor>, Error> {
        WORKSPACE.with(|ws| {
            let mut workspace = ws.borrow_mut();
            let slice = self.knn_prefix_with_workspace(query, k, prefix_len, &mut workspace)?;
            Ok(slice.to_vec())
        })
    }

    /// Returns up to `k` nearest active points whose birth index is `< prefix_len` using a workspace.
    #[inline]
    pub fn knn_prefix_with_workspace<'a>(
        &self,
        query: &[f64; D],
        k: usize,
        prefix_len: usize,
        workspace: &'a mut QueryWorkspace,
    ) -> Result<&'a [Neighbor], Error> {
        self.validate_query_and_prefix(query, prefix_len)?;
        if k == 0 {
            workspace.candidates.reset(0);
            return Ok(&[]);
        }

        let Some(first_active) = self.first_active_before(prefix_len) else {
            workspace.candidates.reset(0);
            return Ok(&[]);
        };

        self.transition_sites_internal_from_index_with_workspace(
            query,
            k,
            prefix_len,
            first_active,
            workspace,
        )?;

        if workspace.processed.len() < self.points.len() {
            workspace.processed.resize(self.points.len(), false);
        }
        if workspace.discovered.len() < self.points.len() {
            workspace.discovered.resize(self.points.len(), false);
        }

        for candidate in workspace.candidates.as_slice() {
            let idx = candidate.index;
            if !workspace.discovered[idx] {
                workspace.discovered[idx] = true;
                workspace.visited_indices.push(idx);
            }
        }

        while let Some(index) = workspace
            .candidates
            .as_slice()
            .iter()
            .find(|candidate| !workspace.processed[candidate.index])
            .map(|candidate| candidate.index)
        {
            workspace.processed[index] = true;

            for &successor in self.successors.list(index) {
                if successor >= prefix_len {
                    break;
                }
                if workspace.discovered[successor] {
                    continue;
                }
                if !self.active[successor] {
                    continue;
                }
                workspace.discovered[successor] = true;
                workspace.visited_indices.push(successor);

                workspace.candidates.insert(Neighbor {
                    index: successor,
                    squared_distance: squared_distance(&self.points[successor], query),
                });
            }
        }

        for &index in &workspace.visited_indices {
            workspace.processed[index] = false;
            workspace.discovered[index] = false;
        }
        workspace.visited_indices.clear();

        Ok(workspace.candidates.as_slice())
    }

    /// Collects transition sites from the dynamic-programming 1-NN path.
    ///
    /// Transition sites are the successive points that become the nearest
    /// neighbor during the birth-ordered insertion history. The returned list is
    /// sorted by query distance and capped to `capacity` entries.
    #[inline]
    pub fn transition_sites(
        &self,
        query: &[f64; D],
        capacity: usize,
    ) -> Result<Vec<Neighbor>, Error> {
        self.transition_sites_prefix(query, capacity, self.points.len())
    }

    /// Collects transition sites using a workspace.
    #[inline]
    pub fn transition_sites_with_workspace<'a>(
        &self,
        query: &[f64; D],
        capacity: usize,
        workspace: &'a mut QueryWorkspace,
    ) -> Result<&'a [Neighbor], Error> {
        self.transition_sites_prefix_with_workspace(query, capacity, self.points.len(), workspace)
    }

    /// Prefix-restricted variant of [`Self::transition_sites`].
    #[inline]
    pub fn transition_sites_prefix(
        &self,
        query: &[f64; D],
        capacity: usize,
        prefix_len: usize,
    ) -> Result<Vec<Neighbor>, Error> {
        WORKSPACE.with(|ws| {
            let mut workspace = ws.borrow_mut();
            let slice = self.transition_sites_prefix_with_workspace(
                query,
                capacity,
                prefix_len,
                &mut workspace,
            )?;
            Ok(slice.to_vec())
        })
    }

    /// Prefix-restricted variant of [`Self::transition_sites`] using a workspace.
    #[inline]
    pub fn transition_sites_prefix_with_workspace<'a>(
        &self,
        query: &[f64; D],
        capacity: usize,
        prefix_len: usize,
        workspace: &'a mut QueryWorkspace,
    ) -> Result<&'a [Neighbor], Error> {
        self.validate_query_and_prefix(query, prefix_len)?;
        self.transition_sites_internal_with_workspace(query, capacity, prefix_len, workspace)?;
        Ok(workspace.candidates.as_slice())
    }

    /// Brute-force k-NN over the active full index.
    ///
    /// This is intended for validation and benchmarking, not production queries.
    #[inline]
    pub fn brute_force_knn(&self, query: &[f64; D], k: usize) -> Result<Vec<Neighbor>, Error> {
        self.brute_force_knn_prefix(query, k, self.points.len())
    }

    /// Brute-force k-NN over active points with birth index `< prefix_len`.
    #[inline]
    pub fn brute_force_knn_prefix(
        &self,
        query: &[f64; D],
        k: usize,
        prefix_len: usize,
    ) -> Result<Vec<Neighbor>, Error> {
        self.validate_query_and_prefix(query, prefix_len)?;
        if k == 0 {
            return Ok(Vec::new());
        }

        let mut neighbors = Vec::new();
        for index in 0..prefix_len {
            if self.active[index] {
                neighbors.push(Neighbor {
                    index,
                    squared_distance: squared_distance(&self.points[index], query),
                });
            }
        }
        neighbors.sort_by(neighbor_cmp);
        neighbors.truncate(k);
        Ok(neighbors)
    }

    /// Marks `index` as deleted and applies externally computed local successor
    /// edges that replace adjacencies created by the deletion.
    ///
    /// This is the safe hook for the paper's local Delaunay deletion algorithm.
    /// The crate validates and applies the successor-table mutations, while the
    /// caller supplies the geometric result of the local retriangulation.
    ///
    /// Each pair `(i, j)` in `new_successors` means "insert successor edge
    /// `i -> j`" and must satisfy `i < j`; both endpoints must be active and must
    /// not be the deleted point.
    #[inline]
    pub fn delete_with_new_successors<I>(
        &mut self,
        index: usize,
        new_successors: I,
    ) -> Result<DeleteReport, Error>
    where
        I: IntoIterator<Item = (usize, usize)>,
    {
        self.ensure_active_index(index)?;

        let mut edges: Vec<(usize, usize)> = new_successors.into_iter().collect();
        edges.sort_unstable();
        edges.dedup();

        for &(owner, successor) in &edges {
            self.validate_new_successor_edge(index, owner, successor)?;
        }

        let removed_references = self.successors.remove_references_to(index);
        self.successors.clear_list(index)?;
        self.active[index] = false;

        let mut inserted_successors = 0;
        let mut already_present = 0;
        for (owner, successor) in edges {
            if self.successors.insert_successor(owner, successor)? {
                inserted_successors += 1;
            } else {
                already_present += 1;
            }
        }

        Ok(DeleteReport {
            removed_references,
            inserted_successors,
            already_present,
        })
    }

    /// Deletes a point and rebuilds a complete quadratic successor table over the
    /// remaining active points.
    ///
    /// This is an exact fallback for applications that need correctness after a
    /// deletion but do not yet have local Delaunay-update integration. It discards
    /// the accelerated successor table and should only be used for small or test
    /// data.
    #[inline]
    pub fn delete_rebuild_complete(&mut self, index: usize) -> Result<DeleteReport, Error> {
        self.ensure_active_index(index)?;
        let removed_references = self.successors.remove_references_to(index);
        self.successors.clear_all();
        self.active[index] = false;

        let mut inserted_successors = 0;
        for owner in 0..self.points.len() {
            if !self.active[owner] {
                continue;
            }
            for successor in (owner + 1)..self.points.len() {
                if self.active[successor] {
                    self.successors.insert_successor(owner, successor)?;
                    inserted_successors += 1;
                }
            }
        }

        Ok(DeleteReport {
            removed_references,
            inserted_successors,
            already_present: 0,
        })
    }

    #[inline]
    fn transition_sites_internal_with_workspace(
        &self,
        query: &[f64; D],
        capacity: usize,
        prefix_len: usize,
        workspace: &mut QueryWorkspace,
    ) -> Result<(), Error> {
        if capacity == 0 {
            workspace.candidates.reset(0);
            return Ok(());
        }
        let Some(first_active) = self.first_active_before(prefix_len) else {
            workspace.candidates.reset(capacity);
            return Ok(());
        };
        self.transition_sites_internal_from_index_with_workspace(
            query,
            capacity,
            prefix_len,
            first_active,
            workspace,
        )
    }

    #[inline]
    fn transition_sites_internal_from_index_with_workspace(
        &self,
        query: &[f64; D],
        capacity: usize,
        prefix_len: usize,
        mut current: usize,
        workspace: &mut QueryWorkspace,
    ) -> Result<(), Error> {
        workspace.candidates.reset(capacity);
        if capacity == 0 {
            return Ok(());
        }

        workspace.candidates.insert(Neighbor {
            index: current,
            squared_distance: squared_distance(&self.points[current], query),
        });

        loop {
            let current_distance = squared_distance(&self.points[current], query);
            let mut next = None;

            for &successor in self.successors.list(current) {
                if successor >= prefix_len {
                    break;
                }
                if !self.active[successor] {
                    continue;
                }

                let successor_distance = squared_distance(&self.points[successor], query);
                if is_strictly_better(successor_distance, successor, current_distance, current) {
                    next = Some((successor, successor_distance));
                    break;
                }
            }

            let Some((successor, successor_distance)) = next else {
                break;
            };

            current = successor;
            workspace.candidates.insert(Neighbor {
                index: current,
                squared_distance: successor_distance,
            });
        }

        Ok(())
    }

    #[inline]
    fn validate_query_and_prefix(&self, query: &[f64; D], prefix_len: usize) -> Result<(), Error> {
        validate_query(query)?;
        if prefix_len > self.points.len() {
            return Err(Error::InvalidPrefix {
                prefix_len,
                len: self.points.len(),
            });
        }
        Ok(())
    }

    #[inline]
    fn first_active_before(&self, prefix_len: usize) -> Option<usize> {
        self.active[..prefix_len]
            .iter()
            .position(|&is_active| is_active)
    }

    #[inline]
    fn ensure_active_index(&self, index: usize) -> Result<(), Error> {
        if index >= self.points.len() {
            return Err(Error::InvalidIndex {
                index,
                len: self.points.len(),
            });
        }
        if !self.active[index] {
            return Err(Error::InactivePoint { index });
        }
        Ok(())
    }

    #[inline]
    fn validate_new_successor_edge(
        &self,
        deleted: usize,
        owner: usize,
        successor: usize,
    ) -> Result<(), Error> {
        if owner >= self.points.len() {
            return Err(Error::InvalidIndex {
                index: owner,
                len: self.points.len(),
            });
        }
        if successor >= self.points.len() {
            return Err(Error::InvalidIndex {
                index: successor,
                len: self.points.len(),
            });
        }
        if owner == deleted || successor == deleted || owner >= successor {
            return Err(Error::InvalidSuccessor {
                owner,
                successor,
                len: self.points.len(),
            });
        }
        if !self.active[owner] {
            return Err(Error::InactivePoint { index: owner });
        }
        if !self.active[successor] {
            return Err(Error::InactivePoint { index: successor });
        }
        Ok(())
    }
}

/// Reusable query workspace to avoid allocations during nearest-neighbor queries.
#[derive(Clone, Debug, Default)]
pub struct QueryWorkspace {
    processed: Vec<bool>,
    discovered: Vec<bool>,
    visited_indices: Vec<usize>,
    candidates: BoundedNeighbors,
}

impl QueryWorkspace {
    /// Creates a new empty query workspace.
    #[must_use]
    #[inline]
    pub const fn new() -> Self {
        Self {
            processed: Vec::new(),
            discovered: Vec::new(),
            visited_indices: Vec::new(),
            candidates: BoundedNeighbors::new_empty(),
        }
    }
}

thread_local! {
    static WORKSPACE: std::cell::RefCell<QueryWorkspace> = const { std::cell::RefCell::new(QueryWorkspace::new()) };
}

#[inline]
pub(crate) fn validate_points<const D: usize>(points: &[[f64; D]]) -> Result<(), Error> {
    #[cfg(feature = "parallel")]
    {
        use rayon::prelude::*;
        points
            .par_iter()
            .enumerate()
            .try_for_each(|(index, point)| validate_point(index, point))
    }
    #[cfg(not(feature = "parallel"))]
    {
        for (index, point) in points.iter().enumerate() {
            validate_point(index, point)?;
        }
        Ok(())
    }
}

#[inline]
pub(crate) fn validate_point<const D: usize>(index: usize, point: &[f64; D]) -> Result<(), Error> {
    for (coordinate, &value) in point.iter().enumerate() {
        if !value.is_finite() {
            return Err(Error::NonFiniteCoordinate {
                point: index,
                coordinate,
                value,
            });
        }
    }
    Ok(())
}

#[inline]
pub(crate) fn validate_query<const D: usize>(query: &[f64; D]) -> Result<(), Error> {
    for (coordinate, &value) in query.iter().enumerate() {
        if !value.is_finite() {
            return Err(Error::NonFiniteQuery { coordinate, value });
        }
    }
    Ok(())
}

#[cfg(feature = "simd")]
#[inline]
pub(crate) fn squared_distance<const D: usize>(point: &[f64; D], query: &[f64; D]) -> f64 {
    use std::simd::{f64x4, num::SimdFloat};

    if D == 0 {
        return 0.0;
    }

    // Fast path for the most common small dimensions (D=2, D=3, D=4)
    // This covers the vast majority of point-cloud use cases.
    if D <= 4 {
        let mut p = [0.0_f64; 4];
        let mut q = [0.0_f64; 4];
        p[..D].copy_from_slice(&point[..D]);
        q[..D].copy_from_slice(&query[..D]);
        let pv = f64x4::from_array(p);
        let qv = f64x4::from_array(q);
        let diff = pv - qv;
        return (diff * diff).reduce_sum();
    }

    // General case: process 4 elements at a time using SIMD
    let mut sum = 0.0_f64;
    let mut i = 0;

    while i + 4 <= D {
        let pv = f64x4::from_array([point[i], point[i + 1], point[i + 2], point[i + 3]]);
        let qv = f64x4::from_array([query[i], query[i + 1], query[i + 2], query[i + 3]]);
        let diff = pv - qv;
        sum += (diff * diff).reduce_sum();
        i += 4;
    }

    // Handle remaining elements (0-3)
    for j in i..D {
        let delta = point[j] - query[j];
        sum += delta * delta;
    }

    sum
}

#[cfg(not(feature = "simd"))]
#[inline]
pub(crate) fn squared_distance<const D: usize>(point: &[f64; D], query: &[f64; D]) -> f64 {
    let mut sum = 0.0;
    for i in 0..D {
        let delta = point[i] - query[i];
        sum += delta * delta;
    }
    sum
}

#[inline]
pub(crate) fn is_strictly_better(
    new_distance: f64,
    new_index: usize,
    old_distance: f64,
    old_index: usize,
) -> bool {
    new_distance < old_distance || (new_distance == old_distance && new_index < old_index)
}