Skip to content

Commit

Permalink
Added width (with tests).
Browse files Browse the repository at this point in the history
Trivial addition for the width of the interval. Implementation also
handles case when type of the width is different from the eltype (eg
ClosedInterval{Date}).
  • Loading branch information
tpapp committed Nov 16, 2016
1 parent 9a4d3ea commit b742e6a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/IntervalSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module IntervalSets
using Base: @pure
import Base: eltype, convert, show, in, length, isempty, isequal, issubset, ==, hash, union, intersect, minimum, maximum

export AbstractInterval, ClosedInterval, , .., ±, ordered
export AbstractInterval, ClosedInterval, , .., ±, ordered, width

abstract AbstractInterval{T}

Expand Down
5 changes: 5 additions & 0 deletions src/closed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,8 @@ end
issubset(A::ClosedInterval, B::ClosedInterval) = ((A.left in B) && (A.right in B)) || isempty(A)

(A::ClosedInterval, B::ClosedInterval) = issubset(B, A)

function width{T}(A::ClosedInterval{T})
_width = A.right - A.left
max(zero(_width), _width) # this works when T is a Date
end
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,11 @@ using Base.Test
@test I ClosedInterval(1, 2)

@test hash(1..3) == hash(1.0..3.0)

let A = Date(1990, 1, 1), B = Date(1990, 3, 1)
@test width(ClosedInterval(A, B)) == Base.Dates.Day(59)
@test width(ClosedInterval(B, A)) == Base.Dates.Day(0)
@test isempty(ClosedInterval(B, A))
end
end
end

0 comments on commit b742e6a

Please sign in to comment.