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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright © 2024 Mikhail Hogrefe
//
// Uses code adopted from the GNU MP Library.
//
//      Copyright © 1991, 1993-1997, 1999-2016, 2020 Free Software Foundation, Inc.
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.

use crate::natural::InnerNatural::{Large, Small};
use crate::natural::Natural;
use crate::platform::Limb;
use core::cmp::Ordering;
use core::mem::swap;
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::logic::traits::LeadingZeros;
use malachite_base::slices::{slice_leading_zeros, slice_test_zero};

// Interpreting two equal-length slices of `Limb`s as the limbs (in ascending order) of two
// `Natural`s, compares the two `Natural`s.
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(1)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
//
// This is equivalent to `mpn_cmp` from `gmp.h`, GMP 6.2.1.
//
// # Panics
// Panics if `xs` and `ys` have different lengths.
pub_crate_test! {limbs_cmp_same_length(xs: &[Limb], ys: &[Limb]) -> Ordering {
    assert_eq!(xs.len(), ys.len());
    xs.iter().rev().cmp(ys.iter().rev())
}}

// Interpreting two slices of `Limb`s as the limbs (in ascending order) of two `Natural`s, compares
// the two `Natural`s. Neither limb slice can contain trailing zeros.
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(1)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `min(xs.len(), ys.len())`.
//
// # Panics
// Panics if the last element of `xs` or `ys` is zero.
pub_crate_test! {limbs_cmp(xs: &[Limb], ys: &[Limb]) -> Ordering {
    assert_ne!(xs.last(), Some(&0));
    assert_ne!(ys.last(), Some(&0));
    xs.len()
        .cmp(&ys.len())
        .then_with(|| limbs_cmp_same_length(xs, ys))
}}

// Interpreting two slices of `Limb`s as the limbs (in ascending order) of two `Natural`s, returns
// their normalized comparison. See `Natural::cmp_normalized` for details.
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(1)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `min(xs.len(), ys.len())`.
//
// # Panics
// Panics if either `xs` or `ys` is empty, or if the last element of `xs` or `ys` is zero.
pub_test! {limbs_cmp_normalized(xs: &[Limb], ys: &[Limb]) -> Ordering {
    let mut xs = &xs[slice_leading_zeros(xs)..];
    let mut ys = &ys[slice_leading_zeros(ys)..];
    let mut xs_leading = LeadingZeros::leading_zeros(*xs.last().unwrap());
    assert_ne!(xs_leading, Limb::WIDTH);
    let mut ys_leading = LeadingZeros::leading_zeros(*ys.last().unwrap());
    assert_ne!(ys_leading, Limb::WIDTH);
    let mut xs_len = xs.len();
    let mut ys_len = ys.len();
    let mut swapped = false;
    match xs_leading.cmp(&ys_leading) {
        Ordering::Equal => {
            return match xs_len.cmp(&ys_len) {
                Ordering::Equal => limbs_cmp_same_length(xs, ys),
                Ordering::Less => {
                    let leading_cmp = limbs_cmp_same_length(xs, &ys[ys_len - xs_len..]);
                    if leading_cmp == Ordering::Greater {
                        Ordering::Greater
                    } else {
                        Ordering::Less
                    }
                }
                Ordering::Greater => {
                    let leading_cmp = limbs_cmp_same_length(&xs[xs_len - ys_len..], ys);
                    if leading_cmp == Ordering::Less {
                        Ordering::Less
                    } else {
                        Ordering::Greater
                    }
                }
            };
        }
        Ordering::Less => {
            swap(&mut xs, &mut ys);
            swap(&mut xs_leading, &mut ys_leading);
            swap(&mut xs_len, &mut ys_len);
            swapped = true;
        }
        _ => {}
    }
    let xs_shift = xs_leading - ys_leading;
    let comp_xs_shift = Limb::WIDTH - xs_shift;
    let mut xs_i = xs_len - 1;
    let mut ys_i = ys_len - 1;
    loop {
        let y = ys[ys_i];
        let xs_hi = xs[xs_i];
        let xs_lo = if xs_i == 0 { 0 } else { xs[xs_i - 1] };
        let x = (xs_hi << xs_shift) | (xs_lo >> comp_xs_shift);
        let cmp = x.cmp(&y);
        if cmp != Ordering::Equal {
            return if swapped { cmp.reverse() } else { cmp };
        }
        if xs_i == 0 {
            return if ys_i == 0 {
                Ordering::Equal
            } else if swapped {
                Ordering::Greater
            } else {
                Ordering::Less
            };
        } else if ys_i == 0 {
            return if xs_lo << xs_shift == 0 && slice_test_zero(&xs[..xs_i - 1]) {
                Ordering::Equal
            } else if swapped {
                Ordering::Less
            } else {
                Ordering::Greater
            };
        }
        xs_i -= 1;
        ys_i -= 1;
    }
}}

