@@ -2404,26 +2404,30 @@ function _typed_hvncat(::Type{T}, dims::NTuple{N, Int}, row_first::Bool, xs::Num
2404
2404
end
2405
2405
2406
2406
function hvncat_fill! (A:: Array , row_first:: Bool , xs:: Tuple )
2407
+ nr, nc = size (A, 1 ), size (A, 2 )
2408
+ na = prod (size (A)[3 : end ])
2409
+ len = length (xs)
2410
+ nrc = nr * nc
2411
+ if nrc * na != len
2412
+ throw (ArgumentError (" argument count $(len) does not match specified shape $(size (A)) " ))
2413
+ end
2407
2414
# putting these in separate functions leads to unnecessary allocations
2408
2415
if row_first
2409
- nr, nc = size (A, 1 ), size (A, 2 )
2410
- nrc = nr * nc
2411
- na = prod (size (A)[3 : end ])
2412
2416
k = 1
2413
2417
for d ∈ 1 : na
2414
2418
dd = nrc * (d - 1 )
2415
2419
for i ∈ 1 : nr
2416
2420
Ai = dd + i
2417
2421
for j ∈ 1 : nc
2418
- A[Ai] = xs[k]
2422
+ @inbounds A[Ai] = xs[k]
2419
2423
k += 1
2420
2424
Ai += nr
2421
2425
end
2422
2426
end
2423
2427
end
2424
2428
else
2425
2429
for k ∈ eachindex (xs)
2426
- A[k] = xs[k]
2430
+ @inbounds A[k] = xs[k]
2427
2431
end
2428
2432
end
2429
2433
end
@@ -2609,28 +2613,36 @@ function _typed_hvncat_shape(::Type{T}, shape::NTuple{N, Tuple}, row_first, as::
2609
2613
return A
2610
2614
end
2611
2615
2612
- function hvncat_fill! (A:: AbstractArray{T, N} , scratch1:: Vector{Int} , scratch2:: Vector{Int} , d1:: Int , d2:: Int , as:: Tuple{Vararg} ) where {T, N}
2616
+ function hvncat_fill! (A:: AbstractArray{T, N} , scratch1:: Vector{Int} , scratch2:: Vector{Int} ,
2617
+ d1:: Int , d2:: Int , as:: Tuple ) where {T, N}
2618
+ N > 1 || throw (ArgumentError (" dimensions of the destination array must be at least 2" ))
2619
+ length (scratch1) == length (scratch2) == N ||
2620
+ throw (ArgumentError (" scratch vectors must have as many elements as the destination array has dimensions" ))
2621
+ 0 < d1 < 3 &&
2622
+ 0 < d2 < 3 &&
2623
+ d1 != d2 ||
2624
+ throw (ArgumentError (" d1 and d2 must be either 1 or 2, exclusive." ))
2613
2625
outdims = size (A)
2614
2626
offsets = scratch1
2615
2627
inneroffsets = scratch2
2616
2628
for a ∈ as
2617
2629
if isa (a, AbstractArray)
2618
2630
for ai ∈ a
2619
- Ai = hvncat_calcindex (offsets, inneroffsets, outdims, N)
2631
+ @inbounds Ai = hvncat_calcindex (offsets, inneroffsets, outdims, N)
2620
2632
A[Ai] = ai
2621
2633
2622
- for j ∈ 1 : N
2634
+ @inbounds for j ∈ 1 : N
2623
2635
inneroffsets[j] += 1
2624
2636
inneroffsets[j] < cat_size (a, j) && break
2625
2637
inneroffsets[j] = 0
2626
2638
end
2627
2639
end
2628
2640
else
2629
- Ai = hvncat_calcindex (offsets, inneroffsets, outdims, N)
2641
+ @inbounds Ai = hvncat_calcindex (offsets, inneroffsets, outdims, N)
2630
2642
A[Ai] = a
2631
2643
end
2632
2644
2633
- for j ∈ (d1, d2, 3 : N... )
2645
+ @inbounds for j ∈ (d1, d2, 3 : N... )
2634
2646
offsets[j] += cat_size (a, j)
2635
2647
offsets[j] < outdims[j] && break
2636
2648
offsets[j] = 0
0 commit comments