Skip to content

Commit 50379b9

Browse files
authored
(rebased & patched) add similar for ReshapedArray, SubArray, ReinterpretArray types (#59236)
#54880 + an extra `similar(::Type{Array{T,N}}, dims::Dims)` method closes #54849, closes #54880
1 parent cbea8cf commit 50379b9

File tree

8 files changed

+13
-0
lines changed

8 files changed

+13
-0
lines changed

base/array.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ similar(a::Array{T,2}, S::Type) where {T} = Matrix{S}(undef, size(a,1)
382382
similar(a::Array{T}, m::Int) where {T} = Vector{T}(undef, m)
383383
similar(a::Array, T::Type, dims::Dims{N}) where {N} = Array{T,N}(undef, dims)
384384
similar(a::Array{T}, dims::Dims{N}) where {T,N} = Array{T,N}(undef, dims)
385+
similar(::Type{Array{T,N}}, dims::Dims) where {T,N} = similar(Array{T}, dims)
385386

386387
# T[x...] constructs Array{T,1}
387388
"""

base/reinterpretarray.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ end
221221
_checkcontiguous(::Type{Bool}, A::ReinterpretArray) = _checkcontiguous(Bool, parent(A))
222222

223223
similar(a::ReinterpretArray, T::Type, d::Dims) = similar(a.parent, T, d)
224+
similar(::Type{TA}, dims::Dims) where {T,N,O,P,TA<:ReinterpretArray{T,N,O,P}} = similar(P, dims)
224225

225226
function check_readable(a::ReinterpretArray{T, N, S} where N) where {T,S}
226227
# See comment in check_writable

base/reshapedarray.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ end
253253
size(A::ReshapedArray) = A.dims
254254
length(A::ReshapedArray) = length(parent(A))
255255
similar(A::ReshapedArray, eltype::Type, dims::Dims) = similar(parent(A), eltype, dims)
256+
similar(::Type{TA}, dims::Dims) where {T,N,P,TA<:ReshapedArray{T,N,P}} = similar(P, dims)
256257
IndexStyle(::Type{<:ReshapedArrayLF}) = IndexLinear()
257258
parent(A::ReshapedArray) = A.parent
258259
parentindices(A::ReshapedArray) = map(oneto, size(parent(A)))

base/subarray.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ viewindexing(I::Tuple{AbstractArray, Vararg{Any}}) = IndexCartesian()
6565
size(V::SubArray) = (@inline; map(length, axes(V)))
6666

6767
similar(V::SubArray, T::Type, dims::Dims) = similar(V.parent, T, dims)
68+
similar(::Type{TA}, dims::Dims) where {T,N,P,TA<:SubArray{T,N,P}} = similar(P, dims)
6869

6970
sizeof(V::SubArray) = length(V) * sizeof(eltype(V))
7071
sizeof(V::SubArray{<:Any,<:Any,<:Array}) = length(V) * elsize(V.parent)

test/offsetarray.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,8 @@ A = OffsetArray(view(rand(4,4), 1:4, 4:-1:1), (-3,5))
597597
# issue #33614
598598
A = OffsetArray(-1:0, (-2,))
599599
@test reshape(A, :) === A
600+
@test axes(similar(typeof(A),axes(A))) == axes(A)
601+
@test eltype(similar(typeof(A),axes(A))) == eltype(A)
600602
Arsc = reshape(A, :, 1)
601603
Arss = reshape(A, 2, 1)
602604
@test Arsc[1,1] == Arss[1,1] == -1

test/reinterpretarray.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,9 @@ end
540540
@test as isa TSlow{Int,3}
541541
@test size(as) == (3, 5, 1)
542542

543+
as = similar(typeof(a),(3, 5, 1))
544+
@test as isa TSlow{Float64,3}
545+
@test size(as) == (3, 5, 1)
543546
a = reinterpret(reshape, NTuple{4,Float64}, TSlow(rand(Float64, 4, 4)))
544547

545548
as = similar(a)

test/subarray.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ end
381381
sA = view(A, 1:2, 3, [1 3; 4 2])
382382
@test ndims(sA) == 3
383383
@test axes(sA) === (Base.OneTo(2), Base.OneTo(2), Base.OneTo(2))
384+
@test axes(similar(typeof(A),axes(A))) == axes(A)
385+
@test eltype(similar(typeof(A),axes(A))) == eltype(A)
384386
end
385387

386388
@testset "logical indexing #4763" begin

test/testhelpers/arrayindexingtypes.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ end
5151

5252
Base.size(A::TSlow) = A.dims
5353
Base.similar(A::TSlow, ::Type{T}, dims::Dims) where {T} = TSlow(T, dims)
54+
Base.similar(::Type{TSlow{T,N}}, dims::Dims) where {T,N} = TSlow(T, dims)
55+
5456
Base.IndexStyle(::Type{A}) where {A<:TSlow} = IndexCartesian()
5557
Base.getindex(A::TSlow{T,N}, i::Vararg{Int,N}) where {T,N} = get(A.data, i, zero(T))
5658
Base.setindex!(A::TSlow{T,N}, v, i::Vararg{Int,N}) where {T,N} = (A.data[i] = v)

0 commit comments

Comments
 (0)