Skip to content

Commit

Permalink
prevent overflow in size_hint for large number of permutations in Que…
Browse files Browse the repository at this point in the history
…ryPermutationIter
  • Loading branch information
Frizi committed Mar 27, 2021
1 parent e0f9176 commit 92621f6
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions crates/bevy_ecs/src/query/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,19 @@ where
.map(|index| self.world.archetypes[ArchetypeId::new(index)].len())
.sum();

// n! / k!(n-k)! = (n*n-1*...*n-k+1) / k!
let k_factorial: usize = (1..=K).product();
let max_permutations =
(0..K).fold(1, |n, i| n * (max_size.saturating_sub(i))) / k_factorial;
if max_size < K {
return (0, Some(0));
}

(0, Some(max_permutations))
// n! / k!(n-k)! = (n*n-1*...*n-k+1) / k!
let max_permutations = (0..K)
.try_fold(1usize, |n, i| n.checked_mul(max_size - i))
.map(|n| {
let k_factorial: usize = (1..=K).product();
n / k_factorial
});

(0, max_permutations)
}
}

Expand Down

0 comments on commit 92621f6

Please sign in to comment.