Skip to content

Commit

Permalink
Minimal update to AnchoredIntervals.
Browse files Browse the repository at this point in the history
  • Loading branch information
rofinn committed May 19, 2023
1 parent 5aaf170 commit 056e7f7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
16 changes: 8 additions & 8 deletions src/Intervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ include("isfinite.jl")
include("endpoint.jl")
include("interval.jl")
# include("interval_sets.jl")
# include("anchoredinterval.jl")
include("anchoredinterval.jl")
include("parse.jl")
# include("description.jl")
include("description.jl")
#include("plotting.jl")
#include("docstrings.jl")
#include("deprecated.jl")
Expand All @@ -73,17 +73,17 @@ export Bound,
AbstractInterval,
Interval,
IntervalSet,
# AnchoredInterval,
# HourEnding,
# HourBeginning,
# HE,
# HB,
AnchoredInterval,
HourEnding,
HourBeginning,
HE,
HB,
first,
last,
span,
bounds_types,
isclosed,
# anchor,
anchor,
merge,
union,
union!,
Expand Down
17 changes: 10 additions & 7 deletions src/anchoredinterval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ AnchoredInterval{Minute(5), DateTime, Closed, Closed}(DateTime("2016-08-11T12:30
See also: [`Interval`](@ref), [`HE`](@ref), [`HB`](@ref)
"""
struct AnchoredInterval{P, T, L <: Bounded, R <: Bounded} <: AbstractInterval{T,L,R}
struct AnchoredInterval{P, T, L <: Bounded, R <: Bounded} <: AbstractInterval{T}
anchor::T

function AnchoredInterval{P,T,L,R}(anchor::T) where {P, T, L <: Bounded, R <: Bounded}
Expand Down Expand Up @@ -174,6 +174,9 @@ end
anchor(interval::AnchoredInterval) = interval.anchor
span(interval::AnchoredInterval{P}) where P = abs(P)

bounds_int(interval::AnchoredInterval{P,T,L,R}) where {P,T,L,R} = bounds_int(L, R)
bounds_types(interval::AnchoredInterval{P,T,L,R}) where {P,T,L,R} = (L, R)

##### CONVERSION #####

# Allows an interval to be converted to a scalar when the set contained by the interval only
Expand All @@ -197,11 +200,11 @@ function Base.convert(::Type{T}, interval::AnchoredInterval{P,T}) where {P,T}
end

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))
return Interval{T}(first(interval), last(interval), (L, R))
end

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))
return Interval{T}(first(interval), last(interval), (L, R))
end

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

function Base.convert(::Type{AnchoredInterval{Ending}}, interval::Interval{T,L,R}) where {T,L,R}
function Base.convert(::Type{AnchoredInterval{Ending}}, interval::Interval{T}) where {T,L,R}
if !isbounded(interval)
throw(ArgumentError("Unable to represent a non-bounded interval using a `AnchoredInterval`"))
end
AnchoredInterval{-span(interval), T, L, R}(last(interval))
AnchoredInterval{-span(interval), T, bounds_types(interval)...}(last(interval))
end

function Base.convert(::Type{AnchoredInterval{Beginning}}, interval::Interval{T,L,R}) where {T,L,R}
function Base.convert(::Type{AnchoredInterval{Beginning}}, interval::Interval{T}) where {T,L,R}
if !isbounded(interval)
throw(ArgumentError("Unable to represent a non-bounded interval using a `AnchoredInterval`"))
end
AnchoredInterval{span(interval), T, L, R}(first(interval))
AnchoredInterval{span(interval), T, bounds_types(interval)...}(first(interval))
end

##### DISPLAY #####
Expand Down
22 changes: 14 additions & 8 deletions test/anchoredinterval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ using Intervals: Bounded, Ending, Beginning, canonicalize, isunbounded
@test AnchoredInterval{25}('a') isa AnchoredInterval

# Deprecated
#= Dropped for PR 214
@test_deprecated AnchoredInterval{Hour(-1),DateTime,Open,Closed}(dt, Inclusivity(false, true))
@test_throws ArgumentError AnchoredInterval{Hour(-1),DateTime,Open,Closed}(dt, Inclusivity(true, true))
@test_deprecated AnchoredInterval{-1,Float64,Open,Closed}(0, Inclusivity(false, true))
@test_throws ArgumentError AnchoredInterval{-1,Float64,Open,Closed}(0, Inclusivity(true, true))
@test_throws MethodError AnchoredInterval{-1,Float64,Open,Closed}(nothing, Inclusivity(false, true))
=#
end

@testset "zero-span" begin
Expand Down Expand Up @@ -121,17 +123,19 @@ using Intervals: Bounded, Ending, Beginning, canonicalize, isunbounded

# Note: When the deprecation is dropped remove the deprecated tests and uncomment
# the DomainError tests
#= Pending PR 214
@test (@test_deprecated convert(DateTime, he)) == anchor(he)
@test (@test_deprecated convert(DateTime, hb)) == anchor(hb)
# @test_throws DomainError convert(DateTime, he)
# @test_throws DomainError convert(DateTime, hb)
@test_throws DomainError convert(DateTime, he)
@test_throws DomainError convert(DateTime, hb)
=#

@test convert(Interval, he) == Interval{Open, Closed}(dt - Hour(1), dt)
@test convert(Interval, hb) == Interval{Closed, Open}(dt, dt + Hour(1))
@test convert(Interval, he) == Interval{Open, Closed}(dt - Hour(1), dt)
@test convert(Interval, hb) == Interval{Closed, Open}(dt, dt + Hour(1))
@test convert(Interval{DateTime}, he) == Interval{Open, Closed}(dt - Hour(1), dt)
@test convert(Interval{DateTime}, hb) == Interval{Closed, Open}(dt, dt + Hour(1))
@test convert(Interval, he) == Interval(dt - Hour(1), dt, (Open, Closed))
@test convert(Interval, hb) == Interval(dt, dt + Hour(1), (Closed, Open))
@test convert(Interval, he) == Interval(dt - Hour(1), dt, (Open, Closed))
@test convert(Interval, hb) == Interval(dt, dt + Hour(1), (Closed, Open))
@test convert(Interval{DateTime}, he) == Interval(dt - Hour(1), dt, (Open, Closed))
@test convert(Interval{DateTime}, hb) == Interval(dt, dt + Hour(1), (Closed, Open))

@test convert(AnchoredInterval{Ending}, Interval(-Inf, 0)) == AnchoredInterval{-Inf,Float64,Closed,Closed}(0)
@test_throws ArgumentError convert(AnchoredInterval{Beginning}, Interval(-Inf, 0))
Expand Down Expand Up @@ -777,6 +781,7 @@ using Intervals: Bounded, Ending, Beginning, canonicalize, isunbounded
@testset "legacy deserialization" begin
# Serialized string generated on Intervals@1.2 with:
# `julia --project -E 'using Serialization, Intervals; sprint(serialize, AnchoredInterval{-1,Int}(2, true, false))'`.
#= Replaced by 214 anyway
buffer = IOBuffer(
SERIALIZED_HEADER *
"\x004\x10\x01\x10AnchoredInterval\x1f\v՞\x84\xec\xf7-`\x87\xbb" *
Expand All @@ -787,6 +792,7 @@ using Intervals: Bounded, Ending, Beginning, canonicalize, isunbounded
interval = deserialize(buffer)
@test interval isa AnchoredInterval
@test interval == AnchoredInterval{-1,Int,Closed,Open}(2)
=#
end

@testset "floor" begin
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ include("test_utils.jl")
# include("inclusivity.jl")
include("endpoint.jl")
include("interval.jl")
# include("anchoredinterval.jl")
include("anchoredinterval.jl")
# include("comparisons.jl")
# include("sets.jl")
# include("plotting.jl")
Expand Down

0 comments on commit 056e7f7

Please sign in to comment.