Skip to content

Commit

Permalink
Fix union!(s::BitSet, r::AbstractUnitRange{<:Integer}) when two range…
Browse files Browse the repository at this point in the history
…s do not overlap. (JuliaLang#45578)

* Fix union!(s::BitSet, r::AbstractUnitRange{<:Integer}) when two ranges do not overlap.
Resizing of BitSet should be filled with 0 by default.
  • Loading branch information
metab0t authored Aug 29, 2022
1 parent 3037c0a commit 5e8e0a5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
14 changes: 2 additions & 12 deletions base/bitset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,10 @@ function union!(s::BitSet, r::AbstractUnitRange{<:Integer})

# grow s.bits as necessary
if diffb >= len
_growend!(s.bits, diffb - len + 1)
# we set only some values to CHK0, those which will not be
# fully overwritten (i.e. only or'ed with `|`)
s.bits[end] = CHK0 # end == diffb + 1
if diffa >= len
s.bits[diffa + 1] = CHK0
end
_growend0!(s.bits, diffb - len + 1)
end
if diffa < 0
_growbeg!(s.bits, -diffa)
s.bits[1] = CHK0
if diffb < 0
s.bits[diffb - diffa + 1] = CHK0
end
_growbeg0!(s.bits, -diffa)
s.offset = cidxa # s.offset += diffa
diffb -= diffa
diffa = 0
Expand Down
9 changes: 9 additions & 0 deletions test/bitset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,12 @@ end
# union! with an empty range doesn't modify the BitSet
@test union!(x, b:a) == y
end

@testset "union!(::BitSet, ::AbstractUnitRange) when two ranges do not overlap" begin
# see #45574
a, b = rand(-10000:-5000), rand(5000:10000)
c, d = minmax(rand(20000:30000, 2)...)
@test length(union!(BitSet(a:b), c:d)) == length(a:b) + length(c:d)
c, d = minmax(rand(-30000:-20000, 2)...)
@test length(union!(BitSet(a:b), c:d)) == length(a:b) + length(c:d)
end

0 comments on commit 5e8e0a5

Please sign in to comment.