Skip to content

Commit

Permalink
Merge pull request #15199 from mbauman/mb/scalarsubindexing
Browse files Browse the repository at this point in the history
SubArray: Fix scalar indexing with non-Int values
  • Loading branch information
andreasnoack committed Feb 24, 2016
2 parents b252d61 + 3dd6d69 commit 2f5975c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
20 changes: 10 additions & 10 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,43 +143,43 @@ reindex(V, idxs::Tuple{DroppedScalar, Any, Vararg{Any}}, subidxs::Tuple{Any, Any

# In general, we simply re-index the parent indices by the provided ones
getindex(V::SubArray) = (@_propagate_inbounds_meta; getindex(V, 1))
function getindex(V::SubArray, I::Int...)
function getindex(V::SubArray, I::Real...)
@_inline_meta
@boundscheck checkbounds(V, I...)
@inbounds r = V.parent[reindex(V, V.indexes, I)...]
@inbounds r = V.parent[reindex(V, V.indexes, to_indexes(I...))...]
r
end

typealias FastSubArray{T,N,P,I} SubArray{T,N,P,I,true}
getindex(V::FastSubArray) = (@_propagate_inbounds_meta; getindex(V, 1))
function getindex(V::FastSubArray, i::Int)
function getindex(V::FastSubArray, i::Real)
@_inline_meta
@boundscheck checkbounds(V, i)
@inbounds r = V.parent[V.first_index + V.stride1*(i-1)]
@inbounds r = V.parent[V.first_index + V.stride1*(to_index(i)-1)]
r
end
# We can avoid a multiplication if the first parent index is a Colon or UnitRange
typealias FastContiguousSubArray{T,N,P,I<:Tuple{Union{Colon, UnitRange}, Vararg{Any}}} SubArray{T,N,P,I,true}
function getindex(V::FastContiguousSubArray, i::Int)
function getindex(V::FastContiguousSubArray, i::Real)
@_inline_meta
@boundscheck checkbounds(V, i)
@inbounds r = V.parent[V.first_index + i-1]
@inbounds r = V.parent[V.first_index + to_index(i)-1]
r
end
# We need this because the ::ViewIndex... method would otherwise obscure the Base fallback
function getindex(V::FastSubArray, I::Int...)
function getindex(V::FastSubArray, I::Real...)
@_inline_meta
@boundscheck checkbounds(V, I...)
@inbounds r = getindex(V, sub2ind(size(V), I...))
@inbounds r = getindex(V, sub2ind(size(V), to_indexes(I...)...))
r
end
getindex{T,N}(V::SubArray{T,N}, I::ViewIndex...) = (@_propagate_inbounds_meta; copy(slice(V, I...)))

setindex!(V::SubArray, x) = (@_propagate_inbounds_meta; setindex!(V, x, 1))
function setindex!{T,N}(V::SubArray{T,N}, x, I::Int...)
function setindex!{T,N}(V::SubArray{T,N}, x, I::Real...)
@_inline_meta
@boundscheck checkbounds(V, I...)
@inbounds V.parent[reindex(V, V.indexes, I)...] = x
@inbounds V.parent[reindex(V, V.indexes, to_indexes(I...))...] = x
V
end
# Nonscalar setindex! falls back to the defaults
Expand Down
7 changes: 7 additions & 0 deletions test/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,10 @@ let A = reshape(1:4, 2, 2)
@test parent(B) === A
@test parent(sub(B, 0x1, :)) === parent(slice(B, 0x1, :)) === A
end

# issue #15168
let A = rand(10), sA = sub(copy(A), :)
@test sA[Int16(1)] === sA[Int32(1)] === sA[Int64(1)] === A[1]
permute!(sA, collect(Int16, 1:10))
@test A == sA
end

0 comments on commit 2f5975c

Please sign in to comment.