Skip to content

Commit

Permalink
Add colon constructor for CartesianIndices
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Sep 30, 2018
1 parent 2c86c1f commit a9f58a6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module IteratorsMD
setindex!, IndexStyle, min, max, zero, one, isless, eachindex,
ndims, IteratorSize, convert, show, iterate, promote_rule

import .Base: +, -, *
import .Base: +, -, *, (:)
import .Base: simd_outer_range, simd_inner_length, simd_index
using .Base: IndexLinear, IndexCartesian, AbstractCartesianIndex, fill_to_length, tail
using .Base.Iterators: Reverse
Expand Down Expand Up @@ -228,6 +228,9 @@ module IteratorsMD

CartesianIndices(A::AbstractArray) = CartesianIndices(axes(A))

(:)(I::CartesianIndex{N}, J::CartesianIndex{N}) where N =
CartesianIndices(map((i,j) -> i:j, Tuple(I), Tuple(J)))

promote_rule(::Type{CartesianIndices{N,R1}}, ::Type{CartesianIndices{N,R2}}) where {N,R1,R2} =
CartesianIndices{N,Base.indices_promote_type(R1,R2)}

Expand Down
4 changes: 4 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,10 @@ end

@test @inferred(convert(NTuple{2,UnitRange}, R)) === (2:5, 3:5)
@test @inferred(convert(Tuple{Vararg{UnitRange}}, R)) === (2:5, 3:5)

I = CartesianIndex(2,3)
J = CartesianIndex(5,4)
@test I:J === CartesianIndices((2:5, 3:4))
end

# All we really care about is that we have an optimized
Expand Down

0 comments on commit a9f58a6

Please sign in to comment.