From fa09434998ad0c6c35d6785319ae14c6869f9e23 Mon Sep 17 00:00:00 2001 From: Martin Holters Date: Wed, 11 Apr 2018 13:21:29 +0200 Subject: [PATCH] Teach selectdim that its slices are slices (#531) ... as done in JuliaLang/julia#26655. --- src/Compat.jl | 2 +- test/runtests.jl | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Compat.jl b/src/Compat.jl index c60daaed2..f46cc7e3f 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index cb365e650..00361596e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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