Skip to content

Commit

Permalink
Deprecate storing 0 in IntSets
Browse files Browse the repository at this point in the history
This paves the way towards better IndexSet semantics.
  • Loading branch information
mbauman committed Jul 22, 2015
1 parent be2c6ea commit 489dde3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
29 changes: 20 additions & 9 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,8 +82,12 @@ function pop!(s::IntSet, n::Integer, deflt)
return deflt
end
end
if n < 0
return deflt
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
Expand Down Expand Up @@ -147,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
10 changes: 5 additions & 5 deletions test/intset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ data_out = collect(s)
@test sprint(show, IntSet([1,2,3])) == "IntSet([1, 2, 3])"


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())
Expand All @@ -53,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 Down

0 comments on commit 489dde3

Please sign in to comment.