Skip to content

Commit eb992be

Browse files
committed
Fix findfirst for InfStepRange for negative step
1 parent 83860f6 commit eb992be

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/infrange.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ MemoryLayout(::Type{<:AbstractInfUnitRange}) = LazyLayout()
571571

572572
# from array.jl
573573
function _step_findfirst(p, r::InfStepRange{T,S}) where {T,S}
574-
first(r) <= p.x || return nothing
574+
first(r) <= p.x && step(r) > zero(step(r)) || first(r) >= p.x && step(r) < zero(step(r)) || return nothing
575575
d = convert(S, p.x - first(r))
576576
iszero(d % step(r)) || return nothing
577577
return d ÷ step(r) + 1
@@ -624,4 +624,9 @@ function inv(D::Diagonal{<:Any, <:InfRanges})
624624
idx = findfirst(iszero, d)
625625
isnothing(idx) || throw(SingularException(idx))
626626
return Diagonal(inv.(d))
627-
end
627+
end
628+
629+
# bounds-checking
630+
function Base.checkindex(::Type{Bool}, inds::NTuple{N, AbstractInfUnitRange}, i::AbstractRange{CartesianIndex{N}}) where {N}
631+
isempty(i) | checkindex(Bool, inds, first(i))
632+
end

test/runtests.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,5 +1229,15 @@ end
12291229
@test v[1:∞] == v
12301230
end
12311231

1232+
@testset "issue #180" begin
1233+
@test isnothing(findfirst(==(21), 10:-1:-∞))
1234+
@test isnothing(findfirst(==(11), 10:-1:-∞))
1235+
@test findfirst(==(9), 10:-1:-∞) == 2
1236+
r = 10:-1:-
1237+
v = -20
1238+
ind = findfirst(==(v), r)
1239+
@test r[ind] == v
1240+
end
1241+
12321242
include("test_infconv.jl")
1233-
include("test_block.jl")
1243+
include("test_block.jl")

0 commit comments

Comments
 (0)