pub fn gen_with_buffer<'a, T, F>(
n: usize,
buffer: &'a mut [bool],
f: F,
) -> impl DoubleEndedIterator<Item = T> + ExactSizeIterator + FusedIterator + 'a
Expand description
Returns an Iterator
producing T
for each possible combinations
of n
bool
s.
Each combination is mapped from &[bool] -> T
using f
,
where f
receives &[bool]
where len() == n
.
The buffer
only needs to be as big as the largest used n
.
Whereas gen_slice()
which always uses a
[bool; MAX]
.
See also implementation for more information
about the Iterator
implementation.
§Panics
- Panics if
buffer.len() < n
- Panics if
n
is larger than theMAX
number of supported variables. However, you likely have other problems, if you encounter this.
See also count(n)
and is_supported(n)
.
§Example
See crate root for more examples.
// buffer only needs to be as big as the largest `n`
let mut buffer = [false; 5];
// `3 <= buffer.len()` so `buffer` can be used
let n = 3;
let combinations = truth_values::gen_with_buffer(n, &mut buffer, |bools| {
match bools {
&[a, b, c] => (a, b, c),
_ => unreachable!(),
}
})
.collect::<Vec<_>>();
assert_eq!(
combinations,
[
(false, false, false),
(true, false, false),
(false, true, false),
(true, true, false),
(false, false, true),
(true, false, true),
(false, true, true),
(true, true, true),
]
);
// `2 <= buffer.len()` so `buffer` can be reused
let n = 2;
let combinations = truth_values::gen_with_buffer(n, &mut buffer, |bools| {
match bools {
&[a, b] => (a, b),
_ => unreachable!(),
}
})
.collect::<Vec<_>>();
assert_eq!(
combinations,
[
(false, false),
(true, false),
(false, true),
(true, true),
]
);