malachite_nz/gaussian_integer/comparison/eq_abs_primitive_int.rs
1// Copyright © 2026 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::gaussian_integer::GaussianInteger;
10use crate::integer::Integer;
11use malachite_base::num::arithmetic::traits::AbsSquared;
12use malachite_base::num::comparison::traits::{EqAbs, PartialOrdAbs};
13
14macro_rules! impl_unsigned {
15 ($t: ident) => {
16 impl EqAbs<$t> for GaussianInteger {
17 /// Determines whether the absolute values of a [`GaussianInteger`] and an unsigned
18 /// primitive integer are equal.
19 ///
20 /// # Worst-case complexity
21 /// $T(n) = O(n \log n \log\log n)$
22 ///
23 /// $M(n) = O(n \log n)$
24 ///
25 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of
26 /// significant bits of the real and imaginary parts of `self`.
27 ///
28 /// # Examples
29 /// See [here](super::eq_abs_primitive_int#eq_abs).
30 fn eq_abs(&self, other: &$t) -> bool {
31 if self.imaginary == 0u32 {
32 self.real.eq_abs(other)
33 } else if self.real == 0u32 {
34 self.imaginary.eq_abs(other)
35 } else {
36 self.real.lt_abs(other)
37 && self.imaginary.lt_abs(other)
38 && self.abs_squared() == Integer::from(*other).abs_squared()
39 }
40 }
41 }
42
43 impl EqAbs<GaussianInteger> for $t {
44 /// Determines whether the absolute values of an unsigned primitive integer and a
45 /// [`GaussianInteger`] are equal.
46 ///
47 /// # Worst-case complexity
48 /// $T(n) = O(n \log n \log\log n)$
49 ///
50 /// $M(n) = O(n \log n)$
51 ///
52 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of
53 /// significant bits of the real and imaginary parts of `other`.
54 ///
55 /// # Examples
56 /// See [here](super::eq_abs_primitive_int#eq_abs).
57 #[inline]
58 fn eq_abs(&self, other: &GaussianInteger) -> bool {
59 other.eq_abs(self)
60 }
61 }
62 };
63}
64apply_to_unsigneds!(impl_unsigned);
65
66macro_rules! impl_signed {
67 ($t: ident) => {
68 impl EqAbs<$t> for GaussianInteger {
69 /// Determines whether the absolute values of a [`GaussianInteger`] and a signed
70 /// primitive integer are equal.
71 ///
72 /// # Worst-case complexity
73 /// $T(n) = O(n \log n \log\log n)$
74 ///
75 /// $M(n) = O(n \log n)$
76 ///
77 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of
78 /// significant bits of the real and imaginary parts of `self`.
79 ///
80 /// # Examples
81 /// See [here](super::eq_abs_primitive_int#eq_abs).
82 fn eq_abs(&self, other: &$t) -> bool {
83 if self.imaginary == 0u32 {
84 self.real.eq_abs(other)
85 } else if self.real == 0u32 {
86 self.imaginary.eq_abs(other)
87 } else {
88 self.real.lt_abs(other)
89 && self.imaginary.lt_abs(other)
90 && self.abs_squared() == Integer::from(*other).abs_squared()
91 }
92 }
93 }
94
95 impl EqAbs<GaussianInteger> for $t {
96 /// Determines whether the absolute values of a signed primitive integer and a
97 /// [`GaussianInteger`] are equal.
98 ///
99 /// # Worst-case complexity
100 /// $T(n) = O(n \log n \log\log n)$
101 ///
102 /// $M(n) = O(n \log n)$
103 ///
104 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of
105 /// significant bits of the real and imaginary parts of `other`.
106 ///
107 /// # Examples
108 /// See [here](super::eq_abs_primitive_int#eq_abs).
109 #[inline]
110 fn eq_abs(&self, other: &GaussianInteger) -> bool {
111 other.eq_abs(self)
112 }
113 }
114 };
115}
116apply_to_signeds!(impl_signed);