malachite_base/num/comparison/
traits.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 core::cmp::Ordering::{self, *};
10
11/// Determines equality between the absolute values of two numbers.
12pub trait EqAbs<Rhs: ?Sized = Self> {
13    /// Compares the absolute values of two numbers for equality, taking both by reference.
14    fn eq_abs(&self, other: &Rhs) -> bool;
15
16    /// Compares the absolute values of two numbers for inequality, taking both by reference.
17    ///
18    /// # Worst-case complexity
19    /// Same as the time and additional memory complexity of [`eq_abs`](Self::eq_abs).
20    #[inline]
21    fn ne_abs(&self, other: &Rhs) -> bool {
22        !self.eq_abs(other)
23    }
24}
25
26/// Determines equality between the absolute values of two numbers, where some pairs of numbers may
27/// not be comparable.
28pub trait PartialOrdAbs<Rhs: ?Sized = Self> {
29    /// Compares the absolute values of two numbers, taking both by reference.
30    ///
31    /// If the two values are not comparable, `None` is returned.
32    fn partial_cmp_abs(&self, other: &Rhs) -> Option<Ordering>;
33
34    /// Determines whether the absolute value of one number is less than the absolute value of
35    /// another.
36    ///
37    /// # Worst-case complexity
38    /// Same as the time and additional memory complexity of
39    /// [`partial_cmp_abs`](Self::partial_cmp_abs).
40    #[inline]
41    fn lt_abs(&self, other: &Rhs) -> bool {
42        matches!(self.partial_cmp_abs(other), Some(Less))
43    }
44
45    /// Determines whether the absolute value of one number is less than or equal to the absolute
46    /// value of another.
47    ///
48    /// # Worst-case complexity
49    /// Same as the time and additional memory complexity of
50    /// [`partial_cmp_abs`](Self::partial_cmp_abs).
51    #[inline]
52    fn le_abs(&self, other: &Rhs) -> bool {
53        matches!(self.partial_cmp_abs(other), Some(Less | Equal))
54    }
55
56    /// Determines whether the absolute value of one number is greater than the absolute value of
57    /// another.
58    ///
59    /// # Worst-case complexity
60    /// Same as the time and additional memory complexity of
61    /// [`partial_cmp_abs`](Self::partial_cmp_abs).
62    #[inline]
63    fn gt_abs(&self, other: &Rhs) -> bool {
64        matches!(self.partial_cmp_abs(other), Some(Greater))
65    }
66
67    /// Determines whether the absolute value of one number is greater than or equal to the absolute
68    /// value of another.
69    ///
70    /// # Worst-case complexity
71    /// Same as the time and additional memory complexity of
72    /// [`partial_cmp_abs`](Self::partial_cmp_abs).
73    #[inline]
74    fn ge_abs(&self, other: &Rhs) -> bool {
75        matches!(self.partial_cmp_abs(other), Some(Greater | Equal))
76    }
77}
78
79/// Compares the absolute values of two numbers.
80pub trait OrdAbs: Eq + PartialOrdAbs<Self> {
81    fn cmp_abs(&self, other: &Self) -> Ordering;
82}