malachite_base/rational_sequences/
conversion.rs

1// Copyright © 2025 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>) -> RationalSequence<T> {
30        RationalSequence {
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>) -> RationalSequence<T> {
73        rational_sequence_reduce(&mut non_repeating, &mut repeating);
74        RationalSequence {
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    pub fn slices_ref(&self) -> (&[T], &[T]) {
115        (&self.non_repeating, &self.repeating)
116    }
117}
118
119impl<T: Clone + Eq> RationalSequence<T> {
120    /// Converts a slice to a finite [`RationalSequence`].
121    ///
122    /// # Worst-case complexity
123    /// $T(n) = O(n)$
124    ///
125    /// $M(n) = O(n)$
126    ///
127    /// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
128    ///
129    /// # Examples
130    /// ```
131    /// use malachite_base::rational_sequences::RationalSequence;
132    ///
133    /// assert_eq!(RationalSequence::<u8>::from_slice(&[]).to_string(), "[]");
134    /// assert_eq!(
135    ///     RationalSequence::<u8>::from_slice(&[1, 2]).to_string(),
136    ///     "[1, 2]"
137    /// );
138    /// ```
139    pub fn from_slice(non_repeating: &[T]) -> RationalSequence<T> {
140        RationalSequence {
141            non_repeating: non_repeating.to_vec(),
142            repeating: vec![],
143        }
144    }
145
146    /// Converts two slices to a finite [`RationalSequence`]. The first slice is the nonrepeating
147    /// part and the second is the repeating part.
148    ///
149    /// # Worst-case complexity
150    /// $T(n, m) = O(n + m^{1+\varepsilon})$ for all $\varepsilon > 0$
151    ///
152    /// $M(n, m) = O(n + m)$
153    ///
154    /// where $T$ is time, $M$ is additional memory, $n$ is `non_repeating.len()`, and $m$ is
155    /// `repeating.len()`.
156    ///
157    /// # Examples
158    /// ```
159    /// use malachite_base::rational_sequences::RationalSequence;
160    ///
161    /// assert_eq!(
162    ///     RationalSequence::<u8>::from_slices(&[], &[]).to_string(),
163    ///     "[]"
164    /// );
165    /// assert_eq!(
166    ///     RationalSequence::<u8>::from_slices(&[], &[1, 2]).to_string(),
167    ///     "[[1, 2]]"
168    /// );
169    /// assert_eq!(
170    ///     RationalSequence::<u8>::from_slices(&[1, 2], &[]).to_string(),
171    ///     "[1, 2]"
172    /// );
173    /// assert_eq!(
174    ///     RationalSequence::<u8>::from_slices(&[1, 2], &[3, 4]).to_string(),
175    ///     "[1, 2, [3, 4]]"
176    /// );
177    /// assert_eq!(
178    ///     RationalSequence::<u8>::from_slices(&[1, 2, 3], &[4, 3]).to_string(),
179    ///     "[1, 2, [3, 4]]"
180    /// );
181    /// ```
182    pub fn from_slices(non_repeating: &[T], repeating: &[T]) -> RationalSequence<T> {
183        let mut non_repeating = non_repeating.to_vec();
184        let mut repeating = repeating.to_vec();
185        rational_sequence_reduce(&mut non_repeating, &mut repeating);
186        RationalSequence {
187            non_repeating,
188            repeating,
189        }
190    }
191
192    /// Converts a [`RationalSequence`] to a pair of [`Vec`]s containing the non-repeating and
193    /// repeating parts, taking the [`RationalSequence`] by reference.
194    ///
195    /// # Worst-case complexity
196    /// $T(n) = O(n)$
197    ///
198    /// $M(n) = O(n)$
199    ///
200    /// where $T$ is time, $M$ is additional memory, and $n$ is `xs.component_len()`.
201    ///
202    /// # Examples
203    /// ```
204    /// use malachite_base::rational_sequences::RationalSequence;
205    ///
206    /// assert_eq!(
207    ///     RationalSequence::from_slices(&[1, 2], &[3, 4]).to_vecs(),
208    ///     (vec![1, 2], vec![3, 4])
209    /// );
210    /// ```
211    pub fn to_vecs(&self) -> (Vec<T>, Vec<T>) {
212        (self.non_repeating.clone(), self.repeating.clone())
213    }
214}