Skip to content

Commit d317c8b

Browse files
committed
Move hash_complex implementation from hash.rs to complex.rs
1 parent 6098eb6 commit d317c8b

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

common/src/hash.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use num_bigint::BigInt;
2-
use num_complex::Complex64;
32
use num_traits::ToPrimitive;
43
use siphasher::sip::SipHasher24;
54
use std::{
65
hash::{BuildHasher, Hash, Hasher},
7-
num::Wrapping,
86
};
97

108
pub type PyHash = i64;
@@ -140,24 +138,6 @@ pub fn hash_float(value: f64) -> Option<PyHash> {
140138
Some(fix_sentinel(x as PyHash * value.signum() as PyHash))
141139
}
142140

143-
#[inline]
144-
pub fn hash_complex(value: &Complex64) -> PyHash {
145-
let re_hash = (match hash_float(value.re) {
146-
Some(value) => Some(value),
147-
None => Some(hash_pointer(value as *const _ as *const std::ffi::c_void)),
148-
})
149-
.unwrap();
150-
151-
let im_hash = (match hash_float(value.im) {
152-
Some(value) => Some(value),
153-
None => Some(hash_pointer(value as *const _ as *const std::ffi::c_void)),
154-
})
155-
.unwrap();
156-
157-
let Wrapping(ret) = Wrapping(re_hash) + Wrapping(im_hash) * Wrapping(IMAG);
158-
fix_sentinel(ret)
159-
}
160-
161141
pub fn hash_iter_unordered<'a, T: 'a, I, F, E>(iter: I, hashf: F) -> Result<PyHash, E>
162142
where
163143
I: IntoIterator<Item = &'a T>,

vm/src/builtins/complex.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::{
1313
types::{AsNumber, Comparable, Constructor, Hashable, PyComparisonOp},
1414
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
1515
};
16+
use std::num::Wrapping;
1617
use num_complex::Complex64;
1718
use num_traits::Zero;
1819
use rustpython_common::{float_ops, hash};
@@ -417,7 +418,26 @@ impl Comparable for PyComplex {
417418
impl Hashable for PyComplex {
418419
#[inline]
419420
fn hash(zelf: &crate::Py<Self>, _vm: &VirtualMachine) -> PyResult<hash::PyHash> {
420-
Ok(hash::hash_complex(&zelf.value))
421+
let value = zelf.value;
422+
423+
let re_hash = (match hash::hash_float(value.re) {
424+
Some(value) => Some(value),
425+
None => Some(hash::hash_pointer(
426+
zelf as *const _ as *const std::ffi::c_void,
427+
)),
428+
})
429+
.unwrap();
430+
431+
let im_hash = (match hash::hash_float(value.im) {
432+
Some(value) => Some(value),
433+
None => Some(hash::hash_pointer(
434+
zelf as *const _ as *const std::ffi::c_void,
435+
)),
436+
})
437+
.unwrap();
438+
439+
let Wrapping(ret) = Wrapping(re_hash) + Wrapping(im_hash) * Wrapping(hash::IMAG);
440+
Ok(hash::fix_sentinel(ret))
421441
}
422442
}
423443

0 commit comments

Comments
 (0)