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
use Integer;
use ext::gmp as xgmp;
use gmp_mpfr_sys::gmp;
use inner::Inner;
use std::cmp::Ordering;
impl Eq for Integer {}
impl Ord for Integer {
#[inline]
fn cmp(&self, other: &Integer) -> Ordering {
let ord = unsafe { gmp::mpz_cmp(self.inner(), other.inner()) };
ord.cmp(&0)
}
}
impl PartialEq for Integer {
#[inline]
fn eq(&self, other: &Integer) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl PartialOrd for Integer {
#[inline]
fn partial_cmp(&self, other: &Integer) -> Option<Ordering> {
Some(self.cmp(other))
}
}
macro_rules! cmp {
{ $T:ty, $func:path } => {
impl PartialEq<$T> for Integer {
#[inline]
fn eq(&self, other: &$T) -> bool {
self.partial_cmp(other) == Some(Ordering::Equal)
}
}
impl PartialEq<Integer> for $T {
#[inline]
fn eq(&self, other: &Integer) -> bool {
other.eq(self)
}
}
impl PartialOrd<$T> for Integer {
#[inline]
fn partial_cmp(&self, other: &$T) -> Option<Ordering> {
let ord = unsafe { $func(self.inner(), (*other).into()) };
Some(ord.cmp(&0))
}
}
impl PartialOrd<Integer> for $T {
#[inline]
fn partial_cmp(&self, other: &Integer) -> Option<Ordering> {
other.partial_cmp(self).map(Ordering::reverse)
}
}
};
}
cmp! { i32, xgmp::mpz_cmp_i32 }
cmp! { i64, xgmp::mpz_cmp_i64 }
cmp! { u32, xgmp::mpz_cmp_u32 }
cmp! { u64, xgmp::mpz_cmp_u64 }
impl PartialEq<f32> for Integer {
#[inline]
fn eq(&self, other: &f32) -> bool {
let o = f64::from(*other);
self.eq(&o)
}
}
impl PartialEq<Integer> for f32 {
#[inline]
fn eq(&self, other: &Integer) -> bool {
other.eq(self)
}
}
impl PartialOrd<f32> for Integer {
#[inline]
fn partial_cmp(&self, other: &f32) -> Option<Ordering> {
let o = f64::from(*other);
self.partial_cmp(&o)
}
}
impl PartialOrd<Integer> for f32 {
#[inline]
fn partial_cmp(&self, other: &Integer) -> Option<Ordering> {
other.partial_cmp(self).map(Ordering::reverse)
}
}
impl PartialEq<f64> for Integer {
#[inline]
fn eq(&self, other: &f64) -> bool {
self.partial_cmp(other) == Some(Ordering::Equal)
}
}
impl PartialEq<Integer> for f64 {
#[inline]
fn eq(&self, other: &Integer) -> bool {
other.eq(self)
}
}
impl PartialOrd<f64> for Integer {
#[inline]
fn partial_cmp(&self, other: &f64) -> Option<Ordering> {
if other.is_nan() {
None
} else {
let ord = unsafe { gmp::mpz_cmp_d(self.inner(), *other) };
Some(ord.cmp(&0))
}
}
}
impl PartialOrd<Integer> for f64 {
#[inline]
fn partial_cmp(&self, other: &Integer) -> Option<Ordering> {
other.partial_cmp(self).map(Ordering::reverse)
}
}