Ai
21 Star 49 Fork 0

Gitee 极速下载/julia-language

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/JuliaLang/julia
克隆/下载
StructArrays.jl 1.37 KB
一键复制 编辑 原始数据 按行查看 历史
Jishnu Bhattacharya 提交于 2024-01-14 10:23 +08:00 . Use similar in empty (#49958)
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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/mirrors/julia-language.git
git@gitee.com:mirrors/julia-language.git
mirrors
julia-language
julia-language
master

搜索帮助