use alloc::vec::Vec; use std::fmt; use std::iter::once;
usesuper::lazy_buffer::LazyBuffer;
/// An iterator adaptor that iterates through all the `k`-permutations of the /// elements from an iterator. /// /// See [`.permutations()`](crate::Itertools::permutations) for /// more information. #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] pubstruct Permutations<I: Iterator> {
vals: LazyBuffer<I>,
state: PermutationState,
}
impl<I> Clone for Permutations<I> where I: Clone + Iterator,
I::Item: Clone,
{
clone_fields!(vals, state);
}
let Permutations { vals, state } = self; match state {
PermutationState::StartUnknownLen { k } => { let n = vals.len() + vals.it.count(); let complete_state = CompleteState::Start { n, k };
from_complete(complete_state)
}
PermutationState::OngoingUnknownLen { k, min_n } => { let prev_iteration_count = min_n - k + 1; let n = vals.len() + vals.it.count(); let complete_state = CompleteState::Start { n, k };
impl CompleteState { fn advance(&mutself) {
*self = match *self {
CompleteState::Start { n, k } => { let indices = (0..n).collect(); let cycles = ((n - k)..n).rev().collect();
CompleteState::Ongoing {
cycles,
indices
}
},
CompleteState::Ongoing { refmut indices, refmut cycles } => { let n = indices.len(); let k = cycles.len();
for i in (0..k).rev() { if cycles[i] == 0 {
cycles[i] = n - i - 1;
let to_push = indices.remove(i);
indices.push(to_push);
} else { let swap_index = n - cycles[i];
indices.swap(i, swap_index);
for (i, &c) in cycles.iter().enumerate() { let radix = indices.len() - i; let next_count = count.checked_mul(radix)
.and_then(|count| count.checked_add(c));
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.