| 1 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::boxedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/::Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#; |
| 2 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#; |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/; |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::oncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/sources/once/once().; |
| 5 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::FusedIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/marker/FusedIterator#; |
| 6 | |
| 7 | use superrust-analyzer cargo itertools 0.14.0 crate/::lazy_bufferrust-analyzer cargo itertools 0.14.0 lazy_buffer/::LazyBufferrust-analyzer cargo itertools 0.14.0 lazy_buffer/LazyBuffer#; |
| 8 | use craterust-analyzer cargo itertools 0.14.0 crate/::size_hintrust-analyzer cargo itertools 0.14.0 size_hint/::{selfrust-analyzer cargo itertools 0.14.0 size_hint/, SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint#}; |
| 9 | |
| 10 | /// An iterator adaptor that iterates through all the `k`-permutations of the |
| 11 | /// elements from an iterator. |
| 12 | /// |
| 13 | /// See [`.permutations()`](crate::Itertools::permutations) for |
| 14 | /// more information. |
| 15 | #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] |
| 16 | pub struct Permutationsrust-analyzer cargo itertools 0.14.0 permutations/Permutations#<I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#> { |
| 17 | valsrust-analyzer cargo itertools 0.14.0 permutations/Permutations#vals.: LazyBufferrust-analyzer cargo itertools 0.14.0 lazy_buffer/LazyBuffer#<I>, |
| 18 | staterust-analyzer cargo itertools 0.14.0 permutations/Permutations#state.: PermutationStaterust-analyzer cargo itertools 0.14.0 permutations/PermutationState#, |
| 19 | } |
| 20 | |
| 21 | impl<I> Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# for Permutationsrust-analyzer cargo itertools 0.14.0 permutations/Permutations#<I> |
| 22 | where |
| 23 | I: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# + Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 24 | I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 25 | { |
| 26 | clone_fieldsrust-analyzer cargo itertools 0.14.0 impl_macros/clone_fields!!(valsrust-analyzer cargo itertools 0.14.0 permutations/Permutations#vals., staterust-analyzer cargo itertools 0.14.0 permutations/Permutations#state.); |
| 27 | } |
| 28 | |
| 29 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 30 | enum PermutationStaterust-analyzer cargo itertools 0.14.0 permutations/PermutationState# { |
| 31 | /// No permutation generated yet. |
| 32 | Startrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Start# { krust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Start#k.: usize }, |
| 33 | /// Values from the iterator are not fully loaded yet so `n` is still unknown. |
| 34 | Bufferedrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Buffered# { krust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Buffered#k.: usize, min_nrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Buffered#min_n.: usize }, |
| 35 | /// All values from the iterator are known so `n` is known. |
| 36 | Loadedrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Loaded# { |
| 37 | indicesrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Loaded#indices.: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[usize]>, |
| 38 | cyclesrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Loaded#cycles.: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[usize]>, |
| 39 | }, |
| 40 | /// No permutation left to generate. |
| 41 | Endrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#End#, |
| 42 | } |
| 43 | |
| 44 | impl<I> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for Permutationsrust-analyzer cargo itertools 0.14.0 permutations/Permutations#<I> |
| 45 | where |
| 46 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# + fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#, |
| 47 | I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#: fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#, |
| 48 | { |
| 49 | debug_fmt_fieldsrust-analyzer cargo itertools 0.14.0 impl_macros/debug_fmt_fields!!(Permutations, valsrust-analyzer cargo itertools 0.14.0 permutations/Permutations#vals., staterust-analyzer cargo itertools 0.14.0 permutations/Permutations#state.); |
| 50 | } |
| 51 | |
| 52 | pub fn permutationsrust-analyzer cargo itertools 0.14.0 permutations/permutations().<I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#>(iter: I, k: usize) -> Permutationsrust-analyzer cargo itertools 0.14.0 permutations/Permutations#<I> { |
| 53 | Permutationsrust-analyzer cargo itertools 0.14.0 permutations/Permutations# { |
| 54 | valsrust-analyzer cargo itertools 0.14.0 permutations/Permutations#vals.: LazyBufferrust-analyzer cargo itertools 0.14.0 lazy_buffer/LazyBuffer#::newrust-analyzer cargo itertools 0.14.0 lazy_buffer/impl#[`LazyBuffer<I>`]new().(iter), |
| 55 | staterust-analyzer cargo itertools 0.14.0 permutations/Permutations#state.: PermutationStaterust-analyzer cargo itertools 0.14.0 permutations/PermutationState#::Startrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Start# { krust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Start#k. }, |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl<I> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for Permutationsrust-analyzer cargo itertools 0.14.0 permutations/Permutations#<I> |
| 60 | where |
| 61 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 62 | I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 63 | { |
| 64 | type Itemrust-analyzer cargo itertools 0.14.0 permutations/impl#[`Permutations<I>`][Iterator]Item# = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#>; |
| 65 | |
| 66 | fn nextrust-analyzer cargo itertools 0.14.0 permutations/impl#[`Permutations<I>`][Iterator]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo itertools 0.14.0 permutations/impl#[`Permutations<I>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 67 | let Selfrust-analyzer cargo itertools 0.14.0 permutations/impl#[`Permutations<I>`][Iterator] { valsrust-analyzer cargo itertools 0.14.0 permutations/Permutations#vals., staterust-analyzer cargo itertools 0.14.0 permutations/Permutations#state. } = self; |
| 68 | match state { |
| 69 | PermutationStaterust-analyzer cargo itertools 0.14.0 permutations/PermutationState#::Startrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Start# { krust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Start#k.: 0 } => { |
| 70 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().state = PermutationStaterust-analyzer cargo itertools 0.14.0 permutations/PermutationState#::Endrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#End#; |
| 71 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().()) |
| 72 | } |
| 73 | &mut PermutationStaterust-analyzer cargo itertools 0.14.0 permutations/PermutationState#::Startrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Start# { krust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Start#k. } => { |
| 74 | vals.prefillrust-analyzer cargo itertools 0.14.0 lazy_buffer/impl#[`LazyBuffer<I>`]prefill().(k); |
| 75 | if vals.lenrust-analyzer cargo itertools 0.14.0 lazy_buffer/impl#[`LazyBuffer<I>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().k { |
| 76 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().state = PermutationStaterust-analyzer cargo itertools 0.14.0 permutations/PermutationState#::Endrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#End#; |
| 77 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 78 | } |
| 79 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().state = PermutationStaterust-analyzer cargo itertools 0.14.0 permutations/PermutationState#::Bufferedrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Buffered# { krust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Buffered#k., min_nrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Buffered#min_n.: k }; |
| 80 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(vals[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#k]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().()) |
| 81 | } |
| 82 | PermutationStaterust-analyzer cargo itertools 0.14.0 permutations/PermutationState#::Bufferedrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Buffered# { ref krust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Buffered#k., min_nrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Buffered#min_n. } => { |
| 83 | if vals.get_nextrust-analyzer cargo itertools 0.14.0 lazy_buffer/impl#[`LazyBuffer<I>`]get_next().() { |
| 84 | let item = (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().k rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1) |
| 85 | .chainrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#chain().(oncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/sources/once/once().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().min_n)) |
| 86 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|i| vals[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().()) |
| 87 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 88 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().min_n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 89 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(item) |
| 90 | } else { |
| 91 | let n = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().min_n; |
| 92 | let prev_iteration_count = n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().k rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1; |
| 93 | let mut indices: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[_]> = (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#n).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 94 | let mut cycles: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[_]> = (n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<&usize>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<&usize>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<&usize>`]sub().k..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#n).revrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#rev().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 95 | // Advance the state to the correct point. |
| 96 | for _ in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#prev_iteration_count { |
| 97 | if advancerust-analyzer cargo itertools 0.14.0 permutations/advance().(&mut indices, &mut cycles) { |
| 98 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().state = PermutationStaterust-analyzer cargo itertools 0.14.0 permutations/PermutationState#::Endrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#End#; |
| 99 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 100 | } |
| 101 | } |
| 102 | let item = vals.get_atrust-analyzer cargo itertools 0.14.0 lazy_buffer/impl#[`LazyBuffer<I>`]get_at().(&indices[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().k]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 103 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().state = PermutationStaterust-analyzer cargo itertools 0.14.0 permutations/PermutationState#::Loadedrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Loaded# { indicesrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Loaded#indices., cyclesrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Loaded#cycles. }; |
| 104 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(item) |
| 105 | } |
| 106 | } |
| 107 | PermutationStaterust-analyzer cargo itertools 0.14.0 permutations/PermutationState#::Loadedrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Loaded# { indicesrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Loaded#indices., cyclesrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Loaded#cycles. } => { |
| 108 | if advancerust-analyzer cargo itertools 0.14.0 permutations/advance().(indices, cycles) { |
| 109 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().state = PermutationStaterust-analyzer cargo itertools 0.14.0 permutations/PermutationState#::Endrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#End#; |
| 110 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 111 | } |
| 112 | let k = cycles.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 113 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(vals.get_atrust-analyzer cargo itertools 0.14.0 lazy_buffer/impl#[`LazyBuffer<I>`]get_at().(&indices[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#k]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)) |
| 114 | } |
| 115 | PermutationStaterust-analyzer cargo itertools 0.14.0 permutations/PermutationState#::Endrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#End# => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | fn countrust-analyzer cargo itertools 0.14.0 permutations/impl#[`Permutations<I>`][Iterator]count().(self) -> usize { |
| 120 | let Selfrust-analyzer cargo itertools 0.14.0 permutations/impl#[`Permutations<I>`][Iterator] { valsrust-analyzer cargo itertools 0.14.0 permutations/Permutations#vals., staterust-analyzer cargo itertools 0.14.0 permutations/Permutations#state. } = self; |
| 121 | let n = vals.countrust-analyzer cargo itertools 0.14.0 lazy_buffer/impl#[`LazyBuffer<I>`]count().(); |
| 122 | state.size_hint_forrust-analyzer cargo itertools 0.14.0 permutations/impl#[PermutationState]size_hint_for().(n).1.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().() |
| 123 | } |
| 124 | |
| 125 | fn size_hintrust-analyzer cargo itertools 0.14.0 permutations/impl#[`Permutations<I>`][Iterator]size_hint().(&self) -> SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint# { |
| 126 | let (mut low, mut upp) = self.valsrust-analyzer cargo itertools 0.14.0 permutations/Permutations#vals..size_hintrust-analyzer cargo itertools 0.14.0 lazy_buffer/impl#[`LazyBuffer<I>`]size_hint().(); |
| 127 | low = self.staterust-analyzer cargo itertools 0.14.0 permutations/Permutations#state..size_hint_forrust-analyzer cargo itertools 0.14.0 permutations/impl#[PermutationState]size_hint_for().(low).0; |
| 128 | upp = upp.and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|n| self.staterust-analyzer cargo itertools 0.14.0 permutations/Permutations#state..size_hint_forrust-analyzer cargo itertools 0.14.0 permutations/impl#[PermutationState]size_hint_for().(n).1); |
| 129 | (low, upp) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | impl<I> FusedIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/marker/FusedIterator# for Permutationsrust-analyzer cargo itertools 0.14.0 permutations/Permutations#<I> |
| 134 | where |
| 135 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 136 | I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 137 | { |
| 138 | } |
| 139 | |
| 140 | fn advancerust-analyzer cargo itertools 0.14.0 permutations/advance().(indices: &mut [usize], cycles: &mut [usize]) -> bool { |
| 141 | let n = indices.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 142 | let k = cycles.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 143 | // NOTE: if `cycles` are only zeros, then we reached the last permutation. |
| 144 | for i in (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#k).revrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#rev().() { |
| 145 | if cycles[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 { |
| 146 | cycles[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1; |
| 147 | indices[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().i..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..rotate_leftrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]rotate_left().(1); |
| 148 | } else { |
| 149 | let swap_index = n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().cycles[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 150 | indices.swaprust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]swap().(i, swap_index); |
| 151 | cycles[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().1; |
| 152 | return false; |
| 153 | } |
| 154 | } |
| 155 | true |
| 156 | } |
| 157 | |
| 158 | impl PermutationStaterust-analyzer cargo itertools 0.14.0 permutations/PermutationState# { |
| 159 | fn size_hint_forrust-analyzer cargo itertools 0.14.0 permutations/impl#[PermutationState]size_hint_for().(&self, n: usize) -> SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint# { |
| 160 | // At the beginning, there are `n!/(n-k)!` items to come. |
| 161 | let at_start = |n, k| { |
| 162 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(n >= k); |
| 163 | let total = (n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().k rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#n).try_foldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/range/impl#[`ops::RangeInclusive<A>`][Iterator]try_fold().(1usize, |acc, i| acc.checked_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]checked_mul().(i)); |
| 164 | (total.unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(usize::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]MAX.), total) |
| 165 | }; |
| 166 | match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self { |
| 167 | Selfrust-analyzer cargo itertools 0.14.0 permutations/impl#[PermutationState]::Startrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Start# { krust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Start#k. } if n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().k => (0, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(0)), |
| 168 | Selfrust-analyzer cargo itertools 0.14.0 permutations/impl#[PermutationState]::Startrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Start# { krust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Start#k. } => at_start(n, k), |
| 169 | Selfrust-analyzer cargo itertools 0.14.0 permutations/impl#[PermutationState]::Bufferedrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Buffered# { krust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Buffered#k., min_nrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Buffered#min_n. } => { |
| 170 | // Same as `Start` minus the previously generated items. |
| 171 | size_hintrust-analyzer cargo itertools 0.14.0 size_hint/::sub_scalarrust-analyzer cargo itertools 0.14.0 size_hint/sub_scalar().(at_start(n, k), min_n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().k rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1) |
| 172 | } |
| 173 | Selfrust-analyzer cargo itertools 0.14.0 permutations/impl#[PermutationState]::Loadedrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Loaded# { |
| 174 | ref indicesrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Loaded#indices., |
| 175 | ref cyclesrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#Loaded#cycles., |
| 176 | } => { |
| 177 | let count = cycles.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().().try_foldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/enumerate/impl#[`Enumerate<I>`][Iterator]try_fold().(0usize, |acc, (i, &c)| { |
| 178 | acc.checked_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]checked_mul().(indices.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().i) |
| 179 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|count| count.checked_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]checked_add().(c)) |
| 180 | }); |
| 181 | (count.unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(usize::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]MAX.), count) |
| 182 | } |
| 183 | Selfrust-analyzer cargo itertools 0.14.0 permutations/impl#[PermutationState]::Endrust-analyzer cargo itertools 0.14.0 permutations/PermutationState#End# => (0, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(0)), |
| 184 | } |
| 185 | } |
| 186 | } |