Skip to content

Commit c773a82

Browse files
authored
Add serde serialize/deserialize. (rust-ml#296)
1 parent 3978394 commit c773a82

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

algorithms/linfa-nn/src/distance.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ use linfa::Float;
22
use ndarray::{Array2, ArrayBase, ArrayView, Axis, Data, Dimension, Ix2, Zip};
33
use ndarray_stats::DeviationExt;
44

5+
#[cfg(feature = "serde")]
6+
use serde_crate::{Deserialize, Serialize};
7+
58
/// A distance function that can be used in spatial algorithms such as nearest neighbour.
69
pub trait Distance<F: Float>: Clone + Send + Sync + Unpin {
710
/// 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 {
3336
}
3437

3538
/// 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+
)]
3644
#[derive(Debug, Clone, PartialEq, Eq)]
3745
pub struct L1Dist;
3846
impl<F: Float> Distance<F> for L1Dist {
@@ -43,6 +51,11 @@ impl<F: Float> Distance<F> for L1Dist {
4351
}
4452

4553
/// 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+
)]
4659
#[derive(Debug, Clone, PartialEq, Eq)]
4760
pub struct L2Dist;
4861
impl<F: Float> Distance<F> for L2Dist {
@@ -68,6 +81,11 @@ impl<F: Float> Distance<F> for L2Dist {
6881
}
6982

7083
/// 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+
)]
7189
#[derive(Debug, Clone, PartialEq, Eq)]
7290
pub struct LInfDist;
7391
impl<F: Float> Distance<F> for LInfDist {
@@ -78,6 +96,11 @@ impl<F: Float> Distance<F> for LInfDist {
7896
}
7997

8098
/// 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+
)]
81104
#[derive(Debug, Clone, PartialEq)]
82105
pub struct LpDist<F: Float>(pub F);
83106
impl<F: Float> LpDist<F> {

0 commit comments

Comments
 (0)