malachite_nz/integer/comparison/partial_cmp_abs_primitive_int.rs
1// Copyright © 2025 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::integer::Integer;
10use core::cmp::Ordering;
11use malachite_base::num::comparison::traits::PartialOrdAbs;
12
13macro_rules! impl_unsigned {
14 ($t: ident) => {
15 impl PartialOrdAbs<$t> for Integer {
16 /// Compares the absolute values of an [`Integer`] and an unsigned primitive integer.
17 ///
18 /// # Worst-case complexity
19 /// Constant time and additional memory.
20 ///
21 /// # Examples
22 /// See [here](super::partial_cmp_abs_primitive_int#partial_cmp_abs).
23 #[inline]
24 fn partial_cmp_abs(&self, other: &$t) -> Option<Ordering> {
25 self.abs.partial_cmp(other)
26 }
27 }
28
29 impl PartialOrdAbs<Integer> for $t {
30 /// Compares the absolute values of an unsigned primitive integer and an [`Integer`].
31 ///
32 /// # Worst-case complexity
33 /// Constant time and additional memory.
34 ///
35 /// See [here](super::partial_cmp_abs_primitive_int#partial_cmp_abs).
36 #[inline]
37 fn partial_cmp_abs(&self, other: &Integer) -> Option<Ordering> {
38 other.partial_cmp_abs(self).map(Ordering::reverse)
39 }
40 }
41 };
42}
43apply_to_unsigneds!(impl_unsigned);
44
45macro_rules! impl_signed {
46 ($t: ident) => {
47 impl PartialOrdAbs<$t> for Integer {
48 /// Compares the absolute values of an [`Integer`] and a signed primitive integer.
49 ///
50 /// # Worst-case complexity
51 /// Constant time and additional memory.
52 ///
53 /// # Examples
54 /// See [here](super::partial_cmp_abs_primitive_int#partial_cmp_abs).
55 fn partial_cmp_abs(&self, other: &$t) -> Option<Ordering> {
56 self.abs.partial_cmp(&other.unsigned_abs())
57 }
58 }
59
60 impl PartialOrdAbs<Integer> for $t {
61 /// Compares the absolute values of a signed primitive integer and an [`Integer`].
62 ///
63 /// # Worst-case complexity
64 /// Constant time and additional memory.
65 ///
66 /// See [here](super::partial_cmp_abs_primitive_int#partial_cmp_abs).
67 #[inline]
68 fn partial_cmp_abs(&self, other: &Integer) -> Option<Ordering> {
69 other.partial_cmp_abs(self).map(Ordering::reverse)
70 }
71 }
72 };
73}
74apply_to_signeds!(impl_signed);