Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rev=true in searchsorted_interval #111

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 14 additions & 45 deletions src/findall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,45 +34,8 @@ julia> findall(in(Interval{:open,:closed}(1,6)), y) # (1,6], does not include 1
5:14
```
"""
function Base.findall(interval_d::Base.Fix2{typeof(in),Interval{L,R,T}}, x::AbstractRange) where {L,R,T}
isempty(x) && return 1:0

interval = interval_d.x
il, ir = firstindex(x), lastindex(x)
δx = step(x)
a,b = if δx < zero(δx)
rev = findall(in(interval), reverse(x))
isempty(rev) && return rev

a = (il+ir)-last(rev)
b = (il+ir)-first(rev)

a,b
else
lx, rx = first(x), last(x)
l = max(leftendpoint(interval), lx - oneunit(δx))
r = min(rightendpoint(interval), rx + oneunit(δx))

(l > rx || r < lx) && return 1:0

a = il + max(0, round(Int, cld(l-lx, δx)))
a += (a ≤ ir && (x[a] == l && L == :open || x[a] < l))

b = min(ir, round(Int, cld(r-lx, δx)) + il)
b -= (b ≥ il && (x[b] == r && R == :open || x[b] > r))

a,b
end
# Reversing a range could change sign of values close to zero (cf
# sign of the smallest element in x and reverse(x), where x =
# range(BigFloat(-0.5),stop=BigFloat(1.0),length=10)), or more
# generally push elements in or out of the interval (as can cld),
# so we need to check once again.
a += +(a < ir && x[a] ∉ interval) - (il < a && x[a-1] ∈ interval)
b += -(il < b && x[b] ∉ interval) + (b < ir && x[b+1] ∈ interval)

a:b
end
Base.findall(interval_d::Base.Fix2{typeof(in), <:Interval}, x::AbstractRange) =
searchsorted_interval(x, interval_d.x; rev=step(x) < zero(step(x)))

# We overload Base._findin to avoid an ambiguity that arises with
# Base.findall(interval_d::Base.Fix2{typeof(in),Interval{L,R,T}}, x::AbstractArray)
Expand All @@ -85,7 +48,7 @@ function Base._findin(a::Union{AbstractArray, Tuple}, b::Interval)
end

"""
searchsorted_interval(a, i::Interval)
searchsorted_interval(a, i::Interval; [rev=false])

Return the range of indices of `a` which is inside of the interval `i` (using binary search), assuming that
`a` is already sorted. Return an empty range located at the insertion point if a does not contain values in `i`.
Expand All @@ -102,9 +65,15 @@ julia> searchsorted_interval(Float64[], 1..3)
1:0
```
"""
function searchsorted_interval end
function searchsorted_interval(X, i::Interval{L, R}; rev=false) where {L, R}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need support for rev::Nothing, just like searchsorted?

julia> searchsorted([1,2,3,3,4],3,rev=nothing)
3:4

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be fixed in another PR.

if rev === true
_searchsorted_begin(X, rightendpoint(i), Val(R); rev):_searchsorted_end(X, leftendpoint(i), Val(L); rev)
else
_searchsorted_begin(X, leftendpoint(i), Val(L); rev):_searchsorted_end(X, rightendpoint(i), Val(R); rev)
end
end

searchsorted_interval(X, i::Interval{:closed, :closed}) = searchsortedfirst(X, leftendpoint(i)) :searchsortedlast(X, rightendpoint(i))
searchsorted_interval(X, i::Interval{:closed, :open}) = searchsortedfirst(X, leftendpoint(i)) :(searchsortedfirst(X, rightendpoint(i)) - 1)
searchsorted_interval(X, i::Interval{ :open, :closed}) = (searchsortedlast(X, leftendpoint(i)) + 1):searchsortedlast(X, rightendpoint(i))
searchsorted_interval(X, i::Interval{ :open, :open}) = (searchsortedlast(X, leftendpoint(i)) + 1):(searchsortedfirst(X, rightendpoint(i)) - 1)
_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
aplavin marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions test/findall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ end
end
end

@testset "zero-step ranges" begin
r = range(1, 1, length=10)
@test findall(in(1..3),r) == 1:10
@test findall(in(2..3),r) |> isempty
end

@testset "searchsorted" begin
x = [-10, 0, 1, 1 + eps(), 1.2, 1.5, 1.9, 2 - eps(), 2]
@test searchsorted_interval(x, -Inf..Inf) == 1:9
Expand Down