Skip to content

Commit

Permalink
hvncat-related restore
Browse files Browse the repository at this point in the history
  • Loading branch information
BioTurboNick committed Jun 5, 2021
1 parent 3e64cee commit b09fd6f
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2134,9 +2134,9 @@ function hvncat_fill!(A::Array, row_first::Bool, xs::Tuple)
if row_first
nr, nc = size(A, 1), size(A, 2)
nrc = nr * nc
@inbounds na = prod(size(A)[3:end])
na = prod(size(A)[3:end])
k = 1
@inbounds for d 1:na
for d 1:na
dd = nrc * (d - 1)
for i 1:nr
Ai = dd + i
Expand All @@ -2148,7 +2148,7 @@ function hvncat_fill!(A::Array, row_first::Bool, xs::Tuple)
end
end
else
@inbounds for k eachindex(xs)
for k eachindex(xs)
A[k] = xs[k]
end
end
Expand All @@ -2160,24 +2160,24 @@ _typed_hvncat(T::Type, ::Val{N}, xs::Number...) where N = _typed_hvncat(T, (ntup
function _typed_hvncat(::Type{T}, ::Val{N}, as::AbstractArray...) where {T, N}
# optimization for arrays that can be concatenated by copying them linearly into the destination
# conditions: the elements must all have 1- or 0-length dimensions above N
@inbounds for a as
for a as
ndims(a) <= N || all(x -> size(a, x) == 1, (N + 1):ndims(a)) ||
return _typed_hvncat(T, (ntuple(x -> 1, N - 1)..., length(as)), false, as...)
end

nd = max(N, ndims(as[1]))

Ndim = 0
@inbounds for i 1:lastindex(as)
for i 1:lastindex(as)
Ndim += cat_size(as[i], N)
for d 1:N - 1
cat_size(as[1], d) == cat_size(as[i], d) || throw(ArgumentError("mismatched size along axis $d in element $i"))
end
end

@inbounds A = Array{T, nd}(undef, ntuple(d -> cat_size(as[1], d), N - 1)..., Ndim, ntuple(x -> 1, nd - N)...)
A = Array{T, nd}(undef, ntuple(d -> cat_size(as[1], d), N - 1)..., Ndim, ntuple(x -> 1, nd - N)...)
k = 1
@inbounds for a as
for a as
for i eachindex(a)
A[k] = a[i]
k += 1
Expand All @@ -2191,19 +2191,19 @@ function _typed_hvncat(::Type{T}, ::Val{N}, as...) where {T, N}
# into the destination
nd = N
Ndim = 0
@inbounds for a as
for a as
if a isa AbstractArray
cat_size(a, N) == length(a) ||
throw(ArgumentError("all dimensions of elements other than $N must be of length 1"))
nd = max(nd, ndims(a))
nd = max(nd, cat_ndims(a))
end
Ndim += cat_size(a, N)
end

@inbounds A = Array{T, nd}(undef, ntuple(x -> 1, N - 1)..., Ndim, ntuple(x -> 1, nd - N)...)
A = Array{T, nd}(undef, ntuple(x -> 1, N - 1)..., Ndim, ntuple(x -> 1, nd - N)...)

k = 1
@inbounds for a as
for a as
if a isa AbstractArray
lena = length(a)
copyto!(A, k, a, 1, lena)
Expand All @@ -2221,17 +2221,17 @@ function _typed_hvncat(::Type{T}, dims::Tuple{Vararg{Int, N}}, row_first::Bool,
d2 = row_first ? 1 : 2

# discover dimensions
@inbounds nd = max(N, ndims(as[1]))
nd = max(N, cat_ndims(as[1]))
outdims = zeros(Int, nd)

# discover number of rows or columns
@inbounds for i 1:dims[d1]
for i 1:dims[d1]
outdims[d1] += cat_size(as[i], d1)
end

currentdims = zeros(Int, nd)
blockcount = 0
@inbounds for i eachindex(as)
for i eachindex(as)
currentdims[d1] += cat_size(as[i], d1)
if currentdims[d1] == outdims[d1]
currentdims[d1] = 0
Expand Down Expand Up @@ -2282,18 +2282,18 @@ function _typed_hvncat(::Type{T}, shape::Tuple{Vararg{Tuple, N}}, row_first::Boo
d1 = row_first ? 2 : 1
d2 = row_first ? 1 : 2
shape = collect(shape) # saves allocations later
@inbounds shapelength = shape[end][1]
shapelength = shape[end][1]
lengthas = length(as)
shapelength == lengthas || throw(ArgumentError("number of elements does not match shape; expected $(shapelength), got $lengthas)"))

# discover dimensions
@inbounds nd = max(N, ndims(as[1]))
nd = max(N, cat_ndims(as[1]))
outdims = zeros(Int, nd)
currentdims = zeros(Int, nd)
blockcounts = zeros(Int, nd)
shapepos = ones(Int, nd)

@inbounds for i eachindex(as)
for i eachindex(as)
wasstartblock = false
for d 1:N
ad = (d < 3 && row_first) ? (d == 1 ? 2 : 1) : d
Expand Down Expand Up @@ -2323,7 +2323,7 @@ function _typed_hvncat(::Type{T}, shape::Tuple{Vararg{Tuple, N}}, row_first::Boo
end

if row_first
@inbounds outdims[1], outdims[2] = outdims[2], outdims[1]
outdims[1], outdims[2] = outdims[2], outdims[1]
end

# @assert all(==(0), currentdims)
Expand All @@ -2335,11 +2335,11 @@ function _typed_hvncat(::Type{T}, shape::Tuple{Vararg{Tuple, N}}, row_first::Boo
return A
end

@inline function hvncat_fill!(A::Array{T, N}, scratch1::Vector{Int}, scratch2::Vector{Int}, d1::Int, d2::Int, as::Tuple{Vararg}) where {T, N}
function hvncat_fill!(A::Array{T, N}, scratch1::Vector{Int}, scratch2::Vector{Int}, d1::Int, d2::Int, as::Tuple{Vararg}) where {T, N}
outdims = size(A)
offsets = scratch1
inneroffsets = scratch2
@inbounds for a as
for a as
if isa(a, AbstractArray)
for ai a
Ai = hvncat_calcindex(offsets, inneroffsets, outdims, N)
Expand Down Expand Up @@ -2377,8 +2377,10 @@ end
Ai
end

cat_length(a::AbstractArray) = length(a)
cat_ndims(a) = 0
cat_ndims(a::AbstractArray) = ndims(a)
cat_length(::Any) = 1
cat_length(a::AbstractArray) = length(a)

## Reductions and accumulates ##

Expand Down

0 comments on commit b09fd6f

Please sign in to comment.