Skip to content

Commit 489dde3

Browse files
committed
Deprecate storing 0 in IntSets
This paves the way towards better IndexSet semantics.
1 parent be2c6ea commit 489dde3

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

Diff for: base/intset.jl

+20-9
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ function push!(s::IntSet, n::Integer)
5555
lim = Int(n + div(n,2))
5656
sizehint!(s, lim)
5757
end
58-
elseif n < 0
59-
throw(ArgumentError("IntSet elements cannot be negative"))
58+
elseif n <= 0
59+
if n < 0
60+
throw(ArgumentError("IntSet elements cannot be negative"))
61+
else
62+
depwarn("storing zero in IntSets is deprecated", :push!)
63+
end
6064
end
6165
s.bits[n>>5 + 1] |= (UInt32(1)<<(n&31))
6266
return s
@@ -78,8 +82,12 @@ function pop!(s::IntSet, n::Integer, deflt)
7882
return deflt
7983
end
8084
end
81-
if n < 0
82-
return deflt
85+
if n <= 0
86+
if n < 0
87+
return deflt
88+
else
89+
depwarn("stored zeros in IntSet is deprecated", :pop!)
90+
end
8391
end
8492
mask = UInt32(1)<<(n&31)
8593
idx = n>>5 + 1
@@ -147,12 +155,15 @@ function in(n::Integer, s::IntSet)
147155
if n >= s.limit
148156
# max IntSet length is typemax(Int), so highest possible element is
149157
# typemax(Int)-1
150-
s.fill1s && n >= 0 && n < typemax(Int)
151-
elseif n < 0
152-
return false
153-
else
154-
(s.bits[n>>5 + 1] & (UInt32(1)<<(n&31))) != 0
158+
return s.fill1s && n >= 0 && n < typemax(Int)
159+
elseif n <= 0
160+
if n < 0
161+
return false
162+
else
163+
depwarn("stored zeros in IntSet is deprecated", :in)
164+
end
155165
end
166+
(s.bits[n>>5 + 1] & (UInt32(1)<<(n&31))) != 0
156167
end
157168

158169
start(s::IntSet) = Int64(0)

Diff for: test/intset.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ data_out = collect(s)
2121
@test sprint(show, IntSet([1,2,3])) == "IntSet([1, 2, 3])"
2222

2323

24-
s = IntSet([0,1,10,20,200,300,1000,10000,10002])
24+
s = IntSet([1,2,10,20,200,300,1000,10000,10002])
2525
@test last(s) == 10002
26-
@test first(s) == 0
26+
@test first(s) == 1
2727
@test length(s) == 9
2828
@test pop!(s) == 10002
2929
@test_throws KeyError pop!(s, -1)
3030
@test length(s) == 8
31-
@test shift!(s) == 0
31+
@test shift!(s) == 1
3232
@test length(s) == 7
33-
@test !in(0,s)
33+
@test !in(1,s)
3434
@test !in(10002,s)
3535
@test in(10000,s)
3636
@test_throws ArgumentError first(IntSet())
@@ -53,7 +53,7 @@ s = IntSet(255)
5353

5454
# issue #7851
5555
@test_throws ArgumentError IntSet(-1)
56-
@test !(-1 in IntSet(0:10))
56+
@test !(-1 in IntSet(1:10))
5757

5858
# # issue #8570
5959
# This requires 2^29 bytes of storage, which is too much for a simple test

0 commit comments

Comments
 (0)