-
Notifications
You must be signed in to change notification settings - Fork 18
/
endpoint.jl
171 lines (137 loc) · 4.93 KB
/
endpoint.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
struct Direction{T} end
const Left = Direction{:Left}()
const Right = Direction{:Right}()
const Beginning = Left
const Ending = Right
struct Endpoint{T, D, B <: Bound}
endpoint::T
function Endpoint{T,D,B}(ep::T) where {T, D, B <: Bounded}
@assert D isa Direction
new{T,D,B}(ep)
end
function Endpoint{T,D,B}(ep::Nothing) where {T, D, B <: Unbounded}
@assert D isa Direction
new{T,D,B}()
end
function Endpoint{T,D,B}(ep::Nothing) where {T, D, B <: Bounded}
throw(MethodError(Endpoint{T,D,B}, (ep,)))
end
end
Endpoint{T,D,B}(ep) where {T, D, B <: Bounded} = Endpoint{T,D,B}(convert(T, ep))
const LeftEndpoint{T,B} = Endpoint{T, Left, B} where {T,B}
const RightEndpoint{T,B} = Endpoint{T, Right, B} where {T,B}
LeftEndpoint{B}(ep::T) where {T,B} = LeftEndpoint{T,B}(ep)
RightEndpoint{B}(ep::T) where {T,B} = RightEndpoint{T,B}(ep)
LeftEndpoint(i::AbstractInterval{T,L,R}) where {T,L,R} = LeftEndpoint{T,L}(L !== Unbounded ? first(i) : nothing)
RightEndpoint(i::AbstractInterval{T,L,R}) where {T,L,R} = RightEndpoint{T,R}(R !== Unbounded ? last(i) : nothing)
endpoint(x::Endpoint) = isbounded(x) ? x.endpoint : nothing
bound_type(x::Endpoint{T,D,B}) where {T,D,B} = B
isclosed(x::Endpoint) = bound_type(x) === Closed
isunbounded(x::Endpoint) = bound_type(x) === Unbounded
isbounded(x::Endpoint) = bound_type(x) !== Unbounded
function Base.hash(x::Endpoint{T,D,B}, h::UInt) where {T,D,B}
h = hash(:Endpoint, h)
h = hash(D, h)
h = hash(B, h)
# Bounded endpoints can skip hashing `T` as this is handled by hashing the endpoint
# value field. Unbounded endpoints however, should ignore the stored garbage value and
# should hash `T`.`
h = B !== Unbounded ? hash(x.endpoint, h) : hash(T, h)
return h
end
Base.broadcastable(e::Endpoint) = Ref(e)
"""
==(a::Endpoint, b::Endpoint) -> Bool
Determine if two endpoints are equal. When both endpoints are left or right then the points
and inclusiveness must be the same.
Checking the equality of left-endpoint and a right-endpoint is slightly more difficult. A
left-endpoint and a right-endpoint are only equal when they use the same point and are
both included. Note that left/right endpoints which are both not included are not equal
as the left-endpoint contains values below that point while the right-endpoint only contains
values that are above that point.
Visualizing two contiguous intervals can assist in understanding this logic:
[x..y][y..z] -> RightEndpoint == LeftEndpoint
[x..y)[y..z] -> RightEndpoint != LeftEndpoint
[x..y](y..z] -> RightEndpoint != LeftEndpoint
[x..y)(y..z] -> RightEndpoint != LeftEndpoint
"""
function Base.:(==)(a::Endpoint, b::Endpoint)
return (
isunbounded(a) && isunbounded(b) ||
a.endpoint == b.endpoint && bound_type(a) == bound_type(b)
)
end
function Base.:(==)(a::LeftEndpoint, b::RightEndpoint)
a.endpoint == b.endpoint && isclosed(a) && isclosed(b)
end
function Base.:(==)(a::RightEndpoint, b::LeftEndpoint)
b == a
end
function Base.isequal(a::Endpoint, b::Endpoint)
return (
isunbounded(a) && isunbounded(b) ||
isequal(a.endpoint, b.endpoint) && isequal(bound_type(a), bound_type(b))
)
end
function Base.isequal(a::LeftEndpoint, b::RightEndpoint)
isequal(a.endpoint, b.endpoint) && isclosed(a) && isclosed(b)
end
function Base.isequal(a::RightEndpoint, b::LeftEndpoint)
isequal(b, a)
end
function Base.isless(a::LeftEndpoint, b::LeftEndpoint)
return (
!isunbounded(b) && (
isunbounded(a) ||
a.endpoint < b.endpoint ||
a.endpoint == b.endpoint && isclosed(a) && !isclosed(b)
)
)
end
function Base.isless(a::RightEndpoint, b::RightEndpoint)
return (
!isunbounded(a) && (
isunbounded(b) ||
a.endpoint < b.endpoint ||
a.endpoint == b.endpoint && !isclosed(a) && isclosed(b)
)
)
end
function Base.isless(a::LeftEndpoint, b::RightEndpoint)
return (
isunbounded(a) ||
isunbounded(b) ||
a.endpoint < b.endpoint
)
end
function Base.isless(a::RightEndpoint, b::LeftEndpoint)
return (
!isunbounded(a) && !isunbounded(b) &&
(
a.endpoint < b.endpoint ||
a.endpoint == b.endpoint && !(isclosed(a) && isclosed(b))
)
)
end
# Comparisons between Scalars and Endpoints
Base.:(==)(a, b::Endpoint) = a == b.endpoint && isclosed(b)
Base.:(==)(a::Endpoint, b) = b == a
function Base.isless(a, b::LeftEndpoint)
return (
!isunbounded(b) && (
a < b.endpoint ||
a == b.endpoint && !isclosed(b)
)
)
end
function Base.isless(a::RightEndpoint, b)
return (
!isunbounded(a) &&
(
a.endpoint < b ||
a.endpoint == b && !isclosed(a)
)
)
end
Base.isless(a, b::RightEndpoint) = isunbounded(b) || a < b.endpoint
Base.isless(a::LeftEndpoint, b) = isunbounded(a) || a.endpoint < b