From ba09752e240c187394ab2179d414ea4b0dea0409 Mon Sep 17 00:00:00 2001 From: Michael Abbott Date: Mon, 1 Jul 2019 17:44:13 +0200 Subject: [PATCH 1/2] efficient subset query for unit ranges --- base/range.jl | 5 +++++ test/ranges.jl | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/base/range.jl b/base/range.jl index 9fbc198d1c372..feb2339fcf7e9 100644 --- a/base/range.jl +++ b/base/range.jl @@ -847,6 +847,11 @@ function _findin(r::AbstractRange{<:Integer}, span::AbstractUnitRange{<:Integer} r isa AbstractUnitRange ? (ifirst:ilast) : (ifirst:1:ilast) end +issubset(r::OneTo, s::OneTo) = r.stop <= s.stop + +issubset(r::AbstractUnitRange{<:Integer}, s::AbstractUnitRange{<:Integer}) = + first(r) >= first(s) && last(r) <= last(s) + ## linear operations on ranges ## -(r::OrdinalRange) = range(-first(r), step=-step(r), length=length(r)) diff --git a/test/ranges.jl b/test/ranges.jl index 38c73b1bef792..e5fa0a72ba215 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -362,6 +362,20 @@ end @test intersect(1:3, 2) === intersect(2, 1:3) === 2:2 @test intersect(1.0:3.0, 2) == intersect(2, 1.0:3.0) == [2.0] end + @testset "issubset" begin + @test issubset(1:3, 1:typemax(Int)) #32461 + @test issubset(1:3, 1:3) + @test issubset(1:3, 1:4) + @test issubset(1:3, 0:3) + @test issubset(1:3, 0:4) + @test !issubset(1:5, 2:5) + @test !issubset(1:5, 1:4) + @test !issubset(1:5, 2:4) + @test issubset(Base.OneTo(5), Base.OneTo(10)) + @test !issubset(Base.OneTo(10), Base.OneTo(5)) + @test issubset(1:3:10, 1:10) + @test !issubset(1:10, 1:3:10) + end @testset "sort/sort!/partialsort" begin @test sort(UnitRange(1,2)) == UnitRange(1,2) @test sort!(UnitRange(1,2)) == UnitRange(1,2) From 4a6cc00794db2309340b126d5e05f8825d0397f0 Mon Sep 17 00:00:00 2001 From: Michael Abbott Date: Mon, 1 Jul 2019 18:22:20 +0200 Subject: [PATCH 2/2] spacectomy --- base/range.jl | 2 +- test/ranges.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/base/range.jl b/base/range.jl index feb2339fcf7e9..7e09bef5c8b08 100644 --- a/base/range.jl +++ b/base/range.jl @@ -849,7 +849,7 @@ end issubset(r::OneTo, s::OneTo) = r.stop <= s.stop -issubset(r::AbstractUnitRange{<:Integer}, s::AbstractUnitRange{<:Integer}) = +issubset(r::AbstractUnitRange{<:Integer}, s::AbstractUnitRange{<:Integer}) = first(r) >= first(s) && last(r) <= last(s) ## linear operations on ranges ## diff --git a/test/ranges.jl b/test/ranges.jl index e5fa0a72ba215..db72385306e1d 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -374,7 +374,7 @@ end @test issubset(Base.OneTo(5), Base.OneTo(10)) @test !issubset(Base.OneTo(10), Base.OneTo(5)) @test issubset(1:3:10, 1:10) - @test !issubset(1:10, 1:3:10) + @test !issubset(1:10, 1:3:10) end @testset "sort/sort!/partialsort" begin @test sort(UnitRange(1,2)) == UnitRange(1,2)