diff --git a/src/iter/combinations.rs b/src/iter/combinations.rs index 990548f94..437fff5de 100644 --- a/src/iter/combinations.rs +++ b/src/iter/combinations.rs @@ -346,7 +346,7 @@ where let k = indices.as_mut().len(); let total = checked_binomial(n, k).expect(OVERFLOW_MSG); match offset.cmp(&total) { - Ordering::Less => { + Ordering::Equal => { unrank(indices.as_mut(), offset - 1, n); Self { indices, @@ -354,7 +354,7 @@ where position: IndicesPosition::End, } } - Ordering::Equal => { + Ordering::Less => { unrank(indices.as_mut(), offset, n); Self { indices, @@ -549,6 +549,8 @@ fn decrement_indices(indices: &mut [usize], n: usize) { // Decrement index, and reset the ones to its right indices[i] -= 1; + for (j, index) in indices.iter_mut().enumerate().skip(i + 1) { + *index = n - k + j; for (j, index) in indices.iter_mut().enumerate().skip(i + 1) { *index = n - k + j; }