Skip to content

Commit

Permalink
remove set operator puns, add issubset(). fixes #3272
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jun 3, 2013
1 parent a17ba51 commit 7f97c4d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
8 changes: 8 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ export PipeString
@deprecate rstrip(a::String, b::String) rstrip(a, collect(b))
@deprecate delete!(a::Vector, x) splice!(a, x)
@deprecate delete!(a::BitVector, x) splice!(a, x)
@deprecate |(s::Set...) union(s...)
@deprecate (&)(s::Set...) intersect(s...)
@deprecate -(a::Set, b::Set) setdiff(a,b)
@deprecate ($)(s1::IntSet, s2::IntSet) symdiff(s1,s2)
@deprecate |(s::IntSet, s2::IntSet) union(s, s2)
@deprecate (&)(s::IntSet, s2::IntSet) intersect(s, s2)
@deprecate -(a::IntSet, b::IntSet) setdiff(a,b)
@deprecate ~(s::IntSet) complement(s)


# note removed macros: str, B_str, I_str, E_str, L_str, L_mstr, I_mstr, E_mstr
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ export
intersect,
intersect!,
isempty,
issubset,
getkey,
keys,
length,
Expand Down
16 changes: 6 additions & 10 deletions base/intset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ function setdiff!(s::IntSet, ns)
end

setdiff(a::IntSet, b::IntSet) = setdiff!(copy(a),b)
symdiff(a::IntSet, b::IntSet) = a $ b
symdiff(a::IntSet, b::IntSet) =
(s1.limit >= s2.limit ? symdiff!(copy(s1), s2) : symdiff!(copy(s2), s1))

function empty!(s::IntSet)
s.bits[:] = 0
Expand Down Expand Up @@ -220,7 +221,8 @@ function intersect!(s::IntSet, s2::IntSet)
end

intersect(s1::IntSet) = copy(s1)
intersect(s1::IntSet, s2::IntSet) = (s1.limit >= s2.limit ? intersect!(copy(s1), s2) : intersect!(copy(s2), s1))
intersect(s1::IntSet, s2::IntSet) =
(s1.limit >= s2.limit ? intersect!(copy(s1), s2) : intersect!(copy(s2), s1))
intersect(s1::IntSet, ss::IntSet...) = intersect(s1, intersect(ss...))

function complement!(s::IntSet)
Expand Down Expand Up @@ -250,14 +252,6 @@ function symdiff!(s::IntSet, s2::IntSet)
s
end

($)(s1::IntSet, s2::IntSet) =
(s1.limit >= s2.limit ? symdiff!(copy(s1), s2) : symdiff!!(copy(s2), s1))

|(s::IntSet, s2::IntSet) = union(s, s2)
(&)(s::IntSet, s2::IntSet) = intersect(s, s2)
-(a::IntSet, b::IntSet) = setdiff(a,b)
~(s::IntSet) = complement(s)

function isequal(s1::IntSet, s2::IntSet)
if s1.fill1s != s2.fill1s
return false
Expand Down Expand Up @@ -285,3 +279,5 @@ function isequal(s1::IntSet, s2::IntSet)
end
return true
end

issubset(a::IntSet, b::IntSet) = isequal(a, intersect(a,b))
8 changes: 3 additions & 5 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,11 @@ function setdiff(a::Set, b::Set)
d
end

|(s::Set...) = union(s...)
(&)(s::Set...) = intersect(s...)
-(a::Set, b::Set) = setdiff(a,b)

isequal(l::Set, r::Set) = (length(l) == length(r)) && (l <= r)
isless(l::Set, r::Set) = (length(l) < length(r)) && (l <= r)
function <=(l::Set, r::Set)
<=(l::Set, r::Set) = issubset(l, r)

function issubset(l, r)
for elt in l
if !contains(r, elt)
return false
Expand Down
31 changes: 16 additions & 15 deletions test/corelib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -365,22 +365,23 @@ s = intersect(Set(5,6,7,8), Set(7,8,9))
@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))

# |, &, -
for (operator, name) in ((|, union), (&, intersect), (-, setdiff))
for (l,r) in ((Set(1,2), Set(3,4)),
(Set(5,6,7,8), Set(7,8,9)),
(Set(1,2), Set(3,4)),
(Set(5,6,7,8), Set(7,8,9)),
(Set(1,2,3), Set()),
(Set(1,2,3), Set(1)),
(Set(1,2,3), Set(1,2)),
(Set(1,2,3), Set(1,2,3)),
(Set(1,2,3), Set(4)),
(Set(1,2,3), Set(4,1)))
@test isequal(operator(l, r), name(l, r))
end
for (l,r) in ((Set(1,2), Set(3,4)),
(Set(5,6,7,8), Set(7,8,9)),
(Set(1,2), Set(3,4)),
(Set(5,6,7,8), Set(7,8,9)),
(Set(1,2,3), Set()),
(Set(1,2,3), Set(1)),
(Set(1,2,3), Set(1,2)),
(Set(1,2,3), Set(1,2,3)),
(Set(1,2,3), Set(4)),
(Set(1,2,3), Set(4,1)))
@test issubset(intersect(l,r), l)
@test issubset(intersect(l,r), r)
@test issubset(l, union(l,r))
@test issubset(r, union(l,r))
@test isequal(union(intersect(l,r),symdiff(l,r)), union(l,r))
end

# union!
s = Set(1,3,5,7)
union!(s,(2,3,4,5))
Expand Down

0 comments on commit 7f97c4d

Please sign in to comment.