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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//! We use the Mersenne prime 2^127-1 (i128::MAX) as the main modulo, which maximize the space of available hashing slots.
//! (The largest Mersenne prime under 2^64 is only 2^61-1, so we use u128 for hashing which is also future proof).
//!
//! The basic algorithm is similar to what is used in Python (see https://docs.python.org/3.8/library/stdtypes.html#hashing-of-numeric-types),
//! specifically if the numerically consistent hash function is denoted as num_hash, then:
//! - for an integer n: num_hash(n) = sgn(n) * (|n| % M127)
//! - for a rational number n/d (including floating numbers): sgn(n/d) * num_hash(|n|) * (num_hash(|d|)^-1 mod M127)
//! - for special values: num_hash(NaN) and num_hash(±∞) are specially chosen such that it won't overlap with normal numbers.

use crate::NumHash;

use core::hash::{Hash, Hasher};
use num_modular::{FixedMersenneInt, ModularAbs, ModularInteger};

// we use 2^127 - 1 (a Mersenne prime) as modulus
type MInt = FixedMersenneInt<127, 1>;
const M127: i128 = i128::MAX;
const M127U: u128 = M127 as u128;
const M127D: u128 = M127U + M127U;
const HASH_INF: i128 = i128::MAX; // 2^127 - 1
const HASH_NEGINF: i128 = i128::MIN + 1; // -(2^127 - 1)
const HASH_NAN: i128 = i128::MIN; // -2^127

#[cfg(feature = "num-complex")]
const PROOT: u128 = i32::MAX as u128; // a Mersenne prime

// TODO (v2.0): Use the coefficients of the characteristic polynomial to represent a number. By this way
//              all algebraic numbers can be represented including complex and quadratic numbers.

// Case1: directly hash the i128 and u128 number (mod M127)
impl NumHash for i128 {
    #[inline]
    fn num_hash<H: Hasher>(&self, state: &mut H) {
        const MINP1: i128 = i128::MIN + 1;
        match *self {
            i128::MAX | MINP1 => 0i128.hash(state),
            i128::MIN => (-1i128).hash(state),
            u => u.hash(state),
        }
    }
}
impl NumHash for u128 {
    #[inline]
    fn num_hash<H: Hasher>(&self, state: &mut H) {
        match *self {
            u128::MAX => 1i128.hash(state),
            M127D => 0i128.hash(state),
            u if u >= M127U => ((u - M127U) as i128).hash(state),
            u => (u as i128).hash(state),
        }
    }
}

// Case2: convert other integers to 64 bit integer
macro_rules! impl_hash_for_small_int {
    ($($signed:ty)*) => ($(
        impl NumHash for $signed {
            #[inline]
            fn num_hash<H: Hasher>(&self, state: &mut H) {
                (&(*self as i128)).hash(state) // these integers are always smaller than M127
            }
        }
    )*);
}
impl_hash_for_small_int! { i8 i16 i32 i64 u8 u16 u32 u64}

impl NumHash for usize {
    #[inline]
    fn num_hash<H: Hasher>(&self, state: &mut H) {
        #[cfg(target_pointer_width = "32")]
        return (&(*self as u32)).num_hash(state);
        #[cfg(target_pointer_width = "64")]
        return (&(*self as u64)).num_hash(state);
    }
}

impl NumHash for isize {
    #[inline]
    fn num_hash<H: Hasher>(&self, state: &mut H) {
        #[cfg(target_pointer_width = "32")]
        return (&(*self as i32)).num_hash(state);
        #[cfg(target_pointer_width = "64")]
        return (&(*self as i64)).num_hash(state);
    }
}

#[cfg(feature = "num-bigint")]
mod _num_bigint {
    use super::*;
    use num_bigint::{BigInt, BigUint};
    use num_traits::ToPrimitive;

    impl NumHash for BigUint {
        fn num_hash<H: Hasher>(&self, state: &mut H) {
            (self % BigUint::from(M127U)).to_i128().unwrap().hash(state)
        }
    }
    impl NumHash for BigInt {
        fn num_hash<H: Hasher>(&self, state: &mut H) {
            (self % BigInt::from(M127)).to_i128().unwrap().hash(state)
        }
    }
}

// Case3: for rational(a, b) including floating numbers, the hash is `hash(a * b^-1 mod M127)` (b > 0)
trait FloatHash {
    // Calculate mantissa * exponent^-1 mod M127
    fn fhash(&self) -> i128;
}

impl FloatHash for f32 {
    fn fhash(&self) -> i128 {
        let bits = self.to_bits();
        let sign_bit = bits >> 31;
        let mantissa_bits = bits & 0x7fffff;
        let mut exponent: i16 = ((bits >> 23) & 0xff) as i16;

        if exponent == 0xff {
            // deal with special floats
            if mantissa_bits != 0 {
                // nan
                HASH_NAN
            } else if sign_bit > 0 {
                HASH_NEGINF // -inf
            } else {
                HASH_INF // inf
            }
        } else {
            // then deal with normal floats
            let mantissa = if exponent == 0 {
                mantissa_bits << 1
            } else {
                mantissa_bits | 0x800000
            };
            exponent -= 0x7f + 23;

            // calculate hash
            let mantissa = MInt::new(mantissa as u128, &M127U);
            // m * 2^e mod M127 = m * 2^(e mod 127) mod M127
            let pow = mantissa.convert(1 << exponent.absm(&127));
            let v = mantissa * pow;
            v.residue() as i128 * if sign_bit == 0 { 1 } else { -1 }
        }
    }
}

