Skip to content

Commit

Permalink
improve searchsorted_interval performance (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin authored Jan 1, 2025
1 parent 56a55ca commit 280ad55
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/findall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ julia> searchsorted_interval(Float64[], 1..3)
```
"""
function searchsorted_interval(X, i::Interval{L, R}; rev=false) where {L, R}
ord = Base.Order.ord(<, identity, rev)
if rev === true
_searchsorted_begin(X, rightendpoint(i), Val(R); rev):_searchsorted_end(X, leftendpoint(i), Val(L); rev)
_searchsorted_begin(X, rightendpoint(i), Val(R), ord):_searchsorted_end(X, leftendpoint(i), Val(L), ord)
else
_searchsorted_begin(X, leftendpoint(i), Val(L); rev):_searchsorted_end(X, rightendpoint(i), Val(R); rev)
_searchsorted_begin(X, leftendpoint(i), Val(L), ord):_searchsorted_end(X, rightendpoint(i), Val(R), ord)
end
end

_searchsorted_begin(X, x, ::Val{:closed}; rev) = searchsortedfirst(X, x; rev, lt=<)
_searchsorted_begin(X, x, ::Val{:open}; rev) = searchsortedlast(X, x; rev, lt=<) + 1
_searchsorted_end(X, x, ::Val{:closed}; rev) = searchsortedlast(X, x; rev, lt=<)
_searchsorted_end(X, x, ::Val{:open}; rev) = searchsortedfirst(X, x; rev, lt=<) - 1
_searchsorted_begin(X, x, ::Val{:closed}, ord) = searchsortedfirst(X, x, ord)
_searchsorted_begin(X, x, ::Val{:open}, ord) = searchsortedlast(X, x, ord) + 1
_searchsorted_end(X, x, ::Val{:closed}, ord) = searchsortedlast(X, x, ord)
_searchsorted_end(X, x, ::Val{:open}, ord) = searchsortedfirst(X, x, ord) - 1

0 comments on commit 280ad55

Please sign in to comment.