Skip to content

Commit b742e6a

Browse files
committed
Added width (with tests).
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}).
1 parent 9a4d3ea commit b742e6a

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

src/IntervalSets.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module IntervalSets
77
using Base: @pure
88
import Base: eltype, convert, show, in, length, isempty, isequal, issubset, ==, hash, union, intersect, minimum, maximum
99

10-
export AbstractInterval, ClosedInterval, , .., ±, ordered
10+
export AbstractInterval, ClosedInterval, , .., ±, ordered, width
1111

1212
abstract AbstractInterval{T}
1313

src/closed.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,8 @@ end
7171
issubset(A::ClosedInterval, B::ClosedInterval) = ((A.left in B) && (A.right in B)) || isempty(A)
7272

7373
(A::ClosedInterval, B::ClosedInterval) = issubset(B, A)
74+
75+
function width{T}(A::ClosedInterval{T})
76+
_width = A.right - A.left
77+
max(zero(_width), _width) # this works when T is a Date
78+
end

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,11 @@ using Base.Test
7373
@test I ClosedInterval(1, 2)
7474

7575
@test hash(1..3) == hash(1.0..3.0)
76+
77+
let A = Date(1990, 1, 1), B = Date(1990, 3, 1)
78+
@test width(ClosedInterval(A, B)) == Base.Dates.Day(59)
79+
@test width(ClosedInterval(B, A)) == Base.Dates.Day(0)
80+
@test isempty(ClosedInterval(B, A))
81+
end
7682
end
7783
end

0 commit comments

Comments
 (0)