Skip to content

Commit 0d439eb

Browse files
authored
Merge pull request #20573 from JuliaLang/teh/remaining_size
Separate dispatch for dropping trailing 1s (fixes for non-1 based arrays)
2 parents 388435e + a67e65a commit 0d439eb

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

base/abstractarray.jl

+21-3
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ function checkbounds_indices(::Type{Bool}, IA::Tuple, I::Tuple{Any})
365365
@_inline_meta
366366
checkbounds_linear_indices(Bool, IA, I[1])
367367
end
368-
function checkbounds_linear_indices(::Type{Bool}, IA::Tuple, i)
368+
function checkbounds_linear_indices(::Type{Bool}, IA::Tuple{Vararg{OneTo}}, i)
369369
@_inline_meta
370370
if checkindex(Bool, IA[1], i)
371371
return true
@@ -375,6 +375,10 @@ function checkbounds_linear_indices(::Type{Bool}, IA::Tuple, i)
375375
end
376376
return false
377377
end
378+
function checkbounds_linear_indices(::Type{Bool}, IA::Tuple{AbstractUnitRange,Vararg{AbstractUnitRange}}, i)
379+
@_inline_meta
380+
checkindex(Bool, IA[1], i)
381+
end
378382
function checkbounds_linear_indices(::Type{Bool}, IA::Tuple, i::Union{Slice,Colon})
379383
partial_linear_indexing_warning_lookup(length(IA))
380384
true
@@ -853,10 +857,24 @@ _to_subscript_indices{T}(A::AbstractArray{T,0}, i::Int) = () # TODO: REMOVE FOR
853857
_to_subscript_indices{T}(A::AbstractArray{T,0}, I::Int...) = () # TODO: DEPRECATE FOR #14770
854858
function _to_subscript_indices{T,N}(A::AbstractArray{T,N}, I::Int...) # TODO: DEPRECATE FOR #14770
855859
@_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)
860+
J, Jrem = IteratorsMD.split(I, Val{N})
861+
_to_subscript_indices(A, J, Jrem)
862+
end
863+
_to_subscript_indices(A::AbstractArray, J::Tuple, Jrem::Tuple{}) =
864+
__to_subscript_indices(A, indices(A), J, Jrem)
865+
# We allow partial linear indexing deprecation for OneTo arrays
866+
function __to_subscript_indices(A::AbstractArray, ::Tuple{Vararg{OneTo}}, J::Tuple, Jrem::Tuple{})
867+
@_inline_meta
868+
sz = _remaining_size(J, indices(A)) # compute trailing size (overlapping the final index)
858869
(front(J)..., _unsafe_ind2sub(sz, last(J))...) # (maybe) extend the last index
859870
end
871+
# After the partial linear indexing deprecation is removed, this next method can
872+
# become the new normal. For now, it's limited to non-OneTo arrays.
873+
function __to_subscript_indices(A::AbstractArray, ::Tuple{AbstractUnitRange,Vararg{AbstractUnitRange}}, J::Tuple, Jrem::Tuple{})
874+
@_inline_meta
875+
(J..., map(first, tail(_remaining_size(J, indices(A))))...)
876+
end
877+
_to_subscript_indices(A, J::Tuple, Jrem::Tuple) = J # already bounds-checked, safe to drop
860878
_to_subscript_indices{T,N}(A::AbstractArray{T,N}, I::Vararg{Int,N}) = I
861879
_remaining_size(::Tuple{Any}, t::Tuple) = t
862880
_remaining_size(h::Tuple, t::Tuple) = (@_inline_meta; _remaining_size(tail(h), tail(t)))

test/offsetarray.jl

+12-4
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@ 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]
33+
# partial indexing
34+
S3 = OffsetArray(view(reshape(collect(1:4*3*2), 4, 3, 2), 1:3, 1:2, :), (-1,-2,1))
35+
@test S3[1,-1] == 2
36+
@test S3[1,0] == 6
37+
@test_throws BoundsError S3[1,1]
38+
@test_throws BoundsError S3[1,-2]
3139

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

0 commit comments

Comments
 (0)