malachite_nz/natural/comparison/partial_cmp_abs_primitive_int.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::natural::Natural;
10use core::cmp::Ordering;
11use malachite_base::num::comparison::traits::PartialOrdAbs;
12
13macro_rules! impl_unsigned {
14 ($t: ident) => {
15 impl PartialOrdAbs<$t> for Natural {
16 /// Compares a [`Natural`] to an unsigned primitive integer.
17 ///
18 /// Since both values are non-negative, this is the same as ordinary
19 /// [`partial_cmp`](PartialOrd::partial_cmp).
20 ///
21 /// # Worst-case complexity
22 /// Constant time and additional memory.
23 ///
24 /// See [here](super::partial_cmp_abs_primitive_int#partial_cmp_abs).
25 #[inline]
26 fn partial_cmp_abs(&self, other: &$t) -> Option<Ordering> {
27 self.partial_cmp(other)
28 }
29 }
30
31 impl PartialOrdAbs<Natural> for $t {
32 /// Compares a value of unsigned primitive integer type to a [`Natural`].
33 ///
34 /// Since both values are non-negative, this is the same as ordinary
35 /// [`partial_cmp`](PartialOrd::partial_cmp).
36 ///
37 /// # Worst-case complexity
38 /// Constant time and additional memory.
39 ///
40 /// See [here](super::partial_cmp_abs_primitive_int#partial_cmp_abs).
41 #[inline]
42 fn partial_cmp_abs(&self, other: &Natural) -> Option<Ordering> {
43 self.partial_cmp(other)
44 }
45 }
46 };
47}
48apply_to_unsigneds!(impl_unsigned);
49
50macro_rules! impl_signed {
51 ($t: ident) => {
52 impl PartialOrdAbs<$t> for Natural {
53 /// Compares a [`Natural`] to the absolute value of a signed primitive integer.
54 ///
55 /// # Worst-case complexity
56 /// Constant time and additional memory.
57 ///
58 /// See [here](super::partial_cmp_abs_primitive_int#partial_cmp_abs).
59 fn partial_cmp_abs(&self, other: &$t) -> Option<Ordering> {
60 self.partial_cmp(&other.unsigned_abs())
61 }
62 }
63
64 impl PartialOrdAbs<Natural> for $t {
65 /// Compares the absolute value of a signed primitive integer to a [`Natural`].
66 ///
67 /// # Worst-case complexity
68 /// Constant time and additional memory.
69 ///
70 /// See [here](super::partial_cmp_abs_primitive_int#partial_cmp_abs).
71 #[inline]
72 fn partial_cmp_abs(&self, other: &Natural) -> Option<Ordering> {
73 other.partial_cmp_abs(self).map(Ordering::reverse)
74 }
75 }
76 };
77}
78apply_to_signeds!(impl_signed);