Skip to content

Commit e5b9ba7

Browse files
committed
Rebased changes
1 parent 16f433b commit e5b9ba7

File tree

1 file changed

+34
-38
lines changed

1 file changed

+34
-38
lines changed

base/abstractarray.jl

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,10 +1923,30 @@ julia> hvcat((2,2,2), a,b,c,d,e,f)
19231923
If the first argument is a single integer `n`, then all block rows are assumed to have `n`
19241924
block columns.
19251925
"""
1926-
hvcat(rows::Tuple{Vararg{Int}}, xs::AbstractVecOrMat...) = typed_hvcat(promote_eltype(xs...), rows, xs...)
1927-
hvcat(rows::Tuple{Vararg{Int}}, xs::AbstractVecOrMat{T}...) where {T} = typed_hvcat(T, rows, xs...)
1928-
1929-
function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, as::AbstractVecOrMat...) where T
1926+
hvcat(::Tuple{}) = []
1927+
hvcat(::Tuple{}, xs...) = []
1928+
hvcat(::Tuple{Vararg{Int, 1}}, xs...) = vcat(xs...) # methods assume 2+ dimensions
1929+
hvcat(rows::Tuple{Vararg{Int}}, xs...) = _hvcat(rows, xs...)
1930+
1931+
_hvcat(::Tuple{Vararg{Int}}) = []
1932+
_hvcat(rows::Tuple{Vararg{Int}}, xs...) = _typed_hvncat(promote_eltypeof(xs...), (length(rows), rows[1]), true, xs...)
1933+
_hvcat(rows::Tuple{Vararg{Int}}, xs::T...) where T<:Number = _typed_hvcat(T, rows, xs...)
1934+
_hvcat(rows::Tuple{Vararg{Int}}, xs::Number...) = _typed_hvcat(promote_typeof(xs...), rows, xs...)
1935+
_hvcat(rows::Tuple{Vararg{Int}}, xs::AbstractVecOrMat...) = _typed_hvcat(promote_eltype(xs...), rows, xs...)
1936+
_hvcat(rows::Tuple{Vararg{Int}}, xs::AbstractVecOrMat{T}...) where T = _typed_hvcat(T, rows, xs...)
1937+
_hvcat(rows::Tuple{Vararg{Int}}, xs::AbstractArray...) = _typed_hvncat(promote_eltype(xs...), (length(rows), rows[1]), true, xs...)
1938+
_hvcat(rows::Tuple{Vararg{Int}}, xs::AbstractArray{T}...) where T = _typed_hvncat(T, (length(rows), rows[1]), true, xs...)
1939+
1940+
typed_hvcat(::Type{T}, ::Tuple{}) where T = Vector{T}()
1941+
typed_hvcat(::Type{T}, ::Tuple{}, xs...) where T = Vector{T}()
1942+
typed_hvcat(T::Type, ::Tuple{Vararg{Any, 1}}, ::Bool, xs...) = typed_vcat(T, xs...)
1943+
typed_hvcat(T::Type, rows::Tuple{Vararg{Int}}, xs...) = _typed_hvcat(T, rows, xs...)
1944+
1945+
_typed_hvcat(::Type{T}, ::Tuple{}) where T = Vector{T}()
1946+
_typed_hvcat(::Type{T}, ::Tuple{}, xs...) where T = Vector{T}()
1947+
_typed_hvcat(::Type{T}, ::Tuple{}, xs::Number...) where T = Vector{T}()
1948+
1949+
function _typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, as::AbstractVecOrMat...) where T
19301950
nbr = length(rows) # number of block rows
19311951

19321952
nc = 0
@@ -1969,28 +1989,15 @@ function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, as::AbstractVecOrMat..
19691989
out
19701990
end
19711991

1972-
hvcat(rows::Tuple{Vararg{Int}}) = []
1973-
typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}) where {T} = Vector{T}()
1974-
1975-
function hvcat(rows::Tuple{Vararg{Int}}, xs::T...) where T<:Number
1992+
function _typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, xs::Number...) where T
19761993
nr = length(rows)
19771994
nc = rows[1]
1978-
1979-
a = Matrix{T}(undef, nr, nc)
1980-
if length(a) != length(xs)
1981-
throw(ArgumentError("argument count does not match specified shape (expected $(length(a)), got $(length(xs)))"))
1982-
end
1983-
k = 1
1984-
@inbounds for i=1:nr
1995+
for i = 2:nr
19851996
if nc != rows[i]
19861997
throw(ArgumentError("row $(i) has mismatched number of columns (expected $nc, got $(rows[i]))"))
19871998
end
1988-
for j=1:nc
1989-
a[i,j] = xs[k]
1990-
k += 1
1991-
end
19921999
end
1993-
a
2000+
hvcat_fill!(Matrix{T}(undef, nr, nc), xs)
19942001
end
19952002

19962003
function hvcat_fill!(a::Array, xs::Tuple)
@@ -2009,21 +2016,11 @@ function hvcat_fill!(a::Array, xs::Tuple)
20092016
a
20102017
end
20112018

2012-
hvcat(rows::Tuple{Vararg{Int}}, xs::Number...) = typed_hvcat(promote_typeof(xs...), rows, xs...)
2013-
hvcat(rows::Tuple{Vararg{Int}}, xs...) = typed_hvcat(promote_eltypeof(xs...), rows, xs...)
2014-
2015-
function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, xs::Number...) where T
2016-
nr = length(rows)
2017-
nc = rows[1]
2018-
for i = 2:nr
2019-
if nc != rows[i]
2020-
throw(ArgumentError("row $(i) has mismatched number of columns (expected $nc, got $(rows[i]))"))
2021-
end
2022-
end
2023-
hvcat_fill!(Matrix{T}(undef, nr, nc), xs)
2024-
end
2019+
_typed_hvcat(T::Type, rows::Tuple{Vararg{Int}}, xs...) = _typed_hvncat(T, (length(rows), rows[1]), true, xs...)
2020+
_typed_hvcat(T::Type, rows::Tuple{Vararg{Int}}, xs::AbstractArray...) = _typed_hvncat(T, (length(rows), rows[1]), true, xs...)
2021+
_typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, xs::AbstractArray{T}...) where T = _typed_hvncat(T, (length(rows), rows[1]), true, xs...)
20252022

2026-
function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, as...) where T
2023+
function _typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, as...) where T
20272024
nbr = length(rows) # number of block rows
20282025
rs = Vector{Any}(undef, nbr)
20292026
a = 1
@@ -2188,9 +2185,6 @@ function _typed_hvncat(::Type{T}, ::Val{N}, as::AbstractArray...) where {T, N}
21882185
return A
21892186
end
21902187

2191-
cat_ndims(a) = 0
2192-
cat_ndims(a::AbstractArray) = ndims(a)
2193-
21942188
function _typed_hvncat(::Type{T}, ::Val{N}, as...) where {T, N}
21952189
# optimization for scalars and 1-length arrays that can be concatenated by copying them linearly
21962190
# into the destination
@@ -2382,8 +2376,10 @@ end
23822376
Ai
23832377
end
23842378

2385-
cat_length(a::AbstractArray) = length(a)
2379+
cat_ndims(a) = 0
2380+
cat_ndims(a::AbstractArray) = ndims(a)
23862381
cat_length(::Any) = 1
2382+
cat_length(a::AbstractArray) = length(a)
23872383

23882384
## Reductions and accumulates ##
23892385

0 commit comments

Comments
 (0)