malachite_base/unsigned_vector/arithmetic/mod_element_sum.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::ModElementSum;
10use crate::num::basic::unsigneds::PrimitiveUnsigned;
11use crate::unsigned_vector::UnsignedVector;
12
13impl<T: PrimitiveUnsigned> ModElementSum<T> for UnsignedVector<T> {
14 type Output = T;
15
16 /// Returns the sum of the elements of an [`UnsignedVector`] modulo another number $m$, taking
17 /// the vector by reference. The elements must already be reduced.
18 ///
19 /// The 0-dimensional vector has no elements, and their sum is 0.
20 ///
21 /// $$
22 /// f(v, m) = \sum_i v_i \bmod m.
23 /// $$
24 ///
25 /// # Worst-case complexity
26 /// $T(n) = O(n)$
27 ///
28 /// $M(n) = O(1)$
29 ///
30 /// where $T$ is time, $M$ is additional memory, $n$ is the total number of bits of the
31 /// elements.
32 ///
33 /// # Panics
34 /// Panics if `m` is zero, or if any element is greater than or equal to `m`.
35 ///
36 /// # Examples
37 /// ```
38 /// use core::str::FromStr;
39 /// use malachite_base::num::arithmetic::traits::ModElementSum;
40 /// use malachite_base::unsigned_vector::UnsignedVector;
41 ///
42 /// let v = UnsignedVector::<u8>::from_str("(5, 6, 7)").unwrap();
43 /// assert_eq!(v.to_mod_element_sum(10), 8);
44 /// let v = UnsignedVector::<u8>::from_str("()").unwrap();
45 /// assert_eq!(v.to_mod_element_sum(10), 0);
46 /// ```
47 #[inline]
48 fn to_mod_element_sum(&self, m: T) -> T {
49 T::mod_sum(self.elements.iter(), m)
50 }
51
52 /// Returns the sum of the elements of an [`UnsignedVector`] modulo another number $m$, taking
53 /// the vector by value. The elements must already be reduced.
54 ///
55 /// The 0-dimensional vector has no elements, and their sum is 0.
56 ///
57 /// $$
58 /// f(v, m) = \sum_i v_i \bmod m.
59 /// $$
60 ///
61 /// # Worst-case complexity
62 /// $T(n) = O(n)$
63 ///
64 /// $M(n) = O(1)$
65 ///
66 /// where $T$ is time, $M$ is additional memory, $n$ is the total number of bits of the
67 /// elements.
68 ///
69 /// # Panics
70 /// Panics if `m` is zero, or if any element is greater than or equal to `m`.
71 ///
72 /// # Examples
73 /// ```
74 /// use core::str::FromStr;
75 /// use malachite_base::num::arithmetic::traits::ModElementSum;
76 /// use malachite_base::unsigned_vector::UnsignedVector;
77 ///
78 /// let v = UnsignedVector::<u8>::from_str("(5, 6, 7)").unwrap();
79 /// assert_eq!(v.into_mod_element_sum(10), 8);
80 /// let v = UnsignedVector::<u8>::from_str("()").unwrap();
81 /// assert_eq!(v.into_mod_element_sum(10), 0);
82 /// ```
83 #[inline]
84 fn into_mod_element_sum(self, m: T) -> T {
85 T::mod_sum(self.elements.into_iter(), m)
86 }
87}