Skip to content

Commit

Permalink
Add isdisjoint (#34427)
Browse files Browse the repository at this point in the history
This was added in #13192, but the PR became stale. We've also changed
a bit how we write these kinds of algorithms, so make use of the tools
we have (e.g. `fastin`).
  • Loading branch information
Keno authored Jan 20, 2020
1 parent f68753c commit bc0e5d1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
28 changes: 27 additions & 1 deletion base/abstractset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ true
"""
issubset, ,

const FASTIN_SET_THRESHOLD = 70

function issubset(l, r)
if haslength(r) && (isa(l, AbstractSet) || !hasfastin(r))
rlen = length(r) # conditions above make this length computed only when needed
Expand All @@ -269,7 +271,7 @@ function issubset(l, r)
end
# when `in` would be too slow and r is big enough, convert it to a Set
# this threshold was empirically determined (cf. #26198)
if !hasfastin(r) && rlen > 70
if !hasfastin(r) && rlen > FASTIN_SET_THRESHOLD
return issubset(l, Set(r))
end
end
Expand Down Expand Up @@ -375,6 +377,30 @@ function issetequal(l, r)
return issetequal(Set(l), Set(r))
end

## set disjoint comparison
"""
isdisjoint(v1, v2) -> Bool
Returns whether the collections `v1` and `v2` are disjoint, i.e. whether
their intersection is empty.
!!! compat "Julia 1.5"
This function requires at least Julia 1.5.
"""
function isdisjoint(l, r)
function _isdisjoint(l, r)
hasfastin(r) && return !any(in(r), l)
hasfastin(l) && return !any(in(l), r)
haslength(r) && length(r) < FASTIN_SET_THRESHOLD &&
return !any(in(r), l)
return !any(in(Set(r)), l)
end
if haslength(l) && haslength(r) && length(r) < length(l)
return _isdisjoint(r, l)
end
_isdisjoint(l, r)
end

## partial ordering of sets by containment

==(l::AbstractSet, r::AbstractSet) = length(l) == length(r) && l r
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ export
in,
intersect!,
intersect,
isdisjoint,
isempty,
issubset,
issetequal,
Expand Down
6 changes: 5 additions & 1 deletion test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,15 @@ end
@test !(Set([1,2,3]) <= Set([1,2,4]))
end

@testset "issubset, symdiff" begin
@testset "issubset, symdiff, isdisjoint" begin
for S in (Set, BitSet, Vector)
for (l,r) in ((S([1,2]), S([3,4])),
(S([5,6,7,8]), S([7,8,9])),
(S([1,2]), S([3,4])),
(S([5,6,7,8]), S([7,8,9])),
(S([1,2,3]), S()),
(S(), S()),
(S(), S([1,2,3])),
(S([1,2,3]), S([1])),
(S([1,2,3]), S([1,2])),
(S([1,2,3]), S([1,2,3])),
Expand All @@ -317,6 +319,8 @@ end
@test issubset(intersect(l,r), r)
@test issubset(l, union(l,r))
@test issubset(r, union(l,r))
@test isdisjoint(l,l) == isempty(l)
@test isdisjoint(l,r) == isempty(intersect(l,r))
if S === Vector
@test sort(union(intersect(l,r),symdiff(l,r))) == sort(union(l,r))
else
Expand Down

0 comments on commit bc0e5d1

Please sign in to comment.