Skip to content

Commit

Permalink
Core support for unbounded AnchoredInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
omus committed Jun 10, 2020
1 parent 9e8525e commit 54d8f1a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
28 changes: 22 additions & 6 deletions src/anchoredinterval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ span(interval::AnchoredInterval{P}) where P = abs(P)

##### CONVERSION #####

function Base.convert(::Type{Interval}, interval::AnchoredInterval{P,T}) where {P,T}
return Interval{T}(first(interval), last(interval), inclusivity(interval))
function Base.convert(::Type{Interval}, interval::AnchoredInterval{P,T,L,R}) where {P,T,L,R}
return Interval{T,L,R}(first(interval), last(interval))
end

function Base.convert(::Type{Interval{T}}, interval::AnchoredInterval{P,T}) where {P,T}
return Interval{T}(first(interval), last(interval), inclusivity(interval))
function Base.convert(::Type{Interval{T}}, interval::AnchoredInterval{P,T,L,R}) where {P,T,L,R}
return Interval{T,L,R}(first(interval), last(interval))
end

# Conversion methods which currently aren't needed but could prove useful. Commented out
Expand All @@ -161,12 +161,25 @@ function Base.convert(::Type{AnchoredInterval{P}}, interval::Interval{T}) where
end
=#

one(T::Type{<:TimeType}) = eps(T)
one(x) = Base.one(x)

function Base.convert(::Type{AnchoredInterval{Ending}}, interval::Interval{T}) where {T}
AnchoredInterval{-span(interval), T}(last(interval), inclusivity(interval))
left, right = LeftEndpoint(interval), RightEndpoint(interval)
if isunbounded(right)
throw(ArgumentError("Unable to represent a right-unbounded interval using a `AnchoredInterval{Ending}`"))
end
s = isunbounded(left) ? one(T) : span(interval)
return AnchoredInterval{-s, T, bound(left), bound(right)}(last(interval))
end

function Base.convert(::Type{AnchoredInterval{Beginning}}, interval::Interval{T}) where {T}
AnchoredInterval{span(interval), T}(first(interval), inclusivity(interval))
left, right = LeftEndpoint(interval), RightEndpoint(interval)
if isunbounded(left)
throw(ArgumentError("Unable to represent a left-unbounded interval using a `AnchoredInterval{Beginning}`"))
end
s = isunbounded(right) ? one(T) : span(interval)
return AnchoredInterval{s, T, bound(left), bound(right)}(first(interval))
end

##### DISPLAY #####
Expand Down Expand Up @@ -246,6 +259,8 @@ function Base.isempty(interval::AnchoredInterval{P,T}) where {P,T}
return P == zero(P) && !isclosed(interval)
end

# TODO: Not required but should be addressed
#=
function Base.intersect(a::AnchoredInterval{P,T}, b::AnchoredInterval{Q, T}) where {P,Q,T}
interval = invoke(intersect, Tuple{AbstractInterval{T}, AbstractInterval{T}}, a, b)
Expand All @@ -260,6 +275,7 @@ function Base.intersect(a::AnchoredInterval{P,T}, b::AnchoredInterval{Q, T}) whe
return AnchoredInterval{new_P, T}(anchor, inclusivity(interval))
end
=#

##### UTILITIES #####

Expand Down
32 changes: 16 additions & 16 deletions test/comparisons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
)

@testset "$a vs. $b" for (a, b) in test_intervals
A <: AnchoredInterval && isunbounded(a) && continue
B <: AnchoredInterval && isunbounded(b) && continue
A == AnchoredInterval{Beginning} && isunbounded(LeftEndpoint(a)) && continue
B == AnchoredInterval{Ending} && isunbounded(RightEndpoint(b)) && continue
A == AnchoredInterval{Beginning} && !isfinite(first(a)) && continue
B == AnchoredInterval{Ending} && !isfinite(last(b)) && continue

Expand Down Expand Up @@ -88,8 +88,8 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
)

