pub struct RationalSequence<T: Eq> { /* private fields */ }
Expand description
A RationalSequence
is a sequence that is either finite or eventually repeating, just like
the digits of a rational number.
In testing, the set of rational sequences may be used as a proxy for the set of all sequences, which is too large to work with.
Implementations
sourceimpl<T: Eq> RationalSequence<T>
impl<T: Eq> RationalSequence<T>
sourcepub fn get(&self, i: usize) -> Option<&T>
pub fn get(&self, i: usize) -> Option<&T>
Gets a reference to an element of a RationalSequence
at an index.
If the index is greater than or equal to the length of the sequence, None
is returned.
Worst-case complexity
Constant time and additional memory.
Examples
use malachite_base::rational_sequences::RationalSequence;
assert_eq!(RationalSequence::from_slices(&[1, 2], &[3, 4]).get(1), Some(&2));
assert_eq!(RationalSequence::from_slices(&[1, 2], &[3, 4]).get(10), Some(&3));
sourceimpl<T: Clone + Eq> RationalSequence<T>
impl<T: Clone + Eq> RationalSequence<T>
sourcepub fn mutate<F: FnOnce(&mut T) -> U, U>(&mut self, i: usize, f: F) -> U
pub fn mutate<F: FnOnce(&mut T) -> U, U>(&mut self, i: usize, f: F) -> U
Mutates an element of a RationalSequence
at an index using a provided closure, and then
returns whatever the closure returns.
If the index is greater than or equal to the length of the sequence, this function panics.
Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is index
.
Panics
Panics if index
is greater than or equal to the length of this sequence.
Examples
use malachite_base::rational_sequences::RationalSequence;
let mut xs = RationalSequence::from_slices(&[1, 2], &[3, 4]);
assert_eq!(xs.mutate(1, |x| { *x = 100; 25 }), 25);
assert_eq!(xs, RationalSequence::from_slices(&[1, 100], &[3, 4]));
let mut xs = RationalSequence::from_slices(&[1, 2], &[3, 4]);
assert_eq!(xs.mutate(6, |x| { *x = 100; 25 }), 25);
assert_eq!(xs, RationalSequence::from_slices(&[1, 2, 3, 4, 3, 4, 100], &[4, 3]));
sourceimpl<T: Eq> RationalSequence<T>
impl<T: Eq> RationalSequence<T>
sourcepub fn from_vec(non_repeating: Vec<T>) -> RationalSequence<T>
pub fn from_vec(non_repeating: Vec<T>) -> RationalSequence<T>
Converts a Vec
to a finite RationalSequence
.
Worst-case complexity
Constant time and additional memory.
Examples
use malachite_base::rational_sequences::RationalSequence;
assert_eq!(RationalSequence::<u8>::from_vec(vec![]).to_string(), "[]");
assert_eq!(RationalSequence::<u8>::from_vec(vec![1, 2]).to_string(), "[1, 2]");
sourcepub fn from_vecs(
non_repeating: Vec<T>,
repeating: Vec<T>
) -> RationalSequence<T>
pub fn from_vecs(
non_repeating: Vec<T>,
repeating: Vec<T>
) -> RationalSequence<T>
Converts two Vec
s to a finite RationalSequence
. The first Vec
is the
nonrepeating part and the second is the repeating part.
Worst-case complexity
$T(n, m) = O(n + m^{1+\epsilon})$ for all $\epsilon > 0$
$M(n, m) = O(1)$
where $T$ is time, $M$ is additional memory, $n$ is non_repeating.len()
, and $m$ is
repeating.len()
.
Examples
use malachite_base::rational_sequences::RationalSequence;
assert_eq!(RationalSequence::<u8>::from_vecs(vec![], vec![]).to_string(), "[]");
assert_eq!(RationalSequence::<u8>::from_vecs(vec![], vec![1, 2]).to_string(), "[[1, 2]]");
assert_eq!(RationalSequence::<u8>::from_vecs(vec![1, 2], vec![]).to_string(), "[1, 2]");
assert_eq!(
RationalSequence::<u8>::from_vecs(vec![1, 2], vec![3, 4]).to_string(),
"[1, 2, [3, 4]]"
);
assert_eq!(
RationalSequence::<u8>::from_vecs(vec![1, 2, 3], vec![4, 3]).to_string(),
"[1, 2, [3, 4]]"
);
sourcepub fn into_vecs(self) -> (Vec<T>, Vec<T>)
pub fn into_vecs(self) -> (Vec<T>, Vec<T>)
Converts a RationalSequence
to a pair of Vec
s containing the non-repeating and
repeating parts, taking the RationalSequence
by value.
Worst-case complexity
Constant time and additional memory.
Examples
use malachite_base::rational_sequences::RationalSequence;
assert_eq!(
RationalSequence::from_slices(&[1, 2], &[3, 4]).into_vecs(),
(vec![1, 2], vec![3, 4])
);
sourcepub fn slices_ref(&self) -> (&[T], &[T])
pub fn slices_ref(&self) -> (&[T], &[T])
Returns references to the non-repeating and repeating parts of a RationalSequence
.
Worst-case complexity
Constant time and additional memory.
Examples
use malachite_base::rational_sequences::RationalSequence;
assert_eq!(
RationalSequence::from_slices(&[1u8, 2], &[3, 4]).slices_ref(),
(&[1u8, 2][..], &[3u8, 4][..])
);
sourceimpl<T: Clone + Eq> RationalSequence<T>
impl<T: Clone + Eq> RationalSequence<T>
sourcepub fn from_slice(non_repeating: &[T]) -> RationalSequence<T>
pub fn from_slice(non_repeating: &[T]) -> RationalSequence<T>
Converts a slice to a finite RationalSequence
.
Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is xs.len()
.
Examples
use malachite_base::rational_sequences::RationalSequence;
assert_eq!(RationalSequence::<u8>::from_slice(&[]).to_string(), "[]");
assert_eq!(RationalSequence::<u8>::from_slice(&[1, 2]).to_string(), "[1, 2]");
sourcepub fn from_slices(non_repeating: &[T], repeating: &[T]) -> RationalSequence<T>
pub fn from_slices(non_repeating: &[T], repeating: &[T]) -> RationalSequence<T>
Converts two slices to a finite RationalSequence
. The first slice is the nonrepeating
part and the second is the repeating part.
Worst-case complexity
$T(n, m) = O(n + m^{1+\epsilon})$ for all $\epsilon > 0$
$M(n, m) = O(n + m)$
where $T$ is time, $M$ is additional memory, $n$ is non_repeating.len()
, and $m$ is
repeating.len()
.
Examples
use malachite_base::rational_sequences::RationalSequence;
assert_eq!(RationalSequence::<u8>::from_slices(&[], &[]).to_string(), "[]");
assert_eq!(RationalSequence::<u8>::from_slices(&[], &[1, 2]).to_string(), "[[1, 2]]");
assert_eq!(RationalSequence::<u8>::from_slices(&[1, 2], &[]).to_string(), "[1, 2]");
assert_eq!(
RationalSequence::<u8>::from_slices(&[1, 2], &[3, 4]).to_string(),
"[1, 2, [3, 4]]"
);
assert_eq!(
RationalSequence::<u8>::from_slices(&[1, 2, 3], &[4, 3]).to_string(),
"[1, 2, [3, 4]]"
);
sourcepub fn to_vecs(&self) -> (Vec<T>, Vec<T>)
pub fn to_vecs(&self) -> (Vec<T>, Vec<T>)
Converts a RationalSequence
to a pair of Vec
s containing the non-repeating and
repeating parts, taking the RationalSequence
by reference.
Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is xs.component_len()
.
Examples
use malachite_base::rational_sequences::RationalSequence;
assert_eq!(
RationalSequence::from_slices(&[1, 2], &[3, 4]).to_vecs(),
(vec![1, 2], vec![3, 4])
);
sourceimpl<T: Eq> RationalSequence<T>
impl<T: Eq> RationalSequence<T>
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether this RationalSequence
is empty.
Worst-case complexity
Constant time and additional memory.
Examples
use malachite_base::rational_sequences::RationalSequence;
assert_eq!(RationalSequence::<u8>::from_slice(&[]).is_empty(), true);
assert_eq!(RationalSequence::<u8>::from_slice(&[1, 2, 3]).is_empty(), false);
assert_eq!(RationalSequence::<u8>::from_slices(&[], &[3, 4]).is_empty(), false);
assert_eq!(RationalSequence::<u8>::from_slices(&[1, 2], &[3, 4]).is_empty(), false);
sourcepub fn is_finite(&self) -> bool
pub fn is_finite(&self) -> bool
Returns whether this RationalSequence
is finite (has no repeating part).
Worst-case complexity
Constant time and additional memory.
Examples
use malachite_base::rational_sequences::RationalSequence;
assert_eq!(RationalSequence::<u8>::from_slice(&[]).is_finite(), true);
assert_eq!(RationalSequence::<u8>::from_slice(&[1, 2, 3]).is_finite(), true);
assert_eq!(RationalSequence::<u8>::from_slices(&[], &[3, 4]).is_finite(), false);
assert_eq!(RationalSequence::<u8>::from_slices(&[1, 2], &[3, 4]).is_finite(), false);
sourcepub fn len(&self) -> Option<usize>
pub fn len(&self) -> Option<usize>
Returns the length of this RationalSequence
. If the sequence is infinite, None
is
returned.
For a measure of length that always exists, try component_len
.
Worst-case complexity
Constant time and additional memory.
Examples
use malachite_base::rational_sequences::RationalSequence;
assert_eq!(RationalSequence::<u8>::from_slice(&[]).len(), Some(0));
assert_eq!(RationalSequence::<u8>::from_slice(&[1, 2, 3]).len(), Some(3));
assert_eq!(RationalSequence::<u8>::from_slices(&[], &[3, 4]).len(), None);
assert_eq!(RationalSequence::<u8>::from_slices(&[1, 2], &[3, 4]).len(), None);
sourcepub fn component_len(&self) -> usize
pub fn component_len(&self) -> usize
Returns the sum of the lengths of the non-repeating and repeating parts of this
RationalSequence
.
This is often a more useful way of measuring the complexity of a sequence than
len
.
Worst-case complexity
Constant time and additional memory.
Examples
use malachite_base::rational_sequences::RationalSequence;
assert_eq!(RationalSequence::<u8>::from_slice(&[]).component_len(), 0);
assert_eq!(RationalSequence::<u8>::from_slice(&[1, 2, 3]).component_len(), 3);
assert_eq!(RationalSequence::<u8>::from_slices(&[], &[3, 4]).component_len(), 2);
assert_eq!(RationalSequence::<u8>::from_slices(&[1, 2], &[3, 4]).component_len(), 4);
sourcepub fn iter(&self) -> Chain<Iter<'_, T>, Cycle<Iter<'_, T>>>
pub fn iter(&self) -> Chain<Iter<'_, T>, Cycle<Iter<'_, T>>>
Returns an iterator of references to the elements of this sequence.
Worst-case complexity
Constant time and additional memory.
Examples
extern crate itertools;
use itertools::Itertools;
use malachite_base::rational_sequences::RationalSequence;
let empty: &[u8] = &[];
assert_eq!(RationalSequence::<u8>::from_slice(empty).iter().cloned().collect_vec(), empty);
assert_eq!(
RationalSequence::<u8>::from_slice(&[1, 2, 3]).iter().cloned().collect_vec(),
&[1, 2, 3]
);
assert_eq!(
RationalSequence::<u8>::from_slices(&[], &[3, 4]).iter().cloned().take(10)
.collect_vec(),
&[3, 4, 3, 4, 3, 4, 3, 4, 3, 4]
);
assert_eq!(
RationalSequence::<u8>::from_slices(&[1, 2], &[3, 4]).iter().cloned().take(10)
.collect_vec(),
&[1, 2, 3, 4, 3, 4, 3, 4, 3, 4]
);
Trait Implementations
sourceimpl<T: Clone + Eq> Clone for RationalSequence<T>
impl<T: Clone + Eq> Clone for RationalSequence<T>
sourcefn clone(&self) -> RationalSequence<T>
fn clone(&self) -> RationalSequence<T>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl<T: Display + Eq> Debug for RationalSequence<T>
impl<T: Display + Eq> Debug for RationalSequence<T>
sourcefn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Converts a RationalSequence
to a String
.
This is the same implementation as for Display
.
Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is self.component_len()
.
Examples
use malachite_base::rational_sequences::RationalSequence;
use malachite_base::strings::ToDebugString;
assert_eq!(RationalSequence::<u8>::from_vecs(vec![], vec![]).to_debug_string(), "[]");
assert_eq!(
RationalSequence::<u8>::from_vecs(vec![], vec![1, 2]).to_debug_string(),
"[[1, 2]]"
);
assert_eq!(
RationalSequence::<u8>::from_vecs(vec![1, 2], vec![]).to_debug_string(),
"[1, 2]"
);
assert_eq!(
RationalSequence::<u8>::from_vecs(vec![1, 2], vec![3, 4]).to_string(),
"[1, 2, [3, 4]]"
);
sourceimpl<T: Default + Eq> Default for RationalSequence<T>
impl<T: Default + Eq> Default for RationalSequence<T>
sourcefn default() -> RationalSequence<T>
fn default() -> RationalSequence<T>
Returns the “default value” for a type. Read more
sourceimpl<T: Display + Eq> Display for RationalSequence<T>
impl<T: Display + Eq> Display for RationalSequence<T>
sourcefn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Converts a RationalSequence
to a String
.
Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is self.component_len()
.
Examples
use malachite_base::rational_sequences::RationalSequence;
assert_eq!(RationalSequence::<u8>::from_vecs(vec![], vec![]).to_string(), "[]");
assert_eq!(RationalSequence::<u8>::from_vecs(vec![], vec![1, 2]).to_string(), "[[1, 2]]");
assert_eq!(RationalSequence::<u8>::from_vecs(vec![1, 2], vec![]).to_string(), "[1, 2]");
assert_eq!(
RationalSequence::<u8>::from_vecs(vec![1, 2], vec![3, 4]).to_string(),
"[1, 2, [3, 4]]"
);
sourceimpl<T: Hash + Eq> Hash for RationalSequence<T>
impl<T: Hash + Eq> Hash for RationalSequence<T>
sourceimpl<T: Eq> Index<usize> for RationalSequence<T>
impl<T: Eq> Index<usize> for RationalSequence<T>
sourcefn index(&self, i: usize) -> &T
fn index(&self, i: usize) -> &T
Gets a reference to an element of a RationalSequence
at an index.
If the index is greater than or equal to the length of the sequence, this function panics.
Worst-case complexity
Constant time and additional memory.
Panics
Panics if index
is greater than or equal to the length of this sequence.
Examples
use malachite_base::rational_sequences::RationalSequence;
assert_eq!(RationalSequence::from_slices(&[1, 2], &[3, 4])[1], 2);
assert_eq!(RationalSequence::from_slices(&[1, 2], &[3, 4])[10], 3);
type Output = T
type Output = T
The returned type after indexing.
sourceimpl<T: Eq + Ord> Ord for RationalSequence<T>
impl<T: Eq + Ord> Ord for RationalSequence<T>
sourcefn cmp(&self, other: &RationalSequence<T>) -> Ordering
fn cmp(&self, other: &RationalSequence<T>) -> Ordering
Compares a RationalSequence
to another RationalSequence
.
The comparison is made lexicographically with respect to the element type’s ordering.
Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.component_len()
.
Examples
use malachite_base::rational_sequences::RationalSequence;
assert!(
RationalSequence::from_slice(&[1, 2]) <
RationalSequence::from_slices(&[1, 2], &[1])
);
assert!(
RationalSequence::from_slice(&[1, 2, 3]) <
RationalSequence::from_slices(&[1, 2], &[3, 4])
);
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
sourceimpl<T: PartialEq + Eq> PartialEq<RationalSequence<T>> for RationalSequence<T>
impl<T: PartialEq + Eq> PartialEq<RationalSequence<T>> for RationalSequence<T>
sourcefn eq(&self, other: &RationalSequence<T>) -> bool
fn eq(&self, other: &RationalSequence<T>) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &RationalSequence<T>) -> bool
fn ne(&self, other: &RationalSequence<T>) -> bool
This method tests for !=
.
sourceimpl<T: Eq + Ord> PartialOrd<RationalSequence<T>> for RationalSequence<T>
impl<T: Eq + Ord> PartialOrd<RationalSequence<T>> for RationalSequence<T>
sourcefn partial_cmp(&self, other: &RationalSequence<T>) -> Option<Ordering>
fn partial_cmp(&self, other: &RationalSequence<T>) -> Option<Ordering>
Compares a RationalSequence
to another RationalSequence
.
See here for more information.
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl<T: Eq + Eq> Eq for RationalSequence<T>
impl<T: Eq> StructuralEq for RationalSequence<T>
impl<T: Eq> StructuralPartialEq for RationalSequence<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for RationalSequence<T> where
T: RefUnwindSafe,
impl<T> Send for RationalSequence<T> where
T: Send,
impl<T> Sync for RationalSequence<T> where
T: Sync,
impl<T> Unpin for RationalSequence<T> where
T: Unpin,
impl<T> UnwindSafe for RationalSequence<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more