-
Notifications
You must be signed in to change notification settings - Fork 321
append, append_row, append_column: methods for appending an array or single rows and columns #932
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
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
115b2af
zip: In Zip, move dimension check to free function
bluss 13dab58
append: Add try_append_row and try_append_column
bluss af47658
append: .try_append_array() to append array to array
bluss a7e3aab
append: Update doc comment for try_append_array
bluss f003aaa
append: Solve axis iteration order problem by sorting axes
bluss 29896d8
append: Fix situations where we need to recompute stride
bluss 54359a9
append: Use try_append_array for append row/column
bluss 6ca4275
stacking: Add a few more test cases for concatenate
bluss e862946
stacking: Port concatenate to append, supporting Clone
bluss 0159715
stacking: Port stack to append, supporting Clone
bluss 9ad7a0f
stacking: .select() now supports A: Clone
bluss 6477157
zip: Fix iteration order debug assertion in Zip
bluss 0599fd2
append: Handle empty arrays in append
bluss 0c578aa
append: Add append test that deals with incorrect layout situations
bluss aad9c74
stacking: Add benchmarks for .select() (based on append)
bluss 3d9a9ba
move_into: New method .move_into() for moving all array elements
bluss f153dc4
append: Adapt memory layout automatically in append if needed
bluss e0d07d3
append: Handle negative stride arrays
bluss b09c770
append: Name methods .append(axis, array), .append_row/column()
bluss 7d522b9
append: Add more append tests for negative stride arrays
bluss d4819cc
zip: Make sure Layout::tendency is inlinable
bluss f2fd6c2
zip: Add Zip::and_unchecked and use to skip redundant dimension check
bluss c1a5ceb
stacking: Improve .select() with special case for 1D arrays
bluss 8d44bbc
append: Edits and clarifications in append comments
bluss b103515
stacking: Update concatenate test
bluss 59013cd
move_into: Implement inner-dimension optimization in move-into
bluss c9dd195
append: Add comment explaining the SetLenOnDrop optimization
bluss 3f59442
append: Use standard order for non-growing axes in append
bluss 1206bd6
append: Use positive stride and ignore stride of len 1 axes
bluss d946360
append: performance optimize stride check
bluss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#![feature(test)] | ||
|
||
extern crate test; | ||
use test::Bencher; | ||
|
||
use ndarray::prelude::*; | ||
|
||
#[bench] | ||
fn select_axis0(bench: &mut Bencher) { | ||
let a = Array::<f32, _>::zeros((256, 256)); | ||
let selectable = vec![0, 1, 2, 0, 1, 3, 0, 4, 16, 32, 128, 147, 149, 220, 221, 255, 221, 0, 1]; | ||
bench.iter(|| { | ||
a.select(Axis(0), &selectable) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn select_axis1(bench: &mut Bencher) { | ||
let a = Array::<f32, _>::zeros((256, 256)); | ||
let selectable = vec![0, 1, 2, 0, 1, 3, 0, 4, 16, 32, 128, 147, 149, 220, 221, 255, 221, 0, 1]; | ||
bench.iter(|| { | ||
a.select(Axis(1), &selectable) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn select_1d(bench: &mut Bencher) { | ||
let a = Array::<f32, _>::zeros(1024); | ||
let mut selectable = (0..a.len()).step_by(17).collect::<Vec<_>>(); | ||
selectable.extend(selectable.clone().iter().rev()); | ||
|
||
bench.iter(|| { | ||
a.select(Axis(0), &selectable) | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -873,20 +873,39 @@ where | |
/// ``` | ||
pub fn select(&self, axis: Axis, indices: &[Ix]) -> Array<A, D> | ||
where | ||
A: Copy, | ||
A: Clone, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! :) |
||
S: Data, | ||
D: RemoveAxis, | ||
{ | ||
let mut subs = vec![self.view(); indices.len()]; | ||
for (&i, sub) in zip(indices, &mut subs[..]) { | ||
sub.collapse_axis(axis, i); | ||
} | ||
if subs.is_empty() { | ||
let mut dim = self.raw_dim(); | ||
dim.set_axis(axis, 0); | ||
unsafe { Array::from_shape_vec_unchecked(dim, vec![]) } | ||
if self.ndim() == 1 { | ||
// using .len_of(axis) means that we check if `axis` is in bounds too. | ||
let axis_len = self.len_of(axis); | ||
// bounds check the indices first | ||
if let Some(max_index) = indices.iter().cloned().max() { | ||
if max_index >= axis_len { | ||
panic!("ndarray: index {} is out of bounds in array of len {}", | ||
max_index, self.len_of(axis)); | ||
} | ||
} // else: indices empty is ok | ||
let view = self.view().into_dimensionality::<Ix1>().unwrap(); | ||
Array::from_iter(indices.iter().map(move |&index| { | ||
// Safety: bounds checked indexes | ||
unsafe { | ||
view.uget(index).clone() | ||
} | ||
})).into_dimensionality::<D>().unwrap() | ||
} else { | ||
concatenate(axis, &subs).unwrap() | ||
let mut subs = vec![self.view(); indices.len()]; | ||
for (&i, sub) in zip(indices, &mut subs[..]) { | ||
sub.collapse_axis(axis, i); | ||
} | ||
if subs.is_empty() { | ||
let mut dim = self.raw_dim(); | ||
dim.set_axis(axis, 0); | ||
unsafe { Array::from_shape_vec_unchecked(dim, vec![]) } | ||
} else { | ||
concatenate(axis, &subs).unwrap() | ||
} | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name
end_nonnull
sounds a little better to me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll leave this for now, I forgot about it, that's mostly why. And that the convention is weirdly strong with preferring "as_" for pointer accessors, so that's what a couple of our methods have. I don't even like
.as_ptr()
- it should normally be just.ptr()
etc.