Skip to content

Specialize indexing a CartesianIndices with a CartesianIndex StepRangeLen #57534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,12 @@ module IteratorsMD
getindex(iter, C.indices...)
end
@inline Base.getindex(iter::CartesianIndices{0}, ::CartesianIndices{0}) = iter
@inline function Base.getindex(iter::CartesianIndices{N}, r::StepRangeLen{CartesianIndex{N},CartesianIndex{N},CartesianIndex{N},<:Integer}) where {N}
@boundscheck checkbounds(iter, r)
start = first(iter) + CartesianIndex((Tuple(first(r)) .- first.(axes(iter))) .* Tuple(step(iter)))
stepsz = CartesianIndex(Tuple(step(iter)) .* Tuple(step(r)))
StepRangeLen(start, stepsz, length(r))
end

# If dimensions permit, we may index into a CartesianIndices directly instead of constructing a SubArray wrapper
@propagate_inbounds function Base.view(c::CartesianIndices{N}, r::Vararg{Union{OrdinalRange{<:Integer, <:Integer}, Colon},N}) where {N}
Expand Down Expand Up @@ -735,7 +741,7 @@ end
@inline checkindex(::Type{Bool}, inds::Tuple, I::CartesianIndex) =
checkbounds_indices(Bool, inds, I.I)
@inline checkindex(::Type{Bool}, inds::Tuple, i::AbstractRange{<:CartesianIndex}) =
isempty(i) | (checkindex(Bool, inds, first(i)) & checkindex(Bool, inds, last(i)))
isempty(i) || (checkindex(Bool, inds, first(i)) & checkindex(Bool, inds, last(i)))

# Indexing into Array with mixtures of Integers and CartesianIndices is
# extremely performance-sensitive. While the abstract fallbacks support this,
Expand Down
28 changes: 28 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,34 @@ end
end
end

@testset "Indexing CartesianIndices as an nD range" begin
for (m, n) in [(4, 6), (4, 4), (6,4)]
C = CartesianIndices((m, n))
for k in -7:7
dlen = length = max(0, k <= 0 ? min(m+k, n) : min(m, n-k))
dind = StepRangeLen(CartesianIndex(1+max(0,-k),1+max(0,k)), CartesianIndex(1,1), dlen)
@test C[dind] === dind
@test C[dind[1:2:end]] === dind[1:2:end]
end
end
for C in [CartesianIndices((20:4:100,)),
CartesianIndices((20:-4:-100,)),
CartesianIndices((20:100,)),
CartesianIndices((Base.IdentityUnitRange(20:100),))]
r = StepRangeLen(CartesianIndex(firstindex(C)+1), CartesianIndex(3), 4)
@test C[r] == C[collect(r)]
end
C = CartesianIndices((3:8, 3:8, 3:8))
r = StepRangeLen(CartesianIndex(1,1,1), CartesianIndex(1,1,1), 6)
@test C[r] == StepRangeLen(CartesianIndex(3,3,3), CartesianIndex(1,1,1), 6)
r = StepRangeLen(CartesianIndex(1,1,1), CartesianIndex(1,0,0), 6)
@test C[r] == StepRangeLen(CartesianIndex(3,3,3), CartesianIndex(1,0,0), 6)

C = CartesianIndices((3:8, 3:8, Base.IdentityUnitRange(3:8)))
r = StepRangeLen(CartesianIndex(1,1,3), CartesianIndex(1,1,1), 6)
@test C[r] == StepRangeLen(CartesianIndex(3,3,3), CartesianIndex(1,1,1), 6)
end

@testset "LinearIndices" begin
@testset "constructors" begin
for oinds in [
Expand Down