Skip to content

Commit a81a7cc

Browse files
committed
Fix starting point and add tests
1 parent 6887f83 commit a81a7cc

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

base/multidimensional.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ module IteratorsMD
399399
@inline Base.getindex(iter::CartesianIndices{0}, ::CartesianIndices{0}) = iter
400400
@inline function Base.getindex(iter::CartesianIndices{N}, r::StepRangeLen{CartesianIndex{N},CartesianIndex{N},CartesianIndex{N},<:Integer}) where {N}
401401
@boundscheck checkbounds(iter, r)
402-
start = first(iter) + first(r) - CartesianIndex(first.(axes(iter)))
402+
start = first(iter) + CartesianIndex((Tuple(first(r)) .- first.(axes(iter))) .* Tuple(step(iter)))
403403
stepsz = CartesianIndex(Tuple(step(iter)) .* Tuple(step(r)))
404404
StepRangeLen(start, stepsz, length(r))
405405
end

test/abstractarray.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,34 @@ end
285285
end
286286
end
287287

288+
@testset "Indexing CartesianIndices as an nD range" begin
289+
for (m, n) in [(4, 6), (4, 4), (6,4)]
290+
C = CartesianIndices((m, n))
291+
for k in -7:7
292+
dlen = length = max(0, k <= 0 ? min(m+k, n) : min(m, n-k))
293+
dind = StepRangeLen(CartesianIndex(1+max(0,-k),1+max(0,k)), CartesianIndex(1,1), dlen)
294+
@test C[dind] === dind
295+
@test C[dind[1:2:end]] === dind[1:2:end]
296+
end
297+
end
298+
for C in [CartesianIndices((20:4:100,)),
299+
CartesianIndices((20:-4:-100,)),
300+
CartesianIndices((20:100,)),
301+
CartesianIndices((Base.IdentityUnitRange(20:100),))]
302+
r = StepRangeLen(CartesianIndex(firstindex(C)+1), CartesianIndex(3), 4)
303+
@test C[r] == C[collect(r)]
304+
end
305+
C = CartesianIndices((3:8, 3:8, 3:8))
306+
r = StepRangeLen(CartesianIndex(1,1,1), CartesianIndex(1,1,1), 6)
307+
@test C[r] == StepRangeLen(CartesianIndex(3,3,3), CartesianIndex(1,1,1), 6)
308+
r = StepRangeLen(CartesianIndex(1,1,1), CartesianIndex(1,0,0), 6)
309+
@test C[r] == StepRangeLen(CartesianIndex(3,3,3), CartesianIndex(1,0,0), 6)
310+
311+
C = CartesianIndices((3:8, 3:8, Base.IdentityUnitRange(3:8)))
312+
r = StepRangeLen(CartesianIndex(1,1,3), CartesianIndex(1,1,1), 6)
313+
@test C[r] == StepRangeLen(CartesianIndex(3,3,3), CartesianIndex(1,1,1), 6)
314+
end
315+
288316
@testset "LinearIndices" begin
289317
@testset "constructors" begin
290318
for oinds in [

0 commit comments

Comments
 (0)