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

map of unequal length bitarray #47013

Merged
merged 24 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 14 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
28 changes: 22 additions & 6 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1779,26 +1779,42 @@ end
# map across the chunks. Otherwise, fall-back to the AbstractArray method that
# iterates bit-by-bit.
function bit_map!(f::F, dest::BitArray, A::BitArray) where F
size(A) == size(dest) || throw(DimensionMismatch("sizes of dest and A must match"))
length(A) <= length(dest) || throw(DimensionMismatch("length of dest must be >= than length of A"))
Moelf marked this conversation as resolved.
Show resolved Hide resolved
isempty(A) && return dest
destc = dest.chunks
Ac = A.chunks
for i = 1:(length(Ac)-1)
len_Ac = length(Ac)
for i = 1:(len_Ac-1)
destc[i] = f(Ac[i])
end
destc[end] = f(Ac[end]) & _msk_end(A)
# the last effected UInt64's original content
dest_last = destc[len_Ac]
_msk = _msk_end(A)
# first zero out the bits mask is going to change
destc[len_Ac] = (dest_last & ~(_msk))
# then update bits by `or`ing with a masked RHS
destc[len_Ac] |= f(Ac[end]) & _msk
dest
end
function bit_map!(f::F, dest::BitArray, A::BitArray, B::BitArray) where F
size(A) == size(B) == size(dest) || throw(DimensionMismatch("sizes of dest, A, and B must all match"))
length(A) <= length(dest) || throw(DimensionMismatch("size of dest must be >= than size of A and B"))
length(B) <= length(dest) || throw(DimensionMismatch("size of dest must be >= than size of A and B"))
Moelf marked this conversation as resolved.
Show resolved Hide resolved
isempty(A) && return dest
isempty(B) && return dest
destc = dest.chunks
Ac = A.chunks
Bc = B.chunks
for i = 1:(length(Ac)-1)
len_Ac = min(length(Ac), length(Bc))
for i = 1:len_Ac-1
destc[i] = f(Ac[i], Bc[i])
end
destc[end] = f(Ac[end], Bc[end]) & _msk_end(A)
# the last effected UInt64's original content
dest_last = destc[len_Ac]
_msk = _msk_end(len_Ac)
# first zero out the bits mask is going to change
destc[len_Ac] = (dest_last & ~(_msk))
# then update bits by `or`ing with a masked RHS
destc[len_Ac] |= f(Ac[end], Bc[end]) & _msk
dest
end

Expand Down
31 changes: 31 additions & 0 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,37 @@ timesofar("reductions")
C17970 = map(x -> x ? false : true, A17970)
@test C17970::BitArray{1} == map(~, A17970)
end

@testset "Issue #47011, map! over unequal length bitarray" begin
for l = [0, 1, 63, 64, 65, 127, 128, 129, 255, 256, 257, 6399, 6400, 6401]
for extra_l = [10, 63, 64, 65, 127, 128, 129, 255, 256, 257, 6399, 6400, 6401]

b1 = bitrand(l+extra_l)
b2 = bitrand(l)
original_tail = last(b1, extra_l)
for op in (!, ~)
map!(op, b1, b2)
@test first(b1, l) == map(op, b2)
# check we didn't change bits we're not suppose to
@test last(b1, extra_l) == original_tail
end

b3 = bitrand(l)
b4 = bitrand(l+extra_len)
Moelf marked this conversation as resolved.
Show resolved Hide resolved
for op in (|, ⊻)
map!(op, b1, b2, b3)
@test first(b1, l) == map(op, b2, b3)
# check we didn't change bits we're not suppose to
@test last(b1, extra_l) == original_tail

map!(op, b1, b2, b4)
@test first(b1, l) == map(op, b2, b4)
# check we didn't change bits we're not suppose to
@test last(b1, extra_l) == original_tail
end
Moelf marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
end

## Filter ##
Expand Down