malachite_nz/natural_polynomial/comparison/partial_eq_integer.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::Integer;
10use crate::natural_polynomial::NaturalPolynomial;
11
12impl PartialEq<Integer> for NaturalPolynomial {
13 /// Determines whether a [`NaturalPolynomial`] is equal to an [`Integer`].
14 ///
15 /// The polynomial is equal to the [`Integer`] when it is the constant polynomial with that
16 /// value, so the zero polynomial is equal to 0 and nothing else, no polynomial is equal to a
17 /// negative [`Integer`], and no polynomial of positive degree is equal to any [`Integer`].
18 ///
19 /// # Worst-case complexity
20 /// $T(n) = O(n)$
21 ///
22 /// $M(n) = O(1)$
23 ///
24 /// where $T$ is time, $M$ is additional memory, and $n$ is
25 /// `min(self.coefficient(0).significant_bits(), other.significant_bits())`.
26 ///
27 /// # Examples
28 /// See [here](super::partial_eq_integer#partial_eq).
29 fn eq(&self, other: &Integer) -> bool {
30 match self.coefficients.as_slice() {
31 [] => *other == 0u32,
32 [c] => other == c,
33 _ => false,
34 }
35 }
36}
37
38impl PartialEq<NaturalPolynomial> for Integer {
39 /// Determines whether an [`Integer`] is equal to a [`NaturalPolynomial`].
40 ///
41 /// The [`Integer`] is equal to the polynomial when the polynomial is the constant polynomial
42 /// with that value, so 0 is equal to the zero polynomial and a negative [`Integer`] is equal to
43 /// no polynomial.
44 ///
45 /// # Worst-case complexity
46 /// $T(n) = O(n)$
47 ///
48 /// $M(n) = O(1)$
49 ///
50 /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
51 /// other.coefficient(0).significant_bits())`.
52 ///
53 /// # Examples
54 /// See [here](super::partial_eq_integer#partial_eq).
55 #[inline]
56 fn eq(&self, other: &NaturalPolynomial) -> bool {
57 other == self
58 }
59}