Skip to main content

malachite_base/unsigned_polynomial/comparison/
partial_eq_unsigned.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::num::basic::unsigneds::PrimitiveUnsigned;
10use crate::unsigned_polynomial::UnsignedPolynomial;
11
12impl<T: PrimitiveUnsigned> PartialEq<T> for UnsignedPolynomial<T> {
13    /// Determines whether an [`UnsignedPolynomial`] is equal to a value of its coefficient type.
14    ///
15    /// The polynomial is equal to the value when it is the constant polynomial with that value, so
16    /// the zero polynomial is equal to 0 and nothing else, and no polynomial of positive degree is
17    /// equal to any value. In particular, `p == 0` and `p == 1` test whether `p` is the zero
18    /// polynomial or the polynomial 1, without building either.
19    ///
20    /// # Worst-case complexity
21    /// Constant time and additional memory.
22    ///
23    /// # Examples
24    /// See [here](super::partial_eq_unsigned#partial_eq).
25    fn eq(&self, other: &T) -> bool {
26        match self.coefficients.as_slice() {
27            [] => *other == T::ZERO,
28            [c] => c == other,
29            _ => false,
30        }
31    }
32}
33
34macro_rules! impl_partial_eq_unsigned {
35    ($t: ident) => {
36        impl PartialEq<UnsignedPolynomial<$t>> for $t {
37            /// Determines whether a value is equal to an [`UnsignedPolynomial`] with coefficients
38            /// of the value's type.
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_unsigned#partial_eq).
48            #[inline]
49            fn eq(&self, other: &UnsignedPolynomial<$t>) -> bool {
50                other == self
51            }
52        }
53    };
54}
55apply_to_unsigneds!(impl_partial_eq_unsigned);