summaryrefslogtreecommitdiffstats
path: root/src/distance.rs
blob: 9ff9fd49c229e0a9383124e276fd249e0e80aa0e (plain)
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
//! Abstract notions of distance.

use num_traits::{Num, NumAssign, Signed};

/// A number type suitable for distance values.
///
/// This trait is automatically implemented for all types that support the required operations.
pub trait Value: Copy + Num + NumAssign + Signed + PartialOrd {}

/// Blanket [Value] implementation.
impl<T: Num + NumAssign + Signed + Copy + PartialOrd> Value for T {}

/// A distance between two points.
///
/// An implementation may be an actual numerical distance, or an [order embedding] of the true
/// distance.  This allows for optimizations whenever distances can be compared more efficiently
/// than their exact values can be computed, as is the case for [Euclidean distance].  Implementors
/// must satisfy, for all distances `x` and `y`:
///
/// * `x < y` iff `x.value() < y.value()`
/// * `x.value() < y` iff `x.value() < y.value()`
/// * `x < y.value()` iff `x.value() < y.value()`
///
/// [order embedding]: https://en.wikipedia.org/wiki/Order_embedding
/// [Euclidean distance]: crate::euclid::EuclideanDistance
pub trait Distance
where
    Self: Copy,
    Self: Into<<Self as Distance>::Value>,
    Self: PartialOrd<<Self as Distance>::Value>,
    <Self as Distance>::Value: PartialOrd<Self>,
    Self: PartialOrd,
{
    /// The type of actual numerical distances.
    type Value: Value;

    /// Get the real numerical value of this distance.
    fn value(self) -> Self::Value {
        self.into()
    }
}

/// Any numerical distance value can be a [Distance].
impl<T: Value> Distance for T {
    type Value = T;
}

/// A space with some notion of distance between points.
///
/// Distances in this space don't need to obey any particular rules like symmetry or the [triangle
/// inequality].  However, spaces that satisfy those rules, at least approximately, often allow for
/// more accurate and efficient searches.
///
/// Type parameters:
///
/// * `T`: The type to compare against.
///
/// [triangle inequality]: https://en.wikipedia.org/wiki/Triangle_inequality
pub trait Proximity<T: ?Sized = Self> {
    /// The type that represents distances.
    type Distance: Distance;

    /// Calculate the distance between this point and another one.
    fn distance(&self, other: &T) -> Self::Distance;
}

// See https://github.com/rust-lang/rust/issues/38078
/// Shorthand for `K::Distance::Value`.
pub type DistanceValue<K, V = K> = <<K as Proximity<V>>::Distance as Distance>::Value;

/// Blanket [Proximity] implementation for references.
impl<'k, 'v, K: Proximity<V>, V> Proximity<&'v V> for &'k K {
    type Distance = K::Distance;

    fn distance(&self, other: &&'v V) -> Self::Distance {
        (*self).distance(*other)
    }
}

/// Marker trait for [metric spaces].
///
/// A metric must be symmetric and obey the [triangle inequality].  More precisely, let `x`, `y`,
/// and `z` be any elements of a metric space, and let `d(x, y) = x.distance(y).value()`.  Then the
/// following rules must hold:
///
/// * `d(x, x) == 0`,
/// * `d(x, y) == d(y, z)` (symmetry), and
/// * `d(x, z) <= d(x, y) + d(y, z)` (triangle inequality).
///
/// Those conditions also imply the following condition:
///
/// * `d(x, y) >= 0` (non-negativity)
///
/// Because we do not prohibit `d(x, y) == 0` for distinct `x` and `y`, these spaces are more
/// properly known as [pseudometric spaces].  This distinction is usually unimportant.
///
/// [metric spaces]: https://en.wikipedia.org/wiki/Metric_space
/// [triangle inequality]: https://en.wikipedia.org/wiki/Triangle_inequality
/// [pseudometric spaces]: https://en.wikipedia.org/wiki/Pseudometric_space
pub trait Metric<T: ?Sized = Self>: Proximity<T> {}

/// Blanket [Metric] implementation for references.
impl<'k, 'v, K: Metric<V>, V> Metric<&'v V> for &'k K {}