malachite_q/rational_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::rational_vector::RationalVector;
10use malachite_base::num::logic::traits::SignificantBits;
11
12impl SignificantBits for &RationalVector {
13 /// Returns the sum of the numbers of significant bits of the elements of a [`RationalVector`],
14 /// each element's count being the sum of the bits of its numerator and denominator, as for
15 /// [`Rational`](crate::Rational#impl-SignificantBits-for-%26Rational).
16 ///
17 /// This is the number of bits needed to store all of the elements, and 0 for the 0-dimensional
18 /// vector.
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 `self.dimension()`.
26 ///
27 /// # Examples
28 /// ```
29 /// use core::str::FromStr;
30 /// use malachite_base::num::logic::traits::SignificantBits;
31 /// use malachite_q::rational_vector::RationalVector;
32 ///
33 /// assert_eq!(
34 /// RationalVector::from_str("(1/2, -1/3)")
35 /// .unwrap()
36 /// .significant_bits(),
37 /// 6
38 /// );
39 /// assert_eq!(
40 /// RationalVector::from_str("()").unwrap().significant_bits(),
41 /// 0
42 /// );
43 /// ```
44 #[inline]
45 fn significant_bits(self) -> u64 {
46 self.elements
47 .iter()
48 .map(SignificantBits::significant_bits)
49 .sum()
50 }
51}