Skip to content

Commit 971d18b

Browse files
sets: reorganize definitions
1 parent 2edec09 commit 971d18b

File tree

1 file changed

+71
-54
lines changed

1 file changed

+71
-54
lines changed

base/abstractset.jl

Lines changed: 71 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ sizehint!(s::AbstractSet, n) = nothing
55

66
copy!(dst::AbstractSet, src::AbstractSet) = union!(empty!(dst), src)
77

8+
## set operations (union, intersection, symmetric difference)
9+
810
"""
911
union(s, itrs...)
1012
∪(s, itrs...)
@@ -77,11 +79,11 @@ max_values(::Type{Nothing}) = 1
7779

7880
function union!(s::AbstractSet{T}, itr) where T
7981
haslength(itr) && sizehint!(s, length(s) + length(itr))
80-
for x=itr
82+
for x in itr
8183
push!(s, x)
8284
length(s) == max_values(T) && break
8385
end
84-
s
86+
return s
8587
end
8688

8789
"""
@@ -220,41 +222,16 @@ function symdiff!(s::AbstractSet, itr)
220222
for x in itr
221223
x in s ? delete!(s, x) : push!(s, x)
222224
end
223-
s
225+
return s
224226
end
225227

226-
==(l::AbstractSet, r::AbstractSet) = length(l) == length(r) && l r
227-
# convenience functions for AbstractSet
228-
# (if needed, only their synonyms ⊊ and ⊆ must be specialized)
229-
<( l::AbstractSet, r::AbstractSet) = l r
230-
<=(l::AbstractSet, r::AbstractSet) = l r
231-
232-
function issubset(l, r)
233-
if haslength(r)
234-
rlen = length(r)
235-
#This threshold was empirically determined by repeatedly
236-
#sampling using these two methods (see #26198)
237-
lenthresh = 70
238-
239-
if rlen > lenthresh && !isa(r, AbstractSet)
240-
return issubset(l, Set(r))
241-
end
242-
end
228+
## non-strict subset comparison
243229

244-
for elt in l
245-
if !in(elt, r)
246-
return false
247-
end
248-
end
249-
return true
250-
end
251-
# use the implementation below when it becomes as efficient
252-
# issubset(l, r) = all(_in(r), l)
253230
const = issubset
254-
(l, r) = r l
231+
function end
255232
"""
256-
issubset(a, b)
257-
⊆(a,b) -> Bool
233+
issubset(a, b) -> Bool
234+
⊆(a, b) -> Bool
258235
⊇(b, a) -> Bool
259236
260237
Determine whether every element of `a` is also in `b`, using [`in`](@ref).
@@ -273,29 +250,32 @@ true
273250
"""
274251
issubset, ,
275252

276-
"""
277-
issetequal(a, b)
253+
function issubset(l, r)
254+
if haslength(r)
255+
rlen = length(r)
256+
#This threshold was empirically determined by repeatedly
257+
#sampling using these two methods (see #26198)
258+
lenthresh = 70
278259

279-
Determine whether `a` and `b` have the same elements. Equivalent
280-
to `a ⊆ b && b ⊆ a`.
260+
if rlen > lenthresh && !isa(r, AbstractSet)
261+
return issubset(l, Set(r))
262+
end
263+
end
264+
for elt in l
265+
elt in r || return false
266+
end
267+
return true
268+
end
281269

282-
# Examples
283-
```jldoctest
284-
julia> issetequal([1, 2], [1, 2, 3])
285-
false
270+
(l, r) = r l
286271

287-
julia> issetequal([1, 2], [2, 1])
288-
true
289-
```
290-
"""
291-
issetequal(l, r) = length(l) == length(r) && l r
292-
issetequal(l::AbstractSet, r::AbstractSet) = l == r
272+
## strict subset comparison
293273

294-
(l, r) = length(l) < length(r) && l r
295-
(l, r) = r l
274+
function end
275+
function end
296276
"""
297-
⊊(a, b)
298-
⊋(b, a)
277+
⊊(a, b) -> Bool
278+
⊋(b, a) -> Bool
299279
300280
Determines if `a` is a subset of, but not equal to, `b`.
301281
@@ -310,11 +290,15 @@ false
310290
"""
311291
,
312292

313-
(l, r) = !(l, r)
314-
(l, r) = r l
293+
(l::AbstractSet, r) = length(l) < length(r) && l r
294+
(l, r) = Set(l) r
295+
(l, r) = r l
296+
297+
function end
298+
function end
315299
"""
316-
⊈(a, b)
317-
⊉(b, a)
300+
⊈(a, b) -> Bool
301+
⊉(b, a) -> Bool
318302
319303
Negation of `⊆` and `⊇`, i.e. checks that `a` is not a subset of `b`.
320304
@@ -329,6 +313,39 @@ false
329313
"""
330314
,
331315

316+
(l, r) = !(l, r)
317+
(l, r) = r l
318+
319+
## set equality comparison
320+
321+
"""
322+
issetequal(a, b) -> Bool
323+
324+
Determine whether `a` and `b` have the same elements. Equivalent
325+
to `a ⊆ b && b ⊆ a` but more efficient when possible.
326+
327+
# Examples
328+
```jldoctest
329+
julia> issetequal([1, 2], [1, 2, 3])
330+
false
331+
332+
julia> issetequal([1, 2], [2, 1])
333+
true
334+
```
335+
"""
336+
issetequal(l, r) = length(l) == length(r) && l r
337+
issetequal(l::AbstractSet, r::AbstractSet) = l == r
338+
339+
## partial ordering of sets by containment
340+
341+
==(l::AbstractSet, r::AbstractSet) = length(l) == length(r) && l r
342+
# convenience functions for AbstractSet
343+
# (if needed, only their synonyms ⊊ and ⊆ must be specialized)
344+
<( l::AbstractSet, r::AbstractSet) = l r
345+
<=(l::AbstractSet, r::AbstractSet) = l r
346+
347+
## filtering sets
348+
332349
filter(pred, s::AbstractSet) = mapfilter(pred, push!, s, emptymutable(s))
333350

334351
# it must be safe to delete the current element while iterating over s:

0 commit comments

Comments
 (0)