Skip to content

Commit

Permalink
efficient subset query for unit ranges (#32463)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott authored and JeffBezanson committed Jul 2, 2019
1 parent 1fc51b7 commit 49fc01a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 14 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 49fc01a

Please sign in to comment.