malachite_base/rational_sequences/conversion.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_sequences::{RationalSequence, rational_sequence_reduce};
10use alloc::vec;
11use alloc::vec::Vec;
12
13impl<T: Eq> RationalSequence<T> {
14 /// Converts a [`Vec`] to a finite [`RationalSequence`].
15 ///
16 /// # Worst-case complexity
17 /// Constant time and additional memory.
18 ///
19 /// # Examples
20 /// ```
21 /// use malachite_base::rational_sequences::RationalSequence;
22 ///
23 /// assert_eq!(RationalSequence::<u8>::from_vec(vec![]).to_string(), "[]");
24 /// assert_eq!(
25 /// RationalSequence::<u8>::from_vec(vec![1, 2]).to_string(),
26 /// "[1, 2]"
27 /// );
28 /// ```
29 pub const fn from_vec(non_repeating: Vec<T>) -> Self {
30 Self {
31 non_repeating,
32 repeating: vec![],
33 }
34 }
35
36 /// Converts two [`Vec`]s to a finite [`RationalSequence`]. The first [`Vec`] is the
37 /// nonrepeating part and the second is the repeating part.
38 ///
39 /// # Worst-case complexity
40 /// $T(n, m) = O(n + m^{1+\varepsilon})$ for all $\varepsilon > 0$
41 ///
42 /// $M(n, m) = O(1)$
43 ///
44 /// where $T$ is time, $M$ is additional memory, $n$ is `non_repeating.len()`, and $m$ is
45 /// `repeating.len()`.
46 ///
47 /// # Examples
48 /// ```
49 /// use malachite_base::rational_sequences::RationalSequence;
50 ///
51 /// assert_eq!(
52 /// RationalSequence::<u8>::from_vecs(vec![], vec![]).to_string(),
53 /// "[]"
54 /// );
55 /// assert_eq!(
56 /// RationalSequence::<u8>::from_vecs(vec![], vec![1, 2]).to_string(),
57 /// "[[1, 2]]"
58 /// );
59 /// assert_eq!(
60 /// RationalSequence::<u8>::from_vecs(vec![1, 2], vec![]).to_string(),
61 /// "[1, 2]"
62 /// );
63 /// assert_eq!(
64 /// RationalSequence::<u8>::from_vecs(vec![1, 2], vec![3, 4]).to_string(),
65 /// "[1, 2, [3, 4]]"
66 /// );
67 /// assert_eq!(
68 /// RationalSequence::<u8>::from_vecs(vec![1, 2, 3], vec![4, 3]).to_string(),
69 /// "[1, 2, [3, 4]]"
70 /// );
71 /// ```
72 pub fn from_vecs(mut non_repeating: Vec<T>, mut repeating: Vec<T>) -> Self {
73 rational_sequence_reduce(&mut non_repeating, &mut repeating);
74 Self {
75 non_repeating,
76 repeating,
77 }
78 }
79
80 /// Converts a [`RationalSequence`] to a pair of [`Vec`]s containing the non-repeating and
81 /// repeating parts, taking the [`RationalSequence`] by value.
82 ///
83 /// # Worst-case complexity
84 /// Constant time and additional memory.
85 ///
86 /// # Examples
87 /// ```
88 /// use malachite_base::rational_sequences::RationalSequence;
89 ///
90 /// assert_eq!(
91 /// RationalSequence::from_slices(&[1, 2], &[3, 4]).into_vecs(),
92 /// (vec![1, 2], vec![3, 4])
93 /// );
94 /// ```
95 #[allow(clippy::missing_const_for_fn)] // can't be const because of destructors
96 pub fn into_vecs(self) -> (Vec<T>, Vec<T>) {
97 (self.non_repeating, self.repeating)
98 }
99
100 /// Returns references to the non-repeating and repeating parts of a [`RationalSequence`].
101 ///
102 /// # Worst-case complexity
103 /// Constant time and additional memory.
104 ///
105 /// # Examples
106 /// ```
107 /// use malachite_base::rational_sequences::RationalSequence;
108 ///
109 /// assert_eq!(
110 /// RationalSequence::from_slices(&[1u8, 2], &[3, 4]).slices_ref(),
111 /// (&[1u8, 2][..], &[3u8, 4][..])
112 /// );
113 /// ```
114 #[allow(clippy::missing_const_for_fn)]
115 pub fn slices_ref(&self) -> (&[T], &[T]) {
116 (&self.non_repeating, &self.repeating)
117 }
118}
119
120impl<T: Clone + Eq> RationalSequence<T> {
121 /// Converts a slice to a finite [`RationalSequence`].
122 ///
123 /// # Worst-case complexity
124 /// $T(n) = O(n)$
125 ///
126 /// $M(n) = O(n)$
127 ///
128 /// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
129 ///
130 /// # Examples
131 /// ```
132 /// use malachite_base::rational_sequences::RationalSequence;
133 ///
134 /// assert_eq!(RationalSequence::<u8>::from_slice(&[]).to_string(), "[]");
135 /// assert_eq!(
136 /// RationalSequence::<u8>::from_slice(&[1, 2]).to_string(),
137 /// "[1, 2]"
138 /// );
139 /// ```
140 pub fn from_slice(non_repeating: &[T]) -> Self {
141 Self {
142 non_repeating: non_repeating.to_vec(),
143 repeating: vec![],
144 }
145 }
146
147 /// Converts two slices to a finite [`RationalSequence`]. The first slice is the nonrepeating
148 /// part and the second is the repeating part.
149 ///
150 /// # Worst-case complexity
151 /// $T(n, m) = O(n + m^{1+\varepsilon})$ for all $\varepsilon > 0$
152 ///
153 /// $M(n, m) = O(n + m)$
154 ///
155 /// where $T$ is time, $M$ is additional memory, $n$ is `non_repeating.len()`, and $m$ is
156 /// `repeating.len()`.
157 ///
158 /// # Examples
159 /// ```
160 /// use malachite_base::rational_sequences::RationalSequence;
161 ///
162 /// assert_eq!(
163 /// RationalSequence::<u8>::from_slices(&[], &[]).to_string(),
164 /// "[]"
165 /// );
166 /// assert_eq!(
167 /// RationalSequence::<u8>::from_slices(&[], &[1, 2]).to_string(),
168 /// "[[1, 2]]"
169 /// );
170 /// assert_eq!(
171 /// RationalSequence::<u8>::from_slices(&[1, 2], &[]).to_string(),
172 /// "[1, 2]"
173 /// );
174 /// assert_eq!(
175 /// RationalSequence::<u8>::from_slices(&[1, 2], &[3, 4]).to_string(),
176 /// "[1, 2, [3, 4]]"
177 /// );
178 /// assert_eq!(
179 /// RationalSequence::<u8>::from_slices(&[1, 2, 3], &[4, 3]).to_string(),
180 /// "[1, 2, [3, 4]]"
181 /// );
182 /// ```
183 pub fn from_slices(non_repeating: &[T], repeating: &[T]) -> Self {
184 let mut non_repeating = non_repeating.to_vec();
185 let mut repeating = repeating.to_vec();
186 rational_sequence_reduce(&mut non_repeating, &mut repeating);
187 Self {
188 non_repeating,
189 repeating,
190 }
191 }
192
193 /// Converts a [`RationalSequence`] to a pair of [`Vec`]s containing the non-repeating and
194 /// repeating parts, taking the [`RationalSequence`] by reference.
195 ///
196 /// # Worst-case complexity
197 /// $T(n) = O(n)$
198 ///
199 /// $M(n) = O(n)$
200 ///
201 /// where $T$ is time, $M$ is additional memory, and $n$ is `xs.component_len()`.
202 ///
203 /// # Examples
204 /// ```
205 /// use malachite_base::rational_sequences::RationalSequence;
206 ///
207 /// assert_eq!(
208 /// RationalSequence::from_slices(&[1, 2], &[3, 4]).to_vecs(),
209 /// (vec![1, 2], vec![3, 4])
210 /// );
211 /// ```
212 pub fn to_vecs(&self) -> (Vec<T>, Vec<T>) {
213 (self.non_repeating.clone(), self.repeating.clone())
214 }
215}