relp_num/integer/big/
properties.rs

1use std::cmp::Ordering;
2use std::ops::Deref;
3
4use crate::{NonZero, Sign, Ubig};
5use crate::integer::big::NonZeroUbig;
6use crate::integer::big::ops::building_blocks::is_well_formed;
7use crate::Signed;
8
9impl<const S: usize> NonZero for Ubig<S> {
10    #[must_use]
11    #[inline]
12    fn is_not_zero(&self) -> bool {
13        !self.0.is_empty()
14    }
15}
16
17impl<const S: usize> NonZero for NonZeroUbig<S> {
18    #[must_use]
19    #[inline]
20    fn is_not_zero(&self) -> bool {
21        true
22    }
23}
24
25impl<const S: usize> Deref for Ubig<S> {
26    type Target = [usize];
27
28    fn deref(&self) -> &Self::Target {
29        &self.0
30    }
31}
32
33impl<const S: usize> Deref for NonZeroUbig<S> {
34    type Target = [usize];
35
36    fn deref(&self) -> &Self::Target {
37        &self.0
38    }
39}
40
41impl<const S: usize> Signed for Ubig<S> {
42    fn signum(&self) -> Sign {
43        Sign::Positive
44    }
45
46    fn is_positive(&self) -> bool {
47        true
48    }
49
50    fn is_negative(&self) -> bool {
51        false
52    }
53}
54
55impl<const S: usize> Signed for NonZeroUbig<S> {
56    fn signum(&self) -> Sign {
57        Sign::Positive
58    }
59
60    fn is_positive(&self) -> bool {
61        true
62    }
63
64    fn is_negative(&self) -> bool {
65        false
66    }
67}
68
69impl<const S: usize> Ord for NonZeroUbig<S> {
70    fn cmp(&self, other: &Self) -> Ordering {
71        self.partial_cmp(other).unwrap()
72    }
73}
74
75impl<const S: usize> Ord for Ubig<S> {
76    fn cmp(&self, other: &Self) -> Ordering {
77        self.partial_cmp(other).unwrap()
78    }
79}
80
81impl<const S: usize> PartialOrd for NonZeroUbig<S> {
82    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
83        Some(cmp(&self.0, &other.0))
84    }
85}
86
87impl<const S: usize> PartialOrd for Ubig<S> {
88    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
89        Some(cmp(&self.0, &other.0))
90    }
91}
92
93#[must_use]
94#[inline]
95pub fn cmp(left: &[usize], right: &[usize]) -> Ordering {
96    debug_assert!(is_well_formed(left));
97    debug_assert!(is_well_formed(right));
98
99    match left.len().cmp(&right.len()) {
100        Ordering::Less => Ordering::Less,
101        Ordering::Equal => {
102            // TODO(PERFORMANCE): Check that bounds checks are not done twice.
103            for (left_word, right_word) in left.iter().zip(right.iter()).rev() {
104                match left_word.cmp(right_word) {
105                    Ordering::Less => return Ordering::Less,
106                    Ordering::Equal => {}
107                    Ordering::Greater => return Ordering::Greater,
108                }
109            }
110
111            Ordering::Equal
112        }
113        Ordering::Greater => Ordering::Greater,
114    }
115}