Skip to content

Instantly share code, notes, and snippets.

@quinnj
Created August 15, 2017 19:12
Show Gist options
  • Save quinnj/fa95879c1ff047def57f9a10b26c4a8f to your computer and use it in GitHub Desktop.
Save quinnj/fa95879c1ff047def57f9a10b26c4a8f to your computer and use it in GitHub Desktop.
"""
Base.selectorbytes(A::Array{T, N}) -> Array{UInt8, N}
For an Array with `isbits` Union elements, return the "selector bytes" that indicate the
index of the type for each array element, i.e. the type of `A[1]` is
`Base.uniontypes(eltype(A))[Base.selectorbytes(A)[1] + 1]`.
**NOTE**: The actual array selector bytes are returned, meaning if individual elements
are modified, the original array will reflect those changes. Setting selector bytes to
invalid or out-of-bounds type indexes may corrupt the original array.
"""
function selectorbytes(a::Array{T, N}) where {T, N}
isbitsunion(T) || return UInt8[]
return unsafe_wrap(Array{UInt8, N}, convert(Ptr{UInt8}, pointer(a)) + length(a) * elsize(a), size(a))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment