malachite_base/unsigned_vector/logic/significant_bits.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::num::logic::traits::SignificantBits;
11use crate::unsigned_vector::UnsignedVector;
12
13impl<T: PrimitiveUnsigned> SignificantBits for &UnsignedVector<T> {
14 /// Returns the sum of the numbers of significant bits of the elements of an [`UnsignedVector`].
15 ///
16 /// This is the number of bits needed to store all of the elements, and 0 for the 0-dimensional
17 /// vector.
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 `self.dimension()`.
25 ///
26 /// # Examples
27 /// ```
28 /// use core::str::FromStr;
29 /// use malachite_base::num::logic::traits::SignificantBits;
30 /// use malachite_base::unsigned_vector::UnsignedVector;
31 ///
32 /// assert_eq!(
33 /// UnsignedVector::<u8>::from_str("(1, 5, 2)")
34 /// .unwrap()
35 /// .significant_bits(),
36 /// 6
37 /// );
38 /// assert_eq!(
39 /// UnsignedVector::<u8>::from_str("()")
40 /// .unwrap()
41 /// .significant_bits(),
42 /// 0
43 /// );
44 /// ```
45 #[inline]
46 fn significant_bits(self) -> u64 {
47 self.elements.iter().map(|&x| x.significant_bits()).sum()
48 }
49}