Skip to content

Commit e677731

Browse files
committed
Support Arrays of Union isbits types
1 parent 163a712 commit e677731

File tree

9 files changed

+528
-152
lines changed

9 files changed

+528
-152
lines changed

base/array.jl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,23 @@ size(a::Array{<:Any,N}) where {N} = (@_inline_meta; ntuple(M -> size(a, M), Val{
8989

9090
asize_from(a::Array, n) = n > ndims(a) ? () : (arraysize(a,n), asize_from(a, n+1)...)
9191

92+
function isbitsunion(U::Union)
93+
for u in Base.uniontypes(U)
94+
isbits(u) || return false
95+
end
96+
return true
97+
end
98+
isbitsunion(T) = false
99+
function bitsunionsize(U::Union)
100+
sz = 0
101+
for u in Base.uniontypes(U)
102+
sz = max(sz, sizeof(u))
103+
end
104+
return sz
105+
end
106+
92107
length(a::Array) = arraylen(a)
93-
elsize(a::Array{T}) where {T} = isbits(T) ? sizeof(T) : sizeof(Ptr)
108+
elsize(a::Array{T}) where {T} = isbits(T) ? sizeof(T) : (isbitsunion(T) ? bitsunionsize(T) : sizeof(Ptr))
94109
sizeof(a::Array) = Core.sizeof(a)
95110

96111
function isassigned(a::Array, i::Int...)
@@ -100,6 +115,12 @@ function isassigned(a::Array, i::Int...)
100115
ccall(:jl_array_isassigned, Cint, (Any, UInt), a, ii) == 1
101116
end
102117

118+
## Union isbits selector bytes
119+
function selectorbytes(a::Array{T, N}) where {T, N}
120+
isbitsunion(T) || return UInt8[]
121+
return unsafe_wrap(Array{UInt8, N}, convert(Ptr{UInt8}, pointer(a)) + length(a) * elsize(T), size(a))
122+
end
123+
103124
## copy ##
104125

105126
function unsafe_copy!(dest::Ptr{T}, src::Ptr{T}, n) where T

0 commit comments

Comments
 (0)