scirs2_spatial/distance/
chebyshevdistance_traits.rs

1//! # ChebyshevDistance - Trait Implementations
2//!
3//! This module contains trait implementations for `ChebyshevDistance`.
4//!
5//! ## Implemented Traits
6//!
7//! - `Default`
8//! - `Distance`
9//!
10//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
11
12use scirs2_core::numeric::Float;
13
14use super::functions::prefetch_read;
15use super::functions::Distance;
16use super::types::ChebyshevDistance;
17
18impl<T: Float> Default for ChebyshevDistance<T> {
19    fn default() -> Self {
20        Self::new()
21    }
22}
23
24impl<T: Float + Send + Sync> Distance<T> for ChebyshevDistance<T> {
25    fn distance(&self, a: &[T], b: &[T]) -> T {
26        if a.len() != b.len() {
27            return T::nan();
28        }
29        if let Some(result) = Self::try_simd_f64(a, b) {
30            return result;
31        }
32        if let Some(result) = Self::try_simd_f32(a, b) {
33            return result;
34        }
35        let len = a.len();
36        let mut max_diff = T::zero();
37        let chunks = len / 4;
38        for i in 0..chunks {
39            let base = i * 4;
40            if base + 8 < len {
41                let end_idx = (base + 8).min(len);
42                prefetch_read(&a[base + 4..end_idx]);
43                prefetch_read(&b[base + 4..end_idx]);
44            }
45            let diff0 = (a[base] - b[base]).abs();
46            let diff1 = (a[base + 1] - b[base + 1]).abs();
47            let diff2 = (a[base + 2] - b[base + 2]).abs();
48            let diff3 = (a[base + 3] - b[base + 3]).abs();
49            let max01 = diff0.max(diff1);
50            let max23 = diff2.max(diff3);
51            let chunk_max = max01.max(max23);
52            max_diff = max_diff.max(chunk_max);
53        }
54        for i in (chunks * 4)..len {
55            let diff = (a[i] - b[i]).abs();
56            if diff > max_diff {
57                max_diff = diff;
58            }
59        }
60        max_diff
61    }
62    fn min_distance_point_rectangle(&self, point: &[T], mins: &[T], maxes: &[T]) -> T {
63        let mut max_diff = T::zero();
64        for i in 0..point.len() {
65            let coord = point[i];
66            let min_val = mins[i];
67            let max_val = maxes[i];
68            let clamped = coord.max(min_val).min(max_val);
69            let diff = (coord - clamped).abs();
70            max_diff = max_diff.max(diff);
71        }
72        max_diff
73    }
74}