Skip to main content

random_hash_sets_from_length_iterator

Function random_hash_sets_from_length_iterator 

Source
pub fn random_hash_sets_from_length_iterator<T: Eq + Hash, I: Iterator<Item = u64>, J: Iterator<Item = T>>(
    seed: Seed,
    lengths_gen: &dyn Fn(Seed) -> I,
    xs_gen: &dyn Fn(Seed) -> J,
) -> RandomHashSets<T, I, J> 
Expand description

Generates random HashSets using elements from an iterator and with lengths from another iterator.

The input iterator must generate at least many distinct elements as any number generated by the lengths iterator; otherwise, this iterator will hang.

$$ P((x_i)_{i=0}^{n-1}) = n!P(n)\prod_{i=0}^{n-1}P(x_i). $$

lengths and xs must be infinite.

§Expected complexity per iteration

$T(i) = O(T^{\prime\prime}(i) + \ell T^\prime(i))$

$M(i) = O(M^{\prime\prime}(i) + \ell M^\prime(i))$

where $T$ is time, $M$ is additional memory, $i$ is the iteration number, $T^\prime$ and $M^\prime$ are the time and memory functions of the iterators produced by xs_gen, $T^{\prime\prime}$ and $M^{\prime\prime}$ are the time and memory functions of lengths, and $\ell$ is the $i$th generated length.

If xs can repeat values, extra draws are needed to reach the required number of distinct elements, and an iteration fails to terminate if fewer distinct values are reachable than the requested length.

§Examples

use itertools::Itertools;
use malachite_base::num::random::random_primitive_ints;
use malachite_base::random::EXAMPLE_SEED;
use malachite_base::sets::random::random_hash_sets_from_length_iterator;
use malachite_base::vecs::random_values_from_vec;
use maplit::hashset;

let xs = random_hash_sets_from_length_iterator(
    EXAMPLE_SEED,
    &|seed| random_values_from_vec(seed, vec![0, 2, 4]),
    &random_primitive_ints::<u8>,
);
let values = xs.take(20).collect_vec();
assert_eq!(
    values,
    &[
        hashset! {11, 85},
        hashset! {134, 136, 200, 235},
        hashset! {203, 223},
        hashset! {38, 177, 217, 235},
        hashset! {32, 162, 166, 234},
        hashset! {30, 218},
        hashset! {},
        hashset! {90, 106},
        hashset! {},
        hashset! {9, 151, 204, 216},
        hashset! {78, 97, 213, 253},
        hashset! {39, 91},
        hashset! {170, 175, 191, 232},
        hashset! {2, 233},
        hashset! {22, 35, 198, 217},
        hashset! {17, 32, 114, 173},
        hashset! {65, 114, 121, 222},
        hashset! {},
        hashset! {25, 144, 148, 173},
        hashset! {}
    ]
);