-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
After this, ```julia julia> S = StructArray{Complex{Int}}(([1,2], [3,4])) 2-element StructArray(::Vector{Int64}, ::Vector{Int64}) with eltype Complex{Int64}: 1 + 3im 2 + 4im julia> empty(S) 0-element StructArray(::Vector{Int64}, ::Vector{Int64}) with eltype Complex{Int64} ``` instead of ```julia julia> empty(S) Complex{Int64}[] ``` This behavior matches the docstring now: "Create an empty vector similar to `v`". Often, `similar` will fall back to creating a `Vector`, so the current behavior will be preserved. ~I'm unsure about what test to add for this, so suggestions are welcome.~ Test added for a `StructArray` through a new test helper
- Loading branch information
Showing
3 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |