Skip to content

Commit 381fbec

Browse files
committed
Separate dispatch for dropping trailing 1s (fixes for non-1 based arrays)
1 parent 3f14659 commit 381fbec

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

base/abstractarray.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -853,10 +853,15 @@ _to_subscript_indices{T}(A::AbstractArray{T,0}, i::Int) = () # TODO: REMOVE FOR
853853
_to_subscript_indices{T}(A::AbstractArray{T,0}, I::Int...) = () # TODO: DEPRECATE FOR #14770
854854
function _to_subscript_indices{T,N}(A::AbstractArray{T,N}, I::Int...) # TODO: DEPRECATE FOR #14770
855855
@_inline_meta
856-
J, _ = IteratorsMD.split(I, Val{N}) # (maybe) drop any trailing indices
857-
sz = _remaining_size(J, size(A)) # compute trailing size (overlapping the final index)
856+
J, Jrem = IteratorsMD.split(I, Val{N})
857+
_to_subscript_indices(A, J, Jrem)
858+
end
859+
function _to_subscript_indices(A::AbstractArray, J::Tuple, Jrem::Tuple{})
860+
@_inline_meta
861+
sz = _remaining_size(J, indices(A)) # compute trailing size (overlapping the final index)
858862
(front(J)..., _unsafe_ind2sub(sz, last(J))...) # (maybe) extend the last index
859863
end
864+
_to_subscript_indices(A, J::Tuple, Jrem::Tuple) = J # already bounds-checked, safe to drop
860865
_to_subscript_indices{T,N}(A::AbstractArray{T,N}, I::Vararg{Int,N}) = I
861866
_remaining_size(::Tuple{Any}, t::Tuple) = t
862867
_remaining_size(h::Tuple, t::Tuple) = (@_inline_meta; _remaining_size(tail(h), tail(t)))

test/offsetarray.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ S = OffsetArray(view(A0, 1:2, 1:2), (-1,2)) # LinearSlow
2222
@test_throws ErrorException size(A, 1)
2323

2424
# Scalar indexing
25-
@test A[0,3] == A[1] == S[0,3] == S[1] == 1
26-
@test A[1,3] == A[2] == S[1,3] == S[2] == 2
27-
@test A[0,4] == A[3] == S[0,4] == S[3] == 3
28-
@test A[1,4] == A[4] == S[1,4] == S[4] == 4
25+
@test A[0,3] == A[1] == A[0,3,1] == S[0,3] == S[1] == S[0,3,1] == 1
26+
@test A[1,3] == A[2] == A[1,3,1] == S[1,3] == S[2] == S[1,3,1] == 2
27+
@test A[0,4] == A[3] == A[0,4,1] == S[0,4] == S[3] == S[0,4,1] == 3
28+
@test A[1,4] == A[4] == A[1,4,1] == S[1,4] == S[4] == S[1,4,1] == 4
2929
@test_throws BoundsError A[1,1]
3030
@test_throws BoundsError S[1,1]
31+
@test_throws BoundsError A[0,3,2]
32+
@test_throws BoundsError S[0,3,2]
3133

3234
# Vector indexing
3335
@test A[:, 3] == S[:, 3] == OffsetArray([1,2], (A.offsets[1],))

0 commit comments

Comments
 (0)