2
2
A `ClosedInterval(left, right)` is an interval set that includes both its upper and lower bounds. In
3
3
mathematical notation, the constructed range is `[left, right]`.
4
4
"""
5
- immutable ClosedInterval{T} <: AbstractInterval{T}
5
+ struct ClosedInterval{T} <: AbstractInterval{T}
6
6
left:: T
7
7
right:: T
8
8
9
- (:: Type{ClosedInterval{T}} ){T}(l:: T , r:: T ) = new {T} (l, r)
9
+ ClosedInterval {T} (l:: T , r:: T ) where {T} = new {T} (l, r)
10
+ ClosedInterval {T} (l, r) where {T} = ((a, b) = checked_conversion (T, l, r); new {T} (a, b))
11
+ ClosedInterval {T} (i:: AbstractInterval ) where {T} = convert (ClosedInterval{T}, i)
10
12
end
11
13
12
- ClosedInterval {T} (left:: T , right:: T ) = ClosedInterval {T} (left, right)
13
- (:: Type{ClosedInterval{T}} ){T}(left, right) =
14
- ClosedInterval {T} (checked_conversion (T, left, right)... )
15
-
16
14
function ClosedInterval (left, right)
17
15
# Defining this as ClosedInterval(promote(left, right)...) has one problem:
18
16
# if left and right do not promote to a common type, it triggers a StackOverflow.
@@ -21,7 +19,6 @@ function ClosedInterval(left, right)
21
19
end
22
20
23
21
ClosedInterval (i:: AbstractInterval ) = convert (ClosedInterval{eltype (i)}, i)
24
- (:: Type{ClosedInterval{T}} ){T}(i:: AbstractInterval ) = convert (ClosedInterval{T}, i)
25
22
26
23
"""
27
24
iv = l..r
@@ -37,7 +34,7 @@ Construct a ClosedInterval `iv` spanning the region from
37
34
`center - halfwidth` to `center + halfwidth`.
38
35
"""
39
36
± (x, y) = ClosedInterval (x - y, x + y)
40
- ± (x:: CartesianIndex , y) = map (ClosedInterval, (x - y ). I, (x + y ). I)
37
+ ± (x:: CartesianIndex , y) = (xy = y * one (x); map (ClosedInterval, (x - xy ). I, (x + xy ). I) )
41
38
42
39
show (io:: IO , I:: ClosedInterval ) = print (io, I. left, " .." , I. right)
43
40
@@ -64,7 +61,7 @@ function intersect(A::ClosedInterval, B::ClosedInterval)
64
61
ClosedInterval (left, right)
65
62
end
66
63
67
- function union {T<:AbstractFloat} (A:: ClosedInterval{T} , B:: ClosedInterval{T} )
64
+ function union (A:: ClosedInterval{T} , B:: ClosedInterval{T} ) where T <: AbstractFloat
68
65
max (A. left, B. left) <= nextfloat (min (A. right, B. right)) || throw (ArgumentError (" Cannot construct union of disjoint sets." ))
69
66
_union (A, B)
70
67
end
@@ -90,19 +87,19 @@ issubset(A::ClosedInterval, B::ClosedInterval) = ((A.left in B) && (A.right in B
90
87
Calculate the width (max-min) of interval `iv`. Note that for integers
91
88
`l` and `r`, `width(l..r) = length(l:r) - 1`.
92
89
"""
93
- function width {T} (A:: ClosedInterval{T} )
90
+ function width (A:: ClosedInterval{T} ) where T
94
91
_width = A. right - A. left
95
92
max (zero (_width), _width) # this works when T is a Date
96
93
end
97
94
98
- length {T <: Integer} (A:: ClosedInterval{T} ) = max (0 , Int (A. right - A. left) + 1 )
95
+ length (A:: ClosedInterval{T} ) where {T <: Integer } = max (0 , Int (A. right - A. left) + 1 )
99
96
100
97
length (A:: ClosedInterval{Date} ) = max (0 , Dates. days (A. right - A. left) + 1 )
101
98
102
- function convert {R<:AbstractUnitRange,I<:Integer} (:: Type{R} , i:: ClosedInterval{I} )
99
+ function convert (:: Type{R} , i:: ClosedInterval{I} ) where {R <: AbstractUnitRange ,I <: Integer }
103
100
R (minimum (i), maximum (i))
104
101
end
105
102
106
- range {I<:Integer} (i:: ClosedInterval{I} ) = convert (UnitRange{I}, i)
103
+ range (i:: ClosedInterval{I} ) where {I <: Integer } = convert (UnitRange{I}, i)
107
104
108
- Base. promote_rule {T1,T2} (:: Type{ClosedInterval{T1}} , :: Type{ClosedInterval{T2}} ) = ClosedInterval{promote_type (T1, T2)}
105
+ Base. promote_rule (:: Type{ClosedInterval{T1}} , :: Type{ClosedInterval{T2}} ) where {T1,T2} = ClosedInterval{promote_type (T1, T2)}
0 commit comments