Function malachite_base::vecs::exhaustive::shortlex_vecs_from_length_iterator
source · pub const fn shortlex_vecs_from_length_iterator<T: Clone, I: Iterator<Item = u64>, J: Clone + Iterator<Item = T>>(
xs: I,
ys: J,
) -> ShortlexVecs<T, I, J> ⓘ
Expand description
Generates all Vec
s with elements from a specified iterator and with lengths from another
iterator.
The length-generating iterator is xs
, and the element-generating iterator is ys
.
If the provided lengths are $\ell_0, \ell_1, \ell_2, \ldots$, then first all Vec
s with
length $\ell_0$ will be generated, in lexicographic order; then all Vec
s with length
$\ell_2$, and so on. If the lengths iterator has repetitions, then the generated Vec
s will
be repeated too.
ys
must be finite; if it’s infinite, the output will never get past the first nonzero $\ell$.
There’s one quirk if ys
is empty: then the iterator will stop as soon as it encounters a
nonzero $\ell$, even if there are zeros later on. This prevents the iterator hanging when given
an empty ys
and lengths $0, 1, 2, \ldots$.
If ys
is empty, the output length is the amount of zeros generated by xs
before the first
nonzero length. If ys
is nonempty and xs
is infinite, the output is infinite. Finally, if
ys
is nonempty and xs
is finite, the output length is
$$
\sum_{k=0}^{m-1} n^{\ell_k},
$$
where $n$ is ys.count()
and $m$ is xs.count()
.
§Examples
use itertools::Itertools;
use malachite_base::bools::exhaustive::exhaustive_bools;
use malachite_base::nevers::nevers;
use malachite_base::vecs::exhaustive::shortlex_vecs_from_length_iterator;
let xss = shortlex_vecs_from_length_iterator([2, 1, 2].iter().cloned(), exhaustive_bools())
.collect_vec();
assert_eq!(
xss.iter().map(Vec::as_slice).collect_vec().as_slice(),
&[
&[false, false][..],
&[false, true],
&[true, false],
&[true, true],
&[false],
&[true],
&[false, false],
&[false, true],
&[true, false],
&[true, true]
]
);
let xss =
shortlex_vecs_from_length_iterator([0, 0, 1, 0].iter().cloned(), nevers()).collect_vec();
// Stops after first empty ys
assert_eq!(
xss.iter().map(Vec::as_slice).collect_vec().as_slice(),
&[&[], &[]]
);