87
87
88
88
Base. copy (x:: Interval{T} ) where T = Interval {T} (x. first, x. last, x. inclusivity)
89
89
90
- function Base. merge (a:: AbstractInterval{T} , b:: AbstractInterval{T} ) where T
91
- ! overlapscontiguous (a,b) && throw (ArgumentError (" $a and $b are not touching." ))
92
-
93
- left = min (LeftEndpoint (a), LeftEndpoint (b))
94
- right = max (RightEndpoint (a), RightEndpoint (b))
95
- return Interval (
96
- left. endpoint,
97
- right. endpoint,
98
- Inclusivity (left. included, right. included)
99
- )
100
- end
101
-
102
- function overlapscontiguous (a:: AbstractInterval , b:: AbstractInterval )
103
- return overlaps (a,b) || contiguous (a,b)
104
- end
105
-
106
- function overlaps (a:: AbstractInterval , b:: AbstractInterval )
107
- left = max (LeftEndpoint (a), LeftEndpoint (b))
108
- right = min (RightEndpoint (a), RightEndpoint (b))
109
-
110
- return left <= right
111
- end
112
-
113
- function contiguous (a:: AbstractInterval , b:: AbstractInterval )
114
- left = max (LeftEndpoint (a), LeftEndpoint (b))
115
- right = min (RightEndpoint (a), RightEndpoint (b))
116
-
117
- return right. endpoint == left. endpoint && left. included != right. included
118
- end
119
-
120
90
# #### ACCESSORS #####
121
91
122
92
Base. first (interval:: Interval ) = interval. first
@@ -175,8 +145,7 @@ Base.:+(a::T, b) where {T <: Interval} = T(first(a) + b, last(a) + b, inclusivit
175
145
176
146
Base.:+ (a, b:: Interval ) = b + a
177
147
Base.:- (a:: Interval , b) = a + - b
178
-
179
- Base.:- (a:: T , b:: Interval{T} ) where T = a + - b
148
+ Base.:- (a, b:: Interval ) = a + - b
180
149
181
150
function Base.:- (a:: Interval{T} ) where T
182
151
inc = inclusivity (a)
@@ -185,23 +154,23 @@ end
185
154
186
155
# #### EQUALITY #####
187
156
188
- function Base.:(== )(a:: AbstractInterval{T} , b:: AbstractInterval{T} ) where T
157
+ function Base.:(== )(a:: AbstractInterval , b:: AbstractInterval )
189
158
return LeftEndpoint (a) == LeftEndpoint (b) && RightEndpoint (a) == RightEndpoint (b)
190
159
end
191
160
192
161
# While it might be convincingly argued that this should define < instead of isless (see
193
162
# https://github.com/invenia/Intervals.jl/issues/14), this breaks sort.
194
- Base. isless (a:: AbstractInterval{T} , b:: T ) where T = LeftEndpoint (a) < b
195
- Base. isless (a:: T , b:: AbstractInterval{T} ) where T = a < LeftEndpoint (b)
163
+ Base. isless (a:: AbstractInterval , b) = LeftEndpoint (a) < b
164
+ Base. isless (a, b:: AbstractInterval ) = a < LeftEndpoint (b)
196
165
197
- less_than_disjoint (a:: AbstractInterval{T} , b:: T ) where T = RightEndpoint (a) < b
198
- less_than_disjoint (a:: T , b:: AbstractInterval{T} ) where T = a < LeftEndpoint (b)
166
+ less_than_disjoint (a:: AbstractInterval , b) = RightEndpoint (a) < b
167
+ less_than_disjoint (a, b:: AbstractInterval ) = a < LeftEndpoint (b)
199
168
200
- function Base.:isless (a:: AbstractInterval{T} , b:: AbstractInterval{T} ) where T
169
+ function Base.:isless (a:: AbstractInterval , b:: AbstractInterval )
201
170
return LeftEndpoint (a) < LeftEndpoint (b)
202
171
end
203
172
204
- function less_than_disjoint (a:: AbstractInterval{T} , b:: AbstractInterval{T} ) where T
173
+ function less_than_disjoint (a:: AbstractInterval , b:: AbstractInterval )
205
174
return RightEndpoint (a) < LeftEndpoint (b)
206
175
end
207
176
@@ -248,12 +217,26 @@ true
248
217
Base. isempty (i:: AbstractInterval ) = LeftEndpoint (i) > RightEndpoint (i)
249
218
Base. in (a:: T , b:: AbstractInterval{T} ) where T = ! (a ≫ b || a ≪ b)
250
219
251
- function Base. issubset (a:: AbstractInterval{T} , b:: AbstractInterval{T} ) where T
220
+ function Base. issubset (a:: AbstractInterval , b:: AbstractInterval )
252
221
return LeftEndpoint (a) ≥ LeftEndpoint (b) && RightEndpoint (a) ≤ RightEndpoint (b)
253
222
end
254
223
255
- Base.:⊈ (a:: AbstractInterval{T} , b:: AbstractInterval{T} ) where T = ! issubset (a, b)
256
- Base.:⊉ (a:: AbstractInterval{T} , b:: AbstractInterval{T} ) where T = ! issubset (b, a)
224
+ Base.:⊈ (a:: AbstractInterval , b:: AbstractInterval ) = ! issubset (a, b)
225
+ Base.:⊉ (a:: AbstractInterval , b:: AbstractInterval ) = ! issubset (b, a)
226
+
227
+ function overlaps (a:: AbstractInterval , b:: AbstractInterval )
228
+ left = max (LeftEndpoint (a), LeftEndpoint (b))
229
+ right = min (RightEndpoint (a), RightEndpoint (b))
230
+
231
+ return left <= right
232
+ end
233
+
234
+ function contiguous (a:: AbstractInterval , b:: AbstractInterval )
235
+ left = max (LeftEndpoint (a), LeftEndpoint (b))
236
+ right = min (RightEndpoint (a), RightEndpoint (b))
237
+
238
+ return right. endpoint == left. endpoint && left. included != right. included
239
+ end
257
240
258
241
function Base. intersect (a:: AbstractInterval{T} , b:: AbstractInterval{T} ) where T
259
242
! overlaps (a,b) && return Interval {T} ()
@@ -290,7 +273,7 @@ function Base.union!(intervals::Union{AbstractVector{<:Interval}, AbstractVector
290
273
curr = intervals[i]
291
274
292
275
# If the current and previous intervals don't meet then move along
293
- if ! overlapscontiguous (prev, curr)
276
+ if ! overlaps (prev, curr) && ! contiguous (prev, curr)
294
277
i = i + 1
295
278
296
279
# If the two intervals meet then we absorb the current interval into
@@ -305,6 +288,20 @@ function Base.union!(intervals::Union{AbstractVector{<:Interval}, AbstractVector
305
288
return intervals
306
289
end
307
290
291
+ function Base. merge (a:: AbstractInterval , b:: AbstractInterval )
292
+ if ! overlaps (a, b) && ! contiguous (a, b)
293
+ throw (ArgumentError (" $a and $b are neither overlapping or contiguous." ))
294
+ end
295
+
296
+ left = min (LeftEndpoint (a), LeftEndpoint (b))
297
+ right = max (RightEndpoint (a), RightEndpoint (b))
298
+ return Interval (
299
+ left. endpoint,
300
+ right. endpoint,
301
+ Inclusivity (left. included, right. included)
302
+ )
303
+ end
304
+
308
305
# #### TIME ZONES #####
309
306
310
307
function astimezone (i:: Interval{ZonedDateTime} , tz:: TimeZone )
0 commit comments