From e6784b98d8c9ba65ef647e024df4f841ac76f9f4 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Sat, 4 Mar 2023 12:56:50 +0000 Subject: [PATCH 01/24] add default constructors for AbstractRange --- base/range.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/base/range.jl b/base/range.jl index ea29516f64d24..16b1d54fa024c 100644 --- a/base/range.jl +++ b/base/range.jl @@ -263,6 +263,8 @@ RangeStepStyle(::Type{<:AbstractRange}) = RangeStepIrregular() RangeStepStyle(::Type{<:AbstractRange{<:Integer}}) = RangeStepRegular() convert(::Type{T}, r::AbstractRange) where {T<:AbstractRange} = r isa T ? r : T(r)::T +AbstractRange{T}(r::AbstractRange) where T = T(first(r)):T(step(r)):T(last(r)) +AbstractRange{T}(r::AbstractUnitRange) where {T<:Integer} = AbstractUnitRange{T}(r) ## ordinal ranges From 25f202185f985052c957c7b3831872ea6a239807 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Thu, 9 Mar 2023 21:16:52 +0000 Subject: [PATCH 02/24] Update range.jl --- base/range.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base/range.jl b/base/range.jl index 16b1d54fa024c..850bb4efe53c5 100644 --- a/base/range.jl +++ b/base/range.jl @@ -264,7 +264,6 @@ RangeStepStyle(::Type{<:AbstractRange{<:Integer}}) = RangeStepRegular() convert(::Type{T}, r::AbstractRange) where {T<:AbstractRange} = r isa T ? r : T(r)::T AbstractRange{T}(r::AbstractRange) where T = T(first(r)):T(step(r)):T(last(r)) -AbstractRange{T}(r::AbstractUnitRange) where {T<:Integer} = AbstractUnitRange{T}(r) ## ordinal ranges @@ -288,6 +287,8 @@ Supertype for ranges with a step size of [`oneunit(T)`](@ref) with elements of t [`UnitRange`](@ref) and other types are subtypes of this. """ abstract type AbstractUnitRange{T} <: OrdinalRange{T,T} end + +AbstractRange{T}(r::AbstractUnitRange) where {T<:Integer} = AbstractUnitRange{T}(r) """ StepRange{T, S} <: OrdinalRange{T, S} From 9f891300b1b5eb5658fccf60bc00594ae78e4bbd Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Thu, 9 Mar 2023 21:37:46 +0000 Subject: [PATCH 03/24] whitespace --- base/range.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/range.jl b/base/range.jl index 850bb4efe53c5..2a9c142da3999 100644 --- a/base/range.jl +++ b/base/range.jl @@ -287,7 +287,7 @@ Supertype for ranges with a step size of [`oneunit(T)`](@ref) with elements of t [`UnitRange`](@ref) and other types are subtypes of this. """ abstract type AbstractUnitRange{T} <: OrdinalRange{T,T} end - + AbstractRange{T}(r::AbstractUnitRange) where {T<:Integer} = AbstractUnitRange{T}(r) """ From bc353540bf5753ef557b2e5c5250b233af9641fd Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Fri, 10 Mar 2023 08:18:16 +0000 Subject: [PATCH 04/24] add tests --- test/ranges.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/ranges.jl b/test/ranges.jl index bbdd303adb3c5..2c02cd6a8214d 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -1182,6 +1182,9 @@ end @test convert(LinRange{Float64}, 0.0:0.1:0.3) === LinRange{Float64}(0.0, 0.3, 4) @test convert(LinRange, 0.0:0.1:0.3) === LinRange{Float64}(0.0, 0.3, 4) @test convert(LinRange, 0:3) === LinRange{Int}(0, 3, 4) + + @test convert(AbstractRange{Int8},0:5) === Int8(0):Int8(5) + @test convert(AbstractRange{Float64},0:5) === 0.:1.:5. @test promote('a':'z', 1:2) === ('a':'z', 1:1:2) @test eltype(['a':'z', 1:2]) == (StepRange{T,Int} where T) From 0d588d4d2c5d7261d46cb413d723c47d7d27b0ea Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Fri, 10 Mar 2023 12:39:27 +0000 Subject: [PATCH 05/24] whitespace --- test/ranges.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ranges.jl b/test/ranges.jl index 2c02cd6a8214d..e5a7ca56de05f 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -1182,7 +1182,7 @@ end @test convert(LinRange{Float64}, 0.0:0.1:0.3) === LinRange{Float64}(0.0, 0.3, 4) @test convert(LinRange, 0.0:0.1:0.3) === LinRange{Float64}(0.0, 0.3, 4) @test convert(LinRange, 0:3) === LinRange{Int}(0, 3, 4) - + @test convert(AbstractRange{Int8},0:5) === Int8(0):Int8(5) @test convert(AbstractRange{Float64},0:5) === 0.:1.:5. From be8943e5541da334f00c9a777f93a79aa28f64f2 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Thu, 16 Mar 2023 21:15:56 +0000 Subject: [PATCH 06/24] add AbstractVector and AbstractArray constructor --- base/range.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/base/range.jl b/base/range.jl index 2a9c142da3999..29a811faea455 100644 --- a/base/range.jl +++ b/base/range.jl @@ -264,6 +264,8 @@ RangeStepStyle(::Type{<:AbstractRange{<:Integer}}) = RangeStepRegular() convert(::Type{T}, r::AbstractRange) where {T<:AbstractRange} = r isa T ? r : T(r)::T AbstractRange{T}(r::AbstractRange) where T = T(first(r)):T(step(r)):T(last(r)) +AbstractVector{T}(r::AbstractRange) where T = AbstractRange{T}(r) +AbstractArray{T}(r::AbstractRange) where T = AbstractRange{T}(r) ## ordinal ranges From 52e7177c8e18332e08bd4607b680d8e888b1df6b Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Thu, 16 Mar 2023 21:18:54 +0000 Subject: [PATCH 07/24] add tests --- test/ranges.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/ranges.jl b/test/ranges.jl index e5a7ca56de05f..b517421f97648 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -1183,8 +1183,8 @@ end @test convert(LinRange, 0.0:0.1:0.3) === LinRange{Float64}(0.0, 0.3, 4) @test convert(LinRange, 0:3) === LinRange{Int}(0, 3, 4) - @test convert(AbstractRange{Int8},0:5) === Int8(0):Int8(5) - @test convert(AbstractRange{Float64},0:5) === 0.:1.:5. + @test convert(AbstractRange{Int8},0:5) === convert(AbstractVector{Int8},0:5) ===convert(AbstractArray{Int8},0:5) === Int8(0):Int8(5) + @test convert(AbstractRange{Float64},0:5) === convert(AbstractVector{Float64},0:5) === convert(AbstractArray{Float64},0:5) === 0.:1.:5. @test promote('a':'z', 1:2) === ('a':'z', 1:1:2) @test eltype(['a':'z', 1:2]) == (StepRange{T,Int} where T) From 4e9740d38986b34f747a9694e24a15ab1754e573 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Fri, 17 Mar 2023 12:06:26 +0000 Subject: [PATCH 08/24] Update range.jl --- base/range.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/range.jl b/base/range.jl index 29a811faea455..d766a3640d6f7 100644 --- a/base/range.jl +++ b/base/range.jl @@ -264,7 +264,7 @@ RangeStepStyle(::Type{<:AbstractRange{<:Integer}}) = RangeStepRegular() convert(::Type{T}, r::AbstractRange) where {T<:AbstractRange} = r isa T ? r : T(r)::T AbstractRange{T}(r::AbstractRange) where T = T(first(r)):T(step(r)):T(last(r)) -AbstractVector{T}(r::AbstractRange) where T = AbstractRange{T}(r) +AbstractArray{T,1}(r::AbstractRange) where T = AbstractRange{T}(r) AbstractArray{T}(r::AbstractRange) where T = AbstractRange{T}(r) ## ordinal ranges From 32d838fc571f934367da63f59b29ed5be302a8e4 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Fri, 17 Mar 2023 13:42:14 +0000 Subject: [PATCH 09/24] fix --- base/range.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/base/range.jl b/base/range.jl index d766a3640d6f7..1699914cf7b4c 100644 --- a/base/range.jl +++ b/base/range.jl @@ -263,9 +263,11 @@ RangeStepStyle(::Type{<:AbstractRange}) = RangeStepIrregular() RangeStepStyle(::Type{<:AbstractRange{<:Integer}}) = RangeStepRegular() convert(::Type{T}, r::AbstractRange) where {T<:AbstractRange} = r isa T ? r : T(r)::T -AbstractRange{T}(r::AbstractRange) where T = T(first(r)):T(step(r)):T(last(r)) -AbstractArray{T,1}(r::AbstractRange) where T = AbstractRange{T}(r) -AbstractArray{T}(r::AbstractRange) where T = AbstractRange{T}(r) + +# there is no complex range +AbstractRange{T}(r::AbstractRange) where T<:Real = T(first(r)):T(step(r)):T(last(r)) +AbstractArray{T,1}(r::AbstractRange) where T<:Real = AbstractRange{T}(r) +AbstractArray{T}(r::AbstractRange) where T<:Real = AbstractRange{T}(r) ## ordinal ranges From 7eb8b63c090209de0ce0e9b50a53b0e02594ca98 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Mon, 25 Mar 2024 09:52:02 +0000 Subject: [PATCH 10/24] Update test/ranges.jl Co-authored-by: Sheehan Olver --- test/ranges.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ranges.jl b/test/ranges.jl index b517421f97648..28be05b8868ae 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -1183,7 +1183,7 @@ end @test convert(LinRange, 0.0:0.1:0.3) === LinRange{Float64}(0.0, 0.3, 4) @test convert(LinRange, 0:3) === LinRange{Int}(0, 3, 4) - @test convert(AbstractRange{Int8},0:5) === convert(AbstractVector{Int8},0:5) ===convert(AbstractArray{Int8},0:5) === Int8(0):Int8(5) + @test convert(AbstractRange{Int8},0:5) === convert(AbstractVector{Int8},0:5) === convert(AbstractArray{Int8},0:5) === Int8(0):Int8(5) @test convert(AbstractRange{Float64},0:5) === convert(AbstractVector{Float64},0:5) === convert(AbstractArray{Float64},0:5) === 0.:1.:5. @test promote('a':'z', 1:2) === ('a':'z', 1:1:2) From 77a5a54dfb1d6dd6fbe2a11e4d964feb2e1e44cd Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Wed, 30 Oct 2024 13:10:30 +0000 Subject: [PATCH 11/24] Update range.jl --- base/range.jl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/base/range.jl b/base/range.jl index 06a32ad7925b5..d1b925346d4f7 100644 --- a/base/range.jl +++ b/base/range.jl @@ -275,10 +275,9 @@ RangeStepStyle(::Type{<:AbstractRange{<:Integer}}) = RangeStepRegular() convert(::Type{T}, r::AbstractRange) where {T<:AbstractRange} = r isa T ? r : T(r)::T -# there is no complex range -AbstractRange{T}(r::AbstractRange) where T<:Real = T(first(r)):T(step(r)):T(last(r)) -AbstractArray{T,1}(r::AbstractRange) where T<:Real = AbstractRange{T}(r) -AbstractArray{T}(r::AbstractRange) where T<:Real = AbstractRange{T}(r) +AbstractRange{T}(r::AbstractRange) where T = T(first(r)):T(step(r)):T(last(r)) +AbstractArray{T,1}(r::AbstractRange) where T = AbstractRange{T}(r) +AbstractArray{T}(r::AbstractRange) where T = AbstractRange{T}(r) ## ordinal ranges From 6f16b365e8b6aaf902d86ec8ba242435cb44f653 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Wed, 30 Oct 2024 13:30:57 +0000 Subject: [PATCH 12/24] add tests --- test/ranges.jl | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/ranges.jl b/test/ranges.jl index 8747885d98320..6c2b00237bf4f 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -1277,8 +1277,14 @@ end @test convert(LinRange, 0.0:0.1:0.3) === LinRange{Float64}(0.0, 0.3, 4) @test convert(LinRange, 0:3) === LinRange{Int}(0, 3, 4) - @test convert(AbstractRange{Int8},0:5) === convert(AbstractVector{Int8},0:5) === convert(AbstractArray{Int8},0:5) === Int8(0):Int8(5) - @test convert(AbstractRange{Float64},0:5) === convert(AbstractVector{Float64},0:5) === convert(AbstractArray{Float64},0:5) === 0.:1.:5. + @test convert(AbstractRange{Int8},0:5) === convert(AbstractVector{Int8},0:5) === convert(AbstractArray{Int8},0:5) === + convert(AbstractRange{Int8},0.0:5.0) === convert(AbstractVector{Int8},0.0:5.0) === convert(AbstractArray{Int8},0.0:5.0) === Int8(0):Int8(5) + @test convert(AbstractRange{Int8},0:1:5) === convert(AbstractVector{Int8},0:1:5) === convert(AbstractArray{Int8},0:1:5) === + convert(AbstractRange{Int8},0.0:1.0:5.0) === convert(AbstractVector{Int8},0.0:1.0:5.0) === convert(AbstractArray{Int8},0.0:1.0:5.0) === Int8(0):Int8(1):Int8(5) + @test convert(AbstractRange{Float64},0:5) === convert(AbstractVector{Float64},0:5) === convert(AbstractArray{Float64},0:5) === + convert(AbstractRange{Float64},0.0:5.0) === convert(AbstractVector{Float64},0.0:5.0) === convert(AbstractArray{Float64},0.0:5.0) === + convert(AbstractRange{Float64},0:1:5) === convert(AbstractVector{Float64},0:1:5) === convert(AbstractArray{Float64},0:1:5) === + convert(AbstractRange{Float64},0.0:1.0:5.0) === convert(AbstractVector{Float64},0.0:1.0:5.0) === convert(AbstractArray{Float64},0.0:1.0:5.0) === 0.:1.:5. @test promote('a':'z', 1:2) === ('a':'z', 1:1:2) @test eltype(['a':'z', 1:2]) == (StepRange{T,Int} where T) From db47bb9d3d118715e5d765167bf39f4feb3f93b3 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Wed, 30 Oct 2024 21:26:20 +0000 Subject: [PATCH 13/24] support `StepRangeLen`, `LinRange`, move codes to promotion section --- base/range.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/base/range.jl b/base/range.jl index 4c7532cd8fa8e..6af251a125b86 100644 --- a/base/range.jl +++ b/base/range.jl @@ -1368,6 +1368,9 @@ promote_rule(a::Type{LinRange{T,L}}, ::Type{OR}) where {T,L,OR<:OrdinalRange} = promote_rule(::Type{LinRange{A,L}}, b::Type{StepRangeLen{T2,R2,S2,L2}}) where {A,L,T2,R2,S2,L2} = promote_rule(StepRangeLen{A,A,A,L}, b) +AbstractRange{T}(r::StepRangeLen) where T = StepRangeLen{T}(r) +AbstractRange{T}(r::LinRange) where T = LinRange{T}(r) + ## concatenation ## function vcat(rs::AbstractRange{T}...) where T From d148d2d9f07b074e7e5eb4a0401f7318dc8b389e Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Wed, 30 Oct 2024 21:45:12 +0000 Subject: [PATCH 14/24] add tests --- test/ranges.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/ranges.jl b/test/ranges.jl index 8d2a14722db13..350339b903458 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -1281,6 +1281,10 @@ end convert(AbstractRange{Float64},0:1:5) === convert(AbstractVector{Float64},0:1:5) === convert(AbstractArray{Float64},0:1:5) === convert(AbstractRange{Float64},0.0:1.0:5.0) === convert(AbstractVector{Float64},0.0:1.0:5.0) === convert(AbstractArray{Float64},0.0:1.0:5.0) === 0.:1.:5. + @test convert(AbstractArray{Float64}, 0*(1:10)) === range(0.0,0.0,10) # https://github.com/JuliaLang/julia/pull/48894#discussion_r1822771487 + @test length(convert(AbstractArray{Float16}, range(1/43^2, 1, 43))) == 43 # https://github.com/JuliaLang/julia/pull/48894#discussion_r1822771487 + @test convert(AbstractArray{Float64}, LinRange(1,5,5) === LinRange(1.0,5.0,5) # LinRange support + @test promote('a':'z', 1:2) === ('a':'z', 1:1:2) @test eltype(['a':'z', 1:2]) == (StepRange{T,Int} where T) end @@ -2459,7 +2463,7 @@ end print(io, " with indices $(axes(A,1))") end - r = ZeroBasedUnitRange(5:8) + r = BasedUnitRange(5:8) @test r[0:2] == r[0]:r[2] @test r[0:1:2] == r[0]:1:r[2] end From 80c70af5a9bd8e9f062ef174d3934e13adeb8ea6 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Thu, 31 Oct 2024 08:39:56 +0000 Subject: [PATCH 15/24] Update ranges.jl --- test/ranges.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ranges.jl b/test/ranges.jl index 350339b903458..5a138bd7a2172 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -2463,7 +2463,7 @@ end print(io, " with indices $(axes(A,1))") end - r = BasedUnitRange(5:8) + r = ZeroBasedUnitRange(5:8) @test r[0:2] == r[0]:r[2] @test r[0:1:2] == r[0]:1:r[2] end From c13c97c1ec76d2ff0df79e75ca0f22b58891d9cd Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:47:52 +0000 Subject: [PATCH 16/24] fix --- test/ranges.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ranges.jl b/test/ranges.jl index 5a138bd7a2172..b145395ff1cc8 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -1283,7 +1283,7 @@ end @test convert(AbstractArray{Float64}, 0*(1:10)) === range(0.0,0.0,10) # https://github.com/JuliaLang/julia/pull/48894#discussion_r1822771487 @test length(convert(AbstractArray{Float16}, range(1/43^2, 1, 43))) == 43 # https://github.com/JuliaLang/julia/pull/48894#discussion_r1822771487 - @test convert(AbstractArray{Float64}, LinRange(1,5,5) === LinRange(1.0,5.0,5) # LinRange support + @test convert(AbstractArray{Float64}, LinRange(1,5,5)) === LinRange(1.0,5.0,5) # LinRange support @test promote('a':'z', 1:2) === ('a':'z', 1:1:2) @test eltype(['a':'z', 1:2]) == (StepRange{T,Int} where T) From 3c0180644a56187f51523b6f1fb230a534612ead Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Thu, 31 Oct 2024 15:34:20 +0000 Subject: [PATCH 17/24] add tests --- test/ranges.jl | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/test/ranges.jl b/test/ranges.jl index b145395ff1cc8..fd2525bd50826 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -1272,23 +1272,28 @@ end @test convert(LinRange, 0.0:0.1:0.3) === LinRange{Float64}(0.0, 0.3, 4) @test convert(LinRange, 0:3) === LinRange{Int}(0, 3, 4) - @test convert(AbstractRange{Int8},0:5) === convert(AbstractVector{Int8},0:5) === convert(AbstractArray{Int8},0:5) === - convert(AbstractRange{Int8},0.0:5.0) === convert(AbstractVector{Int8},0.0:5.0) === convert(AbstractArray{Int8},0.0:5.0) === Int8(0):Int8(5) - @test convert(AbstractRange{Int8},0:1:5) === convert(AbstractVector{Int8},0:1:5) === convert(AbstractArray{Int8},0:1:5) === - convert(AbstractRange{Int8},0.0:1.0:5.0) === convert(AbstractVector{Int8},0.0:1.0:5.0) === convert(AbstractArray{Int8},0.0:1.0:5.0) === Int8(0):Int8(1):Int8(5) - @test convert(AbstractRange{Float64},0:5) === convert(AbstractVector{Float64},0:5) === convert(AbstractArray{Float64},0:5) === - convert(AbstractRange{Float64},0.0:5.0) === convert(AbstractVector{Float64},0.0:5.0) === convert(AbstractArray{Float64},0.0:5.0) === - convert(AbstractRange{Float64},0:1:5) === convert(AbstractVector{Float64},0:1:5) === convert(AbstractArray{Float64},0:1:5) === - convert(AbstractRange{Float64},0.0:1.0:5.0) === convert(AbstractVector{Float64},0.0:1.0:5.0) === convert(AbstractArray{Float64},0.0:1.0:5.0) === 0.:1.:5. - - @test convert(AbstractArray{Float64}, 0*(1:10)) === range(0.0,0.0,10) # https://github.com/JuliaLang/julia/pull/48894#discussion_r1822771487 - @test length(convert(AbstractArray{Float16}, range(1/43^2, 1, 43))) == 43 # https://github.com/JuliaLang/julia/pull/48894#discussion_r1822771487 - @test convert(AbstractArray{Float64}, LinRange(1,5,5)) === LinRange(1.0,5.0,5) # LinRange support - @test promote('a':'z', 1:2) === ('a':'z', 1:1:2) @test eltype(['a':'z', 1:2]) == (StepRange{T,Int} where T) end +@testset "Eltype conversion (PR #48894)" begin + # StepRangeLen is converted to StepRangeLen + @test 0.:5. isa StepRangeLen # just in case + @test convert(AbstractRange{Int},0.:5.) === convert(AbstractVector{Int},0.:5.) === convert(AbstractArray{Int},0.:5.) === StepRangeLen(0,1,5) + @test convert(AbstractRange{Float16},0.:5.) === convert(AbstractVector{Float16},0.:5.) === convert(AbstractArray{Float16},0.:5.) === Float16(0.):Float16(5.) + + # Try to preserve type when possible + @test AbstractArray{Int8}(oneto(5)) === oneto(Int8(5)) # OneTo + @test AbstractArray{BigInt}(0:5) === BigInt(0):5 # UnitRange + @test AbstractArray{Int128}(0:2:5) === Int128(0):2:4 # StepRange + @test AbstractArray{Float64}(LinRange(1,5,5)) === LinRange(1.0,5.0,5) # LinRange + @test AbstractArray{Float64}(oneto(5)) === AbstractArray{Float64}(1:5) === AbstractArray{Float64}(1:1:5) === 1.:1.:5. # fallback + + # Edge cases where floating point can be glitchy. Credit: @mcabbott + @test convert(AbstractArray{Float64}, 0*(1:10)) === range(0.0,0.0,10) + @test length(convert(AbstractArray{Float16}, range(1/43^2, 1, 43))) == 43 +end + @testset "Ranges with <:Integer eltype but non-integer step (issue #32419)" begin @test eltype(StepRange(1, 1//1, 2)) === Int @test_throws ArgumentError StepRange(1, 1//2, 2) From 57854ee4061f40b2592484808a9533377c594852 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Thu, 31 Oct 2024 15:40:58 +0000 Subject: [PATCH 18/24] Update range.jl --- base/range.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base/range.jl b/base/range.jl index 6af251a125b86..cadb7a3b8f709 100644 --- a/base/range.jl +++ b/base/range.jl @@ -275,7 +275,7 @@ RangeStepStyle(::Type{<:AbstractRange{<:Integer}}) = RangeStepRegular() convert(::Type{T}, r::AbstractRange) where {T<:AbstractRange} = r isa T ? r : T(r)::T -AbstractRange{T}(r::AbstractRange) where T = T(first(r)):T(step(r)):T(last(r)) +AbstractRange{T}(r::AbstractRange) where T = range(T(first(r)), T(last(r)), length(r)) AbstractArray{T,1}(r::AbstractRange) where T = AbstractRange{T}(r) AbstractArray{T}(r::AbstractRange) where T = AbstractRange{T}(r) @@ -1368,6 +1368,7 @@ promote_rule(a::Type{LinRange{T,L}}, ::Type{OR}) where {T,L,OR<:OrdinalRange} = promote_rule(::Type{LinRange{A,L}}, b::Type{StepRangeLen{T2,R2,S2,L2}}) where {A,L,T2,R2,S2,L2} = promote_rule(StepRangeLen{A,A,A,L}, b) +AbstractRange{T}(r::OrdinalRange) where T<:Integer = OrdinalRange{T}(r) # float should fall back to StepRangeLen AbstractRange{T}(r::StepRangeLen) where T = StepRangeLen{T}(r) AbstractRange{T}(r::LinRange) where T = LinRange{T}(r) From 2e7618936341aba8999983e62af7d710ff74dca5 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Thu, 31 Oct 2024 15:42:32 +0000 Subject: [PATCH 19/24] whitespace --- test/ranges.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ranges.jl b/test/ranges.jl index fd2525bd50826..f1922227921d1 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -1288,7 +1288,7 @@ end @test AbstractArray{Int128}(0:2:5) === Int128(0):2:4 # StepRange @test AbstractArray{Float64}(LinRange(1,5,5)) === LinRange(1.0,5.0,5) # LinRange @test AbstractArray{Float64}(oneto(5)) === AbstractArray{Float64}(1:5) === AbstractArray{Float64}(1:1:5) === 1.:1.:5. # fallback - + # Edge cases where floating point can be glitchy. Credit: @mcabbott @test convert(AbstractArray{Float64}, 0*(1:10)) === range(0.0,0.0,10) @test length(convert(AbstractArray{Float16}, range(1/43^2, 1, 43))) == 43 From 9324d213db9a5487658968616e1f1b66abffd2b5 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Thu, 31 Oct 2024 18:20:21 +0000 Subject: [PATCH 20/24] Update ranges.jl --- test/ranges.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/ranges.jl b/test/ranges.jl index f1922227921d1..dc68cd174f0db 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -1279,15 +1279,15 @@ end @testset "Eltype conversion (PR #48894)" begin # StepRangeLen is converted to StepRangeLen @test 0.:5. isa StepRangeLen # just in case - @test convert(AbstractRange{Int},0.:5.) === convert(AbstractVector{Int},0.:5.) === convert(AbstractArray{Int},0.:5.) === StepRangeLen(0,1,5) + @test convert(AbstractRange{Int},0.:5.) === convert(AbstractVector{Int},0.:5.) === convert(AbstractArray{Int},0.:5.) === StepRangeLen(0,1,6) @test convert(AbstractRange{Float16},0.:5.) === convert(AbstractVector{Float16},0.:5.) === convert(AbstractArray{Float16},0.:5.) === Float16(0.):Float16(5.) # Try to preserve type when possible - @test AbstractArray{Int8}(oneto(5)) === oneto(Int8(5)) # OneTo + @test AbstractArray{Int8}(Base.oneto(5)) === Base.oneto(Int8(5)) # OneTo @test AbstractArray{BigInt}(0:5) === BigInt(0):5 # UnitRange @test AbstractArray{Int128}(0:2:5) === Int128(0):2:4 # StepRange @test AbstractArray{Float64}(LinRange(1,5,5)) === LinRange(1.0,5.0,5) # LinRange - @test AbstractArray{Float64}(oneto(5)) === AbstractArray{Float64}(1:5) === AbstractArray{Float64}(1:1:5) === 1.:1.:5. # fallback + @test AbstractArray{Float64}(Base.oneto(5)) === AbstractArray{Float64}(1:5) === AbstractArray{Float64}(1:1:5) === 1.:1.:5. # fallback # Edge cases where floating point can be glitchy. Credit: @mcabbott @test convert(AbstractArray{Float64}, 0*(1:10)) === range(0.0,0.0,10) From 02b3d2f6eada9f84b445483decdd5dd98e68ff41 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Thu, 31 Oct 2024 19:24:43 +0000 Subject: [PATCH 21/24] Update range.jl --- base/range.jl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/base/range.jl b/base/range.jl index cadb7a3b8f709..5f700ac74e125 100644 --- a/base/range.jl +++ b/base/range.jl @@ -275,10 +275,6 @@ RangeStepStyle(::Type{<:AbstractRange{<:Integer}}) = RangeStepRegular() convert(::Type{T}, r::AbstractRange) where {T<:AbstractRange} = r isa T ? r : T(r)::T -AbstractRange{T}(r::AbstractRange) where T = range(T(first(r)), T(last(r)), length(r)) -AbstractArray{T,1}(r::AbstractRange) where T = AbstractRange{T}(r) -AbstractArray{T}(r::AbstractRange) where T = AbstractRange{T}(r) - ## ordinal ranges """ @@ -1318,6 +1314,7 @@ AbstractUnitRange{T}(r::UnitRange) where {T} = UnitRange{T}(r) AbstractUnitRange{T}(r::OneTo) where {T} = OneTo{T}(r) OrdinalRange{T, S}(r::OrdinalRange) where {T, S} = StepRange{T, S}(r) +OrdinalRange{T}(r::OrdinalRange{R,S}) where {T, R, S} = OrdinalRange{T, promote_type(T,S)}(r) OrdinalRange{T, T}(r::AbstractUnitRange) where {T} = AbstractUnitRange{T}(r) function promote_rule(::Type{StepRange{T1a,T1b}}, ::Type{StepRange{T2a,T2b}}) where {T1a,T1b,T2a,T2b} @@ -1368,10 +1365,15 @@ promote_rule(a::Type{LinRange{T,L}}, ::Type{OR}) where {T,L,OR<:OrdinalRange} = promote_rule(::Type{LinRange{A,L}}, b::Type{StepRangeLen{T2,R2,S2,L2}}) where {A,L,T2,R2,S2,L2} = promote_rule(StepRangeLen{A,A,A,L}, b) +AbstractRange{T}(r::AbstractRange) where T = range(T(first(r)), T(last(r)), length(r)) + AbstractRange{T}(r::OrdinalRange) where T<:Integer = OrdinalRange{T}(r) # float should fall back to StepRangeLen AbstractRange{T}(r::StepRangeLen) where T = StepRangeLen{T}(r) AbstractRange{T}(r::LinRange) where T = LinRange{T}(r) +AbstractArray{T,1}(r::AbstractRange) where T = AbstractRange{T}(r) +AbstractArray{T}(r::AbstractRange) where T = AbstractRange{T}(r) + ## concatenation ## function vcat(rs::AbstractRange{T}...) where T From fdabcb56974ec7633fa0dc567e8b0f8e39d822cc Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Thu, 31 Oct 2024 19:48:39 +0000 Subject: [PATCH 22/24] fix --- test/ranges.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/ranges.jl b/test/ranges.jl index dc68cd174f0db..ef24fda0f0b20 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -1277,15 +1277,15 @@ end end @testset "Eltype conversion (PR #48894)" begin - # StepRangeLen is converted to StepRangeLen + # StepRangeLen @test 0.:5. isa StepRangeLen # just in case @test convert(AbstractRange{Int},0.:5.) === convert(AbstractVector{Int},0.:5.) === convert(AbstractArray{Int},0.:5.) === StepRangeLen(0,1,6) @test convert(AbstractRange{Float16},0.:5.) === convert(AbstractVector{Float16},0.:5.) === convert(AbstractArray{Float16},0.:5.) === Float16(0.):Float16(5.) # Try to preserve type when possible @test AbstractArray{Int8}(Base.oneto(5)) === Base.oneto(Int8(5)) # OneTo - @test AbstractArray{BigInt}(0:5) === BigInt(0):5 # UnitRange - @test AbstractArray{Int128}(0:2:5) === Int128(0):2:4 # StepRange + @test AbstractArray{Int32}(0:5) === Int32(0):Int32(5) # UnitRange + @test AbstractArray{Int128}(0:2:5) === Int128(0):Int128(2):4 # StepRange @test AbstractArray{Float64}(LinRange(1,5,5)) === LinRange(1.0,5.0,5) # LinRange @test AbstractArray{Float64}(Base.oneto(5)) === AbstractArray{Float64}(1:5) === AbstractArray{Float64}(1:1:5) === 1.:1.:5. # fallback From ebe820e1e8b3da319e5ec0231524cfd50fcc3ad6 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Thu, 31 Oct 2024 19:49:42 +0000 Subject: [PATCH 23/24] Update range.jl --- base/range.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base/range.jl b/base/range.jl index 5f700ac74e125..3afd77dedd429 100644 --- a/base/range.jl +++ b/base/range.jl @@ -1314,8 +1314,9 @@ AbstractUnitRange{T}(r::UnitRange) where {T} = UnitRange{T}(r) AbstractUnitRange{T}(r::OneTo) where {T} = OneTo{T}(r) OrdinalRange{T, S}(r::OrdinalRange) where {T, S} = StepRange{T, S}(r) -OrdinalRange{T}(r::OrdinalRange{R,S}) where {T, R, S} = OrdinalRange{T, promote_type(T,S)}(r) OrdinalRange{T, T}(r::AbstractUnitRange) where {T} = AbstractUnitRange{T}(r) +OrdinalRange{T}(r::OrdinalRange{R,S}) where {T, R, S} = OrdinalRange{T, promote_type(T,S)}(r) # type of step matters +OrdinalRange{T}(r::AbstractUnitRange) where T = AbstractUnitRange{T}(r) # not in this case function promote_rule(::Type{StepRange{T1a,T1b}}, ::Type{StepRange{T2a,T2b}}) where {T1a,T1b,T2a,T2b} Tb = promote_type(T1b, T2b) From c28f960be1b4bfc16decb0c7fe226a4a50655427 Mon Sep 17 00:00:00 2001 From: Tianyi Pu <44583944+putianyi889@users.noreply.github.com> Date: Sat, 2 Nov 2024 09:49:57 +0000 Subject: [PATCH 24/24] Update abstractarray.jl --- base/abstractarray.jl | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index cbbae8e852b2e..40949723f71b3 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1235,13 +1235,7 @@ isempty(a::AbstractArray) = (length(a) == 0) ## range conversions ## - -map(::Type{T}, r::StepRange) where {T<:Real} = T(r.start):T(r.step):T(last(r)) -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 - LinRange(T(r.start), T(r.stop), length(r)) -end +map(::Type{T}, r::AbstractRange) where {T<:Real} = AbstractArray{T}(r) ## unsafe/pointer conversions ##