1
1
"""
2
- A `ClosedInterval(left, right)` is an interval set that includes both its upper and lower bounds. In
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}
5
+ immutable ClosedInterval{T} <: AbstractInterval{T}
6
6
left:: T
7
7
right:: T
8
+
9
+ ClosedInterval (l:: T , r:: T ) = new (l, r)
10
+ end
11
+
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
+ function ClosedInterval (left, right)
17
+ # Defining this as ClosedInterval(promote(left, right)...) has one problem:
18
+ # if left and right do not promote to a common type, it triggers a StackOverflow.
19
+ T = promote_type (typeof (left), typeof (right))
20
+ ClosedInterval {T} (checked_conversion (T, left, right)... )
8
21
end
9
22
10
- ClosedInterval (left, right) = ClosedInterval (promote (left, right)... )
23
+ ClosedInterval (i:: AbstractInterval ) = convert (ClosedInterval{eltype (i)}, i)
24
+ (:: Type{ClosedInterval{T}} ){T}(i:: AbstractInterval ) = convert (ClosedInterval{T}, i)
11
25
12
26
.. (x, y) = ClosedInterval (x, y)
13
27
@@ -17,13 +31,21 @@ ClosedInterval(left, right) = ClosedInterval(promote(left, right)...)
17
31
show (io:: IO , I:: ClosedInterval ) = print (io, I. left, " .." , I. right)
18
32
19
33
in (v, I:: ClosedInterval ) = I. left <= v <= I. right
34
+ in (a:: ClosedInterval , b:: ClosedInterval ) = (b. left <= a. left) & (a. right <= b. right)
20
35
21
36
isempty (A:: ClosedInterval ) = A. left > A. right
22
37
23
38
isequal (A:: ClosedInterval , B:: ClosedInterval ) = (isequal (A. left, B. left) & isequal (A. right, B. right)) | (isempty (A) & isempty (B))
24
39
25
40
== (A:: ClosedInterval , B:: ClosedInterval ) = (A. left == B. left && A. right == B. right) || (isempty (A) && isempty (B))
26
41
42
+ const _closed_interval_hash = UInt == UInt64 ? 0x1588c274e0a33ad4 : 0x1e3f7252
43
+
44
+ hash (I:: ClosedInterval , h:: UInt ) = hash (I. left, hash (I. right, hash (_closed_interval_hash, h)))
45
+
46
+ minimum (I:: ClosedInterval ) = I. left
47
+ maximum (I:: ClosedInterval ) = I. right
48
+
27
49
function intersect (A:: ClosedInterval , B:: ClosedInterval )
28
50
left = max (A. left, B. left)
29
51
right = min (A. right, B. right)
0 commit comments