Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specialize map(T, ::AbstractRange) for more range types to preserve the result as a range #40746

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,11 @@ isempty(a::AbstractArray) = (length(a) == 0)

## range conversions ##

map(::Type{T}, r::AbstractRange{T}) where {T} = r
map(::Type{T}, r::StepRange) where {T<:Real} = T(r.start):T(r.step):T(last(r))
map(::Type{T}, r::Base.OneTo) where {T<:Integer} = oneto(T(last(r)))
map(::Type{Bool}, r::Base.OneTo) = map(Bool, UnitRange(r))
map(::Type{T}, r::Base.OneTo) where {T<:Real} = map(T, UnitRange(r))
jishnub marked this conversation as resolved.
Show resolved Hide resolved
map(::Type{T}, r::UnitRange) where {T<:Real} = T(r.start):T(last(r))
map(::Type{T}, r::StepRangeLen) where {T<:AbstractFloat} = convert(StepRangeLen{T}, r)
function map(::Type{T}, r::LinRange) where T<:AbstractFloat
Expand Down
2 changes: 2 additions & 0 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,8 @@ broadcasted(::DefaultArrayStyle{1}, ::typeof(big), r::StepRange) = big(r.start):
broadcasted(::DefaultArrayStyle{1}, ::typeof(big), r::StepRangeLen) = StepRangeLen(big(r.ref), big(r.step), length(r), r.offset)
broadcasted(::DefaultArrayStyle{1}, ::typeof(big), r::LinRange) = LinRange(big(r.start), big(r.stop), length(r))

broadcasted(::DefaultArrayStyle{1}, ::Type{T}, r::AbstractRange) where {T<:Number} = map(T, r)

## CartesianIndices
broadcasted(::typeof(+), I::CartesianIndices{N}, j::CartesianIndex{N}) where N =
CartesianIndices(map((rng, offset)->rng .+ offset, I.indices, Tuple(j)))
Expand Down
1 change: 1 addition & 0 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module IteratorsMD
CartesianIndex(index::NTuple{N,Integer}) where {N} = CartesianIndex{N}(index)
CartesianIndex(index::Integer...) = CartesianIndex(index)
CartesianIndex{N}(index::Vararg{Integer,N}) where {N} = CartesianIndex{N}(index)
CartesianIndex{N}(I::CartesianIndex{N}) where {N} = I
# Allow passing tuples smaller than N
CartesianIndex{N}(index::Tuple) where {N} = CartesianIndex{N}(fill_to_length(index, 1, Val(N)))
CartesianIndex{N}(index::Integer...) where {N} = CartesianIndex{N}(index)
Expand Down
57 changes: 57 additions & 0 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1048,3 +1048,60 @@ end
# issue 40309
@test Base.broadcasted_kwsyntax(+, [1], [2]) isa Broadcast.Broadcasted{<:Any, <:Any, typeof(+)}
@test Broadcast.BroadcastFunction(+)(2:3, 2:3) === 4:2:6

@testset "broadcast ranges to a different eltype" begin
for r = Any[1:5, Base.OneTo(3), UnitRange(1.0, 4.0), 1:1:5]
for T in [Int32, Int64, BigInt]
r2 = T.(r)
if r isa AbstractUnitRange
@test r2 isa AbstractUnitRange
else
@test r2 isa AbstractRange
end
@test r2 == r
end
for T in [Float32, Float64, BigFloat]
r2 = T.(r)
@test r2 isa AbstractRange
@test r2 == r
end
end
for r = Any[Base.IdentityUnitRange(2:4), Base.IdentityUnitRange(Base.OneTo(3))]
r2 = Int.(r)
@test r2 === r
end

for r in Any[LinRange{Int}(1, 4, 4), LinRange(1, 4, 4), range(float(1), stop = float(4), step = float(1))]
for T in [Float32, Float64, BigFloat]
r2 = T.(r)
@test r2 isa AbstractRange
@test r2 == r
end
end

@testset "broadcast to Bool" begin
for fn = 0:1
for r in Any[1:fn, Base.OneTo(fn), 1:1:1, range(float(1), stop = float(fn), step = float(1)),
LinRange{Int}(1, fn, fn), LinRange(1, fn, fn)]
r2 = Bool.(r)
@test eltype(r2) == Bool
@test r2 == r
@test r2 == map(Bool, r)
end
end
for fn = 0:1
for r in Any[0:fn, 0:1:fn, range(float(0), stop = float(fn), step = float(1)),
LinRange{Int}(0, fn, fn + 1), LinRange(0, fn, fn + 1)]
r2 = Bool.(r)
@test eltype(r2) == Bool
@test r2 == r
@test r2 == map(Bool, r)
end
end
end
# test with a type that is not a subtype of Number
R = range(CartesianIndex(1), step = CartesianIndex(1), length = 4)
@test CartesianIndex.(1:4) == CartesianIndices((1:4,)) == R
R = range(CartesianIndex(1,1), step = CartesianIndex(2,2), length = 4)
@test CartesianIndex.(1:2:7, 1:2:7) == R
end