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

Implement three-argument similar methods #94

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
14 changes: 13 additions & 1 deletion src/structarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,22 @@ end

Base.similar(s::StructArray, sz::Base.DimOrInd...) = similar(s, Base.to_shape(sz))
Base.similar(s::StructArray) = similar(s, Base.to_shape(axes(s)))
function Base.similar(s::StructArray{T}, sz::Tuple) where {T}
function Base.similar(s::StructArray{T,N,C}, ::Type{T}, sz::NTuple{M,Int64}) where {T, N, M, C<:Union{Tuple, NamedTuple}}
StructArray{T}(map(typ -> similar(typ, sz), fieldarrays(s)))
end

function Base.similar(s::StructArray{T,N,C}, S::Type, sz::NTuple{M,Int64}) where {T, N, M, C<:Union{Tuple, NamedTuple}}
# If not specified, we don't really know what kind of array to use for each
# interior type, so we just pick the first one arbitrarily. If users need
# something else, they need to be more specific.
f1 = fieldarrays(s)[1]
if isstructtype(S)
return StructArrays.buildfromschema(typ -> similar(f1, typ, sz), S)
else
return similar(f1, S, sz)
end
end

"""
`fieldarrays(s::StructArray)`

Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,9 @@ end
str = String(take!(io))
@test str == "StructArray(::Array{Int64,1}, ::Array{Int64,1})"
end

@testset "OffsetArray zero" begin
s = StructArray{ComplexF64}((rand(2), rand(2)))
soff = OffsetArray(s, 0:1)
@test isa(zero(soff).parent, StructArray)
end