Skip to content

Commit

Permalink
Fix symdiff/symdiff! for vector inputs with duplicates, closes #34686 (
Browse files Browse the repository at this point in the history
  • Loading branch information
pcjentsch authored Sep 14, 2022
1 parent 2cfcc1a commit c6abccf
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
10 changes: 3 additions & 7 deletions base/abstractset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ end
Construct the symmetric difference of elements in the passed in sets.
When `s` is not an `AbstractSet`, the order is maintained.
Note that in this case the multiplicity of elements matters.
See also [`symdiff!`](@ref), [`setdiff`](@ref), [`union`](@ref) and [`intersect`](@ref).
Expand All @@ -261,11 +260,6 @@ julia> symdiff([1,2,3], [3,4,5], [4,5,6])
6
julia> symdiff([1,2,1], [2, 1, 2])
2-element Vector{Int64}:
1
2
julia> symdiff(unique([1,2,1]), unique([2, 1, 2]))
Int64[]
```
"""
Expand All @@ -286,7 +280,9 @@ function symdiff!(s::AbstractSet, itrs...)
return s
end

function symdiff!(s::AbstractSet, itr)
symdiff!(s::AbstractSet, itr) = symdiff!(s::AbstractSet, Set(itr))

function symdiff!(s::AbstractSet, itr::AbstractSet)
for x in itr
x in s ? delete!(s, x) : push!(s, x)
end
Expand Down
7 changes: 7 additions & 0 deletions base/bitset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,13 @@ function symdiff!(s::BitSet, ns)
return s
end

function symdiff!(s::BitSet, ns::AbstractSet)
for x in ns
int_symdiff!(s, x)
end
return s
end

function int_symdiff!(s::BitSet, n::Integer)
n0 = _check_bitset_bounds(n)
val = !(n0 in s)
Expand Down
2 changes: 1 addition & 1 deletion test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ end
@test isequal(setdiff([1,2,3,4], [7,8,9]), [1,2,3,4])
@test isequal(setdiff([1,2,3,4], Int64[]), Int64[1,2,3,4])
@test isequal(setdiff([1,2,3,4], [1,2,3,4,5]), Int64[])
@test isequal(symdiff([1,2,3], [4,3,4]), [1,2])
@test isequal(symdiff([1,2,3], [4,3,4]), [1,2,4])
@test isequal(symdiff(['e','c','a'], ['b','a','d']), ['e','c','b','d'])
@test isequal(symdiff([1,2,3], [4,3], [5]), [1,2,4,5])
@test isequal(symdiff([1,2,3,4,5], [1,2,3], [3,4]), [3,5])
Expand Down
7 changes: 4 additions & 3 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,10 @@ end
@test symdiff(Set([1]), BitSet()) isa Set{Int}
@test symdiff(BitSet([1]), Set{Int}()) isa BitSet
@test symdiff([1], BitSet()) isa Vector{Int}
# symdiff must NOT uniquify
@test symdiff([1, 2, 1]) == symdiff!([1, 2, 1]) == [2]
@test symdiff([1, 2, 1], [2, 2]) == symdiff!([1, 2, 1], [2, 2]) == [2]
#symdiff does uniquify
@test symdiff([1, 2, 1]) == symdiff!([1, 2, 1]) == [1,2]
@test symdiff([1, 2, 1], [2, 2]) == symdiff!([1, 2, 1], [2, 2]) == [1]
@test symdiff([1, 2, 1], [2, 2]) == symdiff!([1, 2, 1], [2, 2]) == [1]

# Base.hasfastin
@test all(Base.hasfastin, Any[Dict(1=>2), Set(1), BitSet(1), 1:9, 1:2:9,
Expand Down

0 comments on commit c6abccf

Please sign in to comment.