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
use crate::linkedbytes::{LBNum, LBNumRef};
use core::cmp::{self, Ordering};

mod add; mod sub; mod mul; mod div; mod from; mod tryinto; mod fmt; mod gcd;
pub(crate) use sub::DecrementResult;

impl cmp::PartialEq for LBNum {
    #[inline(always)]
    fn eq(&self, rhs: &Self) -> bool {
        self.cmp(rhs) == Ordering::Equal
    }
}
impl cmp::Eq for LBNum {}
impl cmp::PartialOrd for LBNum {
    #[inline(always)]
    fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
        Some(self.cmp(rhs))
    }
}
impl cmp::Ord for LBNum {
    #[inline(always)]
    fn cmp(&self, rhs: &Self) -> Ordering {
        LBNumRef::from(self).cmp(&LBNumRef::from(rhs))
    }
}
impl PartialEq for LBNumRef<'_> {
    #[inline(always)]
    fn eq(&self, rhs: &Self) -> bool {
        self.cmp(rhs) == Ordering::Equal
    }
}
impl Eq for LBNumRef<'_> {}
impl cmp::PartialOrd for LBNumRef<'_> {
    #[inline(always)]
    fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
        Some(self.cmp(rhs))
    }
}
impl<'a> cmp::Ord for LBNumRef<'a> {
    #[inline]
    fn cmp(&self, rhs: &Self) -> Ordering {
        match self.inner().len().cmp(&rhs.inner().len()) {
            Ordering::Greater => Ordering::Greater,
            Ordering::Less => Ordering::Less,
            Ordering::Equal => {
                for (this, other) in self.iter_be().zip(rhs.iter_be()) {
                    match this.cmp(&other) {
                        Ordering::Greater => {return Ordering::Greater},
                        Ordering::Less => {return Ordering::Less},
                        Ordering::Equal => {} // Do nothing in this case, search for the next one.
                    }
                }
                Ordering::Equal
            }
        }
    }
}
impl PartialEq<LBNumRef<'_>> for LBNum {
    #[inline(always)]
    fn eq(&self, rhs: &LBNumRef<'_>) -> bool {
        LBNumRef::from(self).cmp(rhs) == Ordering::Equal
    }
}
impl PartialOrd<LBNumRef<'_>> for LBNum {
    #[inline(always)]
    fn partial_cmp(&self, rhs: &LBNumRef<'_>) -> Option<Ordering> {
        Some(LBNumRef::from(self).cmp(rhs))
    }
}
impl PartialEq<LBNum> for LBNumRef<'_> {
    #[inline(always)]
    fn eq(&self, rhs: &LBNum) -> bool {
        self.cmp(&LBNumRef::from(rhs)) == Ordering::Equal
    }
}
impl PartialOrd<LBNum> for LBNumRef<'_> {
    #[inline(always)]
    fn partial_cmp(&self, rhs: &LBNum) -> Option<Ordering> {
        Some(self.cmp(&LBNumRef::from(rhs)))
    }
}

macro_rules! impl_partial_eq_ord_to_primitive {
    ($ty:ident) => {
        impl PartialEq<$ty> for LBNum {
            #[inline(always)]
            fn eq(&self, rhs: &$ty) -> bool {
                *self == Self::from(*rhs)
            }
        }
        impl PartialEq<$ty> for LBNumRef<'_> {
            #[inline(always)]
            fn eq(&self, rhs:&$ty) -> bool {
                *self == LBNum::from(*rhs)
            }
        }
        impl PartialEq<LBNum> for $ty {
            #[inline(always)]
            fn eq(&self, rhs: &LBNum) -> bool {
                LBNum::from(*self) == *rhs
            }
        }
        impl PartialEq<LBNumRef<'_>> for $ty {
            #[inline(always)]
            fn eq(&self, rhs: &LBNumRef<'_>) -> bool {
                LBNum::from(*self) == *rhs
            }
        }
        impl PartialOrd<$ty> for LBNum {
            /// Compares `self` and `rhs`.
            ///
            /// Never fails, a return value of `Some` can be relied upon.
            #[inline(always)]
            fn partial_cmp(&self, rhs: &$ty) -> Option<Ordering> {
                Some(self.cmp(&Self::from(*rhs)))
            }
        }
        impl PartialOrd<$ty> for LBNumRef<'_> {
            /// Compares `self` and `rhs`.
            ///
            /// Never fails, a return value of `Some` can be relied upon.
            #[inline(always)]
            fn partial_cmp(&self, rhs: &$ty) -> Option<Ordering> {
                self.partial_cmp(&LBNum::from(*rhs)) // Why doesn't Ord have a type parameter?
            }
        }
        impl PartialOrd<LBNum> for $ty {
            /// Compares `self` and `rhs`.
            ///
            /// Never fails, a return value of `Some` can be relied upon.
            #[inline(always)]
            fn partial_cmp(&self, rhs: &LBNum) -> Option<Ordering> {
                Some(LBNum::from(*self).cmp(rhs))
            }
        }
        impl PartialOrd<LBNumRef<'_>> for $ty {
            /// Compares `self` and `rhs`.
            ///
            /// Never fails, a return value of `Some` can be relied upon.
            #[inline(always)]
            fn partial_cmp(&self, rhs: &LBNumRef<'_>) -> Option<Ordering> {
                LBNum::from(*self).partial_cmp(rhs)
            }
        }
    };
}

impl_partial_eq_ord_to_primitive!(u8   );
impl_partial_eq_ord_to_primitive!(u16  );
impl_partial_eq_ord_to_primitive!(u32  );
impl_partial_eq_ord_to_primitive!(u64  );
impl_partial_eq_ord_to_primitive!(u128 );
impl_partial_eq_ord_to_primitive!(usize);

impl LBNum {
    /// Consumes `self` and returns the logarithm of the given base.
    #[inline]
    #[must_use = "this is an expensive non-in-place operation"]
    pub fn logb(mut self, base: Self) -> Self {
        let mut result = Self::ZERO;
        let divide_by = base.clone();
        let base_m1 = base - 1_u8;
        while self > base_m1 {
            self /= &divide_by;
            result.increment();
        }
        result
    }
}