malachite_base/unsigned_polynomial/comparison/eq_truncated.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::polynomial::{EqTruncated, slices_eq_truncated};
11use crate::unsigned_polynomial::UnsignedPolynomial;
12
13impl<T: PrimitiveUnsigned> EqTruncated for UnsignedPolynomial<T> {
14 /// Determines whether an [`UnsignedPolynomial`] and another agree below $x^{\mathrm{len}}$:
15 /// that is, whether they have the same coefficient of $x^i$ for every $i$ less than `len`.
16 ///
17 /// Any two polynomials agree below $x^0$, and once `len` is at least both of their lengths,
18 /// they agree exactly when they are equal.
19 ///
20 /// # Worst-case complexity
21 /// $T(n) = O(n)$
22 ///
23 /// $M(n) = O(1)$
24 ///
25 /// where $T$ is time, $M$ is additional memory, and $n$ is `min(len, max(self.len(),
26 /// other.len()))`.
27 ///
28 /// # Examples
29 /// See [here](super::eq_truncated#eq_truncated).
30 ///
31 /// This is equivalent to `nmod_poly_equal_trunc` from `nmod_poly/equal_trunc.c`, FLINT 3.6.0.
32 #[inline]
33 fn eq_truncated(&self, other: &Self, len: u64) -> bool {
34 slices_eq_truncated(
35 &self.coefficients,
36 &other.coefficients,
37 len,
38 |&x| x == T::ZERO,
39 |&y| y == T::ZERO,
40 |x, y| x == y,
41 )
42 }
43}