Prevent underflow with saturating_sub(1) (#812) (#817)

This commit is contained in:
Johannes
2025-04-01 09:51:03 +02:00
committed by GitHub
parent 176c3bae14
commit 3855592447
2 changed files with 5 additions and 4 deletions

View File

@@ -37,7 +37,7 @@ impl IslandManager {
}
pub(crate) fn num_islands(&self) -> usize {
self.active_islands.len() - 1
self.active_islands.len().saturating_sub(1)
}
/// Update this data-structure after one or multiple rigid-bodies have been removed for `bodies`.
@@ -244,8 +244,8 @@ impl IslandManager {
self.active_islands.clear();
self.active_islands.push(0);
// The max avoid underflow when the stack is empty.
let mut island_marker = self.stack.len().max(1) - 1;
// saturating_sub(1) prevents underflow when the stack is empty.
let mut island_marker = self.stack.len().saturating_sub(1);
// NOTE: islands containing a body with non-standard number of iterations wont
// be merged with another island, unless another island with standard

View File

@@ -58,8 +58,9 @@ impl ParallelInteractionGroups {
let range = self.groups[i]..self.groups[i + 1];
&self.sorted_interactions[range]
}
pub fn num_groups(&self) -> usize {
self.groups.len() - 1
self.groups.len().saturating_sub(1)
}
pub fn group_interactions<Interaction: PairInteraction>(