Skip to content

Commit

Permalink
FIX: Fix the permutation in sort-axis example
Browse files Browse the repository at this point in the history
  • Loading branch information
bluss committed Feb 9, 2021
1 parent 8751d21 commit 1c6c49f
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions examples/sort-axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,27 @@ where
assert_eq!(axis_len, perm.indices.len());
debug_assert!(perm.correct());

if self.is_empty() {
return self;
}

let mut result = Array::uninit(self.dim());

unsafe {
// logically move ownership of all elements from self into result
// the result realizes this ownership at .assume_init() further down
let mut moved_elements = 0;
for i in 0..axis_len {
let perm_i = perm.indices[i];
Zip::from(result.index_axis_mut(axis, perm_i))
.and(self.index_axis(axis, i))
.for_each(|to, from| {
copy_nonoverlapping(from, to.as_mut_ptr(), 1);
moved_elements += 1;
});
}
Zip::from(&perm.indices)
.and(result.axis_iter_mut(axis))
.for_each(|&perm_i, result_pane| {
// possible improvement: use unchecked indexing for `index_axis`
Zip::from(result_pane)
.and(self.index_axis(axis, perm_i))
.for_each(|to, from| {
copy_nonoverlapping(from, to.as_mut_ptr(), 1);
moved_elements += 1;
});
});
debug_assert_eq!(result.len(), moved_elements);
// panic-critical begin: we must not panic
// forget moved array elements but not its vec
Expand Down

0 comments on commit 1c6c49f

Please sign in to comment.