Skip to content

Commit

Permalink
Fix reverse and intersect for StepRange without length. Fix compariso…
Browse files Browse the repository at this point in the history
…n for OrdinalRange
  • Loading branch information
mschauer committed Feb 7, 2015
1 parent 5819644 commit 13b2d65
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 4 additions & 3 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ end
show(io::IO, r::UnitRange) = print(io, repr(first(r)), ':', repr(last(r)))

=={T<:Range}(r::T, s::T) = (first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s))
==(r::OrdinalRange, s::OrdinalRange) = (first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s))

function ==(r::Range, s::Range)
lr = length(r)
Expand All @@ -329,7 +330,7 @@ intersect{T<:Integer}(i::Integer, r::UnitRange{T}) =
intersect{T<:Integer}(r::UnitRange{T}, i::Integer) = intersect(i, r)

function intersect{T1<:Integer, T2<:Integer}(r::UnitRange{T1}, s::StepRange{T2})
if length(s) == 0
if isempty(s)
range(first(r), 0)
elseif step(s) == 0
intersect(first(s), r)
Expand All @@ -356,7 +357,7 @@ function intersect{T1<:Integer, T2<:Integer}(r::StepRange{T1}, s::UnitRange{T2})
end

function intersect(r::StepRange, s::StepRange)
if length(r) == 0 || length(s) == 0
if isempty(r) || isempty(s)
return range(first(r), step(r), 0)
elseif step(s) < 0
return intersect(r, reverse(s))
Expand Down Expand Up @@ -523,7 +524,7 @@ function vcat{T}(rs::Range{T}...)
return a
end

reverse(r::OrdinalRange) = range(last(r), -step(r), length(r))
reverse(r::OrdinalRange) = colon(last(r), -step(r), first(r))
reverse(r::FloatRange) = FloatRange(r.start + (r.len-1)*r.step, -r.step, r.len, r.divisor)

## sorting ##
Expand Down
11 changes: 11 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ end
#@test isempty(findin(5+0*(1:6), 6:7))
#@test findin(5+0*(1:6), 5:5) == 1:6

@test reverse(reverse(1:10)) == 1:10

@test reverse(reverse(typemin(Int):typemax(Int))) == typemin(Int):typemax(Int)
@test reverse(reverse(typemin(Int):2:typemax(Int))) == typemin(Int):2:typemax(Int)

@test intersect(1:5, 2:3) == 2:3
@test intersect(-3:5, 2:8) == 2:5
@test intersect(-8:-3, -8:-3) == -8:-3
Expand Down Expand Up @@ -95,6 +100,12 @@ end
#@test isempty(intersect(6+0*(0:6:24), 0:4:24))
@test intersect(-10:3:24, -10:3:24) == -10:3:23
@test isempty(intersect(-11:3:24, -10:3:24))
@test intersect(typemin(Int):2:typemax(Int),1:10) == 2:2:10
@test intersect(1:10,typemin(Int):2:typemax(Int)) == 2:2:10

@test intersect(reverse(typemin(Int):2:typemax(Int)),typemin(Int):2:typemax(Int)) == reverse(typemin(Int):2:typemax(Int))
@test intersect(typemin(Int):2:typemax(Int),reverse(typemin(Int):2:typemax(Int))) == typemin(Int):2:typemax(Int)


@test !(3.5 in 1:5)
@test (3 in 1:5)
Expand Down

0 comments on commit 13b2d65

Please sign in to comment.