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

Use similar in empty #49958

Merged
merged 4 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 4 additions & 3 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -888,10 +888,11 @@ julia> empty([1.0, 2.0, 3.0], String)
String[]
```
"""
empty(a::AbstractVector{T}, ::Type{U}=T) where {T,U} = Vector{U}()
empty(a::AbstractVector{T}, ::Type{U}=T) where {T,U} = similar(a, U, 0)

# like empty, but should return a mutable collection, a Vector by default
emptymutable(a::AbstractVector{T}, ::Type{U}=T) where {T,U} = Vector{U}()
# like empty, but should return a mutable collection
emptymutable(a::AbstractVector{T}, ::Type{U}=T) where {T,U} = similar(a, U, 0)
jishnub marked this conversation as resolved.
Show resolved Hide resolved
# return a Vector by default for non-array types
emptymutable(itr, ::Type{U}) where {U} = Vector{U}()

"""
Expand Down
13 changes: 13 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ using Random, LinearAlgebra
isdefined(Main, :InfiniteArrays) || @eval Main include("testhelpers/InfiniteArrays.jl")
using .Main.InfiniteArrays

isdefined(Main, :StructArrays) || @eval Main include("testhelpers/StructArrays.jl")
using .Main.StructArrays

A = rand(5,4,3)
@testset "Bounds checking" begin
@test checkbounds(Bool, A, 1, 1, 1) == true
Expand Down Expand Up @@ -999,6 +1002,16 @@ end
@test isempty(v)
@test isempty(v2::Vector{Int})
@test isempty(v3::Vector{Float64})

S = StructArrays.StructArray{Complex{Int}}((v, v))
for T in (Complex{Int}, ComplexF64)
S0 = empty(S, T)
@test S0 isa StructArrays.StructArray{T}
@test length(S0) == 0
end
S0 = empty(S, String)
@test S0 isa Vector{String}
@test length(S0) == 0
end

@testset "CartesianIndices" begin
Expand Down
39 changes: 39 additions & 0 deletions test/testhelpers/StructArrays.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module StructArrays

struct StructArray{T,N,C <: Tuple{Vararg{AbstractArray{<:Any,N}}}} <: AbstractArray{T,N}
components :: C

function StructArray{T,N,C}(components::C) where {T,N,C}
fieldcount(T) == length(components) || throw(ArgumentError("number of components incompatible with eltype"))
allequal(axes.(components)) || throw(ArgumentError("component arrays must have the same axes"))
new{T,N,C}(components)
end
end

function StructArray{T}(components::Tuple{Vararg{AbstractArray{<:Any,N}}}) where {T,N}
StructArray{T,N,typeof(components)}(components)
end

Base.size(S::StructArray) = size(S.components[1])
Base.axes(S::StructArray) = axes(S.components[1])
function Base.getindex(S::StructArray{T,N}, inds::Vararg{Int,N}) where {T,N}
vals = map(x -> x[inds...], S.components)
T(vals...)
end
function Base.setindex!(S::StructArray{T,N}, val, inds::Vararg{Int,N}) where {T,N}
vals = getfield.(Ref(convert(T, val)), fieldnames(T))
for (A,v) in zip(S.components, vals)
A[inds...] = v
end
S
end

isnonemptystructtype(::Type{T}) where {T} = isstructtype(T) && fieldcount(T) != 0

function Base.similar(S::StructArray, ::Type{T}, dims::Tuple{Int, Vararg{Int}}) where {T}
isnonemptystructtype(T) || return similar(S.components[1], T, dims)
arrs = similar.(S.components, fieldtypes(T), Ref(dims))
StructArray{T}(arrs)
end

end