Trait space::Metric[][src]

pub trait Metric<P> {
    type Unit: Unsigned + Ord + Copy;
    fn distance(&self, a: &P, b: &P) -> Self::Unit;
}
Expand description

This trait is implemented for metrics that form a metric space. It is primarily used for keys in nearest neighbor searches. When implementing this trait, you should always choose the smallest unsigned integer that represents your metric space.

It is important that all metrics that implement this trait satisfy the triangle inequality. This requirement basically means that the sum of distances that start at a point A and end at a point B can never be less than the distance from A to B directly. Note that the metric is required to be an unsigned integer, as distances can only be positive and must be fully ordered.

Floating point numbers can be converted to integer metrics by being interpreted as integers by design, although some special patterns (like NaN) do not fit into this model. To be interpreted as an unsigned integer, the float must be positive zero, subnormal, normal, or positive infinity. Any NaN needs to be dealt with before converting into a metric, as they do NOT satisfy the triangle inequality, and will lead to errors. You may want to check for positive infinity as well depending on your use case.

Example

struct AbsDiff;

impl space::Metric<f64> for AbsDiff {
    type Unit = u64;

    fn distance(&self, &a: &f64, &b: &f64) -> Self::Unit {
        let delta = (a - b).abs();
        debug_assert!(!delta.is_nan());
        delta.to_bits()
    }
}

Associated Types

Required methods

Implementors