malachite_base/num/comparison/eq_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::num::arithmetic::traits::UnsignedAbs;
10use crate::num::comparison::traits::EqAbs;
11
12macro_rules! impl_eq_abs_unsigned {
13 ($t:ident) => {
14 impl EqAbs<$t> for $t {
15 /// Compares the absolute values of two numbers for equality, taking both by reference.
16 ///
17 /// For unsigned values, this is the same as ordinary equality.
18 ///
19 /// # Worst-case complexity
20 /// Constant time and additional memory.
21 ///
22 /// # Examples
23 /// See [here](super::eq_abs#eq_abs).
24 #[inline]
25 fn eq_abs(&self, other: &Self) -> bool {
26 self == other
27 }
28 }
29 };
30}
31apply_to_unsigneds!(impl_eq_abs_unsigned);
32
33fn eq_abs_signed<U: Eq, S: Copy + UnsignedAbs<Output = U>>(x: &S, y: &S) -> bool {
34 x.unsigned_abs() == y.unsigned_abs()
35}
36
37macro_rules! impl_eq_abs_signed {
38 ($t:ident) => {
39 impl EqAbs<$t> for $t {
40 /// Compares the absolute values of two numbers for equality, taking both by reference.
41 ///
42 /// # Worst-case complexity
43 /// Constant time and additional memory.
44 ///
45 /// # Examples
46 /// See [here](super::eq_abs#eq_abs).
47 #[inline]
48 fn eq_abs(&self, other: &Self) -> bool {
49 eq_abs_signed(self, other)
50 }
51 }
52 };
53}
54apply_to_signeds!(impl_eq_abs_signed);