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

Handle empty-interval edge case in find_intersections #201

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 8 additions & 8 deletions src/interval_sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -469,16 +469,16 @@ function intersection_isless_fn(::TrackEachEndpoint)
end

"""
find_intersections(
x::AbstractVector{<:AbstractInterval},
y::AbstractVector{<:AbstractInterval}
)
find_intersections(x, y)
haberdashPI marked this conversation as resolved.
Show resolved Hide resolved

Returns a `Vector{Vector{Int}}` where the value at index `i` gives the indices to all
intervals in `y` that intersect with `x[i]`.
For each interval in `x` find all intervals in `y` that it intersects with. Returns a
vector, `z`, where each element `z[i]` is a vector of all indices in `y` that intersect with
`x[i]`. The values `x` and `y` should be iterables of intervals.
"""
find_intersections(x, y) = find_intersections(vcat(x), vcat(y))
function find_intersections(x::AbstractVector{<:AbstractInterval}, y::AbstractVector{<:AbstractInterval})
find_intersections(x, y) = find_intersections_(collect(x), collect(y))
find_intersections(x::AbstractVector, y::AbstractVector) = find_intersections_(x, y)
function find_intersections_(x::AbstractVector, y::AbstractVector)
(isempty(x) || isempty(y)) && return Vector{Int}[]
tracking = endpoint_tracking(x, y)
lt = intersection_isless_fn(tracking)
x_endpoints = unbunch(enumerate(x), tracking; lt)
Expand Down
1 change: 0 additions & 1 deletion test/anchoredinterval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,6 @@ using Intervals: Bounded, Ending, Beginning, canonicalize, isunbounded
@testset "StepRangeLen" begin
r = StepRangeLen(AnchoredInterval{-1}(1), 1, 5)

# https://github.com/JuliaLang/julia/issues/33882
@test r isa StepRangeLen
@test first(r) == AnchoredInterval{-1}(1)
@test step(r) == 1
Expand Down
9 changes: 9 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ end
@test !issubset(11, IntervalSet([1..3, 5..10]))
@test issubset(2, IntervalSet([1.0 .. 3.0, 5.0 .. 10.0]))

# verify the various input types `find_intersections` can handle
@test isempty(find_intersections([], []))
@test_throws MethodError find_intersections([1], [2])
@test isempty(find_intersections(Interval[], Interval[]))
@test isempty(find_intersections([1..3], []))
@test isempty(find_intersections([], [1..3]))
@test !isempty(find_intersections([1..2, 3..4],
AnchoredInterval{-1}(1):1:AnchoredInterval{-1}(5)))

function testsets(a, b)
@test area(a ∪ b) ≤ area(myunion(a)) + area(myunion(b))
@test area(setdiff(a, b)) ≤ area(myunion(a))
Expand Down