Skip to main content

malachite_nz/integer_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::integer_vector::IntegerVector;
10use malachite_base::num::logic::traits::SignificantBits;
11
12impl SignificantBits for &IntegerVector {
13    /// Returns the sum of the numbers of significant bits of the elements of an [`IntegerVector`].
14    ///
15    /// This is the number of bits needed to store all of the elements, and 0 for the 0-dimensional
16    /// vector.
17    ///
18    /// # Worst-case complexity
19    /// $T(n) = O(n)$
20    ///
21    /// $M(n) = O(1)$
22    ///
23    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.dimension()`.
24    ///
25    /// # Examples
26    /// ```
27    /// use core::str::FromStr;
28    /// use malachite_base::num::logic::traits::SignificantBits;
29    /// use malachite_nz::integer_vector::IntegerVector;
30    ///
31    /// assert_eq!(
32    ///     IntegerVector::from_str("(1, -5, 2)")
33    ///         .unwrap()
34    ///         .significant_bits(),
35    ///     6
36    /// );
37    /// assert_eq!(IntegerVector::from_str("()").unwrap().significant_bits(), 0);
38    /// ```
39    #[inline]
40    fn significant_bits(self) -> u64 {
41        self.elements
42            .iter()
43            .map(SignificantBits::significant_bits)
44            .sum()
45    }
46}