Skip to content

Commit ca2446e

Browse files
committed
Make CartesianIndex iterable
Make iterating a CartesianIndex equivalent to iterating the tuple it holds, being consistent with its indexing behavior.
1 parent deceb3b commit ca2446e

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

base/multidimensional.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ module IteratorsMD
8888

8989
# indexing
9090
getindex(index::CartesianIndex, i::Integer) = index.I[i]
91+
eltype(index::CartesianIndex) = eltype(index.I)
92+
93+
# iteration
94+
start(index::CartesianIndex) = start(index.I)
95+
next(index::CartesianIndex, state) = next(index.I, state)
96+
done(index::CartesianIndex, state) = done(index.I, state)
9197

9298
# zeros and ones
9399
zero(::CartesianIndex{N}) where {N} = zero(CartesianIndex{N})

test/arrayops.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,8 @@ end
15421542
@test min(CartesianIndex((2,3)), CartesianIndex((5,2))) == CartesianIndex((2,2))
15431543
@test max(CartesianIndex((2,3)), CartesianIndex((5,2))) == CartesianIndex((5,3))
15441544

1545+
@test [I1...] == [I1[n] for n in 1:length(I1)]
1546+
15451547
# CartesianIndex allows construction at a particular dimensionality
15461548
@test length(CartesianIndex{3}()) == 3
15471549
@test length(CartesianIndex{3}(1,2)) == 3

0 commit comments

Comments
 (0)