Skip to main content

khive_types/
vector.rs

1//! Vector similarity metric.
2
3/// Distance metric for vector similarity search.
4///
5/// # Variants
6/// - `Cosine`: `1 - cosine_similarity`. Value in [0, 2] for unit vectors.
7/// - `Dot`: dot product (negated for min-heap; higher dot = lower distance).
8/// - `L2`: Euclidean (L2) distance.
9#[non_exhaustive]
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
13pub enum DistanceMetric {
14    Cosine,
15    Dot,
16    L2,
17}
18
19impl Default for DistanceMetric {
20    #[inline]
21    fn default() -> Self {
22        Self::Cosine
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29
30    #[test]
31    fn default_is_cosine() {
32        assert_eq!(DistanceMetric::default(), DistanceMetric::Cosine);
33    }
34}