-
Notifications
You must be signed in to change notification settings - Fork 323
Implement trait on Owned and View arrays #528
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
Comments
I reviewed my problem and I think that the best solution is to impl<'a, V> Operation for Wrapper<ViewRepr<&'a V>>
where ViewRepr<&'a V> : Data<Elem=V>,
{
fn op(&mut self, axis: Axis, index: usize) {
let cloned_a = self.a.clone();
self.a = cloned_a.into_subview(axis, index);
}
} This code just clones the shape and strides and reuses the data, so I think is not too bad. However, I was thinking, why does Reviewing the code, pub fn into_subview(mut self, axis: Axis, index: Ix) -> ArrayBase<S, D::Smaller>
where D: RemoveAxis,
{
self.subview_inplace(axis, index);
self.remove_axis(axis)
}
pub fn subview_inplace(&mut self, axis: Axis, index: Ix) {
dimension::do_sub(&mut self.dim, &mut self.ptr, &self.strides,
axis.index(), index)
}
pub fn remove_axis(self, axis: Axis) -> ArrayBase<S, D::Smaller>
where D: RemoveAxis,
{
assert!(self.ndim() != 0);
let d = self.dim.remove_axis(axis);
let s = self.strides.remove_axis(axis);
ArrayBase {
ptr: self.ptr,
data: self.data,
dim: d,
strides: s,
}
} but this code only calls to pub trait RemoveAxis : Dimension {
fn remove_axis(&self, axis: Axis) -> Self::Smaller;
} I assume that must be a good reason to require |
I agree. Another option would be to use the
If I understand correctly, you're asking why we can't do this: pub fn into_subview2(&mut self, axis: Axis, index: Ix) -> ArrayBase<S, D::Smaller>
where
D: RemoveAxis,
{
// implementation
} It would be possible to implement a method with that kind of signature if we clone things: pub fn into_subview2(&mut self, axis: Axis, index: Ix) -> ArrayBase<S, D::Smaller>
where
S: DataClone,
D: RemoveAxis,
{
let mut c = self.clone();
c.subview_inplace(axis, index);
c.remove_axis(axis)
} However, if we want to avoid cloning, then it's not possible to implement this such that the result has the same elements as the input. Consider the case where Alternatively, you might be asking why we can't do this: pub fn into_subview2(&mut self, axis: Axis, index: Ix)
where
D: RemoveAxis,
{
// implementation
} where, unlike Note, however, that it would be possible for us to provide a special case of this for impl<A, S> ArrayBase<S, IxDyn>
where
S: Data<Elem = A>,
{
pub fn into_subview2(&mut self, axis: Axis, index: Ix) {
// implementation
}
} because removing axes does not change the type of the array. (It's still Does this answer your question? |
Thanks for your answer, @jturner314 .
That answers my questions about
I think this (#533) is nice. |
|
@jturner314 Thanks! It solves the issue. |
Hi,
this could be more of a Rust question than ndarray's question, but I submit this issue because maybe some of you have solved this yet.
I have an array inside of a struct
Wrapper
. The array could be owned or view. Now, I am trying to implement a given operation that requires usinginto_subview()
for both owned and viewWrapper
structs. My take on this was:This code does not compile, it errors in the view implementation:
At first, I thought that the
mem::replace()
in the view implementation was bounding the lifetime of theempty_view
. However, it fails the same with themem::replace()
code commented:How would you implement this (if possible)? I don't get why it is failing.
Thanks.
The text was updated successfully, but these errors were encountered: