Skip to content

Commit 46bbdaa

Browse files
authored
simplify union (#113)
1 parent 41028c0 commit 46bbdaa

File tree

1 file changed

+2
-65
lines changed

1 file changed

+2
-65
lines changed

src/interval.jl

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -120,77 +120,14 @@ end
120120
intersect(d1::AbstractInterval, d2::AbstractInterval) = intersect(Interval(d1), Interval(d2))
121121

122122

123-
union(d1::TypedEndpointsInterval{:closed,:closed}, d2::TypedEndpointsInterval{:closed,:closed}) =
124-
_leq_union(d1,d2)
125-
union(d1::Interval{:open,:closed}, d2::TypedEndpointsInterval{:closed,:closed}) =
126-
_leq_union(d1,d2)
127-
union(d1::Interval{:closed,:open}, d2::TypedEndpointsInterval{:closed,:closed}) =
128-
_leq_union(d1,d2)
129-
union(d1::TypedEndpointsInterval{:closed,:closed}, d2::TypedEndpointsInterval{:open,:closed}) =
130-
_leq_union(d1,d2)
131-
union(d1::TypedEndpointsInterval{:closed,:closed}, d2::TypedEndpointsInterval{:closed,:open}) =
132-
_leq_union(d1,d2)
133-
union(d1::TypedEndpointsInterval{:open,:closed}, d2::TypedEndpointsInterval{:open,:closed}) =
134-
_leq_union(d1,d2)
135-
union(d1::TypedEndpointsInterval{:closed,:open}, d2::TypedEndpointsInterval{:closed,:open}) =
136-
_leq_union(d1,d2)
137-
union(d1::TypedEndpointsInterval{:closed,:closed}, d2::TypedEndpointsInterval{:open,:open}) =
138-
_leq_union(d1,d2)
139-
union(d1::TypedEndpointsInterval{:open,:open}, d2::TypedEndpointsInterval{:closed,:closed}) =
140-
_leq_union(d1,d2)
141-
142-
143-
function _leq_union(d1, d2)
123+
function union(d1::TypedEndpointsInterval, d2::TypedEndpointsInterval)
144124
isempty(d1) && return d2
145125
isempty(d2) && return d1
146-
leftendpoint(d1)  leftendpoint(d2)  rightendpoint(d1) || leftendpoint(d1)  rightendpoint(d2)  rightendpoint(d1) ||
147-
leftendpoint(d2)  leftendpoint(d1)  rightendpoint(d2) || leftendpoint(d2)  rightendpoint(d1)  rightendpoint(d2) ||
126+
any((d1), endpoints(d2)) || any((d2), endpoints(d1)) ||
148127
throw(ArgumentError("Cannot construct union of disjoint sets."))
149128
_union(d1, d2)
150129
end
151130

152-
153-
function union(d1::TypedEndpointsInterval{:open,:open}, d2::TypedEndpointsInterval{:open,:open})
154-
isempty(d1) && return d2
155-
isempty(d2) && return d1
156-
leftendpoint(d1)  leftendpoint(d2) < rightendpoint(d1) || leftendpoint(d1) < rightendpoint(d2)  rightendpoint(d1) ||
157-
leftendpoint(d2)  leftendpoint(d1) < rightendpoint(d2) || leftendpoint(d2) < rightendpoint(d1)  rightendpoint(d2) ||
158-
throw(ArgumentError("Cannot construct union of disjoint sets."))
159-
_union(d1, d2)
160-
end
161-
162-
function union(d1::TypedEndpointsInterval{:open,:open}, d2::TypedEndpointsInterval{:open,:closed})
163-
isempty(d1) && return d2
164-
isempty(d2) && return d1
165-
leftendpoint(d1)  leftendpoint(d2) < rightendpoint(d1) || leftendpoint(d1)  rightendpoint(d2)  rightendpoint(d1) ||
166-
leftendpoint(d2)  leftendpoint(d1)  rightendpoint(d2) || leftendpoint(d2) < rightendpoint(d1)  rightendpoint(d2) ||
167-
throw(ArgumentError("Cannot construct union of disjoint sets."))
168-
_union(d1, d2)
169-
end
170-
171-
function union(d1::TypedEndpointsInterval{:open,:open}, d2::TypedEndpointsInterval{:closed,:open})
172-
isempty(d1) && return d2
173-
isempty(d2) && return d1
174-
leftendpoint(d1)  leftendpoint(d2)  rightendpoint(d1) || leftendpoint(d1) < rightendpoint(d2)  rightendpoint(d1) ||
175-
leftendpoint(d2)  leftendpoint(d1) < rightendpoint(d2) || leftendpoint(d2)  rightendpoint(d1)  rightendpoint(d2) ||
176-
throw(ArgumentError("Cannot construct union of disjoint sets."))
177-
_union(d1, d2)
178-
end
179-
180-
union(d1::TypedEndpointsInterval, d2::TypedEndpointsInterval{:open,:open}) = union(d2, d1)
181-
182-
183-
function union(d1::TypedEndpointsInterval{:closed,:open}, d2::TypedEndpointsInterval{:open,:closed})
184-
isempty(d1) && return d2
185-
isempty(d2) && return d1
186-
leftendpoint(d1)  leftendpoint(d2) < rightendpoint(d1) || leftendpoint(d1)  rightendpoint(d2)  rightendpoint(d1) ||
187-
leftendpoint(d2)  leftendpoint(d1)  rightendpoint(d2) || leftendpoint(d2) < rightendpoint(d1)  rightendpoint(d2) ||
188-
throw(ArgumentError("Cannot construct union of disjoint sets."))
189-
_union(d1, d2)
190-
end
191-
192-
union(d1::TypedEndpointsInterval{:open,:closed}, d2::TypedEndpointsInterval{:closed,:open}) = union(d2, d1)
193-
194131
# these assume overlap
195132
function _union(A::TypedEndpointsInterval{L,R}, B::TypedEndpointsInterval{L,R}) where {L,R}
196133
left = min(leftendpoint(A), leftendpoint(B))

0 commit comments

Comments
 (0)