Skip to content

Commit

Permalink
Merge pull request JuliaLang#12334 from JuliaLang/mb/fix12289
Browse files Browse the repository at this point in the history
Fix unsafe SubArray logical indexing
  • Loading branch information
mbauman committed Jul 28, 2015
2 parents ed974b1 + 61ee4e5 commit cd068a1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 8 additions & 6 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ end
# For S4, J[1] corresponds to I[2], because of the slice along
# dimension 1 in S2

@generated function slice_unsafe{T,NP,IndTypes}(A::AbstractArray{T,NP}, J::IndTypes)
slice_unsafe(A::AbstractArray, J) = _slice_unsafe(A, to_indexes(J...))
@generated function _slice_unsafe{T,NP,IndTypes}(A::AbstractArray{T,NP}, J::IndTypes)
N = 0
sizeexprs = Array(Any, 0)
Jp = J.parameters
Expand All @@ -86,14 +87,15 @@ end

# Conventional style (drop trailing singleton dimensions, keep any
# other singletons by converting them to ranges, e.g., 3:3)
sub(A::AbstractArray, I::ViewIndex...) = _sub(A, to_indexes(I...))
sub(A::AbstractArray, I::Tuple{Vararg{ViewIndex}}) = _sub(A, to_indexes(I...))
sub(A::AbstractArray, I::ViewIndex...) = _sub(A, I)
sub(A::AbstractArray, I::Tuple{Vararg{ViewIndex}}) = _sub(A, I)
function _sub(A, I)
checkbounds(A, I...)
sub_unsafe(A, I)
end

@generated function sub_unsafe{T,NP,IndTypes}(A::AbstractArray{T,NP}, J::IndTypes)
sub_unsafe(A::AbstractArray, J) = _sub_unsafe(A, to_indexes(J...))
@generated function _sub_unsafe{T,NP,IndTypes}(A::AbstractArray{T,NP}, J::IndTypes)
sizeexprs = Array(Any, 0)
Itypes = Array(Any, 0)
Iexprs = Array(Any, 0)
Expand Down Expand Up @@ -129,7 +131,7 @@ end

# Constructing from another SubArray
# This "pops" the old SubArray and creates a more compact one
@generated function slice_unsafe{T,NV,PV,IV,PLD,IndTypes}(V::SubArray{T,NV,PV,IV,PLD}, J::IndTypes)
@generated function _slice_unsafe{T,NV,PV,IV,PLD,IndTypes}(V::SubArray{T,NV,PV,IV,PLD}, J::IndTypes)
N = 0
sizeexprs = Array(Any, 0)
indexexprs = Array(Any, 0)
Expand Down Expand Up @@ -220,7 +222,7 @@ end
end
end

@generated function sub_unsafe{T,NV,PV,IV,PLD,IndTypes}(V::SubArray{T,NV,PV,IV,PLD}, J::IndTypes)
@generated function _sub_unsafe{T,NV,PV,IV,PLD,IndTypes}(V::SubArray{T,NV,PV,IV,PLD}, J::IndTypes)
Jp = J.parameters
IVp = IV.parameters
N = length(Jp)
Expand Down
2 changes: 2 additions & 0 deletions test/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,11 @@ sA = sub(A, 1:2:3, 1:3:5, 1:2:8)
# sub logical indexing #4763
A = sub([1:10;], 5:8)
@test A[A.<7] == [5, 6]
@test Base.unsafe_getindex(A, A.<7) == [5, 6]
B = reshape(1:16, 4, 4)
sB = sub(B, 2:3, 2:3)
@test sB[sB.>8] == [10, 11]
@test Base.unsafe_getindex(sB, sB.>8) == [10, 11]

# slice
A = reshape(1:120, 3, 5, 8)
Expand Down

0 comments on commit cd068a1

Please sign in to comment.