| 1 | 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#; |
| 2 | 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/; |
| 3 | 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#; |
| 4 | |
| 5 | use superrust-analyzer cargo itertools 0.14.0 crate/::combinationsrust-analyzer cargo itertools 0.14.0 combinations/::{combinationsrust-analyzer cargo itertools 0.14.0 combinations/combinations()., Combinationsrust-analyzer cargo itertools 0.14.0 combinations/Combinations#}; |
| 6 | use craterust-analyzer cargo itertools 0.14.0 crate/::adaptorsrust-analyzer cargo itertools 0.14.0 adaptors/::checked_binomialrust-analyzer cargo itertools 0.14.0 adaptors/checked_binomial().; |
| 7 | 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#}; |
| 8 | |
| 9 | /// An iterator to iterate through the powerset of the elements from an iterator. |
| 10 | /// |
| 11 | /// See [`.powerset()`](crate::Itertools::powerset) for more |
| 12 | /// information. |
| 13 | #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] |
| 14 | pub struct Powersetrust-analyzer cargo itertools 0.14.0 powerset/Powerset#<I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#> { |
| 15 | combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs.: Combinationsrust-analyzer cargo itertools 0.14.0 combinations/Combinations#<I>, |
| 16 | } |
| 17 | |
| 18 | impl<I> Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# for Powersetrust-analyzer cargo itertools 0.14.0 powerset/Powerset#<I> |
| 19 | where |
| 20 | 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#, |
| 21 | 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#, |
| 22 | { |
| 23 | clone_fieldsrust-analyzer cargo itertools 0.14.0 impl_macros/clone_fields!!(combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs.); |
| 24 | } |
| 25 | |
| 26 | 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 Powersetrust-analyzer cargo itertools 0.14.0 powerset/Powerset#<I> |
| 27 | where |
| 28 | 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#, |
| 29 | 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#, |
| 30 | { |
| 31 | debug_fmt_fieldsrust-analyzer cargo itertools 0.14.0 impl_macros/debug_fmt_fields!!(Powerset, combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs.); |
| 32 | } |
| 33 | |
| 34 | /// Create a new `Powerset` from a clonable iterator. |
| 35 | pub fn powersetrust-analyzer cargo itertools 0.14.0 powerset/powerset().<I>(src: I) -> Powersetrust-analyzer cargo itertools 0.14.0 powerset/Powerset#<I> |
| 36 | where |
| 37 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 38 | 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#, |
| 39 | { |
| 40 | Powersetrust-analyzer cargo itertools 0.14.0 powerset/Powerset# { |
| 41 | combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs.: combinationsrust-analyzer cargo itertools 0.14.0 combinations/combinations().(src, 0), |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | impl<I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#> Powersetrust-analyzer cargo itertools 0.14.0 powerset/Powerset#<I> { |
| 46 | /// Returns true if `k` has been incremented, false otherwise. |
| 47 | fn increment_krust-analyzer cargo itertools 0.14.0 powerset/impl#[`Powerset<I>`]increment_k().(&mut self) -> bool { |
| 48 | if self.combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs..krust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Idx>`]k().() 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().self.combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs..nrust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Idx>`]n().() || self.combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs..krust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Idx>`]k().() 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 { |
| 49 | self.combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs..resetrust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Vec<usize>>`]reset().(self.combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs..krust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Idx>`]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); |
| 50 | true |
| 51 | } else { |
| 52 | false |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | impl<I> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for Powersetrust-analyzer cargo itertools 0.14.0 powerset/Powerset#<I> |
| 58 | where |
| 59 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 60 | 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#, |
| 61 | { |
| 62 | type Itemrust-analyzer cargo itertools 0.14.0 powerset/impl#[`Powerset<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#>; |
| 63 | |
| 64 | fn nextrust-analyzer cargo itertools 0.14.0 powerset/impl#[`Powerset<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 powerset/impl#[`Powerset<I>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 65 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(elt) = self.combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs..nextrust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Idx>`][Iterator]next().() { |
| 66 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(elt) |
| 67 | } else if self.increment_krust-analyzer cargo itertools 0.14.0 powerset/impl#[`Powerset<I>`]increment_k().() { |
| 68 | self.combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs..nextrust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Idx>`][Iterator]next().() |
| 69 | } else { |
| 70 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | fn nthrust-analyzer cargo itertools 0.14.0 powerset/impl#[`Powerset<I>`][Iterator]nth().(&mut self, mut n: usize) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo itertools 0.14.0 powerset/impl#[`Powerset<I>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 75 | loop { |
| 76 | match self.combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs..try_nthrust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Idx>`]try_nth().(n) { |
| 77 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(item) => return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(item), |
| 78 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(steps) => { |
| 79 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.increment_krust-analyzer cargo itertools 0.14.0 powerset/impl#[`Powerset<I>`]increment_k().() { |
| 80 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 81 | } |
| 82 | n 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().steps; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | fn size_hintrust-analyzer cargo itertools 0.14.0 powerset/impl#[`Powerset<I>`][Iterator]size_hint().(&self) -> SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint# { |
| 89 | let k = self.combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs..krust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Idx>`]k().(); |
| 90 | // Total bounds for source iterator. |
| 91 | let (n_min, n_max) = self.combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs..srcrust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Idx>`]src().().size_hintrust-analyzer cargo itertools 0.14.0 lazy_buffer/impl#[`LazyBuffer<I>`]size_hint().(); |
| 92 | let low = remaining_forrust-analyzer cargo itertools 0.14.0 powerset/remaining_for().(n_min, k).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.); |
| 93 | let upp = n_max.and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|n| remaining_forrust-analyzer cargo itertools 0.14.0 powerset/remaining_for().(n, k)); |
| 94 | size_hintrust-analyzer cargo itertools 0.14.0 size_hint/::addrust-analyzer cargo itertools 0.14.0 size_hint/add().(self.combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs..size_hintrust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Idx>`][Iterator]size_hint().(), (low, upp)) |
| 95 | } |
| 96 | |
| 97 | fn countrust-analyzer cargo itertools 0.14.0 powerset/impl#[`Powerset<I>`][Iterator]count().(self) -> usize { |
| 98 | let k = self.combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs..krust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Idx>`]k().(); |
| 99 | let (n, combs_count) = self.combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs..n_and_countrust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Idx>`]n_and_count().(); |
| 100 | combs_count 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().remaining_forrust-analyzer cargo itertools 0.14.0 powerset/remaining_for().(n, k).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().() |
| 101 | } |
| 102 | |
| 103 | fn foldrust-analyzer cargo itertools 0.14.0 powerset/impl#[`Powerset<I>`][Iterator]fold().<B, F>(self, mut init: B, mut f: F) -> B |
| 104 | where |
| 105 | F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(B, Selfrust-analyzer cargo itertools 0.14.0 powerset/impl#[`Powerset<I>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#) -> B, |
| 106 | { |
| 107 | let mut it = self.combsrust-analyzer cargo itertools 0.14.0 powerset/Powerset#combs.; |
| 108 | if it.krust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Idx>`]k().() 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 { |
| 109 | init = it.by_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#by_ref().().foldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/impl#[`&mut I`][Iterator]fold().(init, &mut f); |
| 110 | it.resetrust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Vec<usize>>`]reset().(1); |
| 111 | } |
| 112 | init = it.by_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#by_ref().().foldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/impl#[`&mut I`][Iterator]fold().(init, &mut f); |
| 113 | // n is now known for sure because k >= 1 and all k-combinations have been generated. |
| 114 | for k in it.krust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Idx>`]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#it.nrust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Idx>`]n().() { |
| 115 | it.resetrust-analyzer cargo itertools 0.14.0 combinations/impl#[`CombinationsGeneric<I, Vec<usize>>`]reset().(k); |
| 116 | init = it.by_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#by_ref().().foldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/impl#[`&mut I`][Iterator]fold().(init, &mut f); |
| 117 | } |
| 118 | init |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | impl<I> FusedIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/marker/FusedIterator# for Powersetrust-analyzer cargo itertools 0.14.0 powerset/Powerset#<I> |
| 123 | where |
| 124 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 125 | 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#, |
| 126 | { |
| 127 | } |
| 128 | |
| 129 | fn remaining_forrust-analyzer cargo itertools 0.14.0 powerset/remaining_for().(n: usize, k: usize) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize> { |
| 130 | (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().(0usize, |sum, i| sum.checked_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]checked_add().(checked_binomialrust-analyzer cargo itertools 0.14.0 adaptors/checked_binomial().(n, i)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().)) |
| 131 | } |