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
//! Comparisons operators.
#![allow(deprecated)] // TODO(v0.5): remove after the implementations for AbsEq are removed.

use dashu_base::{AbsEq, AbsOrd};

use crate::{
    arch::word::Word,
    ibig::IBig,
    repr::TypedReprRef::{self, *},
    ubig::UBig,
    Sign::*,
};
use core::cmp::Ordering;

/// Compare lhs with rhs of the same length as numbers.
#[inline]
pub fn cmp_same_len(lhs: &[Word], rhs: &[Word]) -> Ordering {
    debug_assert!(lhs.len() == rhs.len());
    lhs.iter().rev().cmp(rhs.iter().rev())
}

/// Compare lhs with rhs as numbers. The leading zero words of the input must be trimmed.
///
/// # Panics
///
/// Panic if lhs or rhs has leading zero words (including the case where lhs == 0 or rhs == 0)
pub fn cmp_in_place(lhs: &[Word], rhs: &[Word]) -> Ordering {
    debug_assert!(*lhs.last().unwrap() != 0 && *rhs.last().unwrap() != 0);
    lhs.len()
        .cmp(&rhs.len())
        .then_with(|| cmp_same_len(lhs, rhs))
}

impl<'a> PartialOrd for TypedReprRef<'a> {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<'a> Ord for TypedReprRef<'a> {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        match (*self, *other) {
            (RefSmall(dword0), RefSmall(dword1)) => dword0.cmp(&dword1),
            (RefSmall(_), RefLarge(_)) => Ordering::Less,
            (RefLarge(_), RefSmall(_)) => Ordering::Greater,
            (RefLarge(words0), RefLarge(words1)) => cmp_in_place(words0, words1),
        }
    }
}

impl Ord for UBig {
    #[inline]
    fn cmp(&self, other: &UBig) -> Ordering {
        self.repr().cmp(&other.repr())
    }
}

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

impl Ord for IBig {
    #[inline]
    fn cmp(&self, other: &IBig) -> Ordering {
        let (lhs_sign, lhs_mag) = self.as_sign_repr();
        let (rhs_sign, rhs_mag) = other.as_sign_repr();
        match (lhs_sign, rhs_sign) {
            (Positive, Positive) => lhs_mag.cmp(&rhs_mag),
            (Positive, Negative) => Ordering::Greater,
            (Negative, Positive) => Ordering::Less,
            (Negative, Negative) => rhs_mag.cmp(&lhs_mag),
        }
    }
}

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

impl AbsEq for UBig {
    #[inline]
    fn abs_eq(&self, rhs: &Self) -> bool {
        self.eq(rhs)
    }
}
impl AbsEq for IBig {
    #[inline]
    fn abs_eq(&self, rhs: &Self) -> bool {
        self.0.as_sign_slice().1.eq(rhs.0.as_sign_slice().1)
    }
}
impl AbsEq<UBig> for IBig {
    #[inline]
    fn abs_eq(&self, rhs: &UBig) -> bool {
        self.0.as_sign_slice().1.eq(rhs.0.as_slice())
    }
}
impl AbsEq<IBig> for UBig {
    #[inline]
    fn abs_eq(&self, rhs: &IBig) -> bool {
        self.0.as_slice().eq(rhs.0.as_sign_slice().1)
    }
}

impl AbsOrd for UBig {
    #[inline]
    fn abs_cmp(&self, rhs: &Self) -> Ordering {
        self.0.as_typed().cmp(&rhs.0.as_typed())
    }
}
impl AbsOrd for IBig {
    #[inline]
    fn abs_cmp(&self, rhs: &Self) -> Ordering {
        self.0.as_sign_typed().1.cmp(&rhs.0.as_sign_typed().1)
    }
}
impl AbsOrd<UBig> for IBig {
    #[inline]
    fn abs_cmp(&self, rhs: &UBig) -> Ordering {
        self.0.as_sign_typed().1.cmp(&rhs.0.as_typed())
    }
}
impl AbsOrd<IBig> for UBig {
    #[inline]
    fn abs_cmp(&self, rhs: &IBig) -> Ordering {
        self.0.as_typed().cmp(&rhs.0.as_sign_typed().1)
    }
}