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§
source§impl<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)
);
source§impl<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])
);
source§impl<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+\varepsilon})$ for all $\varepsilon > 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][..])
);
source§impl<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+\varepsilon})$ for all $\varepsilon > 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])
);
source§impl<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
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§
source§impl<T: Clone + Eq> Clone for RationalSequence<T>
impl<T: Clone + Eq> Clone for RationalSequence<T>
source§fn clone(&self) -> RationalSequence<T>
fn clone(&self) -> RationalSequence<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T: Display + Eq> Debug for RationalSequence<T>
impl<T: Display + Eq> Debug for RationalSequence<T>
source§fn 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]]"
);
source§impl<T: Default + Eq> Default for RationalSequence<T>
impl<T: Default + Eq> Default for RationalSequence<T>
source§fn default() -> RationalSequence<T>
fn default() -> RationalSequence<T>
source§impl<T: Display + Eq> Display for RationalSequence<T>
impl<T: Display + Eq> Display for RationalSequence<T>
source§fn 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]]"
);
source§impl<T: Eq> Index<usize> for RationalSequence<T>
impl<T: Eq> Index<usize> for RationalSequence<T>
source§fn 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);
source§impl<T: Eq + Ord> Ord for RationalSequence<T>
impl<T: Eq + Ord> Ord for RationalSequence<T>
source§fn 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 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<T: Eq + Ord> PartialOrd for RationalSequence<T>
impl<T: Eq + Ord> PartialOrd for RationalSequence<T>
source§fn 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.
impl<T: Eq + Eq> Eq for RationalSequence<T>
impl<T: Eq> StructuralPartialEq for RationalSequence<T>
Auto Trait Implementations§
impl<T> Freeze for RationalSequence<T>
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§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more