Skip to content
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

Implement construction from negative strides #901

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/impl_raw_views.rs
@@ -1,7 +1,7 @@
use std::mem;
use std::ptr::NonNull;

use crate::dimension::{self, stride_offset};
use crate::dimension::{self, stride_offset, offset_from_ptr_to_memory};
use crate::extension::nonnull::nonnull_debug_checked_from_ptr;
use crate::imp_prelude::*;
use crate::is_aligned;
Expand Down Expand Up @@ -85,7 +85,7 @@ where
}
bluss marked this conversation as resolved.
Show resolved Hide resolved
}
let strides = shape.strides.strides_for_dim(&dim);
RawArrayView::new_(ptr, dim, strides)
RawArrayView::new_(ptr.offset(-offset_from_ptr_to_memory(&dim, &strides)), dim, strides)
}

/// Converts to a read-only view of the array.
Expand Down Expand Up @@ -231,7 +231,7 @@ where
}
}
let strides = shape.strides.strides_for_dim(&dim);
RawArrayViewMut::new_(ptr, dim, strides)
RawArrayViewMut::new_(ptr.offset(-offset_from_ptr_to_memory(&dim, &strides)), dim, strides)
}

/// Converts to a non-mutable `RawArrayView`.
Expand Down
5 changes: 3 additions & 2 deletions src/impl_views/constructors.rs
Expand Up @@ -13,6 +13,7 @@ use crate::error::ShapeError;
use crate::extension::nonnull::nonnull_debug_checked_from_ptr;
use crate::imp_prelude::*;
use crate::{is_aligned, StrideShape};
use crate::dimension::offset_from_ptr_to_memory;

/// Methods for read-only array views.
impl<'a, A, D> ArrayView<'a, A, D>
Expand Down Expand Up @@ -55,7 +56,7 @@ where
let dim = shape.dim;
dimension::can_index_slice_with_strides(xs, &dim, &shape.strides)?;
let strides = shape.strides.strides_for_dim(&dim);
unsafe { Ok(Self::new_(xs.as_ptr(), dim, strides)) }
unsafe { Ok(Self::new_(xs.as_ptr().offset(-offset_from_ptr_to_memory(&dim, &strides)), dim, strides)) }
}

/// Create an `ArrayView<A, D>` from shape information and a raw pointer to
Expand Down Expand Up @@ -152,7 +153,7 @@ where
let dim = shape.dim;
dimension::can_index_slice_with_strides(xs, &dim, &shape.strides)?;
let strides = shape.strides.strides_for_dim(&dim);
unsafe { Ok(Self::new_(xs.as_mut_ptr(), dim, strides)) }
unsafe { Ok(Self::new_(xs.as_mut_ptr().offset(-offset_from_ptr_to_memory(&dim, &strides)), dim, strides)) }
}

/// Create an `ArrayViewMut<A, D>` from shape information and a
Expand Down