代码拉取完成,页面将自动刷新
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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。