10000 Add partition(similar to numpy.partition) by NewBornRustacean · Pull Request #1498 · rust-ndarray/ndarray · GitHub
[go: up one dir, main page]

Skip to content

Add partition(similar to numpy.partition) #1498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
applied feedback: safe unwrap(), avoid navie iter()
  • Loading branch information
NewBornRustacean committed Apr 4, 2025
commit 3344cf8b73fe8c5760d3c0cbdad31f5b52078722
12 changes: 8 additions & 4 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3218,7 +3218,7 @@ impl<A, D: Dimension> ArrayRef<A, D>
/// ```
pub fn partition(&self, kth: usize, axis: Axis) -> Array<A, D>
where
A: Clone + Ord,
A: Clone + Ord + num_traits::Zero,
D: Dimension,
{
// Bounds checking
Expand All @@ -3234,8 +3234,8 @@ impl<A, D: Dimension> ArrayRef<A, D>
.lanes_mut(axis)
.into_iter()
.next()
.map(|lane| lane.is_contiguous())
.unwrap_or(false);
.unwrap()
.is_contiguous();

if is_contiguous {
Zip::from(result.lanes_mut(axis)).for_each(|mut lane| {
Expand All @@ -3246,7 +3246,11 @@ impl<A, D: Dimension> ArrayRef<A, D>

Zip::from(result.lanes_mut(axis)).for_each(|mut lane| {
temp_vec.clear();
temp_vec.extend(lane.iter().cloned());
temp_vec.resize(axis_len, A::zero());

Zip::from(&mut temp_vec).and(&lane).for_each(|dest, src| {
*dest = src.clone();
});

temp_vec.select_nth_unstable(kth);

Expand Down
Loading
0