impl PartialOrd for Natural {
    /// Compares two [`Natural`]s.
    ///
    /// See the documentation for the [`Ord`] implementation.
    #[inline]
    fn partial_cmp(&self, other: &Natural) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Natural {
    /// Compares two [`Natural`]s.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n)$
    ///
    /// $M(n) = O(1)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
    /// other.significant_bits())`.
    ///
    /// # Examples
    /// ```
    /// use malachite_nz::natural::Natural;
    ///
    /// assert!(Natural::from(123u32) > Natural::from(122u32));
    /// assert!(Natural::from(123u32) >= Natural::from(122u32));
    /// assert!(Natural::from(123u32) < Natural::from(124u32));
    /// assert!(Natural::from(123u32) <= Natural::from(124u32));
    /// ```
    fn cmp(&self, other: &Natural) -> Ordering {
        if core::ptr::eq(self, other) {
            return Ordering::Equal;
        }
        match (self, other) {
            (&Natural(Small(ref x)), &Natural(Small(ref y))) => x.cmp(y),
            (&Natural(Small(_)), &Natural(Large(_))) => Ordering::Less,
            (&Natural(Large(_)), &Natural(Small(_))) => Ordering::Greater,
            (&Natural(Large(ref xs)), &Natural(Large(ref ys))) => limbs_cmp(xs, ys),
        }
    }
}

impl Natural {
    /// Returns a result of a comparison between two [`Natural`]s as if each had been multiplied by
    /// some power of 2 to bring it into the interval $[1, 2)$.
    ///
    /// That is, the comparison is equivalent to a comparison between $f(x)$ and $f(y)$, where
    /// $$
    /// f(n) = n2^{\lfloor\log_2 n \rfloor}.
    /// $$
    ///
    /// The multiplication is not actually performed.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n)$
    ///
    /// $M(n) = O(1)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
    /// other.significant_bits())`.
    ///
    /// # Panics
    /// Panics if either argument is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_nz::natural::Natural;
    /// use core::cmp::Ordering;
    ///
    /// // 1 == 1.0 * 2^0, 4 == 1.0 * 2^2
    /// // 1.0 == 1.0
    /// assert_eq!(Natural::from(1u32).cmp_normalized(&Natural::from(4u32)), Ordering::Equal);
    ///
    /// // 5 == 1.25 * 2^2, 6 == 1.5 * 2^2
    /// // 1.25 < 1.5
    /// assert_eq!(Natural::from(5u32).cmp_normalized(&Natural::from(6u32)), Ordering::Less);
    ///
    /// // 3 == 1.5 * 2^1, 17 == 1.0625 * 2^4
    /// // 1.5 > 1.0625
    /// assert_eq!(Natural::from(3u32).cmp_normalized(&Natural::from(17u32)), Ordering::Greater);
    ///
    /// // 9 == 1.125 * 2^3, 36 == 1.125 * 2^5
    /// // 1.125 == 1.125
    /// assert_eq!(Natural::from(9u32).cmp_normalized(&Natural::from(36u32)), Ordering::Equal);
    /// ```
    pub fn cmp_normalized(&self, other: &Natural) -> Ordering {
        assert_ne!(*self, 0);
        assert_ne!(*other, 0);
        if core::ptr::eq(self, other) {
            return Ordering::Equal;
        }
        match (self, other) {
            (&Natural(Small(x)), &Natural(Small(y))) => {
                let leading_x = x.leading_zeros();
                let leading_y = y.leading_zeros();
                match leading_x.cmp(&leading_y) {
                    Ordering::Equal => x.cmp(&y),
                    Ordering::Less => x.cmp(&(y << (leading_y - leading_x))),
                    Ordering::Greater => (x << (leading_x - leading_y)).cmp(&y),
                }
            }
            (&Natural(Small(x)), &Natural(Large(ref ys))) => limbs_cmp_normalized(&[x], ys),
            (&Natural(Large(ref xs)), &Natural(Small(y))) => limbs_cmp_normalized(xs, &[y]),
            (&Natural(Large(ref xs)), &Natural(Large(ref ys))) => limbs_cmp_normalized(xs, ys),
        }
    }

    #[cfg(feature = "float_helpers")]
    pub fn cmp_normalized_no_shift(&self, other: &Natural) -> Ordering {
        assert_ne!(*self, 0);
        assert_ne!(*other, 0);
        if core::ptr::eq(self, other) {
            return Ordering::Equal;
        }
        match (self, other) {
            (&Natural(Small(x)), &Natural(Small(y))) => x.cmp(&y),
            (Natural(Small(x)), &Natural(Large(ref ys))) => {
                let (ys_last, ys_init) = ys.split_last().unwrap();
                let c = x.cmp(ys_last);
                if c != Ordering::Equal {
                    c
                } else if slice_test_zero(ys_init) {
                    Ordering::Equal
                } else {
                    Ordering::Less
                }
            }
            (&Natural(Large(ref xs)), Natural(Small(y))) => {
                let (xs_last, xs_init) = xs.split_last().unwrap();
                let c = xs_last.cmp(y);
                if c != Ordering::Equal {
                    c
                } else if slice_test_zero(xs_init) {
                    Ordering::Equal
                } else {
                    Ordering::Greater
                }
            }
            (&Natural(Large(ref xs)), &Natural(Large(ref ys))) => {
                let xs_len = xs.len();
                let ys_len = ys.len();
                match xs_len.cmp(&ys_len) {
                    Ordering::Equal => xs.iter().rev().cmp(ys.iter().rev()),
                    Ordering::Less => {
                        let (ys_lo, ys_hi) = ys.split_at(ys_len - xs_len);
                        let c = xs.iter().rev().cmp(ys_hi.iter().rev());
                        if c != Ordering::Equal {
                            c
                        } else if slice_test_zero(ys_lo) {
                            Ordering::Equal
                        } else {
                            Ordering::Less
                        }
                    }
                    Ordering::Greater => {
                        let (xs_lo, xs_hi) = xs.split_at(xs_len - ys_len);
                        let c = xs_hi.iter().rev().cmp(ys.iter().rev());
                        if c != Ordering::Equal {
                            c
                        } else if slice_test_zero(xs_lo) {
                            Ordering::Equal
                        } else {
                            Ordering::Greater
                        }
                    }
                }
            }
        }
    }
}