vec_vp_tree/dist/mod.rs
1// Copyright 2016 Austin Bonander
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! Distance-function trait and helper types for `VpTree`.
9
10pub mod num;
11
12#[cfg(feature = "strsim")]
13pub mod string;
14
15/// Describes a type which can act as a distance-function for `T`.
16///
17/// Implemented for `Fn(&T, &T) -> u64`.
18///
19/// Default implementations are provided for common numeric types.
20pub trait DistFn<T> {
21 /// Return the distance between `left` and `right`.
22 ///
23 /// ## Note
24 /// It is a logic error for this method to return different values for the same operands,
25 /// regardless of order (i.e. it is required to be idempotent and commutative).
26 fn dist(&self, left: &T, right: &T) -> u64;
27}
28
29/// Simply calls `(self)(left, right)`
30impl<T, F> DistFn<T> for F
31 where F: Fn(&T, &T) -> u64
32{
33 fn dist(&self, left: &T, right: &T) -> u64 {
34 (self)(left, right)
35 }
36}
37
38/// Trait describing a type where a default distance function is known.
39pub trait KnownDist: Sized {
40 /// The known distance function for `Self`.
41 type DistFn: DistFn<Self> + Sized;
42
43 /// Return an instance of `DistFn`.
44 fn dist_fn() -> Self::DistFn;
45}