malachite_nz/integer_vector/conversion/string/to_string.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 core::fmt::{Debug, Display, Formatter, Result};
11
12impl Display for IntegerVector {
13 /// Converts an [`IntegerVector`] to a [`String`].
14 ///
15 /// The elements are written in order, separated by `, ` and enclosed in parentheses, as a
16 /// vector's coordinates are written by hand. The 0-dimensional vector is `()`.
17 ///
18 /// # Worst-case complexity
19 /// $T(n) = O(n (\log n)^2 \log\log n)$
20 ///
21 /// $M(n) = O(n \log n)$
22 ///
23 /// where $T$ is time, $M$ is additional memory, and $n$ is the sum of the bits of the elements.
24 ///
25 /// # Examples
26 /// ```
27 /// use malachite_nz::integer::Integer;
28 /// use malachite_nz::integer_vector::IntegerVector;
29 ///
30 /// let v = IntegerVector {
31 /// elements: vec![Integer::from(1u32), Integer::from(2u32), Integer::from(3u32)],
32 /// };
33 /// assert_eq!(v.to_string(), "(1, 2, 3)");
34 /// assert_eq!(
35 /// IntegerVector {
36 /// elements: vec![Integer::from(5u32)]
37 /// }
38 /// .to_string(),
39 /// "(5)"
40 /// );
41 /// assert_eq!(
42 /// IntegerVector {
43 /// elements: Vec::new()
44 /// }
45 /// .to_string(),
46 /// "()"
47 /// );
48 /// ```
49 fn fmt(&self, f: &mut Formatter) -> Result {
50 f.write_str("(")?;
51 for (i, x) in self.elements.iter().enumerate() {
52 if i != 0 {
53 f.write_str(", ")?;
54 }
55 Display::fmt(x, f)?;
56 }
57 f.write_str(")")
58 }
59}
60
61impl Debug for IntegerVector {
62 /// Converts an [`IntegerVector`] to a [`String`].
63 ///
64 /// This is the same as the [`Display::fmt`] implementation, so that a collection of
65 /// [`IntegerVector`]s is written the same way its elements are displayed.
66 ///
67 /// # Worst-case complexity
68 /// $T(n) = O(n (\log n)^2 \log\log n)$
69 ///
70 /// $M(n) = O(n \log n)$
71 ///
72 /// where $T$ is time, $M$ is additional memory, and $n$ is the sum of the bits of the elements.
73 ///
74 /// # Examples
75 /// ```
76 /// use malachite_base::strings::ToDebugString;
77 /// use malachite_nz::integer::Integer;
78 /// use malachite_nz::integer_vector::IntegerVector;
79 ///
80 /// let xs = vec![
81 /// IntegerVector {
82 /// elements: vec![Integer::from(1u32), Integer::from(2u32)],
83 /// },
84 /// IntegerVector {
85 /// elements: Vec::new(),
86 /// },
87 /// ];
88 /// assert_eq!(xs[0].to_debug_string(), "(1, 2)");
89 /// assert_eq!(xs.to_debug_string(), "[(1, 2), ()]");
90 /// ```
91 #[inline]
92 fn fmt(&self, f: &mut Formatter) -> Result {
93 Display::fmt(self, f)
94 }
95}