Skip to content

Commit

Permalink
Check bounds upon construction of SubArrays. Fixes #4044, fixes #9924
Browse files Browse the repository at this point in the history
In places where one wants to live dangerously, one can call
Base.sub_unchecked/Base.slice_unchecked. So in a sense this gives the
benefits of #4044.
  • Loading branch information
timholy committed Feb 23, 2015
1 parent 5e2a617 commit a7b2e39
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ parentindexes(a::AbstractArray) = ntuple(ndims(a), i->1:size(a,i))
# Drops singleton dimensions (those indexed with a scalar)
slice(A::AbstractArray, I::ViewIndex...) = _slice(A, I)
slice(A::AbstractArray, I::(ViewIndex...)) = _slice(A, I)
stagedfunction _slice{T,NP,IndTypes}(A::AbstractArray{T,NP}, I::IndTypes)
function _slice(A, I)
checkbounds(A, I...)
slice_unchecked(A, I)
end

stagedfunction slice_unchecked{T,NP,IndTypes}(A::AbstractArray{T,NP}, I::IndTypes)
N = 0
sizeexprs = Array(Any, 0)
for k = 1:length(I)
Expand All @@ -59,7 +64,12 @@ end
# other singletons)
sub(A::AbstractArray, I::ViewIndex...) = _sub(A, I)
sub(A::AbstractArray, I::(ViewIndex...)) = _sub(A, I)
stagedfunction _sub{T,NP,IndTypes}(A::AbstractArray{T,NP}, I::IndTypes)
function _sub(A, I)
checkbounds(A, I...)
sub_unchecked(A, I)
end

stagedfunction sub_unchecked{T,NP,IndTypes}(A::AbstractArray{T,NP}, I::IndTypes)
sizeexprs = Array(Any, 0)
Itypes = Array(Any, 0)
Iexprs = Array(Any, 0)
Expand Down Expand Up @@ -93,7 +103,7 @@ end

# Constructing from another SubArray
# This "pops" the old SubArray and creates a more compact one
stagedfunction _slice{T,NV,PV,IV,PLD,IndTypes}(V::SubArray{T,NV,PV,IV,PLD}, I::IndTypes)
stagedfunction slice_unchecked{T,NV,PV,IV,PLD,IndTypes}(V::SubArray{T,NV,PV,IV,PLD}, I::IndTypes)
N = 0
sizeexprs = Array(Any, 0)
indexexprs = Array(Any, 0)
Expand Down Expand Up @@ -163,7 +173,7 @@ stagedfunction _slice{T,NV,PV,IV,PLD,IndTypes}(V::SubArray{T,NV,PV,IV,PLD}, I::I
end
end

stagedfunction _sub{T,NV,PV,IV,PLD,IndTypes}(V::SubArray{T,NV,PV,IV,PLD}, I::IndTypes)
stagedfunction sub_unchecked{T,NV,PV,IV,PLD,IndTypes}(V::SubArray{T,NV,PV,IV,PLD}, I::IndTypes)
N = length(I)
while N > 0 && I[N] <: Real
N -= 1
Expand Down

0 comments on commit a7b2e39

Please sign in to comment.