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

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

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

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

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

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

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][..])
);

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

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

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

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

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

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

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

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

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

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

Feeds this value into the given Hasher. Read more

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

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

The returned type after indexing.

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

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Compares a RationalSequence to another RationalSequence.

See here for more information.

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

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

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Should always be Self

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

The resulting type after obtaining ownership.

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

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

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.