Skip to content

Commit

Permalink
add isdisjoint (JuliaLang/julia#34427) (#695)
Browse files Browse the repository at this point in the history
* add isdisjoint (JuliaLang/julia#34427)

* fix formatting

* Use correct version in README entry

* Add missing link to README

And sort/unify the entries using NEWS-update.jl

Co-authored-by: colinxs <me@colinxsummers.com>
Co-authored-by: Martin Holters <martin.holters@hsu-hh.de>
  • Loading branch information
3 people authored Apr 30, 2020
1 parent 08956a1 commit f27d92d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Please check the list below for the specific syntax you need.

## Supported features

* `isdisjoint(l, r)` indicates whether two collections are disjoint ([#34427]). (since Compat 3.9.0)

* `mergewith(combine, dicts...)` and `mergewith!(combine, dicts...)` are like
`merge(combine, dicts...)` and `merge!(combine, dicts...)` but without the restriction
that the argument `combine` must be a `Function` ([#34296]). (since Compat 3.9.0).
Expand Down Expand Up @@ -138,7 +140,8 @@ Note that you should specify the correct minimum version for `Compat` in the
[#33129]: https://github.com/JuliaLang/julia/issues/33129
[#33568]: https://github.com/JuliaLang/julia/issues/33568
[#33736]: https://github.com/JuliaLang/julia/issues/33736
[#34548]: https://github.com/JuliaLang/julia/pull/34548
[#34296]: https://github.com/JuliaLang/julia/issues/34296
[#34427]: https://github.com/JuliaLang/julia/issues/34427
[#34548]: https://github.com/JuliaLang/julia/issues/34548
[#34652]: https://github.com/JuliaLang/julia/issues/34652
[#34773]: https://github.com/JuliaLang/julia/issues/34773
[#34296]: https://github.com/JuliaLang/julia/pull/34296
30 changes: 30 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,36 @@ if VERSION < v"1.5.0-DEV.182"
mergewith!(f) = (dicts...) -> mergewith!(f, dicts...)
end

# https://github.com/JuliaLang/julia/pull/32003
if VERSION < v"1.4.0-DEV.29"
hasfastin(::Type) = false
hasfastin(::Union{Type{<:AbstractSet},Type{<:AbstractDict},Type{<:AbstractRange}}) = true
hasfastin(x) = hasfastin(typeof(x))
else
const hasfastin = Base.hasfastin
end

# https://github.com/JuliaLang/julia/pull/34427
if VERSION < v"1.5.0-DEV.124"
const FASTIN_SET_THRESHOLD = 70

function isdisjoint(l, r)
function _isdisjoint(l, r)
hasfastin(r) && return !any(in(r), l)
hasfastin(l) && return !any(in(l), r)
Base.haslength(r) && length(r) < FASTIN_SET_THRESHOLD &&
return !any(in(r), l)
return !any(in(Set(r)), l)
end
if Base.haslength(l) && Base.haslength(r) && length(r) < length(l)
return _isdisjoint(r, l)
end
_isdisjoint(l, r)
end

export isdisjoint
end

include("deprecated.jl")

end # module Compat
21 changes: 21 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,25 @@ end
Dict("A" => 1, "B" => 21, "C" => 36)
end

# https://github.com/JuliaLang/julia/pull/34427
@testset "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])),
(S([1,2,3]), S([4])),
(S([1,2,3]), S([4,1])))
@test isdisjoint(l,l) == isempty(l)
@test isdisjoint(l,r) == isempty(intersect(l,r))
end
end
end

nothing

0 comments on commit f27d92d

Please sign in to comment.