Skip to content

Commit

Permalink
Support StepRange{T,U} where U is non-numeric (#32671)
Browse files Browse the repository at this point in the history
With thanks to @quinnj
  • Loading branch information
cmcaine authored and StefanKarpinski committed Aug 2, 2019
1 parent 62857f8 commit a0936f9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -777,9 +777,9 @@ end
function intersect(r::StepRange, s::StepRange)
if isempty(r) || isempty(s)
return range(first(r), step=step(r), length=0)
elseif step(s) < 0
elseif step(s) < zero(step(s))
return intersect(r, reverse(s))
elseif step(r) < 0
elseif step(r) < zero(step(r))
return reverse(intersect(reverse(r), s))
end

Expand All @@ -804,7 +804,7 @@ function intersect(r::StepRange, s::StepRange)

g, x, y = gcdx(step1, step2)

if rem(start1 - start2, g) != 0
if !iszero(rem(start1 - start2, g))
# Unaligned, no overlap possible.
return range(start1, step=a, length=0)
end
Expand Down
10 changes: 10 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,16 @@ 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]

@testset "Support StepRange with a non-numeric step" begin
start = Date(1914, 7, 28)
stop = Date(1918, 11, 11)

@test intersect(start:Day(1):stop, start:Day(1):stop) == start:Day(1):stop
@test intersect(start:Day(1):stop, start:Day(5):stop) == start:Day(5):stop
@test intersect(start-Day(10):Day(1):stop-Day(10), start:Day(5):stop) ==
start:Day(5):stop-Day(10)-mod(stop-start, Day(5))
end
end
@testset "issubset" begin
@test issubset(1:3, 1:typemax(Int)) #32461
Expand Down

2 comments on commit a0936f9

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

Please sign in to comment.