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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::{
    cmp::Ordering,
    hash::{Hash, Hasher},
};

use glam::Vec2;

/// A wrapper for Vec2Ord that implements [`Ord`], [`Eq`], and [`Hash`] traits.
#[derive(Debug, Copy, Clone)]
pub struct Vec2Ord(pub Vec2);

impl PartialOrd for Vec2Ord {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Vec2Ord {
    fn cmp(&self, other: &Self) -> Ordering {
        cmp(self.0.x, other.0.x).then_with(|| cmp(self.0.y, other.0.y))
    }
}

impl PartialEq for Vec2Ord {
    fn eq(&self, other: &Self) -> bool {
        eq(self.0.x, other.0.x) && eq(self.0.y, other.0.y)
    }
}

impl Eq for Vec2Ord {}

impl Hash for Vec2Ord {
    fn hash<H: Hasher>(&self, state: &mut H) {
        hash(self.0.x, state);
        hash(self.0.y, state);
    }
}

impl From<Vec2> for Vec2Ord {
    fn from(vec: Vec2) -> Self {
        Self(vec)
    }
}

fn eq(a: f32, b: f32) -> bool {
    (a.is_nan() && b.is_nan()) || a == b
}

fn cmp(a: f32, b: f32) -> Ordering {
    a.partial_cmp(&b).unwrap_or_else(|| {
        if a.is_nan() && !b.is_nan() {
            Ordering::Less
        } else if !a.is_nan() && b.is_nan() {
            Ordering::Greater
        } else {
            Ordering::Equal
        }
    })
}

fn hash<H: Hasher>(value: f32, state: &mut H) {
    if value.is_nan() {
        // Ensure all NaN representations hash to the same value
        state.write(&f32::to_ne_bytes(f32::NAN));
    } else if value == 0.0 {
        // Ensure both zeroes hash to the same value
        state.write(&f32::to_ne_bytes(0.0f32));
    } else {
        state.write(&f32::to_ne_bytes(value));
    }
}