@@ -2,6 +2,9 @@ use linfa::Float;
2
2
use ndarray:: { Array2 , ArrayBase , ArrayView , Axis , Data , Dimension , Ix2 , Zip } ;
3
3
use ndarray_stats:: DeviationExt ;
4
4
5
+ #[ cfg( feature = "serde" ) ]
6
+ use serde_crate:: { Deserialize , Serialize } ;
7
+
5
8
/// A distance function that can be used in spatial algorithms such as nearest neighbour.
6
9
pub trait Distance < F : Float > : Clone + Send + Sync + Unpin {
7
10
/// Computes the distance between two points. For most spatial algorithms to work correctly,
@@ -33,6 +36,11 @@ pub trait Distance<F: Float>: Clone + Send + Sync + Unpin {
33
36
}
34
37
35
38
/// L1 or [Manhattan](https://en.wikipedia.org/wiki/Taxicab_geometry) distance
39
+ #[ cfg_attr(
40
+ feature = "serde" ,
41
+ derive( Serialize , Deserialize ) ,
42
+ serde( crate = "serde_crate" )
43
+ ) ]
36
44
#[ derive( Debug , Clone , PartialEq , Eq ) ]
37
45
pub struct L1Dist ;
38
46
impl < F : Float > Distance < F > for L1Dist {
@@ -43,6 +51,11 @@ impl<F: Float> Distance<F> for L1Dist {
43
51
}
44
52
45
53
/// L2 or [Euclidean](https://en.wikipedia.org/wiki/Euclidean_distance) distance
54
+ #[ cfg_attr(
55
+ feature = "serde" ,
56
+ derive( Serialize , Deserialize ) ,
57
+ serde( crate = "serde_crate" )
58
+ ) ]
46
59
#[ derive( Debug , Clone , PartialEq , Eq ) ]
47
60
pub struct L2Dist ;
48
61
impl < F : Float > Distance < F > for L2Dist {
@@ -68,6 +81,11 @@ impl<F: Float> Distance<F> for L2Dist {
68
81
}
69
82
70
83
/// L-infinte or [Chebyshev](https://en.wikipedia.org/wiki/Chebyshev_distance) distance
84
+ #[ cfg_attr(
85
+ feature = "serde" ,
86
+ derive( Serialize , Deserialize ) ,
87
+ serde( crate = "serde_crate" )
88
+ ) ]
71
89
#[ derive( Debug , Clone , PartialEq , Eq ) ]
72
90
pub struct LInfDist ;
73
91
impl < F : Float > Distance < F > for LInfDist {
@@ -78,6 +96,11 @@ impl<F: Float> Distance<F> for LInfDist {
78
96
}
79
97
80
98
/// L-p or [Minkowsky](https://en.wikipedia.org/wiki/Minkowski_distance) distance
99
+ #[ cfg_attr(
100
+ feature = "serde" ,
101
+ derive( Serialize , Deserialize ) ,
102
+ serde( crate = "serde_crate" )
103
+ ) ]
81
104
#[ derive( Debug , Clone , PartialEq ) ]
82
105
pub struct LpDist < F : Float > ( pub F ) ;
83
106
impl < F : Float > LpDist < F > {
0 commit comments