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>

source

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>

source

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>

source

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]");
source

pub fn from_vecs( non_repeating: Vec<T>, repeating: Vec<T> ) -> RationalSequence<T>

Converts two Vecs 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]]"
);
source

pub fn into_vecs(self) -> (Vec<T>, Vec<T>)

Converts a RationalSequence to a pair of Vecs 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])
);
source

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>

source

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]");
source

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]]"
);
source

pub fn to_vecs(&self) -> (Vec<T>, Vec<T>)

Converts a RationalSequence to a pair of Vecs 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>

source

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);
source

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);
source

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);
source

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);
source

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>

source§

fn clone(&self) -> RationalSequence<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Display + Eq> Debug for RationalSequence<T>

source§

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>

source§

fn default() -> RationalSequence<T>

Returns the “default value” for a type. Read more
source§

impl<T: Display + Eq> Display for RationalSequence<T>

source§

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: Hash + Eq> Hash for RationalSequence<T>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<T: Eq> Index<usize> for RationalSequence<T>

source§

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

The returned type after indexing.
source§

impl<T: Eq + Ord> Ord for RationalSequence<T>

source§

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) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<T: PartialEq + Eq> PartialEq for RationalSequence<T>

source§

fn eq(&self, other: &RationalSequence<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Eq + Ord> PartialOrd for RationalSequence<T>

source§

fn partial_cmp(&self, other: &RationalSequence<T>) -> Option<Ordering>

Compares a RationalSequence to another RationalSequence.

See here for more information.

1.0.0 · source§

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 · source§

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
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T: Eq + Eq> Eq for RationalSequence<T>

source§

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<T, U> ExactFrom<T> for U
where U: TryFrom<T>,

source§

fn exact_from(value: T) -> U

source§

impl<T, U> ExactInto<U> for T
where U: ExactFrom<T>,

source§

fn exact_into(self) -> U

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> OverflowingInto<U> for T
where U: OverflowingFrom<T>,

source§

impl<T, U> RoundingInto<U> for T
where U: RoundingFrom<T>,

source§

impl<T, U> SaturatingInto<U> for T
where U: SaturatingFrom<T>,

source§

impl<T> ToDebugString for T
where T: Debug,

source§

fn to_debug_string(&self) -> String

Returns the String produced by Ts Debug implementation.

§Examples
use malachite_base::strings::ToDebugString;

assert_eq!([1, 2, 3].to_debug_string(), "[1, 2, 3]");
assert_eq!(
    [vec![2, 3], vec![], vec![4]].to_debug_string(),
    "[[2, 3], [], [4]]"
);
assert_eq!(Some(5).to_debug_string(), "Some(5)");
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T, U> WrappingInto<U> for T
where U: WrappingFrom<T>,

source§

fn wrapping_into(self) -> U