Skip to main content

malachite_nz/integer_polynomial/comparison/
partial_eq_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::integer_polynomial::IntegerPolynomial;
10
11macro_rules! impl_partial_eq_primitive_int {
12    ($t: ident) => {
13        impl PartialEq<$t> for IntegerPolynomial {
14            /// Determines whether an [`IntegerPolynomial`] is equal to a value of a primitive
15            /// integer type.
16            ///
17            /// The polynomial is equal to the value when it is the constant polynomial with that
18            /// value, so the zero polynomial is equal to 0 and nothing else, and no polynomial of
19            /// positive degree is equal to any value. In particular, `p == 0` and `p == 1` test
20            /// whether `p` is the zero polynomial or the polynomial 1, without building either.
21            ///
22            /// # Worst-case complexity
23            /// Constant time and additional memory.
24            ///
25            /// # Examples
26            /// See [here](super::partial_eq_primitive_int#partial_eq).
27            fn eq(&self, other: &$t) -> bool {
28                match self.coefficients.as_slice() {
29                    [] => *other == 0,
30                    [c] => *c == *other,
31                    _ => false,
32                }
33            }
34        }
35
36        impl PartialEq<IntegerPolynomial> for $t {
37            /// Determines whether a value of a primitive integer type is equal to an
38            /// [`IntegerPolynomial`].
39            ///
40            /// The value is equal to the polynomial when the polynomial is the constant polynomial
41            /// with that value, so 0 is equal to the zero polynomial.
42            ///
43            /// # Worst-case complexity
44            /// Constant time and additional memory.
45            ///
46            /// # Examples
47            /// See [here](super::partial_eq_primitive_int#partial_eq).
48            #[inline]
49            fn eq(&self, other: &IntegerPolynomial) -> bool {
50                other == self
51            }
52        }
53    };
54}
55apply_to_primitive_ints!(impl_partial_eq_primitive_int);