-
Notifications
You must be signed in to change notification settings - Fork 18
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
Fix union and union! of intervals #74
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -279,22 +279,24 @@ end | |
|
||
# There is power in a union. | ||
""" | ||
union(intervals::AbstractVector{<:AbstractInterval}) | ||
union(intervals::Union{AbstractInterval, AbstractVector{<:AbstractInterval}}, itrs...) | ||
|
||
Flattens a vector of overlapping intervals into a new, smaller vector containing only | ||
Flattens sets of overlapping intervals into a new, smaller vector containing only | ||
non-overlapping intervals. | ||
""" | ||
function Base.union(intervals::AbstractVector{<:AbstractInterval}) | ||
return union!(convert(Vector{AbstractInterval}, intervals)) | ||
function Base.union( | ||
interval::Union{AbstractInterval, AbstractVector{<:AbstractInterval}}, intervals..., | ||
) | ||
return union!(AbstractInterval[interval; intervals...]) | ||
end | ||
omus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
""" | ||
union!(intervals::AbstractVector{<:Union{Interval, AbstractInterval}}) | ||
function Base.union!(intervals::AbstractVector{T}) where T <: AbstractInterval | ||
if !(T <: Interval) && T != AbstractInterval | ||
throw(ArgumentError( | ||
"Cannot `union!` array of intervals of type $T as the type may change" | ||
)) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to use dispatch here for future interval types can support their own function Base.union!(intervals::AbstractVector{<:AbstractInterval})
throw(ArgumentError("Cannot `union!` array of intervals of type $(eltype(intervals)) as the type may change"))
end
Base.union!(intervals::AbstractVector{<:Union{AbstractInterval,Interval}) = ... (I think this works) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't the existing definition already allow that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think my point here was that this approach in the PR isn't the best for new interval types that want to define their own |
||
|
||
Flattens a vector of overlapping intervals in-place to be a smaller vector containing only | ||
non-overlapping intervals. | ||
""" | ||
function Base.union!(intervals::Union{AbstractVector{<:Interval}, AbstractVector{AbstractInterval}}) | ||
sort!(intervals) | ||
|
||
i = 2 | ||
|
@@ -319,6 +321,28 @@ function Base.union!(intervals::Union{AbstractVector{<:Interval}, AbstractVector | |
return intervals | ||
end | ||
|
||
""" | ||
union!(intervals::AbstractVector{<:Union{Interval, AbstractInterval}}, itrs...) | ||
|
||
Flattens sets of overlapping intervals in-place into the first argument, making it a smaller | ||
vector containing only non-overlapping intervals. | ||
""" | ||
function union!( | ||
intervals::AbstractVector{<:AbstractInterval}, | ||
interval_varargs... | ||
) | ||
# imitates the behaviour of vcat/promote_eltypeof | ||
for interval_vararg in interval_varargs | ||
if interval_vararg isa AbstractInterval | ||
push!(intervals, interval_vararg) | ||
else | ||
append!(intervals, interval_vararg) | ||
end | ||
end | ||
|
||
return union!(intervals) | ||
end | ||
|
||
""" | ||
superset(intervals::AbstractArray{<:AbstractInterval}) -> Interval | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we do:
Avoids a call such as
union(Interval(1,2), 3)
causing a confusing error