Skip to content

Commit

Permalink
Add superset
Browse files Browse the repository at this point in the history
  • Loading branch information
omus committed Jul 17, 2018
1 parent b07ac86 commit 4cd278a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Intervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ export AbstractInterval,
merge,
union,
union!,
superset,
.., , , , , ,
end
12 changes: 12 additions & 0 deletions src/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,18 @@ function Base.union!(intervals::Union{AbstractVector{<:Interval}, AbstractVector
return intervals
end

"""
superset(intervals::AbstractArray{<:AbstractInterval}) -> Interval
Create the smallest single interval which encompasses all of the provided intervals.
"""
function superset(intervals::AbstractArray{<:AbstractInterval})
left = minimum(LeftEndpoint.(intervals))
right = maximum(RightEndpoint.(intervals))

return Interval(left, right)
end

function Base.merge(a::AbstractInterval, b::AbstractInterval)
if !isoverlapping(a, b) && !iscontiguous(a, b)
throw(ArgumentError("$a and $b are neither overlapping or contiguous."))
Expand Down
14 changes: 14 additions & 0 deletions test/comparisons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
@test union([earlier, later]) == [earlier, later]
@test !isoverlapping(earlier, later)
@test !iscontiguous(earlier, later)
@test superset([earlier, later]) == Interval(1, 5, true, true)
end

# Compare two intervals which "touch" but both intervals do not include that point:
Expand Down Expand Up @@ -74,6 +75,7 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
@test union([earlier, later]) == [earlier, later]
@test !isoverlapping(earlier, later)
@test !iscontiguous(earlier, later)
@test superset([earlier, later]) == Interval(1, 5, false, false)
end

# Compare two intervals which "touch" and the later interval includes that point:
Expand Down Expand Up @@ -106,6 +108,7 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
@test union([earlier, later]) == [Interval(1, 5, false, true)]
@test !isoverlapping(earlier, later)
@test iscontiguous(earlier, later)
@test superset([earlier, later]) == Interval(1, 5, false, true)
end

# Compare two intervals which "touch" and the earlier interval includes that point:
Expand Down Expand Up @@ -138,6 +141,7 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
@test union([earlier, later]) == [Interval(1, 5, true, false)]
@test !isoverlapping(earlier, later)
@test iscontiguous(earlier, later)
@test superset([earlier, later]) == Interval(1, 5, true, false)
end

# Compare two intervals which "touch" and both intervals include that point:
Expand Down Expand Up @@ -170,6 +174,7 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
@test union([earlier, later]) == [Interval(1, 5, true, true)]
@test isoverlapping(earlier, later)
@test !iscontiguous(earlier, later)
@test superset([earlier, later]) == Interval(1, 5, true, true)
end

# Compare two intervals which overlap
Expand Down Expand Up @@ -202,6 +207,7 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
@test union([earlier, later]) == [Interval(1, 5, true, true)]
@test isoverlapping(earlier, later)
@test !iscontiguous(earlier, later)
@test superset([earlier, later]) == Interval(1, 5, true, true)
end

@testset "equal ()/()" begin
Expand Down Expand Up @@ -229,6 +235,7 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
@test union([a, b]) == [Interval(1, 5, false, false)]
@test isoverlapping(a, b)
@test !iscontiguous(a, b)
@test superset([a, b]) == Interval(1, 5, false, false)
end

@testset "equal [)/()" begin
Expand Down Expand Up @@ -256,6 +263,7 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
@test union([a, b]) == [Interval(1, 5, true, false)]
@test isoverlapping(a, b)
@test !iscontiguous(a, b)
@test superset([a, b]) == Interval(1, 5, true, false)
end

@testset "equal (]/()" begin
Expand Down Expand Up @@ -283,6 +291,7 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
@test union([a, b]) == [Interval(1, 5, false, true)]
@test isoverlapping(a, b)
@test !iscontiguous(a, b)
@test superset([a, b]) == Interval(1, 5, false, true)
end

@testset "equal []/()" begin
Expand Down Expand Up @@ -310,6 +319,7 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
@test union([a, b]) == [Interval(1, 5, true, true)]
@test isoverlapping(a, b)
@test !iscontiguous(a, b)
@test superset([a, b]) == Interval(1, 5, true, true)
end

@testset "equal [)/[]" begin
Expand Down Expand Up @@ -337,6 +347,7 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
@test union([a, b]) == [Interval(1, 5, true, true)]
@test isoverlapping(a, b)
@test !iscontiguous(a, b)
@test superset([a, b]) == Interval(1, 5, true, true)
end

@testset "equal (]/[]" begin
Expand Down Expand Up @@ -364,6 +375,7 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
@test union([a, b]) == [Interval(1, 5, true, true)]
@test isoverlapping(a, b)
@test !iscontiguous(a, b)
@test superset([a, b]) == Interval(1, 5, true, true)
end

@testset "equal []/[]" begin
Expand Down Expand Up @@ -391,6 +403,7 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
@test union([a, b]) == [Interval(1, 5, true, true)]
@test isoverlapping(a, b)
@test !iscontiguous(a, b)
@test superset([a, b]) == Interval(1, 5, true, true)
end

# Compare two intervals where the first interval is contained by the second
Expand Down Expand Up @@ -423,5 +436,6 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
@test union([smaller, larger]) == [Interval(larger)]
@test isoverlapping(smaller, larger)
@test !iscontiguous(smaller, larger)
@test superset([smaller, larger]) == Interval(1, 5, true, true)
end
end

0 comments on commit 4cd278a

Please sign in to comment.