@testset "$a vs. $b" for (a, b) in test_intervals
A <: AnchoredInterval && isunbounded(a) && continue
B <: AnchoredInterval && isunbounded(b) && continue
A == AnchoredInterval{Beginning} && isunbounded(LeftEndpoint(a)) && continue
B == AnchoredInterval{Ending} && isunbounded(RightEndpoint(b)) && continue
A == AnchoredInterval{Beginning} && !isfinite(first(a)) && continue
B == AnchoredInterval{Ending} && !isfinite(last(b)) && continue

Expand Down Expand Up @@ -143,8 +143,8 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
)

@testset "$a vs. $b" for (a, b) in test_intervals
A <: AnchoredInterval && isunbounded(a) && continue
B <: AnchoredInterval && isunbounded(b) && continue
A == AnchoredInterval{Beginning} && isunbounded(LeftEndpoint(a)) && continue
B == AnchoredInterval{Ending} && isunbounded(RightEndpoint(b)) && continue
A == AnchoredInterval{Beginning} && !isfinite(first(a)) && continue
B == AnchoredInterval{Ending} && !isfinite(last(b)) && continue

Expand Down Expand Up @@ -198,8 +198,8 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
)

@testset "$a vs $b" for (a, b) in test_intervals
A <: AnchoredInterval && isunbounded(a) && continue
B <: AnchoredInterval && isunbounded(b) && continue
A == AnchoredInterval{Beginning} && isunbounded(LeftEndpoint(a)) && continue
B == AnchoredInterval{Ending} && isunbounded(RightEndpoint(b)) && continue
A == AnchoredInterval{Beginning} && !isfinite(first(a)) && continue
B == AnchoredInterval{Ending} && !isfinite(last(b)) && continue

Expand Down Expand Up @@ -254,8 +254,8 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
)

@testset "$a vs $b" for (a, b) in test_intervals
A <: AnchoredInterval && isunbounded(a) && continue
B <: AnchoredInterval && isunbounded(b) && continue
A == AnchoredInterval{Beginning} && isunbounded(LeftEndpoint(a)) && continue
B == AnchoredInterval{Ending} && isunbounded(RightEndpoint(b)) && continue
A == AnchoredInterval{Beginning} && !isfinite(first(a)) && continue
B == AnchoredInterval{Ending} && !isfinite(last(b)) && continue

Expand Down Expand Up @@ -309,8 +309,8 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
)

@testset "$a vs. $b" for (a, b) in test_intervals
A <: AnchoredInterval && isunbounded(a) && continue
B <: AnchoredInterval && isunbounded(b) && continue
A == AnchoredInterval{Beginning} && isunbounded(LeftEndpoint(a)) && continue
B == AnchoredInterval{Ending} && isunbounded(RightEndpoint(b)) && continue
A == AnchoredInterval{Beginning} && !isfinite(first(a)) && continue
B == AnchoredInterval{Ending} && !isfinite(last(b)) && continue

Expand Down Expand Up @@ -714,8 +714,8 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
)

@testset "$a vs. $b" for (a, b) in test_intervals
A <: AnchoredInterval && isunbounded(a) && continue
B <: AnchoredInterval && isunbounded(b) && continue
A == AnchoredInterval{Beginning} && isunbounded(LeftEndpoint(a)) && continue
B == AnchoredInterval{Beginning} && isunbounded(LeftEndpoint(b)) && continue
A == AnchoredInterval{Beginning} && !isfinite(first(a)) && continue
B == AnchoredInterval{Beginning} && !isfinite(first(b)) && continue

Expand Down Expand Up @@ -848,8 +848,8 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
)

@testset "$a vs $b" for (a, b) in test_intervals
A <: AnchoredInterval && isunbounded(a) && continue
B <: AnchoredInterval && isunbounded(b) && continue
B == AnchoredInterval{Beginning} && isunbounded(LeftEndpoint(b)) && continue
B == AnchoredInterval{Ending} && isunbounded(RightEndpoint(b)) && continue
B == AnchoredInterval{Beginning} && !isfinite(first(b)) && continue
B == AnchoredInterval{Ending} && !isfinite(last(b)) && continue

Expand Down

0 comments on commit 54d8f1a

Please sign in to comment.