Skip to content

Commit 2f1a958

Browse files
authored
hvncat: enable concatenations to return an array of the same kind (#41194)
1 parent 0ff49d0 commit 2f1a958

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

base/abstractarray.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,8 +1644,12 @@ cat_ndims(a::AbstractArray) = ndims(a)
16441644
cat_indices(A, d) = OneTo(1)
16451645
cat_indices(A::AbstractArray, d) = axes(A, d)
16461646

1647-
cat_similar(A, ::Type{T}, shape) where T = Array{T}(undef, shape)
1648-
cat_similar(A::AbstractArray, ::Type{T}, shape) where T = similar(A, T, shape)
1647+
cat_similar(A, ::Type{T}, shape::Tuple) where T = Array{T}(undef, shape)
1648+
cat_similar(A, ::Type{T}, shape::Vector) where T = Array{T}(undef, shape...)
1649+
cat_similar(A::Array, ::Type{T}, shape::Tuple) where T = Array{T}(undef, shape)
1650+
cat_similar(A::Array, ::Type{T}, shape::Vector) where T = Array{T}(undef, shape...)
1651+
cat_similar(A::AbstractArray, T::Type, shape::Tuple) = similar(A, T, shape)
1652+
cat_similar(A::AbstractArray, T::Type, shape::Vector) = similar(A, T, shape...)
16491653

16501654
# These are for backwards compatibility (even though internal)
16511655
cat_shape(dims, shape::Tuple{Vararg{Int}}) = shape
@@ -2204,7 +2208,7 @@ function _typed_hvncat(::Type{T}, ::Val{N}, as::AbstractArray...) where {T, N}
22042208
end
22052209
end
22062210

2207-
A = Array{T, nd}(undef, ntuple(d -> cat_size(as[1], d), N - 1)..., Ndim, ntuple(x -> 1, nd - N)...)
2211+
A = cat_similar(as[1], T, (ntuple(d -> size(as[1], d), N - 1)..., Ndim, ntuple(x -> 1, nd - N)...))
22082212
k = 1
22092213
for a as
22102214
for i eachindex(a)
@@ -2300,7 +2304,7 @@ function _typed_hvncat(::Type{T}, dims::Tuple{Vararg{Int, N}}, row_first::Bool,
23002304
len == outlen || ArgumentError("too many elements in arguments; expected $(outlen), got $(len)") |> throw
23012305

23022306
# copy into final array
2303-
A = Array{T, nd}(undef, outdims...)
2307+
A = cat_similar(as[1], T, outdims)
23042308
# @assert all(==(0), currentdims)
23052309
outdims .= 0
23062310
hvncat_fill!(A, currentdims, outdims, d1, d2, as)
@@ -2361,12 +2365,12 @@ function _typed_hvncat(::Type{T}, shape::Tuple{Vararg{Tuple, N}}, row_first::Boo
23612365
# @assert all(==(0), blockcounts)
23622366

23632367
# copy into final array
2364-
A = Array{T, nd}(undef, outdims...)
2368+
A = cat_similar(as[1], T, outdims)
23652369
hvncat_fill!(A, currentdims, blockcounts, d1, d2, as)
23662370
return A
23672371
end
23682372

2369-
function hvncat_fill!(A::Array{T, N}, scratch1::Vector{Int}, scratch2::Vector{Int}, d1::Int, d2::Int, as::Tuple{Vararg}) where {T, N}
2373+
function hvncat_fill!(A::AbstractArray{T, N}, scratch1::Vector{Int}, scratch2::Vector{Int}, d1::Int, d2::Int, as::Tuple{Vararg}) where {T, N}
23702374
outdims = size(A)
23712375
offsets = scratch1
23722376
inneroffsets = scratch2

test/bitarray.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,3 +1704,10 @@ end
17041704
@check_bit_operation all!(falses(100), trues(100, 100))
17051705
@check_bit_operation all!(falses(1000), trues(1000, 100))
17061706
end
1707+
1708+
@testset "multidimensional concatenation returns BitArrays" begin
1709+
a = BitVector(ones(5))
1710+
@test typeof([a ;;; a]) <: BitArray
1711+
@test typeof([a a ;;; a a]) <: BitArray
1712+
@test typeof([a a ;;; [a a]]) <: BitArray
1713+
end

0 commit comments

Comments
 (0)