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.

Resizing of BitSet should be filled with 0 by default.
  • Loading branch information
metab0t committed Jun 4, 2022
1 parent bd8dbc3 commit fd7c29c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions base/bitset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function union!(s::BitSet, r::AbstractUnitRange{<:Integer})

# grow s.bits as necessary
if diffb >= len
_growend!(s.bits, diffb - len + 1)
_growend0!(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
Expand All @@ -146,7 +146,7 @@ function union!(s::BitSet, r::AbstractUnitRange{<:Integer})
end
end
if diffa < 0
_growbeg!(s.bits, -diffa)
_growbeg0!(s.bits, -diffa)
s.bits[1] = CHK0
if diffb < 0
s.bits[diffb - diffa + 1] = CHK0
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 = minmax(rand(-1000:1000, 2)...)
c, d = minmax(rand(2000:3000, 2)...)
@test length(union!(BitSet(a:b), c:d)) == length(a:b) + length(c:d)
c, d = minmax(rand(-3000:-2000, 2)...)
@test length(union!(BitSet(a:b), c:d)) == length(a:b) + length(c:d)
end

0 comments on commit fd7c29c

Please sign in to comment.