pub const fn exhaustive_dependent_pairs_stop_after_empty_ys<X: Clone, Y, G: Iterator<Item = usize>, S: ExhaustiveDependentPairsYsGenerator<X, Y, J>, I: Iterator<Item = X>, J: Iterator<Item = Y>>(
index_generator: G,
xs: I,
ys_generator: S,
) -> ExhaustiveDependentPairs<X, Y, G, S, I, J> ⓘ
Expand description
Generates pairs $(x, y)$, where the possible values of $y$ depend on the value of $x$. $x$ values with no corresponding $y$ values are treated specially.
See exhaustive_dependent_pairs
for context.
If the output iterator encounters an $x$ value whose corresponding ys
iterator is empty, the
output iterator stops iterating altogether. This prevents the iterator from getting stuck if all
ys
iterators after a certain point are empty.
§Examples
use itertools::Itertools;
use malachite_base::num::iterators::ruler_sequence;
use malachite_base::tuples::exhaustive::{
exhaustive_dependent_pairs_stop_after_empty_ys, ExhaustiveDependentPairsYsGenerator,
};
use maplit::hashmap;
use std::collections::HashMap;
use std::hash::Hash;
use std::iter::Cloned;
use std::slice::Iter;
#[derive(Clone, Debug)]
pub struct MultiplesGeneratorHelper {
u: u64,
step: u64,
}
impl Iterator for MultiplesGeneratorHelper {
type Item = u64;
fn next(&mut self) -> Option<u64> {
let next = self.u;
self.u += self.step;
Some(next)
}
}
#[derive(Clone, Debug)]
struct DPGeneratorFromMap<X: Clone + Eq + Hash, Y: 'static + Clone> {
map: HashMap<X, &'static [Y]>,
}
impl<X: Clone + Eq + Hash, Y: 'static + Clone>
ExhaustiveDependentPairsYsGenerator<X, Y, Cloned<Iter<'static, Y>>>
for DPGeneratorFromMap<X, Y>
{
#[inline]
fn get_ys(&self, x: &X) -> Cloned<Iter<'static, Y>> {
self.map[x].iter().cloned()
}
}
let xs = [1, 2, 3, 2, 3, 2, 2].iter().cloned();
let xss = exhaustive_dependent_pairs_stop_after_empty_ys(
ruler_sequence(),
xs,
DPGeneratorFromMap {
map: hashmap! {
1 => &[100, 101, 102][..],
2 => &[][..],
3 => &[300, 301, 302][..]
},
},
)
.take(20)
.collect_vec();
assert_eq!(xss.as_slice(), &[(1, 100)]);