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

Support intervals endpoints using infinity #99

Merged
merged 7 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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: 13 additions & 3 deletions src/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ Interval(interval::AbstractInterval) = convert(Interval, interval)
Interval{T}(interval::AbstractInterval) where T = convert(Interval{T}, interval)

# Endpoint constructors
function Interval{T}(left::LeftEndpoint{T}, right::RightEndpoint{T}) where T
Interval{T}(left.endpoint, right.endpoint, left.included, right.included)
function Interval{T}(left::LeftEndpoint, right::RightEndpoint) where T
Interval{T}(T(left.endpoint), T(right.endpoint), left.included, right.included)
end

Interval(left::LeftEndpoint{T}, right::RightEndpoint{T}) where T = Interval{T}(left, right)
function Interval(left::LeftEndpoint{S}, right::RightEndpoint{T}) where {S, T}
Interval{promote_type(S, T)}(left, right)
end

# Empty Intervals
Interval{T}() where T = Interval{T}(zero(T), zero(T), Inclusivity(false, false))
Expand Down Expand Up @@ -277,6 +279,14 @@ function Base.intersect(a::AbstractInterval{T}, b::AbstractInterval{T}) where T
return Interval{T}(left, right)
end

function Base.intersect(a::AbstractInterval{S}, b::AbstractInterval{T}) where {S, T}
!overlaps(a, b) && return Interval{promote_type(S, T)}()
left = max(LeftEndpoint(a), LeftEndpoint(b))
right = min(RightEndpoint(a), RightEndpoint(b))

return Interval(left, right)
end
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll note this implementation could probably replace the intersect(::AbstractInterval{T}, ::AbstractInterval{T}) defined above this

Copy link
Member

Choose a reason for hiding this comment

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

when you define a function with S, and T does julia enforce that it will only be called if S and T are different? or can they be the same?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As there is no constraint between them they can be anything including the same. Since there a more specific definition where they have to be the same then this method will only be called when the are different.


# There is power in a union.
"""
union(intervals::AbstractVector{<:AbstractInterval})
Expand Down
Loading