malachite_q/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::Rational;
10
11macro_rules! impl_unsigned {
12    ($t: ident) => {
13        impl PartialEq<$t> for Rational {
14            /// Determines whether a [`Rational`] is equal to an unsigned primitive integer.
15            ///
16            /// # Worst-case complexity
17            /// $T(n) = O(n)$
18            ///
19            /// $M(n) = O(1)$
20            ///
21            /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
22            ///
23            /// # Examples
24            /// See [here](super::partial_eq_primitive_int#partial_eq).
25            #[inline]
26            fn eq(&self, other: &$t) -> bool {
27                self.sign && self.denominator == 1u32 && self.numerator == *other
28            }
29        }
30
31        impl PartialEq<Rational> for $t {
32            /// Determines whether an unsigned primitive integer is equal to a [`Rational`].
33            ///
34            /// # Worst-case complexity
35            /// $T(n) = O(n)$
36            ///
37            /// $M(n) = O(1)$
38            ///
39            /// where $T$ is time, $M$ is additional memory, and $n$ is `other.significant_bits()`.
40            ///
41            /// # Examples
42            /// See [here](super::partial_eq_primitive_int#partial_eq).
43            #[inline]
44            fn eq(&self, other: &Rational) -> bool {
45                other == self
46            }
47        }
48    };
49}
50apply_to_unsigneds!(impl_unsigned);
51
52macro_rules! impl_signed {
53    ($t: ident) => {
54        impl PartialEq<$t> for Rational {
55            /// Determines whether a [`Rational`] is equal to a signed primitive integer.
56            ///
57            /// # Worst-case complexity
58            /// $T(n) = O(n)$
59            ///
60            /// $M(n) = O(1)$
61            ///
62            /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
63            ///
64            /// # Examples
65            /// See [here](super::partial_eq_primitive_int#partial_eq).
66            fn eq(&self, other: &$t) -> bool {
67                self.sign == (*other >= 0)
68                    && self.denominator == 1
69                    && self.numerator == other.unsigned_abs()
70            }
71        }
72
73        impl PartialEq<Rational> for $t {
74            /// Determines whether a signed primitive integer is equal to a [`Rational`].
75            ///
76            /// # Worst-case complexity
77            /// $T(n) = O(n)$
78            ///
79            /// $M(n) = O(1)$
80            ///
81            /// where $T$ is time, $M$ is additional memory, and $n$ is `other.significant_bits()`.
82            ///
83            /// # Examples
84            /// See [here](super::partial_eq_primitive_int#partial_eq).
85            #[inline]
86            fn eq(&self, other: &Rational) -> bool {
87                other == self
88            }
89        }
90    };
91}
92apply_to_signeds!(impl_signed);