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
24 changes: 24 additions & 0 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,15 @@ function getindex(V::FastContiguousSubArray, i::Int)
@inbounds r = V.parent[V.offset1 + i]
r
end
# parents of FastContiguousSubArrays may support fast indexing with AbstractUnitRanges,
# so we may just forward the indexing to the parent
function getindex(V::FastContiguousSubArray, i::AbstractUnitRange{Int})
@inline
@boundscheck checkbounds(V, i)
@inbounds r = V.parent[V.offset1 .+ i]
r
end

# For vector views with linear indexing, we disambiguate to favor the stride/offset
# computation as that'll generally be faster than (or just as fast as) re-indexing into a range.
function getindex(V::FastSubArray{<:Any, 1}, i::Int)
Expand All @@ -348,6 +357,7 @@ function getindex(V::FastContiguousSubArray{<:Any, 1}, i::Int)
@inbounds r = V.parent[V.offset1 + i]
r
end
@inline getindex(V::FastContiguousSubArray, i::Colon) = getindex(V, to_indices(V, (:,))...)

# Indexed assignment follows the same pattern as `getindex` above
function setindex!(V::SubArray{T,N}, x, I::Vararg{Int,N}) where {T,N}
Expand All @@ -368,6 +378,19 @@ function setindex!(V::FastContiguousSubArray, x, i::Int)
@inbounds V.parent[V.offset1 + i] = x
V
end
function setindex!(V::FastSubArray, x, i::AbstractUnitRange{Int})
@inline
@boundscheck checkbounds(V, i)
@inbounds V.parent[V.offset1 .+ V.stride1 .* i] = x
V
end
function setindex!(V::FastContiguousSubArray, x, i::AbstractUnitRange{Int})
@inline
@boundscheck checkbounds(V, i)
@inbounds V.parent[V.offset1 .+ i] = x
V
end

function setindex!(V::FastSubArray{<:Any, 1}, x, i::Int)
@inline
@boundscheck checkbounds(V, i)
Expand All @@ -380,6 +403,7 @@ function setindex!(V::FastContiguousSubArray{<:Any, 1}, x, i::Int)
@inbounds V.parent[V.offset1 + i] = x
V
end
@inline setindex!(V::FastSubArray, x, i::Colon) = setindex!(V, x, to_indices(V, (i,))...)

function isassigned(V::SubArray{T,N}, I::Vararg{Int,N}) where {T,N}
@inline
Expand Down
107 changes: 107 additions & 0 deletions test/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,113 @@ end
@test sA[[1 2 4 4; 6 1 1 4]] == [34 35 38 38; 50 34 34 38]
end

@testset "fast linear indexing with AbstractUnitRange or Colon indices" begin
@testset "getindex" begin
@testset "1D" begin
for a1 in Any[1:5, [1:5;]]
b1 = @view a1[:]; # FastContiguousSubArray
c1 = @view a1[eachindex(a1)]; # FastContiguousSubArray
d1 = @view a1[begin:1:end]; # FastSubArray

ax1 = eachindex(a1);
@test b1[ax1] == c1[ax1] == d1[ax1] == a1[ax1]
@test b1[:] == c1[:] == d1[:] == a1[:]

# some arbitrary indices
inds1 = 2:4
c1 = @view a1[inds1]
@test c1[axes(c1,1)] == c1[:] == a1[inds1]

inds12 = Base.IdentityUnitRange(Base.OneTo(4))
c1 = @view a1[inds12]
@test c1[axes(c1,1)] == c1[:] == a1[inds12]

inds2 = 3:2:5
d1 = @view a1[inds2]
@test d1[axes(d1,1)] == d1[:] == a1[inds2]
end
end

@testset "2D" begin
a2_ = reshape(1:25, 5, 5)
for a2 in Any[a2_, collect(a2_)]
b2 = @view a2[:, :]; # 2D FastContiguousSubArray
b22 = @view a2[:]; # 1D FastContiguousSubArray
c2 = @view a2[eachindex(a2)]; # 1D FastContiguousSubArray
d2 = @view a2[begin:1:end]; # 1D FastSubArray

ax2 = eachindex(a2);
@test b2[ax2] == b22[ax2] == c2[ax2] == d2[ax2] == a2[ax2]
@test b2[:] == b22[:] == c2[:] == d2[:] == a2[:]

# some arbitrary indices
inds1 = 2:4
c2 = @view a2[inds1]
@test c2[axes(c2,1)] == c2[:] == a2[inds1]

inds12 = Base.IdentityUnitRange(Base.OneTo(4))
c2 = @view a2[inds12]
@test c2[axes(c2,1)] == c2[:] == a2[inds12]

inds2 = 2:2:4
d2 = @view a2[inds2];
@test d2[axes(d2,1)] == d2[:] == a2[inds2]
end
end
end
@testset "setindex!" begin
@testset "1D" begin
a1 = rand(10);
a12 = copy(a1);
b1 = @view a1[:]; # 1D FastContiguousSubArray
c1 = @view a1[eachindex(a1)]; # 1D FastContiguousSubArray
d1 = @view a1[begin:1:end]; # 1D FastSubArray

ax1 = eachindex(a1);
@test (b1[ax1] = a12; b1) == (c1[ax1] = a12; c1) == (d1[ax1] = a12; d1) == (a1[ax1] = a12; a1)
@test (b1[:] = a12; b1) == (c1[:] = a12; c1) == (d1[:] = a12; d1) == (a1[:] = a12; a1)

# some arbitrary indices
ind1 = 2:4
c1 = a12[ind1]
@test (c1[axes(c1,1)] = a12[ind1]; c1) == (c1[:] = a12[ind1]; c1) == a12[ind1]

inds1 = Base.IdentityUnitRange(Base.OneTo(4))
c1 = @view a1[inds1]
@test (c1[eachindex(c1)] = @view(a12[inds1]); c1) == @view(a12[inds1])

ind2 = 2:2:8
d1 = a12[ind2]
@test (d1[axes(d1,1)] = a12[ind2]; d1) == (d1[:] = a12[ind2]; d1) == a12[ind2]
end

@testset "2D" begin
a2 = rand(10, 10);
a22 = copy(a2);
a2v = vec(a22);
b2 = @view a2[:, :]; # 2D FastContiguousSubArray
c2 = @view a2[eachindex(a2)]; # 1D FastContiguousSubArray
d2 = @view a2[begin:1:end]; # 1D FastSubArray

@test (b2[eachindex(b2)] = a2v; vec(b2)) == (c2[eachindex(c2)] = a2v; c2) == a2v
@test (d2[eachindex(d2)] = a2v; d2) == a2v

# some arbitrary indices
inds1 = 3:9
c2 = @view a2[inds1]
@test (c2[eachindex(c2)] = @view(a22[inds1]); c2) == @view(a22[inds1])

inds1 = Base.IdentityUnitRange(Base.OneTo(4))
c2 = @view a2[inds1]
@test (c2[eachindex(c2)] = @view(a22[inds1]); c2) == @view(a22[inds1])

inds2 = 3:3:9
d2 = @view a2[inds2]
@test (d2[eachindex(d2)] = @view(a22[inds2]); d2) == @view(a22[inds2])
end
end
end

@testset "issue #11871" begin
a = fill(1., (2,2))
b = view(a, 1:2, 1:2)
Expand Down