Skip to content

Commit

Permalink
Fix equality for one-element ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
sostock committed May 29, 2020
1 parent 7c980c6 commit 7385876
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
27 changes: 21 additions & 6 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -741,14 +741,29 @@ show(io::IO, r::AbstractRange) = print(io, repr(first(r)), ':', repr(step(r)), '
show(io::IO, r::UnitRange) = print(io, repr(first(r)), ':', repr(last(r)))
show(io::IO, r::OneTo) = print(io, "Base.OneTo(", r.stop, ")")

==(r::T, s::T) where {T<:AbstractRange} =
(isempty(r) & isempty(s)) | ((first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s)))
==(r::OrdinalRange, s::OrdinalRange) =
(isempty(r) & isempty(s)) | ((first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s)))
function ==(r::T, s::T) where {T<:AbstractRange}
isempty(r) && return isempty(s)
_has_length_one(r) && return _has_length_one(s) & (first(r) == first(s))
(first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s))
end

function ==(r::OrdinalRange, s::OrdinalRange)
isempty(r) && return isempty(s)
_has_length_one(r) && return _has_length_one(s) & (first(r) == first(s))
(first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s))
end

==(r::T, s::T) where {T<:Union{StepRangeLen,LinRange}} =
(isempty(r) & isempty(s)) | ((first(r) == first(s)) & (length(r) == length(s)) & (last(r) == last(s)))
==(r::Union{StepRange{T},StepRangeLen{T,T}}, s::Union{StepRange{T},StepRangeLen{T,T}}) where {T} =
(isempty(r) & isempty(s)) | ((first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s)))

function ==(r::Union{StepRange{T},StepRangeLen{T,T}}, s::Union{StepRange{T},StepRangeLen{T,T}}) where {T}
isempty(r) && return isempty(s)
_has_length_one(r) && return _has_length_one(s) & (first(r) == first(s))
(first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s))
end

_has_length_one(r::OrdinalRange) = first(r) == last(r)
_has_length_one(r::AbstractRange) = isone(length(r))

function ==(r::AbstractRange, s::AbstractRange)
lr = length(r)
Expand Down
4 changes: 3 additions & 1 deletion test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,9 @@ end
0.0:0.1:1.0, map(Float32,0.0:0.1:1.0),map(Float32,LinRange(0.0, 1.0, 11)),
1.0:eps():1.0 .+ 10eps(), 9007199254740990.:1.0:9007199254740994,
range(0, stop=1, length=20), map(Float32, range(0, stop=1, length=20)),
3:2, 5:-2:7, range(0.0, step=2.0, length=0), 3//2:3//2:0//1, LinRange(2,3,0)]
3:2, 5:-2:7, range(0.0, step=2.0, length=0), 3//2:3//2:0//1, LinRange(2,3,0),
Base.OneTo(1), 1:1, 1:-3:1, 1//1:1//3:1//1, range(1.0, step=2.5, length=1),
LinRange(1,1,1), LinRange(1,1,2)]
for r in Rs
local r
ar = Vector(r)
Expand Down

0 comments on commit 7385876

Please sign in to comment.