From a7b2e390b74877fdc5e2444d7be28807501f1e46 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Mon, 23 Feb 2015 08:44:20 -0600 Subject: [PATCH] Check bounds upon construction of SubArrays. Fixes #4044, fixes #9924 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. --- base/subarray.jl | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/base/subarray.jl b/base/subarray.jl index a3417a1191b088..45b163770ee990 100644 --- a/base/subarray.jl +++ b/base/subarray.jl @@ -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) @@ -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) @@ -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) @@ -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