From 0dd6d089cf92b8f5277f0e3a729b0782a3cf8bef Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Wed, 8 Apr 2026 13:23:02 +0200 Subject: [PATCH] fix(descriptor): correct index tracking in `combinations()` function The combinations function was pushing `new_index` (the enumerate index relative to the skipped iterator) instead of `index + 1 + new_index` (the absolute index in the original vec). This caused duplicate and incorrect combinations to be generated for inputs larger than trivial sizes. Co-Authored-By: HAL 9000 Signed-off-by: Elias Rohrer --- src/descriptor/policy.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/descriptor/policy.rs b/src/descriptor/policy.rs index 044937cb..9ecd3e88 100644 --- a/src/descriptor/policy.rs +++ b/src/descriptor/policy.rs @@ -196,7 +196,7 @@ fn combinations(vec: &[usize], size: usize) -> Vec> { for (new_index, val) in vec.iter().skip(index + 1).enumerate() { let mut cloned = vals.clone(); cloned.push(*val); - queue.push_front((new_index, cloned)); + queue.push_front((index + 1 + new_index, cloned)); } } } @@ -1937,4 +1937,18 @@ mod test { } ); } + + #[test] + fn test_combinations_four_choose_three() { + let vec = vec![0, 1, 2, 3]; + let mut result = combinations(&vec, 3); + for combo in &mut result { + combo.sort(); + } + result.sort(); + assert_eq!( + result, + vec![vec![0, 1, 2], vec![0, 1, 3], vec![0, 2, 3], vec![1, 2, 3]] + ); + } }