Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check bounds upon construction of SubArrays. #10296

Merged
merged 2 commits into from
Feb 24, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Check bounds upon construction of SubArrays. Fixes #4044, fixes #9924
In places where one wants to live dangerously, one can call
Base.sub_unsafe/Base.slice_unsafe. So in a sense this gives the
benefits of #4044.
  • Loading branch information
timholy committed Feb 23, 2015
commit dcf3ec8f3c9cab3c0373e439cdd17b9d2ce80d42
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_unsafe(A, I)
end

stagedfunction slice_unsafe{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_unsafe(A, I)
end

stagedfunction sub_unsafe{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_unsafe{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_unsafe{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