malachite_nz/integer/comparison/
partial_eq_primitive_int.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 malachite_base::num::arithmetic::traits::UnsignedAbs;
12use malachite_base::num::basic::traits::Zero;
13
14macro_rules! impl_unsigned {
15    ($t: ident) => {
16        impl PartialEq<$t> for Integer {
17            /// Determines whether an [`Integer`] is equal to an unsigned primitive integer.
18            ///
19            /// # Worst-case complexity
20            /// Constant time and additional memory.
21            ///
22            /// # Examples
23            /// See [here](super::partial_eq_primitive_int#partial_eq).
24            #[inline]
25            fn eq(&self, other: &$t) -> bool {
26                self.sign && self.abs == *other
27            }
28        }
29
30        impl PartialEq<Integer> for $t {
31            /// Determines whether an unsigned primitive integer is equal to an [`Integer`].
32            ///
33            /// # Worst-case complexity
34            /// Constant time and additional memory.
35            ///
36            /// # Examples
37            /// See [here](super::partial_eq_primitive_int#partial_eq).
38            #[inline]
39            fn eq(&self, other: &Integer) -> bool {
40                other == self
41            }
42        }
43    };
44}
45apply_to_unsigneds!(impl_unsigned);
46
47fn eq_signed<U, S: Copy + Ord + UnsignedAbs<Output = U> + Zero>(x: &Integer, other: &S) -> bool
48where
49    Natural: PartialEq<U>,
50{
51    x.sign == (*other >= S::ZERO) && x.abs == other.unsigned_abs()
52}
53
54macro_rules! impl_signed {
55    ($t: ident) => {
56        impl PartialEq<$t> for Integer {
57            /// Determines whether an [`Integer`] is equal to a signed primitive integer.
58            ///
59            /// # Worst-case complexity
60            /// Constant time and additional memory.
61            ///
62            /// # Examples
63            /// See [here](super::partial_eq_primitive_int#partial_eq).
64            fn eq(&self, other: &$t) -> bool {
65                eq_signed(self, other)
66            }
67        }
68
69        impl PartialEq<Integer> for $t {
70            /// Determines whether a signed primitive integer is equal to an [`Integer`].
71            ///
72            /// # Worst-case complexity
73            /// Constant time and additional memory.
74            ///
75            /// # Examples
76            /// See [here](super::partial_eq_primitive_int#partial_eq).
77            #[inline]
78            fn eq(&self, other: &Integer) -> bool {
79                other == self
80            }
81        }
82    };
83}
84apply_to_signeds!(impl_signed);