From a965580ba7fd0e8314001521df254e30d686afbf Mon Sep 17 00:00:00 2001 From: Michael Abbott <32575566+mcabbott@users.noreply.github.com> Date: Mon, 6 Apr 2020 13:45:36 +0200 Subject: [PATCH] Use parent for similar(::PermutedDimsArray) (#35304) --- base/permuteddimsarray.jl | 4 +++- test/arrayops.jl | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/base/permuteddimsarray.jl b/base/permuteddimsarray.jl index d50cc11678e78..348d4ad449a53 100644 --- a/base/permuteddimsarray.jl +++ b/base/permuteddimsarray.jl @@ -46,9 +46,11 @@ function PermutedDimsArray(data::AbstractArray{T,N}, perm) where {T,N} end Base.parent(A::PermutedDimsArray) = A.parent -Base.size(A::PermutedDimsArray{T,N,perm}) where {T,N,perm} = genperm(size(parent(A)), perm) +Base.size(A::PermutedDimsArray{T,N,perm}) where {T,N,perm} = genperm(size(parent(A)), perm) Base.axes(A::PermutedDimsArray{T,N,perm}) where {T,N,perm} = genperm(axes(parent(A)), perm) +Base.similar(A::PermutedDimsArray, T::Type, dims::Base.Dims) = similar(parent(A), T, dims) + Base.unsafe_convert(::Type{Ptr{T}}, A::PermutedDimsArray{T}) where {T} = Base.unsafe_convert(Ptr{T}, parent(A)) # It's OK to return a pointer to the first element, and indeed quite diff --git a/test/arrayops.jl b/test/arrayops.jl index 6ea8d6a43e41e..667ca080034f8 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -3,6 +3,9 @@ # Array test isdefined(Main, :OffsetArrays) || @eval Main include("testhelpers/OffsetArrays.jl") using .Main.OffsetArrays + +isdefined(@__MODULE__, :T24Linear) || include("testhelpers/arrayindexingtypes.jl") + using SparseArrays using Random, LinearAlgebra @@ -683,6 +686,13 @@ end y = [0, 0, 0, 0] copyto!(y, x) @test y == [1, 2, 3, 4] + + # similar, https://github.com/JuliaLang/julia/pull/35304 + x = PermutedDimsArray([1 2; 3 4], (2, 1)) + @test similar(x, 3,3) isa Array + z = TSlow([1 2; 3 4]) + x_slow = PermutedDimsArray(z, (2, 1)) + @test similar(x_slow, 3,3) isa TSlow end @testset "circshift" begin