Skip to content

Commit 0c04558

Browse files
authored
Perform bounds checking in indexing instead of deferring to the parent array (#130)
This PR improves the error message in case an out-of-bounds indexing operation has been performed.
1 parent 643a447 commit 0c04558

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/OffsetArrays.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,29 +148,32 @@ Base.falses(inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {N} =
148148
# and one obtains the result below.
149149
parentindex(r::IdOffsetRange, i) = i - r.offset
150150

151-
@propagate_inbounds function Base.getindex(A::OffsetArray{T,N}, I::Vararg{Int,N}) where {T,N}
151+
@inline function Base.getindex(A::OffsetArray{T,N}, I::Vararg{Int,N}) where {T,N}
152+
@boundscheck checkbounds(A, I...)
152153
J = map(parentindex, axes(A), I)
153-
return parent(A)[J...]
154+
@inbounds parent(A)[J...]
154155
end
155156

156-
@propagate_inbounds Base.getindex(A::OffsetVector, i::Int) = parent(A)[parentindex(Base.axes1(A), i)]
157+
@inline function Base.getindex(A::OffsetVector, i::Int)
158+
@boundscheck checkbounds(A, i)
159+
@inbounds parent(A)[parentindex(Base.axes1(A), i)]
160+
end
157161
@propagate_inbounds Base.getindex(A::OffsetArray, i::Int) = parent(A)[i]
158162

159-
@propagate_inbounds function Base.setindex!(A::OffsetArray{T,N}, val, I::Vararg{Int,N}) where {T,N}
163+
@inline function Base.setindex!(A::OffsetArray{T,N}, val, I::Vararg{Int,N}) where {T,N}
160164
@boundscheck checkbounds(A, I...)
161165
J = @inbounds map(parentindex, axes(A), I)
162166
@inbounds parent(A)[J...] = val
163167
A
164168
end
165169

166-
@propagate_inbounds function Base.setindex!(A::OffsetVector, val, i::Int)
170+
@inline function Base.setindex!(A::OffsetVector, val, i::Int)
167171
@boundscheck checkbounds(A, i)
168172
@inbounds parent(A)[parentindex(Base.axes1(A), i)] = val
169173
A
170174
end
171175
@propagate_inbounds function Base.setindex!(A::OffsetArray, val, i::Int)
172-
@boundscheck checkbounds(A, i)
173-
@inbounds parent(A)[i] = val
176+
parent(A)[i] = val
174177
A
175178
end
176179

test/runtests.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,14 @@ end
185185
@test @inbounds(A[1,3]) == @inbounds(A[1,3,1]) == @inbounds(A[2]) == @inbounds(S[1,3]) == @inbounds(S[1,3,1]) == @inbounds(S[2]) == 2
186186
@test @inbounds(A[0,4]) == @inbounds(A[0,4,1]) == @inbounds(A[3]) == @inbounds(S[0,4]) == @inbounds(S[0,4,1]) == @inbounds(S[3]) == 3
187187
@test @inbounds(A[1,4]) == @inbounds(A[1,4,1]) == @inbounds(A[4]) == @inbounds(S[1,4]) == @inbounds(S[1,4,1]) == @inbounds(S[4]) == 4
188-
@test_throws BoundsError A[1,1]
189-
@test_throws BoundsError S[1,1]
190-
@test_throws BoundsError A[0,3,2]
191-
@test_throws BoundsError A[0,3,0]
188+
@test_throws BoundsError(A, (1,1)) A[1,1]
189+
@test_throws BoundsError(A, (1,1)) A[1,1] = 4
190+
@test_throws BoundsError(S, (1,1)) S[1,1]
191+
@test_throws BoundsError(S, (1,1)) S[1,1] = 4
192+
@test_throws BoundsError(A, (0,3,2)) A[0,3,2]
193+
@test_throws BoundsError(A, (0,3,2)) A[0,3,2] = 4
194+
@test_throws BoundsError(A, (0,3,0)) A[0,3,0]
195+
@test_throws BoundsError(A, (0,3,0)) A[0,3,0] = 4
192196
Ac = copy(A)
193197
Ac[0,3] = 10
194198
@test Ac[0,3] == 10

0 commit comments

Comments
 (0)