malachite_nz/integer/comparison/
cmp_abs.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::{OrdAbs, PartialOrdAbs};
12
13impl PartialOrdAbs for Integer {
14    /// Compares the absolute values of two [`Integer`]s.
15    ///
16    /// See the documentation for the [`OrdAbs`] implementation.
17    #[inline]
18    fn partial_cmp_abs(&self, other: &Integer) -> Option<Ordering> {
19        Some(self.cmp_abs(other))
20    }
21}
22
23impl OrdAbs for Integer {
24    /// Compares the absolute values of two [`Integer`]s.
25    ///
26    /// # Worst-case complexity
27    /// $T(n) = O(n)$
28    ///
29    /// $M(n) = O(1)$
30    ///
31    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
32    /// other.significant_bits())`.
33    ///
34    /// # Examples
35    /// ```
36    /// use malachite_base::num::comparison::traits::PartialOrdAbs;
37    /// use malachite_nz::integer::Integer;
38    ///
39    /// assert!(Integer::from(-123).lt_abs(&Integer::from(-124)));
40    /// assert!(Integer::from(-123).le_abs(&Integer::from(-124)));
41    /// assert!(Integer::from(-124).gt_abs(&Integer::from(-123)));
42    /// assert!(Integer::from(-124).ge_abs(&Integer::from(-123)));
43    /// ```
44    fn cmp_abs(&self, other: &Integer) -> Ordering {
45        self.abs.cmp(&other.abs)
46    }
47}