kiddo 6.0.0

A high-performance, flexible, ergonomic k-d tree library. Ideal for geo- and astro- nearest-neighbour and k-nearest-neighbor queries
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
use aligned_vec::AVec;
use std::mem::MaybeUninit;
use std::ptr::NonNull;

use crate::kd_tree::query_stack::{ScalarStackContext, StackTrait};
use crate::stem_strategy::donnelly::simd_full::{BacktrackBlock3, BacktrackBlock4};
use crate::{Axis, Content};

const MAX_MUTABLE_STEM_LEVEL_SLACK: usize = 4;

/// Cache-line-aligned, phase-specific SIMD query lanes. Strategies initialize
/// only phases and lanes their traversal can reach.
#[doc(hidden)]
#[repr(C, align(64))]
pub struct PreparedBlockQuery<A, const K: usize>(pub [[MaybeUninit<A>; 16]; K]);

impl<A, const K: usize> std::ops::Index<usize> for PreparedBlockQuery<A, K> {
    type Output = [MaybeUninit<A>; 16];

    #[inline(always)]
    fn index(&self, index: usize) -> &Self::Output {
        &self.0[index]
    }
}

/// Trait that needs to be implemented by any potential stem ordering
/// algorithm used by a KdTree.
///
/// To see which stem strategies are available, see the [`stem_strategies`](`crate::stem_strategy`) module.
pub trait StemStrategy: Clone + Sync + Send + 'static {
    /// The stem index of the root node of the tree
    const ROOT_IDX: usize = 0;

    /// The block size of this strategy
    ///
    /// The default is 1, which means that the strategy is not block-based.
    const BLOCK_SIZE: usize = 1;

    /// Whether construction must pad the stem height to a whole layout block.
    ///
    /// A strategy can use block-shaped addressing without requiring a complete
    /// final block. Such strategies should override this to `false` so that
    /// queries do not traverse synthetic root levels.
    const REQUIRES_BLOCK_ALIGNED_STEM_HEIGHT: bool = Self::BLOCK_SIZE > 1;

    /// Whether this strategy maintains the arithmetic leaf-path index required
    /// by immutable trees' inline continuation-stack query path.
    ///
    /// Level-at-a-time layouts preserve the historical behavior by default.
    /// Block layouts may opt in when their scalar branching operations maintain
    /// an equivalent arithmetic leaf index.
    const SUPPORTS_ARITHMETIC_LEAF_RESOLUTION: bool = Self::BLOCK_SIZE == 1;

    /// Whether scalar descent has dedicated head/tail operations for complete
    /// block-unrolled bodies.
    const USES_UNROLLED_SCALAR_TRAVERSAL: bool = false;

    /// Whether complete blocks use one SIMD comparison to select the near
    /// root-to-terminal path before replaying that path through the ordinary
    /// scalar continuation machinery.
    const USES_SIMD_BLOCK_DESCENT: bool = false;

    /// Whether the strategy prepares phase-specific SIMD query lanes once per
    /// query and reuses them for every complete block.
    #[doc(hidden)]
    const USES_PREPARED_BLOCK_QUERY: bool = false;

    /// Whether SIMD block selection is also used after entering a deferred far
    /// subtree. Initial descent is always eligible when
    /// `USES_SIMD_BLOCK_DESCENT` is set.
    #[doc(hidden)]
    const SIMD_BLOCK_DESCENT_ON_BACKTRACK: bool = true;

    /// Compact state persisted on scalar backtracking stacks.
    ///
    /// Scalar strategies can use this to store only the state needed to resume a deferred branch.
    /// SIMD / block strategies may ignore this and continue to use custom stack types.
    type DeferredState: Sized;

    /// Query stack context type for backtracking queries.
    ///
    /// Non-block strategies use simple scalar stack context (QueryStackContext).
    /// Block-based SIMD strategies use SimdQueryStackContext.
    type StackContext<A>: Sized
        + crate::kd_tree::query_stack::ScalarStackContext<A, Self::DeferredState>
    where
        Self: Sized;

    /// Query stack type for backtracking queries.
    ///
    /// Non-block strategies use simple scalar stack (QueryStack).
    /// Block-based SIMD strategies use SimdQueryStack.
    type Stack<A>: Default + crate::kd_tree::query_stack::StackTrait<A, Self>
    where
        Self: Sized;

    /// Create a new instance of this strategy at the root.
    fn new(stems_ptr: NonNull<u8>) -> Self;

    /// Create a new instance of this strategy at the root, with a dangling pointer.
    ///
    /// Useful for generating traversal indices without performing prefetches
    #[inline(always)]
    fn new_no_ptr() -> Self {
        Self::new(NonNull::dangling())
    }

    /// Returns the block size of this strategy
    #[inline(always)]
    fn block_size() -> usize {
        Self::BLOCK_SIZE
    }

    /// Get the current stem index this strategy points to.
    fn stem_idx(&self) -> usize;

    /// Snapshot the minimal scalar deferred state needed to resume traversal later.
    fn deferred_state(&self) -> Self::DeferredState;

    /// Restore this strategy from deferred scalar traversal state.
    ///
    /// Implementations may assume `self` already holds a valid `stems_ptr`.
    fn rehydrate_deferred_state(&mut self, state: Self::DeferredState);

    /// Get the current leaf index this strategy points to.
    fn leaf_idx(&self) -> usize;

    /// Get the current dimension (query time)
    fn dim<const K: usize>(&self) -> usize;

    /// Get the current dimension (construction time)
    fn construction_dim<const K: usize>(&self) -> usize {
        self.dim::<K>()
    }

    /// Select the terminal child of the complete block rooted at the current
    /// stem. The returned child index encodes the scalar path bits from most to
    /// least significant.
    #[doc(hidden)]
    #[inline(always)]
    fn select_block_child<A: Axis<Coord = A>, const K: usize>(
        &self,
        _stems: &[A],
        _query: &[A; K],
        _start_dim: usize,
    ) -> u8 {
        unreachable!("strategy does not implement SIMD block descent")
    }

    /// Prepare phase-specific SIMD query lanes once for this query.
    #[doc(hidden)]
    #[inline(always)]
    fn prepare_block_query<A: Axis<Coord = A>, const K: usize>(
        _query: &[A; K],
    ) -> PreparedBlockQuery<A, K> {
        PreparedBlockQuery([[MaybeUninit::uninit(); 16]; K])
    }

    /// Select a terminal block child using a query representation prepared by
    /// [`Self::prepare_block_query`].
    #[doc(hidden)]
    #[inline(always)]
    fn select_prepared_block_child<A: Axis<Coord = A>, const K: usize>(
        &self,
        stems: &[A],
        query: &[A; K],
        _prepared: &PreparedBlockQuery<A, K>,
        start_dim: usize,
    ) -> u8 {
        self.select_block_child(stems, query, start_dim)
    }

    /// Get the current level
    fn level(&self) -> i32;

    /// Advance `self` down to a child in-place.
    fn traverse<A: Axis<Coord = A>, const K: usize>(&mut self, is_right: bool);

    /// Advance `self` down to a child in-place. Specialized for use as one
    /// of the non-final stages when loop-unrolling to the level of a minor tri height
    #[inline(always)]
    fn traverse_head<A: Axis<Coord = A>, const K: usize>(&mut self, is_right: bool) {
        self.traverse::<A, K>(is_right);
    }

    /// Advance `self` down to a child in-place. Specialized for use as the
    /// last stage when loop-unrolled to the level of a minor tri height
    #[inline(always)]
    fn traverse_tail<A: Axis<Coord = A>, const K: usize>(&mut self, is_right: bool) {
        self.traverse::<A, K>(is_right);
    }

    /// Advance `self` down to one child, returning the other.
    /// - `self` mutates into the left child
    /// - return value is the right child
    fn branch<A: Axis<Coord = A>, const K: usize>(&mut self) -> Self;

    /// Advance `self` to the "closer" child, returning the "further" one.
    #[inline(always)]
    fn branch_relative<A: Axis<Coord = A>, const K: usize>(&mut self, is_right: bool) -> Self {
        if is_right {
            let mut right = self.branch::<A, K>();
            std::mem::swap(self, &mut right);
            right
        } else {
            self.branch::<A, K>()
        }
    }

    /// Branch within a known non-final level of a block-unrolled traversal.
    #[inline(always)]
    fn branch_relative_head<A: Axis<Coord = A>, const K: usize>(&mut self, is_right: bool) -> Self {
        self.branch_relative::<A, K>(is_right)
    }

    /// Branch from a known final level of a block-unrolled traversal.
    #[inline(always)]
    fn branch_relative_tail<A: Axis<Coord = A>, const K: usize>(&mut self, is_right: bool) -> Self {
        self.branch_relative::<A, K>(is_right)
    }

    /// Split `self` into two independent child strategies (left, right).
    fn split<A: Axis<Coord = A>, const K: usize>(mut self) -> (Self, Self)
    where
        Self: Sized,
    {
        let right = self.branch::<A, K>();
        (self, right)
    }

    /// Split `self` into (closer, further) given a direction.
    fn split_relative<A: Axis<Coord = A>, const K: usize>(self, is_right: bool) -> (Self, Self)
    where
        Self: Sized,
    {
        let (l, r) = self.split::<A, K>();
        if is_right {
            (r, l)
        } else {
            (l, r)
        }
    }

    /// Get the stem indices where the left and right children would be located.
    /// Returns (left_child_stem_idx, right_child_stem_idx).
    fn child_indices<A: Axis<Coord = A>>(&self) -> (usize, usize);

    /// Whether a mutable split at the current terminal would make this stem
    /// layout impractically sparse.
    ///
    /// The default bounds path depth relative to a compact tree and can be
    /// overridden by strategies with a more precise layout-specific check.
    #[doc(hidden)]
    fn mutable_split_requires_rebuild(&self, leaf_count_after_split: usize) -> bool {
        let compact_terminal_level = leaf_count_after_split
            .checked_next_power_of_two()
            .map_or(usize::MAX, |count| count.ilog2() as usize);
        let layout_padding = Self::BLOCK_SIZE.saturating_sub(1);
        let maximum_level = compact_terminal_level
            .saturating_add(layout_padding)
            .saturating_add(MAX_MUTABLE_STEM_LEVEL_SLACK);
        let prospective_terminal_level = (self.level().max(0) as usize).saturating_add(1);

        prospective_terminal_level > maximum_level
            || prospective_terminal_level >= u32::BITS as usize
    }

    /// Calculate the stem node count for a given leaf node count.
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn get_stem_node_count_from_leaf_node_count(_leaf_node_count: usize) -> usize {
        unimplemented!()
    }

    /// Factor by which to pad the stem node allocation.
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn stem_node_padding_factor() -> usize {
        1
    }

    /// Trim unneeded stem nodes.
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn trim_unneeded_stems<A: Axis<Coord = A>, const K: usize>(
        _stems: &mut AVec<A>,
        _max_stem_level: usize,
    ) {
        // Default: no-op
    }

    /// Emit cache-simulation events while advancing one level in the stem tree.
    ///
    /// Implementations should mirror `traverse` behavior but also report memory
    /// accesses via `event_tx` for the cache simulator.
    #[cfg(feature = "simulator")]
    fn simulate_traverse<A: Axis<Coord = A>, const K: usize>(
        &mut self,
        _is_right: bool,
        _event_tx: &std::sync::mpsc::Sender<crate::test_utils::cache_simulator::Event>,
    ) {
        unimplemented!();
    }

    /// Get leaf index for a query point. Default uses simple while loop.
    /// Block-based strategies override with unrolled loops.
    fn get_leaf_idx<A: Axis<Coord = A>, const K: usize>(
        stems: &[A],
        query: &[A; K],
        max_stem_level: i32,
    ) -> usize
    where
        Self: Sized,
    {
        let stems_ptr = NonNull::new(stems.as_ptr() as *mut u8).unwrap();
        let mut stem_strat = Self::new(stems_ptr);

        while stem_strat.level() <= max_stem_level {
            let pivot = unsafe { stems.get_unchecked(stem_strat.stem_idx()) };
            let is_right = unsafe { *query.get_unchecked(stem_strat.dim::<K>()) } >= *pivot;
            stem_strat.traverse::<A, K>(is_right);
        }

        stem_strat.leaf_idx()
    }

    /// Single step of backtracking traversal.
    ///
    /// Default implementation handles level-by-level traversal with one pivot per step.
    /// Block-based strategies override to handle multiple levels at once with SIMD.
    ///
    /// Returns true if traversal should continue (more levels to go), false if at leaf.
    #[inline(always)]
    fn backtracking_traverse_step<A, O, D, const K: usize>(
        &mut self,
        stems: &[A],
        query: &[A; K],
        query_wide: &[O; K],
        off: &mut [O; K],
        dim: &mut usize,
        rd: O,
        max_stem_level: i32,
        best_dist: O,
        stack: &mut Self::Stack<O>,
    ) -> bool
    where
        Self: Sized,
        A: Axis<Coord = A>,
        O: Axis<Coord = O> + BacktrackBlock3 + BacktrackBlock4,
        D: crate::dist::DistanceMetric<A, Output = O>,
        Self::Stack<O>: StackTrait<O, Self>,
    {
        // Default implementation for scalar strategies
        // SIMD strategies override this entire method
        if self.level() > max_stem_level {
            return false;
        }

        #[cfg(feature = "result_collection_stats")]
        crate::results::result_collection_stats::record_query_scalar_traverse_step();

        #[cfg(feature = "result_collection_stats")]
        {
            let rd_from_off = D::rect_dist_from_off(off);
            crate::results::result_collection_stats::record_query_scalar_rd_off_check(O::cmp(
                rd_from_off,
                rd,
            ));
        }

        let pivot = *unsafe { stems.get_unchecked(self.stem_idx()) };

        if pivot < A::max_value() {
            let query_elem = *unsafe { query.get_unchecked(*dim) };
            let is_right_child = query_elem >= pivot;

            let old_stem_idx = self.stem_idx();
            let far_ctx = self.branch_relative::<A, K>(is_right_child);

            tracing::trace!(
                %pivot,
                dim = %self.dim::<K>(),
                %query_elem,
                %is_right_child,
                %old_stem_idx,
                new_stem_idx = %self.stem_idx(),
                level = self.level(),
                "Traverse down one level"
            );

            let pivot_wide: O = D::widen_coord(pivot);
            let query_elem_wide = *unsafe { query_wide.get_unchecked(*dim) };

            let new_off = O::saturating_dist(query_elem_wide, pivot_wide);
            #[cfg(feature = "test_utils")]
            let old_off = *unsafe { off.get_unchecked(*dim) };
            let rd_far = D::rect_dist_after_update(rd, off, *dim, new_off);

            #[cfg(feature = "test_utils")]
            {
                if crate::test_utils::exact_query_trace::enabled()
                    && std::any::type_name::<A>() == "f64"
                    && std::any::type_name::<O>() == "f64"
                {
                    let pivot_f = unsafe { *(&pivot as *const A as *const f64) };
                    let query_elem_f = unsafe { *(&query_elem as *const A as *const f64) };
                    let old_off_f = unsafe { *(&old_off as *const O as *const f64) };
                    let new_off_f = unsafe { *(&new_off as *const O as *const f64) };
                    let rd_f = unsafe { *(&rd as *const O as *const f64) };
                    let rd_far_f = unsafe { *(&rd_far as *const O as *const f64) };
                    crate::test_utils::exact_query_trace::push(
                        crate::test_utils::exact_query_trace::ExactQueryTraceEvent::ScalarStep {
                            stem_idx: old_stem_idx,
                            level: self.level() - 1,
                            dim: *dim,
                            pivot: pivot_f,
                            query_elem: query_elem_f,
                            is_right_child,
                            old_off: old_off_f,
                            new_off: new_off_f,
                            rd: rd_f,
                            rd_far: rd_far_f,
                            near_stem_idx: self.stem_idx(),
                            far_stem_idx: far_ctx.stem_idx(),
                        },
                    );
                }
            }

            // Only push if the sibling is worth exploring
            if O::cmp(rd_far, best_dist) != std::cmp::Ordering::Greater {
                stack.push(Self::StackContext::<O>::from_parts_with_restore_dim(
                    far_ctx.deferred_state(),
                    *dim,
                    new_off,
                    rd_far,
                ));
                #[cfg(feature = "result_collection_stats")]
                crate::results::result_collection_stats::record_query_scalar_far_child_push();
            } else {
                #[cfg(feature = "result_collection_stats")]
                crate::results::result_collection_stats::record_query_scalar_far_child_reject();
            }
        } else {
            self.traverse::<A, K>(false);
        }

        *dim = self.dim::<K>();
        true
    }

    /// Single step of backtracking traversal for interval-aware SIMD exact paths.
    ///
    /// Default implementation ignores the interval bounds and delegates to
    /// `backtracking_traverse_step`.
    #[inline(always)]
    fn backtracking_traverse_step_with_bounds<A, O, D, const K: usize>(
        &mut self,
        stems: &[A],
        query: &[A; K],
        query_wide: &[O; K],
        lower: &mut [O; K],
        upper: &mut [O; K],
        off: &mut [O; K],
        dim: &mut usize,
        rd: O,
        max_stem_level: i32,
        best_dist: O,
        stack: &mut Self::Stack<O>,
    ) -> bool
    where
        Self: Sized,
        A: Axis<Coord = A>,
        O: Axis<Coord = O>
            + crate::stem_strategy::SimdSelectBestChildBlock3
            + BacktrackBlock3
            + BacktrackBlock4,
        D: crate::dist::DistanceMetric<A, Output = O>,
        Self::Stack<O>: StackTrait<O, Self>,
    {
        let _ = lower;
        let _ = upper;
        self.backtracking_traverse_step::<A, O, D, K>(
            stems,
            query,
            query_wide,
            off,
            dim,
            rd,
            max_stem_level,
            best_dist,
            stack,
        )
    }

    /// Execute backtracking query with explicit stack.
    ///
    /// Default implementation delegates to KdTree's backtracking_query_with_scratch_impl.
    /// Block-based SIMD strategies can override to use SIMD stack with block pruning.
    ///
    /// This method exists to allow strategies to customize backtracking behavior at compile time.
    #[allow(clippy::too_many_arguments)]
    #[inline(always)]
    fn backtracking_query_with_scratch<Tree, A, T, O, D, QC, LS, const K2: usize, const B: usize>(
        tree: &Tree,
        query_ctx: &mut QC,
        stack: &mut Self::Stack<O>,
        process_leaf: impl FnMut(usize, &[O; K2], &mut QC),
    ) where
        Self: Sized,
        Tree: crate::kd_tree::KdTreeAccessor<A, T, Self, LS, K2, B>
            + crate::kd_tree::KdTreeQueryOps<A, T, Self, LS, K2, B>,
        A: Axis<Coord = A>,
        T: Content,
        O: Axis<Coord = O>
            + crate::stem_strategy::SimdPrune
            + crate::stem_strategy::SimdSelectBestChildBlock3
            + BacktrackBlock3
            + BacktrackBlock4,
        D: crate::dist::DistanceMetric<A, Output = O>,
        QC: crate::kd_tree::query_context::QueryContext<A, O, K2>,
        LS: crate::LeafStrategy<A, T, Self, K2, B>,
        Self::Stack<O>: StackTrait<O, Self>,
    {
        tree.backtracking_query_with_scratch_impl::<QC, O, D>(query_ctx, stack, process_leaf);
    }

    /// Execute arithmetic-resolution backtracking query with explicit stack.
    ///
    /// Default implementation delegates to KdTree's scalar arithmetic implementation.
    /// Strategies may override this to provide a more specialized arithmetic walk.
    #[allow(clippy::too_many_arguments)]
    #[inline(always)]
    fn arithmetic_query_with_scratch<Tree, A, T, O, D, QC, LS, const K2: usize, const B: usize>(
        tree: &Tree,
        query_ctx: &mut QC,
        stack: &mut Self::Stack<O>,
        process_leaf: impl FnMut(usize, &[O; K2], &mut QC),
    ) where
        Self: Sized,
        Tree: crate::kd_tree::KdTreeAccessor<A, T, Self, LS, K2, B>
            + crate::kd_tree::KdTreeQueryOps<A, T, Self, LS, K2, B>,
        A: Axis<Coord = A>,
        T: Content,
        O: Axis<Coord = O>
            + crate::stem_strategy::SimdPrune
            + crate::stem_strategy::SimdSelectBestChildBlock3
            + BacktrackBlock3
            + BacktrackBlock4,
        D: crate::dist::DistanceMetric<A, Output = O>,
        QC: crate::kd_tree::query_context::QueryContext<A, O, K2>,
        LS: crate::LeafStrategy<A, T, Self, K2, B>,
        Self::Stack<O>: StackTrait<O, Self>,
    {
        tree.arithmetic_query_with_scratch_impl::<QC, O, D>(query_ctx, stack, process_leaf);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dist::SquaredEuclidean;
    use crate::kd_tree::query_stack::{QueryStack, QueryStackContext};

    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    struct TestStemState {
        stem_idx: usize,
        leaf_idx: usize,
        dim: usize,
        level: i32,
    }

    #[derive(Clone, Debug)]
    struct TestStemStrategy {
        state: TestStemState,
        _stems_ptr: NonNull<u8>,
    }

    unsafe impl Send for TestStemStrategy {}
    unsafe impl Sync for TestStemStrategy {}

    impl StemStrategy for TestStemStrategy {
        type DeferredState = TestStemState;
        type StackContext<A>
            = QueryStackContext<A, Self::DeferredState>
        where
            Self: Sized;
        type Stack<A>
            = QueryStack<A, Self>
        where
            Self: Sized;

        fn new(stems_ptr: NonNull<u8>) -> Self {
            Self {
                state: TestStemState {
                    stem_idx: 0,
                    leaf_idx: 0,
                    dim: 0,
                    level: 0,
                },
                _stems_ptr: stems_ptr,
            }
        }

        fn stem_idx(&self) -> usize {
            self.state.stem_idx
        }

        fn deferred_state(&self) -> Self::DeferredState {
            self.state
        }

        fn rehydrate_deferred_state(&mut self, state: Self::DeferredState) {
            self.state = state;
        }

        fn leaf_idx(&self) -> usize {
            self.state.leaf_idx
        }

        fn dim<const K: usize>(&self) -> usize {
            self.state.dim
        }

        fn level(&self) -> i32 {
            self.state.level
        }

        fn traverse<A: Axis<Coord = A>, const K: usize>(&mut self, is_right: bool) {
            self.state.stem_idx = self.state.stem_idx * 2 + 1 + usize::from(is_right);
            self.state.leaf_idx = (self.state.leaf_idx << 1) | usize::from(is_right);
            self.state.dim = (self.state.dim + 1) % 2;
            self.state.level += 1;
        }

        fn branch<A: Axis<Coord = A>, const K: usize>(&mut self) -> Self {
            let mut right = self.clone();
            self.state.stem_idx = self.state.stem_idx * 2 + 1;
            self.state.leaf_idx <<= 1;
            self.state.dim = (self.state.dim + 1) % 2;
            self.state.level += 1;

            right.state.stem_idx = right.state.stem_idx * 2 + 2;
            right.state.leaf_idx = (right.state.leaf_idx << 1) | 1;
            right.state.dim = (right.state.dim + 1) % 2;
            right.state.level += 1;
            right
        }

        fn child_indices<A: Axis<Coord = A>>(&self) -> (usize, usize) {
            (self.state.stem_idx * 2 + 1, self.state.stem_idx * 2 + 2)
        }
    }

    #[test]
    fn default_traverse_head_and_split_relative_follow_direction() {
        let mut stems = [0.0f32; 8];
        let stems_ptr = NonNull::new(stems.as_mut_ptr() as *mut u8).unwrap();

        let mut head = TestStemStrategy::new(stems_ptr);
        head.traverse_head::<f32, 2>(true);
        assert_eq!(head.stem_idx(), 2);
        assert_eq!(head.leaf_idx(), 1);
        assert_eq!(head.dim::<2>(), 1);
        assert_eq!(head.level(), 1);

        let left_first = TestStemStrategy::new(stems_ptr).split_relative::<f32, 2>(false);
        assert_eq!(left_first.0.stem_idx(), 1);
        assert_eq!(left_first.1.stem_idx(), 2);
        assert_eq!(left_first.0.leaf_idx(), 0);
        assert_eq!(left_first.1.leaf_idx(), 1);

        let right_first = TestStemStrategy::new(stems_ptr).split_relative::<f32, 2>(true);
        assert_eq!(right_first.0.stem_idx(), 2);
        assert_eq!(right_first.1.stem_idx(), 1);
        assert_eq!(right_first.0.leaf_idx(), 1);
        assert_eq!(right_first.1.leaf_idx(), 0);
    }

    #[test]
    fn default_backtracking_traverse_step_with_bounds_delegates_to_scalar_step() {
        let mut stems = [5.0f32, 0.0, 0.0, 0.0];
        let mut strat = TestStemStrategy::new(NonNull::new(stems.as_mut_ptr() as *mut u8).unwrap());
        let query = [7.0f32, 1.0f32];
        let query_wide = [7.0f32, 1.0f32];
        let mut lower = [f32::NEG_INFINITY; 2];
        let mut upper = [f32::INFINITY; 2];
        let mut off = [0.0f32; 2];
        let mut dim = 0usize;
        let mut stack = QueryStack::<f32, TestStemStrategy>::default();

        let should_continue = strat
            .backtracking_traverse_step_with_bounds::<f32, f32, SquaredEuclidean<f32>, 2>(
                &stems,
                &query,
                &query_wide,
                &mut lower,
                &mut upper,
                &mut off,
                &mut dim,
                0.0,
                3,
                100.0,
                &mut stack,
            );

        assert!(should_continue);
        assert_eq!(strat.stem_idx(), 2);
        assert_eq!(strat.leaf_idx(), 1);
        assert_eq!(strat.level(), 1);
        assert_eq!(strat.dim::<2>(), 1);
        assert_eq!(dim, 1);
        assert_eq!(off, [0.0, 0.0]);
        assert_eq!(lower, [f32::NEG_INFINITY, f32::NEG_INFINITY]);
        assert_eq!(upper, [f32::INFINITY, f32::INFINITY]);

        let ctx = stack.pop().expect("far child should be pushed");
        let (stem_state, restore_dim, old_off, rd) = ctx.into_parts_with_restore_dim();
        assert_eq!(stem_state.stem_idx, 1);
        assert_eq!(stem_state.leaf_idx, 0);
        assert_eq!(stem_state.level, 1);
        assert_eq!(stem_state.dim, 1);
        assert_eq!(restore_dim, Some(0));
        assert_eq!(old_off, 2.0);
        assert_eq!(rd, 4.0);
    }

    #[cfg(feature = "test_utils")]
    #[test]
    fn default_backtracking_traverse_step_emits_exact_query_trace_event() {
        let mut stems = [5.0f64, 0.0, 0.0, 0.0];
        let mut strat = TestStemStrategy::new(NonNull::new(stems.as_mut_ptr() as *mut u8).unwrap());
        let query = [7.0f64, 1.0f64];
        let query_wide = [7.0f64, 1.0f64];
        let mut off = [0.0f64; 2];
        let mut dim = 0usize;
        let mut stack = QueryStack::<f64, TestStemStrategy>::default();

        crate::test_utils::exact_query_trace::set_enabled(true);

        let should_continue = strat
            .backtracking_traverse_step::<f64, f64, SquaredEuclidean<f64>, 2>(
                &stems,
                &query,
                &query_wide,
                &mut off,
                &mut dim,
                0.0,
                3,
                100.0,
                &mut stack,
            );

        let events = crate::test_utils::exact_query_trace::snapshot();
        crate::test_utils::exact_query_trace::set_enabled(false);

        assert!(should_continue);
        assert_eq!(events.len(), 1);

        match &events[0] {
            crate::test_utils::exact_query_trace::ExactQueryTraceEvent::ScalarStep {
                stem_idx,
                level,
                dim,
                pivot,
                query_elem,
                is_right_child,
                old_off,
                new_off,
                rd,
                rd_far,
                near_stem_idx,
                far_stem_idx,
            } => {
                assert_eq!(*stem_idx, 0);
                assert_eq!(*level, 0);
                assert_eq!(*dim, 0);
                assert_eq!(*pivot, 5.0);
                assert_eq!(*query_elem, 7.0);
                assert!(*is_right_child);
                assert_eq!(*old_off, 0.0);
                assert_eq!(*new_off, 2.0);
                assert_eq!(*rd, 0.0);
                assert_eq!(*rd_far, 4.0);
                assert_eq!(*near_stem_idx, 2);
                assert_eq!(*far_stem_idx, 1);
            }
            other => panic!("expected ScalarStep trace event, got {other:?}"),
        }
    }
}