Skip to content

Commit

Permalink
Fix ranges of CartesianIndexes
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Aug 22, 2021
1 parent e37e290 commit 14ba473
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 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
4 changes: 2 additions & 2 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ range_stop(stop::Integer) = range_length(stop)
# Stop and length as the only argument
range_stop_length(a::Real, len::Integer) = UnitRange{typeof(a)}(oftype(a, a-len+1), a)
range_stop_length(a::AbstractFloat, len::Integer) = range_step_stop_length(oftype(a, 1), a, len)
range_stop_length(a, len::Integer) = range_step_stop_length(oftype(a-a, 1), a, len)
range_stop_length(a, len::Integer) = range_step_stop_length(oftype(a-a, oneunit(a)), a, len)

range_step_stop_length(step, stop, length) = reverse(range_start_step_length(stop, -step, length))

range_start_length(a::Real, len::Integer) = UnitRange{typeof(a)}(a, oftype(a, a+len-1))
range_start_length(a::AbstractFloat, len::Integer) = range_start_step_length(a, oftype(a, 1), len)
range_start_length(a, len::Integer) = range_start_step_length(a, oftype(a-a, 1), len)
range_start_length(a, len::Integer) = range_start_step_length(a, oftype(a-a, oneunit(a)), len)

range_start_stop(start, stop) = start:stop

Expand Down
10 changes: 10 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2112,3 +2112,13 @@ 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
a = CartesianIndex(1,1)
r1 = range(a, step = CartesianIndex(2,3), length = 3)
r2 = StepRangeLen(CartesianIndex(1,1), CartesianIndex(2,3), 3)
@test r1 == r2

@test range(a, length = 4) == StepRangeLen(a, a, 4)
@test range(stop = a, length = 4) == StepRangeLen(-2a, a, 4)
end

0 comments on commit 14ba473

Please sign in to comment.