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
// Copyright © 2016–2018 University of Malta

// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// this program. If not, see <http://www.gnu.org/licenses/>.

use {Integer, Rational};
use cast::cast;
use ext::gmp as xgmp;
use gmp_mpfr_sys::gmp;
use inner::Inner;
use rational::SmallRational;
use std::cmp::Ordering;
use std::i32;

impl Eq for Rational {}

impl Ord for Rational {
    #[inline]
    fn cmp(&self, other: &Rational) -> Ordering {
        let ord = unsafe { gmp::mpq_cmp(self.inner(), other.inner()) };
        ord.cmp(&0)
    }
}

impl PartialEq for Rational {
    #[inline]
    fn eq(&self, other: &Rational) -> bool {
        unsafe { gmp::mpq_equal(self.inner(), other.inner()) != 0 }
    }
}

impl PartialOrd for Rational {
    #[inline]
    fn partial_cmp(&self, other: &Rational) -> Option<Ordering> {
        Some(<Rational as Ord>::cmp(self, other))
    }
}

impl PartialEq<Integer> for Rational {
    #[inline]
    fn eq(&self, other: &Integer) -> bool {
        unsafe { gmp::mpq_cmp_z(self.inner(), other.inner()) == 0 }
    }
}

impl PartialEq<Rational> for Integer {
    #[inline]
    fn eq(&self, other: &Rational) -> bool {
        <Rational as PartialEq<Integer>>::eq(other, self)
    }
}

impl PartialOrd<Integer> for Rational {
    #[inline]
    fn partial_cmp(&self, other: &Integer) -> Option<Ordering> {
        let ord = unsafe { gmp::mpq_cmp_z(self.inner(), other.inner()) };
        Some(ord.cmp(&0))
    }
}

impl PartialOrd<Rational> for Integer {
    #[inline]
    fn partial_cmp(&self, other: &Rational) -> Option<Ordering> {
        <Rational as PartialOrd<Integer>>::partial_cmp(other, self)
            .map(Ordering::reverse)
    }
}

macro_rules! cmp {
    { $T:ty, $cmp:path } => {
        impl PartialEq<$T> for Rational {
            #[inline]
            fn eq(&self, other: &$T) -> bool {
                unsafe { $cmp(self.inner(), cast(*other), 1) == 0 }
            }
        }

        impl PartialEq<Rational> for $T {
            #[inline]
            fn eq(&self, other: &Rational) -> bool {
                <Rational as PartialEq<$T>>::eq(other, self)
            }
        }

        impl PartialOrd<$T> for Rational {
            #[inline]
            fn partial_cmp(&self, other: &$T) -> Option<Ordering> {
                let ord = unsafe { $cmp(self.inner(), cast(*other), 1) };
                Some(ord.cmp(&0))
            }
        }

        impl PartialOrd<Rational> for $T {
            #[inline]
            fn partial_cmp(&self, other: &Rational) -> Option<Ordering> {
                <Rational as PartialOrd<$T>>::partial_cmp(other, self)
                    .map(Ordering::reverse)
            }
        }
    }
}

cmp! { i8, gmp::mpq_cmp_si }
cmp! { i16, gmp::mpq_cmp_si }
cmp! { i32, gmp::mpq_cmp_si }
cmp! { i64, xgmp::mpq_cmp_i64 }
#[cfg(target_pointer_width = "32")]
cmp! { isize, gmp::mpq_cmp_si }
#[cfg(target_pointer_width = "64")]
cmp! { isize, xgmp::mpq_cmp_i64 }

cmp! { u8, gmp::mpq_cmp_ui }
cmp! { u16, gmp::mpq_cmp_ui }
cmp! { u32, gmp::mpq_cmp_ui }
cmp! { u64, xgmp::mpq_cmp_u64 }
#[cfg(target_pointer_width = "32")]
cmp! { usize, gmp::mpq_cmp_ui }
#[cfg(target_pointer_width = "64")]
cmp! { usize, xgmp::mpq_cmp_u64 }