impl NumHash for f32 {
    fn num_hash<H: Hasher>(&self, state: &mut H) {
        self.fhash().num_hash(state)
    }
}

impl FloatHash for f64 {
    fn fhash(&self) -> i128 {
        let bits = self.to_bits();
        let sign_bit = bits >> 63;
        let mantissa_bits = bits & 0xfffffffffffff;
        let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16;

        if exponent == 0x7ff {
            // deal with special floats
            if mantissa_bits != 0 {
                // nan
                HASH_NAN
            } else if sign_bit > 0 {
                HASH_NEGINF // -inf
            } else {
                HASH_INF // inf
            }
        } else {
            // deal with normal floats
            let mantissa = if exponent == 0 {
                mantissa_bits << 1
            } else {
                mantissa_bits | 0x10000000000000
            };
            // Exponent bias + mantissa shift
            exponent -= 0x3ff + 52;

            // calculate hash
            let mantissa = MInt::new(mantissa as u128, &M127U);
            // m * 2^e mod M127 = m * 2^(e mod 127) mod M127
            let pow = mantissa.convert(1 << exponent.absm(&127));
            let v = mantissa * pow;
            v.residue() as i128 * if sign_bit == 0 { 1 } else { -1 }
        }
    }
}

impl NumHash for f64 {
    fn num_hash<H: Hasher>(&self, state: &mut H) {
        self.fhash().num_hash(state)
    }
}

#[cfg(feature = "num-rational")]
mod _num_rational {
    use super::*;
    use core::ops::Neg;
    use num_rational::Ratio;

    macro_rules! impl_hash_for_ratio {
        ($($int:ty)*) => ($(
            impl NumHash for Ratio<$int> {
                fn num_hash<H: Hasher>(&self, state: &mut H) {
                    let ub = *self.denom() as u128; // denom is always positive in Ratio
                    let binv = if ub != M127U {
                        MInt::new(ub, &M127U).inv().unwrap()
                    } else {
                        // no modular inverse, use INF or NEGINF as the result
                        return if self.numer() > &0 { HASH_INF.num_hash(state) } else { HASH_NEGINF.num_hash(state) }
                    };

                    let ua = if self.numer() < &0 { (*self.numer() as u128).wrapping_neg() } else { *self.numer() as u128 }; // essentially calculate |self.numer()|
                    let ua = binv.convert(ua);
                    let ab = (ua * binv).residue() as i128;
                    if self.numer() >= &0 {
                        ab.num_hash(state)
                    } else {
                        ab.neg().num_hash(state)
                    }
                }
            }
        )*);
    }

    impl_hash_for_ratio!(i8 i16 i32 i64 i128 isize);

    #[cfg(feature = "num-bigint")]
    mod _num_bigint {
        use super::*;
        use num_bigint::{BigInt, BigUint};
        use num_traits::{Signed, ToPrimitive, Zero};

        impl NumHash for Ratio<BigInt> {
            fn num_hash<H: Hasher>(&self, state: &mut H) {
                let ub = (self.denom().magnitude() % BigUint::from(M127U))
                    .to_u128()
                    .unwrap();
                let binv = if !ub.is_zero() {
                    MInt::new(ub, &M127U).inv().unwrap()
                } else {
                    // no modular inverse, use INF or NEGINF as the result
                    return if self.numer().is_negative() {
                        HASH_NEGINF.num_hash(state)
                    } else {
                        HASH_INF.num_hash(state)
                    };
                };

                let ua = (self.numer().magnitude() % BigUint::from(M127U))
                    .to_u128()
                    .unwrap();
                let ua = binv.convert(ua);
                let ab = (ua * binv).residue() as i128;
                if self.numer().is_negative() {
                    ab.neg().num_hash(state)
                } else {
                    ab.num_hash(state)
                }
            }
        }
    }
}

// Case4: for a + b*sqrt(r) where a, b are rational numbers, the hash is
// - `hash(a + PROOT^2*b^2*r)` if b > 0
// - `hash(a - PROOT^2*b^2*r)` if b < 0
// The generalized version is that, hash of (a + b*r^(1/k)) will be `hash(a + PROOT^k*b^k*r)`
// Some Caveats:
// 1. if r = 1, the hash is not consistent with normal integer, but r = 1 is forbidden in QuadraticSurd
// 2. a - b*sqrt(r) and a + b*sqrt(-r) has the same hash, which is usually not a problem
#[cfg(feature = "num-complex")]
mod _num_complex {
    use super::*;
    use num_complex::Complex;

    macro_rules! impl_complex_hash_for_float {
        ($($float:ty)*) => ($(
            impl NumHash for Complex<$float> {
                fn num_hash<H: Hasher>(&self, state: &mut H) {
                    let a = self.re.fhash();
                    let b = self.im.fhash();

                    let bterm = if b >= 0 {
                        let pb = MInt::new(b as u128, &M127U) * PROOT;
                        -((pb * pb).residue() as i128)
                    } else {
                        let pb = MInt::new((-b) as u128, &M127U) * PROOT;
                        (pb * pb).residue() as i128
                    };
                    (a + bterm).num_hash(state)
                }
            }
        )*);
    }
    impl_complex_hash_for_float!(f32 f64);
}