Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hvncat: enable concatenations to return an array of the same kind #41194

Merged
merged 5 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1638,8 +1638,15 @@ cat_size(A::AbstractArray, d) = size(A, d)
cat_indices(A, d) = OneTo(1)
cat_indices(A::AbstractArray, d) = axes(A, d)

cat_similar(A, ::Type{T}, shape) where T = Array{T}(undef, shape)
cat_similar(A::AbstractArray, ::Type{T}, shape) where T = similar(A, T, shape)
cat_similar(A, ::Type{T}, shape::Tuple) where T = Array{T}(undef, shape)
cat_similar(A, ::Type{T}, shape::Vector) where T = Array{T}(undef, shape...)
cat_similar(A, ::Type{T}, shape::Int...) where T = Array{T}(undef, shape...)
cat_similar(A::Array, ::Type{T}, shape::Tuple) where T = Array{T}(undef, shape)
cat_similar(A::Array, ::Type{T}, shape::Vector) where T = Array{T}(undef, shape...)
cat_similar(A::Array, ::Type{T}, shape::Int...) where T = Array{T}(undef, shape...)
cat_similar(A::AbstractArray, ::Type{T}, shape::Tuple) where T = similar(A, T, shape)
cat_similar(A::AbstractArray, ::Type{T}, shape::Vector) where T = similar(A, T, shape...)
cat_similar(A::AbstractArray, ::Type{T}, shape::Int...) where T = similar(A, T, shape...)

# These are for backwards compatibility (even though internal)
cat_shape(dims, shape::Tuple{Vararg{Int}}) = shape
Expand Down Expand Up @@ -2177,7 +2184,7 @@ function _typed_hvncat(::Type{T}, ::Val{N}, as::AbstractArray...) where {T, N}
end
end

A = Array{T, nd}(undef, ntuple(d -> cat_size(as[1], d), N - 1)..., Ndim, ntuple(x -> 1, nd - N)...)
A = cat_similar(as[1], T, ntuple(d -> size(as[1], d), N - 1)..., Ndim, ntuple(x -> 1, nd - N)...)
k = 1
for a ∈ as
for i ∈ eachindex(a)
Expand Down Expand Up @@ -2276,7 +2283,7 @@ function _typed_hvncat(::Type{T}, dims::Tuple{Vararg{Int, N}}, row_first::Bool,
len == outlen || ArgumentError("too many elements in arguments; expected $(outlen), got $(len)") |> throw

# copy into final array
A = Array{T, nd}(undef, outdims...)
A = cat_similar(as[1], T, outdims)
BioTurboNick marked this conversation as resolved.
Show resolved Hide resolved
# @assert all(==(0), currentdims)
outdims .= 0
hvncat_fill!(A, currentdims, outdims, d1, d2, as)
Expand Down Expand Up @@ -2335,12 +2342,12 @@ function _typed_hvncat(::Type{T}, shape::Tuple{Vararg{Tuple, N}}, row_first::Boo
# @assert all(==(0), blockcounts)

# copy into final array
A = Array{T, nd}(undef, outdims...)
A = cat_similar(as[1], T, outdims)
hvncat_fill!(A, currentdims, blockcounts, d1, d2, as)
return A
end

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::AbstractArray{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
Expand Down
7 changes: 7 additions & 0 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1704,3 +1704,10 @@ end
@check_bit_operation all!(falses(100), trues(100, 100))
@check_bit_operation all!(falses(1000), trues(1000, 100))
end

@testset "multidimensional concatenation returns BitArrays" begin
a = BitVector(ones(5))
typeof([a ;;; a]) <: BitArray
typeof([a a ;;; a a]) <: BitArray
typeof([a a ;;; [a a]]) <: BitArray
end