Skip to content

Commit

Permalink
ReinterpretArray: use IndexLinear() for IndexLinear parents and no el…
Browse files Browse the repository at this point in the history
…type size changes
  • Loading branch information
timholy committed Jul 2, 2018
1 parent 7cf81b9 commit e83f4b6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
19 changes: 19 additions & 0 deletions base/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ struct ReinterpretArray{T,N,S,A<:AbstractArray{S, N}} <: AbstractArray{T, N}
end
end

IndexStyle(a::ReinterpretArray) = _indexstyle(IndexStyle(a.parent), a)
_indexstyle(::IndexCartesian, ::ReinterpretArray) = IndexCartesian()
function _indexstyle(::IndexLinear, ::ReinterpretArray{T,N,S}) where {T,N,S}
return sizeof(T) == sizeof(S) ? IndexLinear() : IndexCartesian()
end

parent(a::ReinterpretArray) = a.parent
dataids(a::ReinterpretArray) = dataids(a.parent)

Expand Down Expand Up @@ -81,6 +87,13 @@ unsafe_convert(::Type{Ptr{T}}, a::ReinterpretArray{T,N,S} where N) where {T,S} =
end
end

@inline @propagate_inbounds function _getindex(::IndexLinear, a::ReinterpretArray{T,N,S}, i::Int) where {T,N,S}
@assert sizeof(T) == sizeof(S)
return reinterpret(T, a.parent[i])
end
error_if_canonical_getindex(::IndexLinear, A::ReinterpretArray, ::Int) = nothing


@inline @propagate_inbounds setindex!(a::ReinterpretArray{T,0,S} where T, v) where {S} = (a.parent[] = reinterpret(S, v))
@inline @propagate_inbounds setindex!(a::ReinterpretArray, v) = (a[1] = v)

Expand Down Expand Up @@ -136,3 +149,9 @@ end
end
return a
end

@inline @propagate_inbounds function _setindex!(::IndexLinear, a::ReinterpretArray{T,N,S}, v, i::Int) where {T,N,S}
@assert sizeof(T) == sizeof(S)
return setindex!(a.parent, reinterpret(S, convert(T, v)::T), i)
end
error_if_canonical_setindex(::IndexLinear, A::ReinterpretArray, ::Int) = nothing
9 changes: 9 additions & 0 deletions test/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,12 @@ let A = collect(reshape(1:20, 5, 4))
@test view(R, :, :) isa StridedArray
@test reshape(R, :) isa StridedArray
end

# IndexStyle
a = fill(1.0, 5, 3)
r = reinterpret(Int64, a)
@test @inferred(IndexStyle(r)) == IndexLinear()
r = reinterpret(Int32, a)
@test @inferred(IndexStyle(r)) == IndexCartesian()
r = reinterpret(Int64, view(a, 1:2:5, :))
@test @inferred(IndexStyle(r)) == IndexCartesian()

0 comments on commit e83f4b6

Please sign in to comment.