Skip to content

Commit

Permalink
Make CartesianIndex iterable
Browse files Browse the repository at this point in the history
Make iterating a CartesianIndex equivalent to iterating the tuple it
holds, being consistent with its indexing behavior.
  • Loading branch information
martinholters committed Sep 14, 2017
1 parent deceb3b commit ca2446e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
6 changes: 6 additions & 0 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ module IteratorsMD

# indexing
getindex(index::CartesianIndex, i::Integer) = index.I[i]
eltype(index::CartesianIndex) = eltype(index.I)

# iteration
start(index::CartesianIndex) = start(index.I)
next(index::CartesianIndex, state) = next(index.I, state)
done(index::CartesianIndex, state) = done(index.I, state)

# zeros and ones
zero(::CartesianIndex{N}) where {N} = zero(CartesianIndex{N})
Expand Down
2 changes: 2 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,8 @@ end
@test min(CartesianIndex((2,3)), CartesianIndex((5,2))) == CartesianIndex((2,2))
@test max(CartesianIndex((2,3)), CartesianIndex((5,2))) == CartesianIndex((5,3))

@test [I1...] == [I1[n] for n in 1:length(I1)]

# CartesianIndex allows construction at a particular dimensionality
@test length(CartesianIndex{3}()) == 3
@test length(CartesianIndex{3}(1,2)) == 3
Expand Down

0 comments on commit ca2446e

Please sign in to comment.