Skip to content

Commit

Permalink
Merge pull request #23139 from KlausC/fix#23137
Browse files Browse the repository at this point in the history
make union(::Set, itr...), intersect(::Set, itr...), and setdiff(::Set, itr) return a Set (fix #23137)
  • Loading branch information
JeffBezanson authored Aug 5, 2017
2 parents 83007fb + 8316ffe commit 41a26ac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ next(s::Set, i) = (s.dict.keys[i], skip_deleted(s.dict, i+1))

union() = Set()
union(s::Set) = copy(s)
function union(s::Set, sets::Set...)
function union(s::Set, sets...)
u = Set{join_eltype(s, sets...)}()
union!(u, s)
for t in sets
Expand Down Expand Up @@ -105,7 +105,7 @@ join_eltype() = Bottom
join_eltype(v1, vs...) = typejoin(eltype(v1), join_eltype(vs...))

intersect(s::Set) = copy(s)
function intersect(s::Set, sets::Set...)
function intersect(s::Set, sets...)
i = similar(s)
for x in s
inall = true
Expand All @@ -121,7 +121,7 @@ function intersect(s::Set, sets::Set...)
end
const = intersect

function setdiff(a::Set, b::Set)
function setdiff(a::Set, b)
d = similar(a)
for x in a
if !(x in b)
Expand Down
12 changes: 12 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ s = union(Set([5,6,7,8]), Set([7,8,9]))
s = Set([1,3,5,7])
union!(s,(2,3,4,5))
@test isequal(s,Set([1,2,3,4,5,7]))
@test ===(typeof(union(Set([1]), IntSet())), Set{Int})
@test isequal(union(Set([1,2,3]), 2:4), Set([1,2,3,4]))
@test isequal(union(Set([1,2,3]), [2,3,4]), Set([1,2,3,4]))
@test isequal(union(Set([1,2,3]), [2,3,4], Set([5])), Set([1,2,3,4,5]))

# intersect
@test isequal(intersect(Set([1])),Set([1]))
Expand All @@ -155,6 +159,10 @@ s = ∩(Set([1,2]), Set([3,4]))
s = intersect(Set([5,6,7,8]), Set([7,8,9]))
@test isequal(s, Set([7,8]))
@test isequal(intersect(Set([2,3,1]), Set([4,2,3]), Set([5,4,3,2])), Set([2,3]))
@test ===(typeof(intersect(Set([1]), IntSet())), Set{Int})
@test isequal(intersect(Set([1,2,3]), 2:10), Set([2,3]))
@test isequal(intersect(Set([1,2,3]), [2,3,4]), Set([2,3]))
@test isequal(intersect(Set([1,2,3]), [2,3,4], 3:4), Set([3]))

# setdiff
@test isequal(setdiff(Set([1,2,3]), Set()), Set([1,2,3]))
Expand All @@ -163,6 +171,10 @@ s = intersect(Set([5,6,7,8]), Set([7,8,9]))
@test isequal(setdiff(Set([1,2,3]), Set([1,2,3])), Set())
@test isequal(setdiff(Set([1,2,3]), Set([4])), Set([1,2,3]))
@test isequal(setdiff(Set([1,2,3]), Set([4,1])), Set([2,3]))
@test ===(typeof(setdiff(Set([1]), IntSet())), Set{Int})
@test isequal(setdiff(Set([1,2,3]), 2:10), Set([1]))
@test isequal(setdiff(Set([1,2,3]), [2,3,4]), Set([1]))
@test_throws MethodError setdiff(Set([1,2,3]), Set([2,3,4]), Set([1]))
s = Set([1,3,5,7])
setdiff!(s,(3,5))
@test isequal(s,Set([1,7]))
Expand Down

0 comments on commit 41a26ac

Please sign in to comment.