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

deprecate bitpack/unpack in favor of BitArray/Array constructors #16010

Merged
merged 1 commit into from
Apr 25, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 17 additions & 21 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,6 @@ convert{T,N}(::Type{AbstractArray{T,N}}, B::BitArray{N}) = convert(Array{T,N}, B
reinterpret{N}(::Type{Bool}, B::BitArray, dims::NTuple{N,Int}) = reinterpret(B, dims)
reinterpret{N}(B::BitArray, dims::NTuple{N,Int}) = reshape(B, dims)

# shorthand forms BitArray <-> Array
bitunpack{N}(B::BitArray{N}) = convert(Array{Bool,N}, B)
bitpack{T,N}(A::AbstractArray{T,N}) = convert(BitArray{N}, A)

## Indexing: getindex ##

@inline function unsafe_bitgetindex(Bc::Vector{UInt64}, i::Int)
Expand Down Expand Up @@ -665,8 +661,8 @@ function append!(B::BitVector, items::BitVector)
return B
end

append!(B::BitVector, items::AbstractVector{Bool}) = append!(B, bitpack(items))
append!(A::Vector{Bool}, items::BitVector) = append!(A, bitunpack(items))
append!(B::BitVector, items::AbstractVector{Bool}) = append!(B, BitArray(items))
append!(A::Vector{Bool}, items::BitVector) = append!(A, Array(items))

function prepend!(B::BitVector, items::BitVector)
n0 = length(B)
Expand All @@ -687,8 +683,8 @@ function prepend!(B::BitVector, items::BitVector)
return B
end

prepend!(B::BitVector, items::AbstractVector{Bool}) = prepend!(B, bitpack(items))
prepend!(A::Vector{Bool}, items::BitVector) = prepend!(A, bitunpack(items))
prepend!(B::BitVector, items::AbstractVector{Bool}) = prepend!(B, BitArray(items))
prepend!(A::Vector{Bool}, items::BitVector) = prepend!(A, Array(items))

function sizehint!(B::BitVector, sz::Integer)
ccall(:jl_array_sizehint, Void, (Any, UInt), B.chunks, num_bit_chunks(sz))
Expand Down Expand Up @@ -1068,19 +1064,19 @@ end

for f in (:/, :\)
@eval begin
($f)(A::BitArray, B::BitArray) = ($f)(bitunpack(A), bitunpack(B))
($f)(A::BitArray, B::BitArray) = ($f)(Array(A), Array(B))
end
end
(/)(B::BitArray, x::Number) = (/)(bitunpack(B), x)
(/)(x::Number, B::BitArray) = (/)(x, bitunpack(B))
(/)(B::BitArray, x::Number) = (/)(Array(B), x)
(/)(x::Number, B::BitArray) = (/)(x, Array(B))

function div(A::BitArray, B::BitArray)
shp = promote_shape(size(A), size(B))
all(B) || throw(DivideError())
return reshape(copy(A), shp)
end
div(A::BitArray, B::Array{Bool}) = div(A, bitpack(B))
div(A::Array{Bool}, B::BitArray) = div(bitpack(A), B)
div(A::BitArray, B::Array{Bool}) = div(A, BitArray(B))
div(A::Array{Bool}, B::BitArray) = div(BitArray(A), B)
function div(B::BitArray, x::Bool)
return x ? copy(B) : throw(DivideError())
end
Expand All @@ -1100,8 +1096,8 @@ function mod(A::BitArray, B::BitArray)
all(B) || throw(DivideError())
return falses(shp)
end
mod(A::BitArray, B::Array{Bool}) = mod(A, bitpack(B))
mod(A::Array{Bool}, B::BitArray) = mod(bitpack(A), B)
mod(A::BitArray, B::Array{Bool}) = mod(A, BitArray(B))
mod(A::Array{Bool}, B::BitArray) = mod(BitArray(A), B)
function mod(B::BitArray, x::Bool)
return x ? falses(size(B)) : throw(DivideError())
end
Expand Down Expand Up @@ -1157,10 +1153,10 @@ for f in (:&, :|, :$)
Fc[end] &= _msk_end(F)
return F
end
($f)(A::DenseArray{Bool}, B::BitArray) = ($f)(bitpack(A), B)
($f)(B::BitArray, A::DenseArray{Bool}) = ($f)(B, bitpack(A))
($f)(x::Number, B::BitArray) = ($f)(x, bitunpack(B))
($f)(B::BitArray, x::Number) = ($f)(bitunpack(B), x)
($f)(A::DenseArray{Bool}, B::BitArray) = ($f)(BitArray(A), B)
($f)(B::BitArray, A::DenseArray{Bool}) = ($f)(B, BitArray(A))
($f)(x::Number, B::BitArray) = ($f)(x, Array(B))
($f)(B::BitArray, x::Number) = ($f)(Array(B), x)
end
end

Expand Down Expand Up @@ -1233,8 +1229,8 @@ end

(.*)(x::Bool, B::BitArray) = x & B
(.*)(B::BitArray, x::Bool) = B & x
(.*)(x::Number, B::BitArray) = x .* bitunpack(B)
(.*)(B::BitArray, x::Number) = bitunpack(B) .* x
(.*)(x::Number, B::BitArray) = x .* Array(B)
(.*)(B::BitArray, x::Number) = Array(B) .* x

## promotion to complex ##

Expand Down
6 changes: 3 additions & 3 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ for (f, scalarf, bitf, bitfbody) in ((:.==, :(==), :biteq , :(~a $ b)),
end
F = BitArray(shape)
Fc = F.chunks
Ac = bitpack(A).chunks
Bc = bitpack(B).chunks
Ac = BitArray(A).chunks
Bc = BitArray(B).chunks
if !isempty(Ac) && !isempty(Bc)
for i = 1:length(Fc) - 1
Fc[i] = ($bitf)(Ac[i], Bc[i])
Expand Down Expand Up @@ -430,7 +430,7 @@ for (sigA, sigB) in ((BitArray, BitArray),
(BitArray, AbstractArray{Bool}))
@eval function (.*)(A::$sigA, B::$sigB)
try
return bitpack(A) & bitpack(B)
return BitArray(A) & BitArray(B)
catch
return bitbroadcast(&, A, B)
end
Expand Down
2 changes: 2 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,8 @@ end
@deprecate specialized_bitwise_unary(f::Function) f
@deprecate specialized_bitwise_binary(f::Function) f

@deprecate bitunpack(B::BitArray) Array(B)
@deprecate bitpack(A::AbstractArray) BitArray(A)

# During the 0.5 development cycle, do not add any deprecations below this line
# To be deprecated in 0.6
Expand Down
14 changes: 0 additions & 14 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4374,13 +4374,6 @@ Pick a random element or array of random elements from the set of values specifi
"""
rand

"""
bitpack(A::AbstractArray{T,N}) -> BitArray

Converts a numeric array to a packed boolean array.
"""
bitpack

"""
base(base, n, [pad])

Expand Down Expand Up @@ -8184,13 +8177,6 @@ all elements of the string.
"""
ispunct

"""
bitunpack(B::BitArray{N}) -> Array{Bool,N}

Converts a packed boolean array to an array of booleans.
"""
bitunpack

"""
size(A, [dim...])

Expand Down
2 changes: 0 additions & 2 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -723,8 +723,6 @@ export
full,

# bitarrays
bitpack,
bitunpack,
falses,
flipbits!,
rol,
Expand Down
10 changes: 5 additions & 5 deletions base/linalg/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ end
## diff and gradient

# TODO: this could be improved (is it worth it?)
gradient(F::BitVector) = gradient(bitunpack(F))
gradient(F::BitVector, h::Real) = gradient(bitunpack(F), h)
gradient(F::Vector, h::BitVector) = gradient(F, bitunpack(h))
gradient(F::BitVector, h::Vector) = gradient(bitunpack(F), h)
gradient(F::BitVector, h::BitVector) = gradient(bitunpack(F), bitunpack(h))
gradient(F::BitVector) = gradient(Array(F))
gradient(F::BitVector, h::Real) = gradient(Array(F), h)
gradient(F::Vector, h::BitVector) = gradient(F, Array(h))
gradient(F::BitVector, h::Vector) = gradient(Array(F), h)
gradient(F::BitVector, h::BitVector) = gradient(Array(F), Array(h))

## diag and related

Expand Down
12 changes: 6 additions & 6 deletions base/linalg/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ zero{T}(::Type{UniformScaling{T}}) = UniformScaling(zero(T))
zero{T}(J::UniformScaling{T}) = zero(UniformScaling{T})

(+)(J1::UniformScaling, J2::UniformScaling) = UniformScaling(J1.λ+J2.λ)
(+){T}(B::BitArray{2},J::UniformScaling{T}) = bitunpack(B) + J
(+)(J::UniformScaling, B::BitArray{2}) = J + bitunpack(B)
(+){T}(B::BitArray{2},J::UniformScaling{T}) = Array(B) + J
(+)(J::UniformScaling, B::BitArray{2}) = J + Array(B)
(+)(J::UniformScaling, A::AbstractMatrix) = A + J
(+)(J::UniformScaling, x::Number) = J.λ + x
(+)(x::Number, J::UniformScaling) = x + J.λ

(-)(J::UniformScaling) = UniformScaling(-J.λ)
(-)(J1::UniformScaling, J2::UniformScaling) = UniformScaling(J1.λ-J2.λ)
(-)(B::BitArray{2}, J::UniformScaling) = bitunpack(B) - J
(-)(J::UniformScaling, B::BitArray{2}) = J - bitunpack(B)
(-)(B::BitArray{2}, J::UniformScaling) = Array(B) - J
(-)(J::UniformScaling, B::BitArray{2}) = J - Array(B)
(-)(J::UniformScaling, x::Number) = J.λ - x
(-)(x::Number, J::UniformScaling) = x - J.λ

Expand Down Expand Up @@ -119,8 +119,8 @@ end
inv(J::UniformScaling) = UniformScaling(inv(J.λ))

*(J1::UniformScaling, J2::UniformScaling) = UniformScaling(J1.λ*J2.λ)
*(B::BitArray{2}, J::UniformScaling) = *(bitunpack(B), J::UniformScaling)
*(J::UniformScaling, B::BitArray{2}) = *(J::UniformScaling, bitunpack(B))
*(B::BitArray{2}, J::UniformScaling) = *(Array(B), J::UniformScaling)
*(J::UniformScaling, B::BitArray{2}) = *(J::UniformScaling, Array(B))
*(A::AbstractMatrix, J::UniformScaling) = J.λ == 1 ? A : J.λ*A
*(J::UniformScaling, A::AbstractVecOrMat) = J.λ == 1 ? A : J.λ*A

Expand Down
16 changes: 4 additions & 12 deletions doc/stdlib/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -805,17 +805,10 @@ Combinatorics
BitArrays
---------

.. function:: bitpack(A::AbstractArray{T,N}) -> BitArray

.. Docstring generated from Julia source

Converts a numeric array to a packed boolean array.

.. function:: bitunpack(B::BitArray{N}) -> Array{Bool,N}

.. Docstring generated from Julia source

Converts a packed boolean array to an array of booleans.
BitArrays are space-efficient "packed" boolean arrays, which store
one bit per boolean value. They can be used similarly to ``Array{Bool}``
arrays (which store one byte per boolean value), and can be converted
to/from the latter via ``Array(bitarray)`` and ``BitArray(array)``, respectively.

.. function:: flipbits!(B::BitArray{N}) -> BitArray{N}

Expand Down Expand Up @@ -1008,4 +1001,3 @@ dense counterparts. The following functions are specific to sparse arrays.
# perform sparse wizardry...
end
end

16 changes: 8 additions & 8 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1264,21 +1264,21 @@ let x = fill(0.9, 1000)
end

#binary ops on bool arrays
A = bitunpack(trues(5))
A = Array(trues(5))
@test A + true == [2,2,2,2,2]
A = bitunpack(trues(5))
A = Array(trues(5))
@test A + false == [1,1,1,1,1]
A = bitunpack(trues(5))
A = Array(trues(5))
@test true + A == [2,2,2,2,2]
A = bitunpack(trues(5))
A = Array(trues(5))
@test false + A == [1,1,1,1,1]
A = bitunpack(trues(5))
A = Array(trues(5))
@test A - true == [0,0,0,0,0]
A = bitunpack(trues(5))
A = Array(trues(5))
@test A - false == [1,1,1,1,1]
A = bitunpack(trues(5))
A = Array(trues(5))
@test true - A == [0,0,0,0,0]
A = bitunpack(trues(5))
A = Array(trues(5))
@test false - A == [-1,-1,-1,-1,-1]

# simple transposes
Expand Down
Loading