Skip to content

Commit

Permalink
Merge pull request #7 from tpapp/pull-request/7e234fa3
Browse files Browse the repository at this point in the history
added width (updated)
  • Loading branch information
mronian authored Nov 18, 2016
2 parents 9a4d3ea + 7e234fa commit e7777c2
Show file tree
Hide file tree
Showing 3 changed files with 15 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
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,14 @@ 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

@test width(ClosedInterval(3,7)) 4
@test width(ClosedInterval(4.0,8.0)) 4.0
end
end

0 comments on commit e7777c2

Please sign in to comment.