src/loc.rs

branch
dev
changeset 64
4f6ca107ccb1
parent 63
f7b87d84864d
child 80
f802ddbabcfc
child 82
981069ef919b
child 86
d5b0e496b72f
--- a/src/loc.rs	Sun Dec 22 14:54:46 2024 -0500
+++ b/src/loc.rs	Sun Dec 22 15:30:34 2024 -0500
@@ -448,9 +448,9 @@
         self.iter().fold(F::ZERO, |m, &v| m + v * v)
     }
 
-    fn dist2_squared(&self, other : &Self) -> F {
+    fn dist2_squared<I : Instance<Self>>(&self, other : I) -> F {
         self.iter()
-            .zip(other.iter())
+            .zip(other.ref_instance().iter())
             .fold(F::ZERO, |m, (&v, &w)| { let d = v - w; m + d * d })
     }
 
@@ -465,10 +465,11 @@
     }
 
     #[inline]
-    fn dist2(&self, other : &Self) -> F {
+    fn dist2<I : Instance<Self>>(&self, other : I) -> F {
         // Optimisation for N==1 that avoids squaring and square rooting.
+        let otherr = other.ref_instance();
         if N==1 {
-            unsafe { *self.0.get_unchecked(0) - *other.0.get_unchecked(0) }.abs()
+            unsafe { *self.0.get_unchecked(0) - *otherr.0.get_unchecked(0) }.abs()
         } else {
             self.dist2_squared(other).sqrt()
         }
@@ -603,7 +604,7 @@
 
 impl<F : Float, const N : usize> Dist<F, L2> for Loc<F, N> {
     #[inline]
-    fn dist(&self, other : &Self, _ : L2) -> F { self.dist2(other) }
+    fn dist<I : Instance<Self>>(&self, other : I, _ : L2) -> F { self.dist2(other) }
 }
 
 /* Implemented automatically as Euclidean.
@@ -628,9 +629,9 @@
 
 impl<F : Float, const N : usize> Dist<F, L1> for Loc<F, N> {
     #[inline]
-    fn dist(&self, other : &Self, _ : L1) -> F {
+    fn dist<I : Instance<Self>>(&self, other : I, _ : L1) -> F {
         self.iter()
-            .zip(other.iter())
+            .zip(other.ref_instance().iter())
             .fold(F::ZERO, |m, (&v, &w)| m + (v-w).abs() )
     }
 }
@@ -653,9 +654,9 @@
 
 impl<F : Float, const N : usize> Dist<F, Linfinity> for Loc<F, N> {
     #[inline]
-    fn dist(&self, other : &Self, _ : Linfinity) -> F {
+    fn dist<I : Instance<Self>>(&self, other : I, _ : Linfinity) -> F {
         self.iter()
-            .zip(other.iter())
+            .zip(other.ref_instance().iter())
             .fold(F::ZERO, |m, (&v, &w)| m.max((v-w).abs()))
     }
 }

mercurial