Skip to main content

malachite_base/unsigned_vector/arithmetic/
mod_is_reduced.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::arithmetic::traits::ModIsReduced;
10use crate::num::basic::unsigneds::PrimitiveUnsigned;
11use crate::unsigned_vector::UnsignedVector;
12
13impl<T: PrimitiveUnsigned> ModIsReduced<T> for UnsignedVector<T> {
14    /// Returns whether an [`UnsignedVector`] is reduced modulo a `T` $m$; in other words, whether
15    /// every one of its elements is less than $m$.
16    ///
17    /// The 0-dimensional vector has no elements and is reduced modulo every $m$.
18    ///
19    /// $m$ cannot be zero.
20    ///
21    /// $f(v, m) = (\max_i v_i < m)$, with the maximum of no elements taken to be 0.
22    ///
23    /// # Worst-case complexity
24    /// $T(n) = O(n)$
25    ///
26    /// $M(n) = O(1)$
27    ///
28    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.dimension()`.
29    ///
30    /// # Panics
31    /// Panics if `m` is 0.
32    ///
33    /// # Examples
34    /// ```
35    /// use core::str::FromStr;
36    /// use malachite_base::num::arithmetic::traits::ModIsReduced;
37    /// use malachite_base::unsigned_vector::UnsignedVector;
38    ///
39    /// // The elements are 2, 3, and 1, so 4 is large enough and 3 is not.
40    /// let v = UnsignedVector::<u64>::from_str("(2, 3, 1)").unwrap();
41    /// assert_eq!(v.mod_is_reduced(&4), true);
42    /// assert_eq!(v.mod_is_reduced(&3), false);
43    ///
44    /// // The 0-dimensional vector is reduced modulo everything.
45    /// assert_eq!(
46    ///     UnsignedVector::<u64>::from_str("()")
47    ///         .unwrap()
48    ///         .mod_is_reduced(&1),
49    ///     true
50    /// );
51    /// ```
52    #[inline]
53    fn mod_is_reduced(&self, m: &T) -> bool {
54        assert_ne!(*m, T::ZERO, "the modulus is zero");
55        self.elements.iter().all(|x| x < m)
56    }
57}