Skip to content

Commit c07893d

Browse files
jishnubN5N3
andauthored
Cartesian indexing for SubArrays with non-integer AbstractRanges (#52094)
With #50457 now merged, an `AbstractRange` of `CartesianIndex`es should use `CartesianIndexing` in constructing a `SubArray`. I've limited the method to integer ranges, as this is the case where we know for sure that the indexing may be linear. Fixes ```julia julia> view(1:2, StepRangeLen(CartesianIndex(1), CartesianIndex(1), 0)) 0-element view(::UnitRange{Int64}, StepRangeLen(CartesianIndex(1,), CartesianIndex(1,), 0)) with eltype Int64 ``` --------- Co-authored-by: N5N3 <[email protected]>
1 parent ec3911c commit c07893d

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

base/subarray.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ viewindexing(I::Tuple{Slice, Slice, Vararg{Any}}) = (@inline; viewindexing(tail(
5252
# A UnitRange can follow Slices, but only if all other indices are scalar
5353
viewindexing(I::Tuple{Slice, AbstractUnitRange, Vararg{ScalarIndex}}) = IndexLinear()
5454
viewindexing(I::Tuple{Slice, Slice, Vararg{ScalarIndex}}) = IndexLinear() # disambiguate
55-
# In general, ranges are only fast if all other indices are scalar
56-
viewindexing(I::Tuple{AbstractRange, Vararg{ScalarIndex}}) = IndexLinear()
55+
# In general, scalar ranges are only fast if all other indices are scalar
56+
# Other ranges, such as those of `CartesianIndex`es, are not fast even if these
57+
# are followed by `ScalarIndex`es
58+
viewindexing(I::Tuple{AbstractRange{<:ScalarIndex}, Vararg{ScalarIndex}}) = IndexLinear()
5759
# All other index combinations are slow
5860
viewindexing(I::Tuple{Vararg{Any}}) = IndexCartesian()
5961
# Of course, all other array types are slow

test/subarray.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,3 +809,11 @@ end
809809
b = view(a, Base.IdentityUnitRange(4:7))
810810
@test first(b) == a[Base.first_index(b)]
811811
end
812+
813+
@testset "StepRangeLen of CartesianIndex-es" begin
814+
v = view(1:2, StepRangeLen(CartesianIndex(1,1), CartesianIndex(1,1), 0))
815+
@test isempty(v)
816+
r = StepRangeLen(CartesianIndex(1), CartesianIndex(1), 1)
817+
v = view(1:2, r)
818+
@test v == view(1:2, collect(r))
819+
end

0 commit comments

Comments
 (0)