Skip to content

Commit

Permalink
Merge pull request JuliaLang#12270 from JuliaLang/mb/deprecatingcompl…
Browse files Browse the repository at this point in the history
…iments

Deprecate IntSet complement and stored zeros
  • Loading branch information
JeffBezanson committed Jul 28, 2015
2 parents 416a23e + 83f4f2e commit 673b2d7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 58 deletions.
11 changes: 11 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -727,3 +727,14 @@ write(io::IO, s::RopeString) = (write(io, s.head); write(io, s.tail))
sizeof(s::RopeString) = sizeof(s.head) + sizeof(s.tail)

export RopeString

function complement!(s::IntSet)
depwarn("complement IntSets are deprecated", :complement!);
for n = 1:length(s.bits)
s.bits[n] = ~s.bits[n]
end
s.fill1s = !s.fill1s
s
end
complement(s::IntSet) = complement!(copy(s))
export complement, complement!
2 changes: 0 additions & 2 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -749,8 +749,6 @@ export
any!,
any,
collect,
complement!,
complement,
contains,
count,
delete!,
Expand Down
38 changes: 21 additions & 17 deletions base/intset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ function push!(s::IntSet, n::Integer)
lim = Int(n + div(n,2))
sizehint!(s, lim)
end
elseif n < 0
throw(ArgumentError("IntSet elements cannot be negative"))
elseif n <= 0
if n < 0
throw(ArgumentError("IntSet elements cannot be negative"))
else
depwarn("storing zero in IntSets is deprecated", :push!)
end
end
s.bits[n>>5 + 1] |= (UInt32(1)<<(n&31))
return s
Expand All @@ -78,6 +82,13 @@ function pop!(s::IntSet, n::Integer, deflt)
return deflt
end
end
if n <= 0
if n < 0
return deflt
else
depwarn("stored zeros in IntSet is deprecated", :pop!)
end
end
mask = UInt32(1)<<(n&31)
idx = n>>5 + 1
b = s.bits[idx]
Expand Down Expand Up @@ -144,12 +155,15 @@ function in(n::Integer, s::IntSet)
if n >= s.limit
# max IntSet length is typemax(Int), so highest possible element is
# typemax(Int)-1
s.fill1s && n >= 0 && n < typemax(Int)
elseif n < 0
return false
else
(s.bits[n>>5 + 1] & (UInt32(1)<<(n&31))) != 0
return s.fill1s && n >= 0 && n < typemax(Int)
elseif n <= 0
if n < 0
return false
else
depwarn("stored zeros in IntSet is deprecated", :in)
end
end
(s.bits[n>>5 + 1] & (UInt32(1)<<(n&31))) != 0
end

start(s::IntSet) = Int64(0)
Expand Down Expand Up @@ -236,16 +250,6 @@ 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)
for n = 1:length(s.bits)
s.bits[n] = ~s.bits[n]
end
s.fill1s = !s.fill1s
s
end

complement(s::IntSet) = complement!(copy(s))

function symdiff!(s::IntSet, s2::IntSet)
if s2.limit > s.limit
sizehint!(s, s2.limit)
Expand Down
45 changes: 6 additions & 39 deletions test/intset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,25 @@ data_out = collect(s)
# show
@test sprint(show, IntSet()) == "IntSet([])"
@test sprint(show, IntSet([1,2,3])) == "IntSet([1, 2, 3])"
@test contains(sprint(show, complement(IntSet())), "...,")


s = IntSet([0,1,10,20,200,300,1000,10000,10002])
s = IntSet([1,2,10,20,200,300,1000,10000,10002])
@test last(s) == 10002
@test first(s) == 0
@test first(s) == 1
@test length(s) == 9
@test pop!(s) == 10002
@test_throws KeyError pop!(s, -1)
@test length(s) == 8
@test shift!(s) == 0
@test shift!(s) == 1
@test length(s) == 7
@test !in(0,s)
@test !in(1,s)
@test !in(10002,s)
@test in(10000,s)
@test_throws ArgumentError first(IntSet())
@test_throws ArgumentError last(IntSet())
t = copy(s)
sizehint!(t, 20000) #check that hash does not depend on size of internal Array{UInt32, 1}
@test hash(s) == hash(t)
@test hash(complement(s)) == hash(complement(t))

@test setdiff(IntSet([1, 2, 3, 4]), IntSet([2, 4, 5, 6])) == IntSet([1, 3])
@test symdiff(IntSet([1, 2, 3, 4]), IntSet([2, 4, 5, 6])) == IntSet([1, 3, 5, 6])
Expand All @@ -54,7 +53,7 @@ s = IntSet(255)

# issue #7851
@test_throws ArgumentError IntSet(-1)
@test !(-1 in IntSet(0:10))
@test !(-1 in IntSet(1:10))

# # issue #8570
# This requires 2^29 bytes of storage, which is too much for a simple test
Expand All @@ -63,21 +62,6 @@ s = IntSet(255)
# for b in s; b; end

i = IntSet([1, 2, 3])
j = complement(i)

for n in (0, 4, 171)
@test n in j
end

@test j.limit == 256
@test length(j) == typemax(Int) - 3
push!(j, 257)
@test length(j) == typemax(Int) - 3

pop!(j, 171)
@test !(171 in j)
@test length(j) == typemax(Int) - 4
@test complement(j) == IntSet([1, 2, 3, 171])

union!(i, [1, 2])
@test length(i) == 3
Expand All @@ -96,23 +80,14 @@ empty!(i)

i = IntSet(1:6)
@test symdiff!(i, IntSet([6, 513])) == IntSet([1:5; 513])
@test length(symdiff!(i, complement(i))) == typemax(Int)

i = IntSet([1, 2, 3])
k = IntSet([4, 5])
copy!(k, i)
@test k == i
@test !(k === i)

union!(i, complement(i))
copy!(k, i)
@test k == i
@test !(k === i)

# unions
l = union!(i, complement(i))
@test length(l) == typemax(Int)

i = IntSet([1, 2, 3])
j = union(i)
@test j == i
Expand All @@ -138,16 +113,8 @@ push!(j, 257)
push!(j, 2, 3, 17)
@test intersect(i, j) == IntSet([2, 3])

k = complement(j)
@test intersect(i, k) == IntSet([1])

l = IntSet([1, 3])
@test intersect(i, k, l) == IntSet([1])


## equality
i = IntSet([1, 2, 3])
@test !(i == complement(i))
j = IntSet([1, 2, 4])
@test i != j

Expand Down

0 comments on commit 673b2d7

Please sign in to comment.