linfa_nn/
heap_elem.rs

1use std::cmp::{Ordering, Reverse};
2
3use linfa::Float;
4use noisy_float::{checkers::FiniteChecker, NoisyFloat};
5
6#[derive(Debug, Clone)]
7pub(crate) struct HeapElem<D: Ord, T> {
8    pub(crate) dist: D,
9    pub(crate) elem: T,
10}
11
12impl<D: Ord, T> PartialEq for HeapElem<D, T> {
13    fn eq(&self, other: &Self) -> bool {
14        self.dist.eq(&other.dist)
15    }
16}
17impl<D: Ord, T> Eq for HeapElem<D, T> {}
18
19#[allow(clippy::non_canonical_partial_ord_impl)]
20impl<D: Ord, T> PartialOrd for HeapElem<D, T> {
21    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
22        self.dist.partial_cmp(&other.dist)
23    }
24}
25
26impl<D: Ord, T> Ord for HeapElem<D, T> {
27    fn cmp(&self, other: &Self) -> Ordering {
28        self.dist.cmp(&other.dist)
29    }
30}
31
32pub(crate) type MinHeapElem<F, T> = HeapElem<Reverse<NoisyFloat<F, FiniteChecker>>, T>;
33
34impl<F: Float, T> MinHeapElem<F, T> {
35    pub(crate) fn new(dist: F, elem: T) -> Self {
36        Self {
37            dist: Reverse(NoisyFloat::new(dist)),
38            elem,
39        }
40    }
41}
42
43pub(crate) type MaxHeapElem<F, T> = HeapElem<NoisyFloat<F, FiniteChecker>, T>;
44
45impl<F: Float, T> MaxHeapElem<F, T> {
46    pub(crate) fn new(dist: F, elem: T) -> Self {
47        Self {
48            dist: NoisyFloat::new(dist),
49            elem,
50        }
51    }
52}