Skip to content
Closed
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
7 changes: 7 additions & 0 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ module IteratorsMD

CartesianIndex(index::NTuple{N,Integer}) where {N} = CartesianIndex{N}(index)
CartesianIndex(index::Integer...) = CartesianIndex(index)
CartesianIndex{N}(c::CartesianIndex{N}) where {N} = c
CartesianIndex{N}(index::Vararg{Integer,N}) where {N} = CartesianIndex{N}(index)
# Allow passing tuples smaller than N
CartesianIndex{N}(index::Tuple) where {N} = CartesianIndex{N}(fill_to_length(index, 1, Val(N)))
Expand Down Expand Up @@ -319,6 +320,12 @@ module IteratorsMD
(:)(I::CartesianIndex{N}, S::CartesianIndex{N}, J::CartesianIndex{N}) where N =
CartesianIndices(map((i,s,j) -> i:s:j, Tuple(I), Tuple(S), Tuple(J)))

function show(io::IO, r::Base.StepRangeLen{C,C,C,<:Integer}) where {C<:CartesianIndex}
# Since a:b:c with CartesianIndex arguments produces CartesianIndices instead of a StepRangeLen,
# the display of a StepRangeLen of CartesianIndexes should not use this notation
print(io, "StepRangeLen(", repr(first(r)), ", ", repr(step(r)), ", ", repr(length(r)), ")")
end

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

Expand Down
17 changes: 17 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2112,3 +2112,20 @@ end
@test length(range(1, 100, length=big(100)^100)) == big(100)^100
@test length(range(big(1), big(100)^100, length=big(100)^100)) == big(100)^100
@test length(0 * (1:big(100)^100)) == big(100)^100

@testset "ranges of CartesianIndexes" begin
start = CartesianIndex(1,1)
step = CartesianIndex(2,3)
len = 3
r1 = StepRangeLen(start, step, len)
@test r1[1] == start
@test r1[2] == start + step
@test r1[3] == start + 2step
r2 = range(start, step = step, length = len)
@test r1 == r2

# Make sure that a StepRangeLen of CartesianIndexes does not use the a:b:c notation
# while displayed
s = repr(r1)
@test s == "StepRangeLen($start, $step, $len)"
end