Skip to content

Commit 0dd6d08

Browse files
committed
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 <dev@tnull.de>
1 parent fb7681a commit 0dd6d08

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

src/descriptor/policy.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ fn combinations(vec: &[usize], size: usize) -> Vec<Vec<usize>> {
196196
for (new_index, val) in vec.iter().skip(index + 1).enumerate() {
197197
let mut cloned = vals.clone();
198198
cloned.push(*val);
199-
queue.push_front((new_index, cloned));
199+
queue.push_front((index + 1 + new_index, cloned));
200200
}
201201
}
202202
}
@@ -1937,4 +1937,18 @@ mod test {
19371937
}
19381938
);
19391939
}
1940+
1941+
#[test]
1942+
fn test_combinations_four_choose_three() {
1943+
let vec = vec![0, 1, 2, 3];
1944+
let mut result = combinations(&vec, 3);
1945+
for combo in &mut result {
1946+
combo.sort();
1947+
}
1948+
result.sort();
1949+
assert_eq!(
1950+
result,
1951+
vec![vec![0, 1, 2], vec![0, 1, 3], vec![0, 2, 3], vec![1, 2, 3]]
1952+
);
1953+
}
19401954
}

0 commit comments

Comments
 (0)