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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use crate::{
    Negative, NegativeFinite, NonNaN, NonNaNFinite, NonZeroNonNaN, NonZeroNonNaNFinite, Positive,
    PositiveFinite, StrictlyNegative, StrictlyNegativeFinite, StrictlyPositive,
    StrictlyPositiveFinite,
};

// > When implementing both Hash and Eq, it is important that the following property holds:
// > `k1 == k2 -> hash(k1) == hash(k2)`
// This is sound because `NaN` is not a possible value.
// https://doc.rust-lang.org/core/hash/trait.Hash.html

const ZERO_BITS_F32: u32 = 0;
const ZERO_BITS_F64: u64 = 0;

impl core::hash::Hash for NonNaN<f32> {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        let bits = if self.0 == 0.0 {
            // `+0.0` and `-0.0` are equal to they must have the same hash
            ZERO_BITS_F32
        } else {
            self.0.to_bits()
        };

        bits.hash(state);
    }
}

impl core::hash::Hash for NonNaN<f64> {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        let bits = if self.0 == 0.0 {
            // `+0.0` and `-0.0` are equal to they must have the same hash
            ZERO_BITS_F64
        } else {
            self.0.to_bits()
        };

        bits.hash(state);
    }
}

impl core::hash::Hash for NonNaNFinite<f32> {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        let bits = if self.0 == 0.0 {
            // `+0.0` and `-0.0` are equal to they must have the same hash
            ZERO_BITS_F32
        } else {
            self.0.to_bits()
        };

        bits.hash(state);
    }
}

impl core::hash::Hash for NonNaNFinite<f64> {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        let bits = if self.0 == 0.0 {
            // `+0.0` and `-0.0` are equal to they must have the same hash
            ZERO_BITS_F64
        } else {
            self.0.to_bits()
        };

        bits.hash(state);
    }
}

macro_rules! impl_hash {
    ($test:ident, $type:ident) => {
        impl core::hash::Hash for $type<f32> {
            #[inline]
            fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
                self.0.to_bits().hash(state);
            }
        }

        impl core::hash::Hash for $type<f64> {
            #[inline]
            fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
                self.0.to_bits().hash(state);
            }
        }

        #[cfg(test)]
        mod $test {
            extern crate std;
            use crate::*;
            use std::vec::Vec; // Required for the tests to compile in no_std mode

            #[test]
            fn f32() {
                let mut hash_set = std::collections::HashSet::new();

                let neg_zero = $type::<f32>::new(-0.0);
                let pos_zero = $type::<f32>::new(0.0);
                let accept_both_zeroes = neg_zero.is_ok() && pos_zero.is_ok();
                if accept_both_zeroes {
                    let pos_one = $type::<f32>::new(1.0);
                    let neg_one = $type::<f32>::new(-1.0);

                    hash_set.insert(neg_zero.unwrap());
                    hash_set.insert(pos_zero.unwrap());
                    let mut count = 1; // Zeros are equal

                    if pos_one.is_ok() {
                        hash_set.insert(pos_one.unwrap());
                        count += 1;
                    }

                    if neg_one.is_ok() {
                        hash_set.insert(neg_one.unwrap());
                        count += 1;
                    }

                    assert_eq!(hash_set.len(), count);
                }

                let values = tf32::TEST_VALUES
                    .iter()
                    .map(|&x| tf32::$type::new(x))
                    .filter_map(|x| x.ok())
                    .collect::<Vec<_>>();

                let mut distincs = Vec::new();
                for x in values.iter() {
                    if !distincs.contains(x) {
                        distincs.push(*x);
                    }
                }

                let mut hash_set = std::collections::HashSet::new();

                for value in &values {
                    hash_set.insert(value);
                }

                assert_eq!(hash_set.len(), distincs.len());
            }

            #[test]
            fn f64() {
                let mut hash_set = std::collections::HashSet::new();

                let neg_zero = $type::<f32>::new(-0.0);
                let pos_zero = $type::<f32>::new(0.0);
                let accept_both_zeroes = neg_zero.is_ok() && pos_zero.is_ok();
                if accept_both_zeroes {
                    let pos_one = $type::<f32>::new(1.0);
                    let neg_one = $type::<f32>::new(-1.0);

                    hash_set.insert(neg_zero.unwrap());
                    hash_set.insert(pos_zero.unwrap());
                    let mut count = 1; // Zeros are equal

                    if pos_one.is_ok() {
                        hash_set.insert(pos_one.unwrap());
                        count += 1;
                    }

                    if neg_one.is_ok() {
                        hash_set.insert(neg_one.unwrap());
                        count += 1;
                    }

                    assert_eq!(hash_set.len(), count);
                }

                let values = tf64::TEST_VALUES
                    .iter()
                    .map(|&x| tf64::$type::new(x))
                    .filter_map(|x| x.ok())
                    .collect::<Vec<_>>();

                let mut distincs = Vec::new();
                for x in values.iter() {
                    if !distincs.contains(x) {
                        distincs.push(*x);
                    }
                }

                let mut hash_set = std::collections::HashSet::new();

                for value in &values {
                    hash_set.insert(value);
                }

                assert_eq!(hash_set.len(), distincs.len());
            }
        }
    };
}

impl_hash!(non_zero_non_nan, NonZeroNonNaN);
impl_hash!(non_zero_non_nan_finite, NonZeroNonNaNFinite);
impl_hash!(positive, Positive);
impl_hash!(negative, Negative);
impl_hash!(positive_finite, PositiveFinite);
impl_hash!(negative_finite, NegativeFinite);
impl_hash!(strictly_positive, StrictlyPositive);
impl_hash!(strictly_negative, StrictlyNegative);
impl_hash!(strictly_positive_finite, StrictlyPositiveFinite);
impl_hash!(strictly_negative_finite, StrictlyNegativeFinite);