malachite_nz/integer/comparison/cmp_abs_natural.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 crate::natural::Natural;
11use core::cmp::Ordering;
12use malachite_base::num::comparison::traits::PartialOrdAbs;
13
14impl PartialOrdAbs<Natural> for Integer {
15 /// Compares the absolute values of an [`Integer`] and a [`Natural`].
16 ///
17 /// # Worst-case complexity
18 /// $T(n) = O(n)$
19 ///
20 /// $M(n) = O(1)$
21 ///
22 /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
23 /// other.significant_bits())`.
24 ///
25 /// # Examples
26 /// ```
27 /// use malachite_base::num::comparison::traits::PartialOrdAbs;
28 /// use malachite_nz::integer::Integer;
29 /// use malachite_nz::natural::Natural;
30 ///
31 /// assert!(Integer::from(123).gt_abs(&Natural::from(122u32)));
32 /// assert!(Integer::from(123).ge_abs(&Natural::from(122u32)));
33 /// assert!(Integer::from(123).lt_abs(&Natural::from(124u32)));
34 /// assert!(Integer::from(123).le_abs(&Natural::from(124u32)));
35 /// assert!(Integer::from(-124).gt_abs(&Natural::from(123u32)));
36 /// assert!(Integer::from(-124).ge_abs(&Natural::from(123u32)));
37 /// ```
38 #[inline]
39 fn partial_cmp_abs(&self, other: &Natural) -> Option<Ordering> {
40 self.abs.partial_cmp(other)
41 }
42}
43
44impl PartialOrdAbs<Integer> for Natural {
45 /// Compares the absolute values of a [`Natural`] and an [`Integer`].
46 ///
47 /// # Worst-case complexity
48 /// $T(n) = O(n)$
49 ///
50 /// $M(n) = O(1)$
51 ///
52 /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
53 /// other.significant_bits())`.
54 ///
55 /// # Examples
56 /// ```
57 /// use malachite_base::num::comparison::traits::PartialOrdAbs;
58 /// use malachite_nz::integer::Integer;
59 /// use malachite_nz::natural::Natural;
60 ///
61 /// assert!(Natural::from(123u32).gt_abs(&Integer::from(122)));
62 /// assert!(Natural::from(123u32).ge_abs(&Integer::from(122)));
63 /// assert!(Natural::from(123u32).lt_abs(&Integer::from(124)));
64 /// assert!(Natural::from(123u32).le_abs(&Integer::from(124)));
65 /// assert!(Natural::from(123u32).lt_abs(&Integer::from(-124)));
66 /// assert!(Natural::from(123u32).le_abs(&Integer::from(-124)));
67 /// ```
68 #[inline]
69 fn partial_cmp_abs(&self, other: &Integer) -> Option<Ordering> {
70 self.partial_cmp(&other.abs)
71 }
72}