Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion ext/StridedViewsPtrArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 6 additions & 4 deletions src/auxiliary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
4 changes: 2 additions & 2 deletions src/stridedview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

"""
Expand Down
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,))
Expand Down
Loading