kdtree/
heap_element.rs

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
use num_traits::Float;
use std::cmp::Ordering;

pub struct HeapElement<A, T> {
    pub distance: A,
    pub element: T,
}

impl<A: Float, T> Ord for HeapElement<A, T> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(other).unwrap_or(Ordering::Equal)
    }
}

impl<A: Float, T> PartialOrd for HeapElement<A, T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.distance.partial_cmp(&other.distance)
    }
}

impl<A: Float, T> PartialOrd<A> for HeapElement<A, T>
where
    HeapElement<A, T>: PartialEq<A>,
{
    fn partial_cmp(&self, other: &A) -> Option<Ordering> {
        self.distance.partial_cmp(other)
    }
}

impl<A: Float, T> Eq for HeapElement<A, T> {}

impl<A: Float, T> PartialEq for HeapElement<A, T> {
    fn eq(&self, other: &Self) -> bool {
        self.distance == other.distance
    }
}

impl<A: Float, T> PartialEq<A> for HeapElement<A, T> {
    fn eq(&self, other: &A) -> bool {
        self.distance == *other
    }
}

impl<A: Float, T> From<HeapElement<A, T>> for (A, T) {
    fn from(e: HeapElement<A, T>) -> Self {
        (e.distance, e.element)
    }
}