Skip to content

Commit

Permalink
Simplify ravel_multi_index and unravel_index
Browse files Browse the repository at this point in the history
  • Loading branch information
cschwan committed Apr 25, 2024
1 parent 4227d48 commit cac8915
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions pineappl/src/packed_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,17 @@ fn ravel_multi_index<const D: usize>(multi_index: &[usize; D], shape: &[usize])
multi_index
.iter()
.zip(shape)
.skip(1)
.fold(multi_index[0], |acc, (i, d)| acc * d + i)
.fold(0, |acc, (i, d)| acc * d + i)
}

/// Converts a flat `index` into a `multi_index`.
fn unravel_index<const D: usize>(index: usize, shape: &[usize]) -> [usize; D] {
fn unravel_index<const D: usize>(mut index: usize, shape: &[usize]) -> [usize; D] {
assert!(index < shape.iter().product());
let mut indices = [0; D];
indices
.iter_mut()
.zip(shape)
.rev()
.fold(index, |acc, (i, d)| {
*i = acc % d;
acc / d
});
for (i, d) in indices.iter_mut().zip(shape).rev() {
*i = index % d;
index /= d;
}
indices
}

Expand Down

0 comments on commit cac8915

Please sign in to comment.