macro_rules! cross {
    { $Num:ty; $Den:ty } => {
        impl PartialEq<($Num, $Den)> for Rational {
            #[inline]
            fn eq(&self, other: &($Num, $Den)) -> bool {
                <Rational as PartialEq>::eq(self, &*SmallRational::from(*other))
            }
        }
        impl PartialEq<Rational> for ($Num, $Den) {
            #[inline]
            fn eq(&self, other: &Rational) -> bool {
                <Rational as PartialEq>::eq(&*SmallRational::from(*self), other)
            }
        }
        impl PartialOrd<($Num, $Den)> for Rational {
            #[inline]
            fn partial_cmp(&self, other: &($Num, $Den)) -> Option<Ordering> {
                <Rational as PartialOrd>::partial_cmp(
                    self,
                    &*SmallRational::from(*other),
                )
            }
        }
        impl PartialOrd<Rational> for ($Num, $Den) {
            #[inline]
            fn partial_cmp(&self, other: &Rational) -> Option<Ordering> {
                <Rational as PartialOrd>::partial_cmp(
                    &*SmallRational::from(*self),
                    other,
                )
            }
        }
    }
}

// (Major, Major), (Major, Minor*), (Minor*, Major)
macro_rules! matrix {
    { $Major:ty $(; $Minor:ty)* } => {
        cross! { $Major; $Major }
        $( cross! { $Major; $Minor } )*
        $( cross! { $Minor; $Major } )*
    }
}

matrix! { u8 }
matrix! { i8; u8 }
matrix! { u16; i8; u8 }
matrix! { i16; u16; i8; u8 }
matrix! { u32; i16; u16; i8; u8 }
matrix! { i32; u32; i16; u16; i8; u8 }
matrix! { usize; i32; u32; i16; u16; i8; u8 }
matrix! { isize; usize; i32; u32; i16; u16; i8; u8 }
matrix! { u64; isize; usize; i32; u32; i16; u16; i8; u8 }
matrix! { i64; u64; isize; usize; i32; u32; i16; u16; i8; u8 }

#[cfg(test)]
mod tests {
    use Rational;
    use std::{i32, u32};
    use std::cmp::Ordering;

    #[test]
    fn check_cmp_frac() {
        let zero = Rational::new();
        let u = [0, 1, 100, u32::MAX];
        let s = [i32::MIN, -100, -1, 0, 1, 100, i32::MAX];
        for &n in &u {
            for &d in &u {
                if d != 0 {
                    let ans = 0.partial_cmp(&n);
                    assert_eq!(zero.partial_cmp(&(n, d)), ans);
                    assert_eq!(zero.partial_cmp(&Rational::from((n, d))), ans);
                }
            }
            for &d in &s {
                if d != 0 {
                    let mut ans = 0.partial_cmp(&n);
                    if d < 0 {
                        ans = ans.map(Ordering::reverse);
                    }
                    assert_eq!(zero.partial_cmp(&(n, d)), ans);
                    assert_eq!(zero.partial_cmp(&Rational::from((n, d))), ans);
                }
            }
        }
        for &n in &s {
            for &d in &u {
                if d != 0 {
                    let ans = 0.partial_cmp(&n);
                    assert_eq!(zero.partial_cmp(&(n, d)), ans);
                    assert_eq!(zero.partial_cmp(&Rational::from((n, d))), ans);
                }
            }
            for &d in &s {
                if d != 0 {
                    let mut ans = 0.partial_cmp(&n);
                    if d < 0 {
                        ans = ans.map(Ordering::reverse);
                    }
                    assert_eq!(zero.partial_cmp(&(n, d)), ans);
                    assert_eq!(zero.partial_cmp(&Rational::from((n, d))), ans);
                }
            }
        }
    }
}