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
use crate::Complex;
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
#[derive(Clone, Debug)]
#[repr(transparent)]
pub struct OrdComplex {
inner: Complex,
}
fn _static_assertions() {
static_assert_size!(OrdComplex, Complex);
}
impl OrdComplex {
#[inline]
pub fn as_complex(&self) -> &Complex {
&self.inner
}
#[inline]
pub fn as_complex_mut(&mut self) -> &mut Complex {
&mut self.inner
}
}
impl Hash for OrdComplex {
#[inline]
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
self.inner.real().as_ord().hash(state);
self.inner.imag().as_ord().hash(state);
}
}
impl Eq for OrdComplex {}
impl Ord for OrdComplex {
#[inline]
fn cmp(&self, other: &OrdComplex) -> Ordering {
let real = self.inner.real().as_ord().cmp(other.inner.real().as_ord());
let imag = self.inner.imag().as_ord().cmp(other.inner.imag().as_ord());
real.then(imag)
}
}
impl PartialEq for OrdComplex {
#[inline]
fn eq(&self, other: &OrdComplex) -> bool {
let real = self.inner.real().as_ord().eq(other.inner.real().as_ord());
let imag = self.inner.imag().as_ord().eq(other.inner.imag().as_ord());
real && imag
}
}
impl PartialOrd for OrdComplex {
#[inline]
fn partial_cmp(&self, other: &OrdComplex) -> Option<Ordering> {
Some(<OrdComplex as Ord>::cmp(self, other))
}
}
impl From<Complex> for OrdComplex {
#[inline]
fn from(src: Complex) -> Self {
OrdComplex { inner: src }
}
}
impl From<OrdComplex> for Complex {
#[inline]
fn from(src: OrdComplex) -> Self {
src.inner
}
}
#[cfg(test)]
mod tests {
use crate::float::Special;
use crate::Complex;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
fn calculate_hash<T>(t: &T) -> u64
where
T: Hash,
{
let mut s = DefaultHasher::new();
t.hash(&mut s);
s.finish()
}
#[test]
fn check_zero() {
let pp = Complex::with_val(53, (Special::Zero, Special::Zero));
let pn = Complex::with_val(53, (Special::Zero, Special::NegZero));
let np = Complex::with_val(53, (Special::NegZero, Special::Zero));
let nn = Complex::with_val(53, (Special::NegZero, Special::NegZero));
assert_eq!(pp, pn);
assert_eq!(pn, np);
assert_eq!(np, nn);
assert_eq!(nn, pp);
let ord_pp = pp.as_ord();
let ord_pn = pn.as_ord();
let ord_np = np.as_ord();
let ord_nn = nn.as_ord();
assert_eq!(ord_pp, ord_pp);
assert_eq!(ord_pn, ord_pn);
assert_eq!(ord_np, ord_np);
assert_eq!(ord_nn, ord_nn);
assert_eq!(calculate_hash(ord_pp), calculate_hash(ord_pp));
assert_eq!(calculate_hash(ord_pn), calculate_hash(ord_pn));
assert_eq!(calculate_hash(ord_np), calculate_hash(ord_np));
assert_eq!(calculate_hash(ord_nn), calculate_hash(ord_nn));
assert_ne!(ord_pp, ord_pn);
assert_ne!(ord_pn, ord_np);
assert_ne!(ord_np, ord_nn);
assert_ne!(ord_nn, ord_pp);
assert_ne!(calculate_hash(ord_pp), calculate_hash(ord_pn));
assert_ne!(calculate_hash(ord_pn), calculate_hash(ord_np));
assert_ne!(calculate_hash(ord_np), calculate_hash(ord_nn));
assert_ne!(calculate_hash(ord_nn), calculate_hash(ord_pp));
}
}