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
13 changes: 12 additions & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2567,16 +2567,23 @@ function _typed_hvncat_dims(::Type{T}, dims::NTuple{N, Int}, row_first::Bool, as
end

# discover number of rows or columns
# d1 dimension is increased by 1 to appropriately handle 0-length arrays
for i ∈ 1:dims[d1]
outdims[d1] += cat_size(as[i], d1)
end

# adjustment to handle 0-length arrays
first_dim_zero = outdims[d1] == 0
if first_dim_zero
outdims[d1] = dims[d1]
end

currentdims = zeros(Int, N)
blockcount = 0
elementcount = 0
for i ∈ eachindex(as)
elementcount += cat_length(as[i])
currentdims[d1] += cat_size(as[i], d1)
currentdims[d1] += first_dim_zero ? 1 : cat_size(as[i], d1)
if currentdims[d1] == outdims[d1]
currentdims[d1] = 0
for d ∈ (d2, 3:N...)
Expand Down Expand Up @@ -2604,6 +2611,10 @@ function _typed_hvncat_dims(::Type{T}, dims::NTuple{N, Int}, row_first::Bool, as
throw(DimensionMismatch("argument $i has too many elements along axis $d1"))
end
end
# restore 0-length adjustment
if first_dim_zero
outdims[d1] = 0
end

outlen = prod(outdims)
elementcount == outlen ||
Expand Down
6 changes: 6 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,9 @@ function test_cat(::Type{TestAbstractArray})
r = rand(Float32, 56, 56, 64, 1);
f(r) = cat(r, r, dims=(3,))
@inferred f(r);

#58866 - ensure proper dimension calculation for 0-dimension elements
@test [zeros(1, 0) zeros(1,0); zeros(0,0) zeros(0, 0)] == Matrix{Float64}(undef, 1, 0)
end

function test_ind2sub(::Type{TestAbstractArray})
Expand Down Expand Up @@ -1743,6 +1746,9 @@ using Base: typed_hvncat
@test ["A";;"B";;"C";;"D"] == ["A" "B" "C" "D"]
@test ["A";"B";;"C";"D"] == ["A" "C"; "B" "D"]
@test [["A";"B"];;"C";"D"] == ["A" "C"; "B" "D"]

#58866 - ensure proper dimension calculation for 0-dimension elements
@test [zeros(1, 0) zeros(1,0);;; zeros(0,0) zeros(0, 0)] == Array{Float64, 3}(undef, 1, 0, 0)
end

@testset "stack" begin
Expand Down