Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix union!(s::BitSet, r::AbstractUnitRange{<:Integer}) when two ranges do not overlap. #45578

Merged
merged 4 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
metab0t marked this conversation as resolved.
Show resolved Hide resolved
# 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