Skip to content

Commit

Permalink
Convert BitArray specializations to fill!
Browse files Browse the repository at this point in the history
  • Loading branch information
mbauman committed Mar 13, 2018
1 parent 96ef055 commit a5a8fd6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 45 deletions.
36 changes: 0 additions & 36 deletions base/bitarray.jl
Expand Up @@ -649,46 +649,10 @@ end
indexoffset(i) = first(i)-1
indexoffset(::Colon) = 0

# TODO: re-implement this guy
# @inline function setindex!(B::BitArray, x, J0::Union{UnitRange{Int}})
# I0 = to_indices(B, (J0,))[1]
# @boundscheck checkbounds(B, I0)
# y = Bool(x)
# l0 = length(I0)
# l0 == 0 && return B
# f0 = indexoffset(I0)+1
# fill_chunks!(B.chunks, y, f0, l0)
# return B
# end
@propagate_inbounds function setindex!(B::BitArray, X::AbstractArray, J0::Union{Colon,UnitRange{Int}})
_setindex!(IndexStyle(B), B, X, to_indices(B, (J0,))[1])
end

# logical indexing

# TODO: reimplement these guys
# When indexing with a BitArray, we can operate whole chunks at a time for a ~100x gain
# @inline function setindex!(B::BitArray, x, I::BitArray)
# @boundscheck checkbounds(B, I)
# _unsafe_setindex!(B, x, I)
# end
# function _unsafe_setindex!(B::BitArray, x, I::BitArray)
# y = convert(Bool, x)
# Bc = B.chunks
# Ic = I.chunks
# length(Bc) == length(Ic) || throw_boundserror(B, I)
# @inbounds if y
# for i = 1:length(Bc)
# Bc[i] |= Ic[i]
# end
# else
# for i = 1:length(Bc)
# Bc[i] &= ~Ic[i]
# end
# end
# return B
# end

# Assigning an array of bools is more complicated, but we can still do some
# work on chunks by combining X and I 64 bits at a time to improve perf by ~40%
@inline function setindex!(B::BitArray, X::AbstractArray, I::BitArray)
Expand Down
25 changes: 16 additions & 9 deletions base/multidimensional.jl
Expand Up @@ -1602,20 +1602,26 @@ end
end
end

# TODO: reimplement this guy
# @inline function setindex!(B::BitArray, x,
# I0::Union{Colon,UnitRange{Int}}, I::Union{Int,UnitRange{Int},Colon}...)
# J = to_indices(B, (I0, I...))
# @boundscheck checkbounds(B, J...)
# _unsafe_setindex!(B, x, J...)
# end
@propagate_inbounds function setindex!(B::BitArray, X::AbstractArray,
I0::Union{Colon,UnitRange{Int}}, I::Union{Int,UnitRange{Int},Colon}...)
_setindex!(IndexStyle(B), B, X, to_indices(B, (I0, I...))...)
end

@generated function _unsafe_setindex!(B::BitArray, x,
I0::Union{Slice,UnitRange{Int}}, I::Union{Int,UnitRange{Int},Slice}...)
## fill! contiguous views of BitArrays with a single value
function fill!(V::SubArray{Bool, <:Any, <:BitArray, Tuple{AbstractUnitRange{Int}}}, x)
B = V.parent
I0 = V.indices[1]
l0 = length(I0)
l0 == 0 && return V
fill_chunks!(B.chunks, Bool(x), first(I0), l0)
return V
end

fill!(V::SubArray{Bool, <:Any, <:BitArray, Tuple{AbstractUnitRange{Int}, Vararg{Union{Int,AbstractUnitRange{Int}}}}}, x) =
_unsafe_fill_indices!(V.parent, x, V.indices...)

@generated function _unsafe_fill_indices!(B::BitArray, x,
I0::AbstractUnitRange{Int}, I::Union{Int,AbstractUnitRange{Int}}...)
N = length(I)
quote
y = Bool(x)
Expand Down Expand Up @@ -1647,6 +1653,7 @@ end
end
end


## isassigned

@generated function isassigned(B::BitArray, I_0::Int, I::Int...)
Expand Down

0 comments on commit a5a8fd6

Please sign in to comment.