Skip to content

Commit

Permalink
Teach selectdim that its slices are slices (#531)
Browse files Browse the repository at this point in the history
... as done in JuliaLang/julia#26655.
  • Loading branch information
martinholters authored Apr 11, 2018
1 parent 040ccc1 commit fa09434
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1780,7 +1780,7 @@ end

if !isdefined(Base, :selectdim) # 0.7.0-DEV.3976
export selectdim
@inline selectdim(A::AbstractArray, d::Integer, i) = _selectdim(A, d, i, Base.setindex(axes(A), i, d))
@inline selectdim(A::AbstractArray, d::Integer, i) = _selectdim(A, d, i, Base.setindex(map(Base.Slice, axes(A)), i, d))
@noinline function _selectdim(A, d, i, idxs)
d >= 1 || throw(ArgumentError("dimension must be ≥ 1"))
nd = ndims(A)
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1681,6 +1681,13 @@ let A = rand(5,5)
@test selectdim(A, 2, 1:3) == A[:, 1:3]
selectdim(A, 1, 3)[3] = 42
@test A[3,3] == 42
if VERSION < v"0.7.0-DEV.3976" || VERSION >= v"0.7.0-DEV.4739"
# in the omitted version range, Julia's selectdim always gives IndexCartesian()
B = rand(4, 3, 2)
@test IndexStyle(selectdim(B, 1, 1)) == IndexStyle(view(B, 1, :, :)) == IndexLinear()
@test IndexStyle(selectdim(B, 2, 1)) == IndexStyle(view(B, :, 1, :)) == IndexCartesian()
@test IndexStyle(selectdim(B, 3, 1)) == IndexStyle(view(B, :, :, 1)) == IndexLinear()
end
end

nothing

0 comments on commit fa09434

Please sign in to comment.