diff --git a/ext/StridedViewsPtrArraysExt.jl b/ext/StridedViewsPtrArraysExt.jl index 65f58f8..de48a78 100644 --- a/ext/StridedViewsPtrArraysExt.jl +++ b/ext/StridedViewsPtrArraysExt.jl @@ -3,6 +3,6 @@ module StridedViewsPtrArraysExt using StridedViews using PtrArrays -StridedViews._normalizeparent(A::PtrArray) = PtrArray(A.ptr, length(A)) +StridedViews._normalizeparent(A::PtrArray, offset::Int) = PtrArray(A.ptr, length(A)), offset end diff --git a/src/auxiliary.jl b/src/auxiliary.jl index 0b19941..c543189 100644 --- a/src/auxiliary.jl +++ b/src/auxiliary.jl @@ -46,12 +46,14 @@ function _normalizestrides(size::Dims{N}, strides::Dims{N}) where {N} end # 'Normalize' the layout of a DenseArray, in order to reduce the number of required -# specializations in functions. -@inline _normalizeparent(A) = A +# specializations in functions. The offset is adjusted to the start of `A` in the +# normalized parent. +@inline _normalizeparent(A, offset::Int) = A, offset @static if isdefined(Core, :Memory) - @inline _normalizeparent(A::Array) = A.ref.mem + @inline _normalizeparent(A::Array, offset::Int) = + A.ref.mem, offset + Base.memoryrefoffset(A.ref) - 1 else - @inline _normalizeparent(A::Array) = reshape(A, length(A)) + @inline _normalizeparent(A::Array, offset::Int) = reshape(A, length(A)), offset end # Auxiliary methods for `sview` diff --git a/src/stridedview.jl b/src/stridedview.jl index a996aac..3af05e0 100644 --- a/src/stridedview.jl +++ b/src/stridedview.jl @@ -51,9 +51,9 @@ function StridedView{T}( offset::Int = 0, op::F = identity ) where {T, N, F} - parent′ = _normalizeparent(parent) + parent′, offset′ = _normalizeparent(parent, offset) strides′ = _normalizestrides(size, strides) - return StridedView{T, N, typeof(parent′), F}(parent′, size, strides′, offset, op) + return StridedView{T, N, typeof(parent′), F}(parent′, size, strides′, offset′, op) end """ diff --git a/test/runtests.jl b/test/runtests.jl index ae42ec0..c00624a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -151,6 +151,19 @@ if !is_buildkite @test !isstrided(Diagonal([0.5, 1.0, 1.5])) end + @testset "arrays not starting at the beginning of their memory" begin + v = collect(1.0:6.0) + popfirst!(v) + @test StridedView(v) == v + @test pointer(StridedView(v)) == pointer(v) + M = reshape(deleteat!(collect(1.0:8.0), 1:2), 2, 3) + @test StridedView(M) == M + @test StridedView(M)[:, 2:3] == M[:, 2:3] + @test sview(M, 2, :) == M[2, :] + StridedView(M)[2, 1] = 0 + @test M[2, 1] == 0 + end + @testset "transpose and adjoint with vector StridedView" begin @testset for T in (Float32, Float64, Complex{Float32}, Complex{Float64}) A = randn(T, (60,))