Skip to content

Commit

Permalink
Merge pull request #24017 from pabloferz/pz/anyhvcat
Browse files Browse the repository at this point in the history
Avoid row promotion in heterogeneous arrays
  • Loading branch information
pabloferz authored Nov 14, 2017
2 parents 3b843cf + fe690a4 commit 0b2bce6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
13 changes: 1 addition & 12 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,7 @@ function hvcat_fill(a::Array, xs::Tuple)
end

hvcat(rows::Tuple{Vararg{Int}}, xs::Number...) = typed_hvcat(promote_typeof(xs...), rows, xs...)
hvcat(rows::Tuple{Vararg{Int}}, xs...) = typed_hvcat(promote_eltypeof(xs...), rows, xs...)

function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, xs::Number...) where T
nr = length(rows)
Expand All @@ -1509,18 +1510,6 @@ function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, xs::Number...) where T
hvcat_fill(Array{T,2}(nr, nc), xs)
end

# fallback definition of hvcat in terms of hcat and vcat
function hvcat(rows::Tuple{Vararg{Int}}, as...)
nbr = length(rows) # number of block rows
rs = Array{Any,1}(nbr)
a = 1
for i = 1:nbr
rs[i] = hcat(as[a:a-1+rows[i]]...)
a += rows[i]
end
vcat(rs...)
end

function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, as...) where T
nbr = length(rows) # number of block rows
rs = Array{Any,1}(nbr)
Expand Down
8 changes: 8 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,14 @@ end
@test X32[i:(i+1), j:(j+2)] == X
end
end
@testset "fallback hvcat" begin
#Issue #23994
A23994 = [1 "two"; 3im 4.0; 5 6//1]
@test A23994[2] isa Complex{Int}
@test A23994[3] isa Int
@test A23994[5] isa Float64
@test A23994[6] isa Rational{Int}
end
@testset "end" begin
X = [ i+2j for i=1:5, j=1:5 ]
@test X[end,end] == 15
Expand Down

0 comments on commit 0b2bce6

Please sign in to comment.