Skip to content

Commit

Permalink
Fix findfirst for InfStepRange for negative step
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Aug 27, 2024
1 parent 83860f6 commit eb992be
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/infrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ MemoryLayout(::Type{<:AbstractInfUnitRange}) = LazyLayout()

# from array.jl
function _step_findfirst(p, r::InfStepRange{T,S}) where {T,S}
first(r) <= p.x || return nothing
first(r) <= p.x && step(r) > zero(step(r)) || first(r) >= p.x && step(r) < zero(step(r)) || return nothing
d = convert(S, p.x - first(r))
iszero(d % step(r)) || return nothing
return d ÷ step(r) + 1
Expand Down Expand Up @@ -624,4 +624,9 @@ function inv(D::Diagonal{<:Any, <:InfRanges})
idx = findfirst(iszero, d)
isnothing(idx) || throw(SingularException(idx))
return Diagonal(inv.(d))
end
end

# bounds-checking
function Base.checkindex(::Type{Bool}, inds::NTuple{N, AbstractInfUnitRange}, i::AbstractRange{CartesianIndex{N}}) where {N}
isempty(i) | checkindex(Bool, inds, first(i))

Check warning on line 631 in src/infrange.jl

View check run for this annotation

Codecov / codecov/patch

src/infrange.jl#L630-L631

Added lines #L630 - L631 were not covered by tests
end
12 changes: 11 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1229,5 +1229,15 @@ end
@test v[1:∞] == v
end

@testset "issue #180" begin
@test isnothing(findfirst(==(21), 10:-1:-∞))
@test isnothing(findfirst(==(11), 10:-1:-∞))
@test findfirst(==(9), 10:-1:-∞) == 2
r = 10:-1:-
v = -20
ind = findfirst(==(v), r)
@test r[ind] == v
end

include("test_infconv.jl")
include("test_block.jl")
include("test_block.jl")

0 comments on commit eb992be

Please sign in to comment.