Skip to content

Commit

Permalink
add searchsorted_interval (#109)
Browse files Browse the repository at this point in the history
* add searchsorted_interval





Co-authored-by: hyrodium <[email protected]>

* bump version

Co-authored-by: hyrodium <[email protected]>
  • Loading branch information
aplavin and hyrodium authored May 31, 2022
1 parent 021cf45 commit b3f0d6e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "IntervalSets"
uuid = "8197267c-284f-5f27-9208-e0e47529a953"
version = "0.7.0"
version = "0.7.1"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
3 changes: 2 additions & 1 deletion src/IntervalSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export AbstractInterval, Interval, OpenInterval, ClosedInterval,
, .., ±, ordered, width, leftendpoint, rightendpoint, endpoints,
isopenset, isclosedset, isleftclosed, isrightclosed,
isleftopen, isrightopen, closedendpoints,
infimum, supremum
infimum, supremum,
searchsorted_interval

"""
A subtype of `Domain{T}` represents a subset of type `T`, that provides `in`.
Expand Down
25 changes: 25 additions & 0 deletions src/findall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,28 @@ function Base._findin(a::Union{AbstractArray, Tuple}, b::Interval)
end
ind
end

"""
searchsorted_interval(a, i::Interval)
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`.
# Examples
```jldoctest
julia> searchsorted_interval([1,2,3,5], 2..4)
2:3
julia> searchsorted_interval([1,2,3,5], 4..1)
4:3
julia> searchsorted_interval(Float64[], 1..3)
1:0
```
"""
function searchsorted_interval 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)
29 changes: 29 additions & 0 deletions test/findall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,33 @@ end
assert_in_interval(reverse(x), interval)
end
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
i = Interval{:closed, :closed}(1, 2)
@test searchsorted_interval(x, i) == findall(in(i),x) == 3:9
i = Interval{:open , :closed}(1, 2)
@test searchsorted_interval(x, i) == findall(in(i),x) == 4:9
i = Interval{:closed, :open }(1, 2)
@test searchsorted_interval(x, i) == findall(in(i),x) == 3:8
i = Interval{:open , :open }(1, 2)
@test searchsorted_interval(x, i) == findall(in(i),x) == 4:8
i = Interval{:closed, :closed}(1, 1)
@test searchsorted_interval(x, i) == findall(in(i),x) == 3:3
i = Interval{:open , :closed}(1, 1)
@test searchsorted_interval(x, i) == findall(in(i),x) == 3:2
i = Interval{:closed, :open }(1, 1)
@test searchsorted_interval(x, i) == findall(in(i),x) == 3:2
i = Interval{:open , :open }(1, 1)
@test searchsorted_interval(x, i) == findall(in(i),x) == 3:2
i = Interval{:closed, :closed}(2, 1)
@test searchsorted_interval(x, i) == findall(in(i),x) == 9:8
i = Interval{:open , :closed}(2, 1)
@test searchsorted_interval(x, i) == findall(in(i),x) == 9:8
i = Interval{:closed, :open }(2, 1)
@test searchsorted_interval(x, i) == findall(in(i),x) == 9:8
i = Interval{:open , :open }(2, 1)
@test searchsorted_interval(x, i) == findall(in(i),x) == 9:8
end
end

2 comments on commit b3f0d6e

@hyrodium
Copy link
Collaborator

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/61401

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.1 -m "<description of version>" b3f0d6ebcb8718f2e14168280237ea014a01d8f6
git push origin v0.7.1

Please sign in to comment.