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

mod add; mod sub; mod mul; mod div; mod from; mod tryinto; mod fmt;
#[allow(unused_imports)]
pub(crate) use {add::*, sub::*, mul::*, div::*, from::*, tryinto::*, fmt::*};

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]
    fn cmp(&self, rhs: &Self) -> Ordering {
        match self.0.len().cmp(&rhs.0.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
            }
        }
    }
}

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<LBNum> for $ty {
            #[inline(always)]
            fn eq(&self, rhs: &LBNum) -> 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<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_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: LBNum) -> Self {
        let mut result = Self::ZERO;
        let divide_by = base.clone();
        let base_m1 = base - 1u8;
        while self > base_m1 {
            self /= &divide_by;
            result.increment();
        }
        result
    }
}