| 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/::FusedIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/marker/FusedIterator#; |
| 5 | |
| 6 | 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#; |
| 7 | 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().; |
| 8 | |
| 9 | /// An iterator to iterate through all the `n`-length combinations in an iterator, with replacement. |
| 10 | /// |
| 11 | /// See [`.combinations_with_replacement()`](crate::Itertools::combinations_with_replacement) |
| 12 | /// for more information. |
| 13 | #[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:)] |
| 14 | #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] |
| 15 | pub struct CombinationsWithReplacementrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#<I> |
| 16 | where |
| 17 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 18 | 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#, |
| 19 | { |
| 20 | indicesrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#indices.: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[usize]>, |
| 21 | poolrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#pool.: LazyBufferrust-analyzer cargo itertools 0.14.0 lazy_buffer/LazyBuffer#<I>, |
| 22 | firstrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#first.: bool, |
| 23 | } |
| 24 | |
| 25 | 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 CombinationsWithReplacementrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#<I> |
| 26 | where |
| 27 | 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#, |
| 28 | 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# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 29 | { |
| 30 | debug_fmt_fieldsrust-analyzer cargo itertools 0.14.0 impl_macros/debug_fmt_fields!!(CombinationsWithReplacement, indicesrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#indices., poolrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#pool., firstrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#first.); |
| 31 | } |
| 32 | |
| 33 | /// Create a new `CombinationsWithReplacement` from a clonable iterator. |
| 34 | pub fn combinations_with_replacementrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/combinations_with_replacement().<I>(iter: I, k: usize) -> CombinationsWithReplacementrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#<I> |
| 35 | where |
| 36 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 37 | 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#, |
| 38 | { |
| 39 | let indices = 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 macros/vec!![0; k].into_boxed_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]into_boxed_slice().(); |
| 40 | let pool: LazyBufferrust-analyzer cargo itertools 0.14.0 lazy_buffer/LazyBuffer#<I> = LazyBufferrust-analyzer cargo itertools 0.14.0 lazy_buffer/LazyBuffer#::newrust-analyzer cargo itertools 0.14.0 lazy_buffer/impl#[`LazyBuffer<I>`]new().(iter); |
| 41 | |
| 42 | CombinationsWithReplacementrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement# { |
| 43 | indicesrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#indices., |
| 44 | poolrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#pool., |
| 45 | firstrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#first.: true, |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | impl<I> CombinationsWithReplacementrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#<I> |
| 50 | where |
| 51 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 52 | 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#, |
| 53 | { |
| 54 | /// Increments indices representing the combination to advance to the next |
| 55 | /// (in lexicographic order by increasing sequence) combination. |
| 56 | /// |
| 57 | /// Returns true if we've run out of combinations, false otherwise. |
| 58 | fn increment_indicesrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/impl#[`CombinationsWithReplacement<I>`]increment_indices().(&mut self) -> bool { |
| 59 | // Check if we need to consume more from the iterator |
| 60 | // This will run while we increment our first index digit |
| 61 | self.poolrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#pool..get_nextrust-analyzer cargo itertools 0.14.0 lazy_buffer/impl#[`LazyBuffer<I>`]get_next().(); |
| 62 | |
| 63 | // Work out where we need to update our indices |
| 64 | let mut increment = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 65 | for (i, indices_int) in self.indicesrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#indices..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().().revrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#rev().() { |
| 66 | if *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().indices_int 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.poolrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#pool..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 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 { |
| 67 | increment = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((i, indices_int rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[`&usize`][`Add<usize>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[`&usize`][`Add<usize>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[`&usize`][`Add<usize>`]add().1)); |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | match increment { |
| 72 | // If we can update the indices further |
| 73 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((increment_from, increment_value)) => { |
| 74 | // We need to update the rightmost non-max value |
| 75 | // and all those to the right |
| 76 | self.indicesrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#indices.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().increment_from..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()..fillrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]fill().(increment_value); |
| 77 | false |
| 78 | } |
| 79 | // Otherwise, we're done |
| 80 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => true, |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | impl<I> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for CombinationsWithReplacementrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#<I> |
| 86 | where |
| 87 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 88 | 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#, |
| 89 | { |
| 90 | type Itemrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/impl#[`CombinationsWithReplacement<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#>; |
| 91 | |
| 92 | fn nextrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/impl#[`CombinationsWithReplacement<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 combinations_with_replacement/impl#[`CombinationsWithReplacement<I>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 93 | if self.firstrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#first. { |
| 94 | // In empty edge cases, stop iterating immediately |
| 95 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().(self.indicesrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#indices..is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() || self.poolrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#pool..get_nextrust-analyzer cargo itertools 0.14.0 lazy_buffer/impl#[`LazyBuffer<I>`]get_next().()) { |
| 96 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 97 | } |
| 98 | self.firstrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#first. = false; |
| 99 | } else if self.increment_indicesrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/impl#[`CombinationsWithReplacement<I>`]increment_indices().() { |
| 100 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 101 | } |
| 102 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(self.poolrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#pool..get_atrust-analyzer cargo itertools 0.14.0 lazy_buffer/impl#[`LazyBuffer<I>`]get_at().(&self.indicesrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#indices.)) |
| 103 | } |
| 104 | |
| 105 | fn nthrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/impl#[`CombinationsWithReplacement<I>`][Iterator]nth().(&mut self, n: usize) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/impl#[`CombinationsWithReplacement<I>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 106 | if self.firstrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#first. { |
| 107 | // In empty edge cases, stop iterating immediately |
| 108 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().(self.indicesrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#indices..is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() || self.poolrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#pool..get_nextrust-analyzer cargo itertools 0.14.0 lazy_buffer/impl#[`LazyBuffer<I>`]get_next().()) { |
| 109 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 110 | } |
| 111 | self.firstrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#first. = false; |
| 112 | } else if self.increment_indicesrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/impl#[`CombinationsWithReplacement<I>`]increment_indices().() { |
| 113 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 114 | } |
| 115 | for _ in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#n { |
| 116 | if self.increment_indicesrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/impl#[`CombinationsWithReplacement<I>`]increment_indices().() { |
| 117 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 118 | } |
| 119 | } |
| 120 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(self.poolrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#pool..get_atrust-analyzer cargo itertools 0.14.0 lazy_buffer/impl#[`LazyBuffer<I>`]get_at().(&self.indicesrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#indices.)) |
| 121 | } |
| 122 | |
| 123 | fn size_hintrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/impl#[`CombinationsWithReplacement<I>`][Iterator]size_hint().(&self) -> (usize, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>) { |
| 124 | let (mut low, mut upp) = self.poolrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#pool..size_hintrust-analyzer cargo itertools 0.14.0 lazy_buffer/impl#[`LazyBuffer<I>`]size_hint().(); |
| 125 | low = remaining_forrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/remaining_for().(low, self.firstrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#first., &self.indicesrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#indices.).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.); |
| 126 | upp = upp.and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|upp| remaining_forrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/remaining_for().(upp, self.firstrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#first., &self.indicesrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#indices.)); |
| 127 | (low, upp) |
| 128 | } |
| 129 | |
| 130 | fn countrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/impl#[`CombinationsWithReplacement<I>`][Iterator]count().(self) -> usize { |
| 131 | let Selfrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/impl#[`CombinationsWithReplacement<I>`][Iterator] { |
| 132 | indicesrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#indices., |
| 133 | poolrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#pool., |
| 134 | firstrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#first., |
| 135 | } = self; |
| 136 | let n = pool.countrust-analyzer cargo itertools 0.14.0 lazy_buffer/impl#[`LazyBuffer<I>`]count().(); |
| 137 | remaining_forrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/remaining_for().(n, first, &indices).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().() |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | impl<I> FusedIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/marker/FusedIterator# for CombinationsWithReplacementrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/CombinationsWithReplacement#<I> |
| 142 | where |
| 143 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 144 | 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#, |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | /// For a given size `n`, return the count of remaining combinations with replacement or None if it would overflow. |
| 149 | fn remaining_forrust-analyzer cargo itertools 0.14.0 combinations_with_replacement/remaining_for().(n: usize, first: bool, indices: &[usize]) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize> { |
| 150 | // With a "stars and bars" representation, choose k values with replacement from n values is |
| 151 | // like choosing k out of k + n − 1 positions (hence binomial(k + n - 1, k) possibilities) |
| 152 | // to place k stars and therefore n - 1 bars. |
| 153 | // Example (n=4, k=6): ***|*||** represents [0,0,0,1,3,3]. |
| 154 | let count = |n: usize, k: usize| { |
| 155 | let positions = if n 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 { |
| 156 | k.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(1) |
| 157 | } else { |
| 158 | (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().1).checked_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]checked_add().(k)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch(). |
| 159 | }; |
| 160 | checked_binomialrust-analyzer cargo itertools 0.14.0 adaptors/checked_binomial().(positions, k) |
| 161 | }; |
| 162 | let k = indices.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 163 | if first { |
| 164 | count(n, k) |
| 165 | } else { |
| 166 | // The algorithm is similar to the one for combinations *without replacement*, |
| 167 | // except we choose values *with replacement* and indices are *non-strictly* monotonically sorted. |
| 168 | |
| 169 | // The combinations generated after the current one can be counted by counting as follows: |
| 170 | // - The subsequent combinations that differ in indices[0]: |
| 171 | // If subsequent combinations differ in indices[0], then their value for indices[0] |
| 172 | // must be at least 1 greater than the current indices[0]. |
| 173 | // As indices is monotonically sorted, this means we can effectively choose k values with |
| 174 | // replacement from (n - 1 - indices[0]), leading to count(n - 1 - indices[0], k) possibilities. |
| 175 | // - The subsequent combinations with same indices[0], but differing indices[1]: |
| 176 | // Here we can choose k - 1 values with replacement from (n - 1 - indices[1]) values, |
| 177 | // leading to count(n - 1 - indices[1], k - 1) possibilities. |
| 178 | // - (...) |
| 179 | // - The subsequent combinations with same indices[0..=i], but differing indices[i]: |
| 180 | // Here we can choose k - i values with replacement from (n - 1 - indices[i]) values: count(n - 1 - indices[i], k - i). |
| 181 | // Since subsequent combinations can in any index, we must sum up the aforementioned binomial coefficients. |
| 182 | |
| 183 | // Below, `n0` resembles indices[i]. |
| 184 | indices.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, |sum, (i, n0)| { |
| 185 | sum.checked_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]checked_add().(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().1 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().n0, 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().i)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().) |
| 186 | }) |
| 187 | } |
| 188 | } |