Skip to content

Commit

Permalink
Fix falses(A) and trues(A)
Browse files Browse the repository at this point in the history
Make falses(A::BitArray) and trues(A::BitArray) not change their
arguments by implementing falses(A::AbstractArray) and
trues(A::AbstractArray).
  • Loading branch information
martinholters committed Apr 11, 2016
1 parent 9ee9aef commit 4ad278c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
8 changes: 6 additions & 2 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,12 @@ function fill!(B::BitArray, x)
return B
end

falses(args...) = fill!(BitArray(args...), false)
trues(args...) = fill!(BitArray(args...), true)
falses(ds::Dims) = fill!(BitArray(ds), false)
falses(ds::Integer...) = falses(ds)
falses(A::AbstractArray) = falses(size(A))
trues(ds::Dims) = fill!(BitArray(ds), true)
trues(ds::Integer...) = trues(ds)
trues(A::AbstractArray) = trues(size(A))

function one(x::BitMatrix)
m, n = size(x)
Expand Down
8 changes: 8 additions & 0 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,10 @@ any!
falses(dims)
Create a `BitArray` with all values set to `false`.
falses(A)
Create a `BitArray` with all values set to `false` of the same shape as `A`.
"""
falses

Expand Down Expand Up @@ -7829,6 +7833,10 @@ mod
trues(dims)
Create a `BitArray` with all values set to `true`.
trues(A)
Create a `BitArray` with all values set to `true` of the same shape as `A`.
"""
trues

Expand Down
2 changes: 2 additions & 0 deletions doc/manual/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ Function Description
``type`` not specified
:func:`ones(A) <ones>` an array of all ones of same element type and shape of ``A``
:func:`trues(dims...) <trues>` a ``Bool`` array with all values ``true``
:func:`trues(A) <trues>` a ``Bool`` array with all values ``true`` and the shape of ``A``
:func:`falses(dims...) <falses>` a ``Bool`` array with all values ``false``
:func:`falses(A) <falses>` a ``Bool`` array with all values ``false`` and the shape of ``A``
:func:`reshape(A, dims...) <reshape>` an array with the same data as the given array, but with
different dimensions.
:func:`copy(A) <copy>` copy ``A``
Expand Down
12 changes: 12 additions & 0 deletions doc/stdlib/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,24 @@ Constructors
Create a ``BitArray`` with all values set to ``true``\ .

.. code-block:: julia
trues(A)
Create a ``BitArray`` with all values set to ``true`` of the same shape as ``A``\ .

.. function:: falses(dims)

.. Docstring generated from Julia source
Create a ``BitArray`` with all values set to ``false``\ .

.. code-block:: julia
falses(A)
Create a ``BitArray`` with all values set to ``false`` of the same shape as ``A``\ .

.. function:: fill(x, dims)

.. Docstring generated from Julia source
Expand Down
20 changes: 20 additions & 0 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ s1, s2, s3, s4 = 5, 8, 3, 4
allsizes = [((), BitArray{0}), ((v1,), BitVector),
((n1,n2), BitMatrix), ((s1,s2,s3,s4), BitArray{4})]

# trues and falses
for (sz,T) in allsizes
a = falses(sz...)
@test a == falses(sz)
@test !any(a)
@test sz == size(a)
b = trues(sz...)
@test b == trues(sz)
@test all(b)
@test sz == size(b)
c = trues(a)
@test all(c)
@test !any(a)
@test sz == size(c)
d = falses(b)
@test !any(d)
@test all(b)
@test sz == size(d)
end

## Conversions ##

for (sz,T) in allsizes
Expand Down

0 comments on commit 4ad278c

Please sign in to comment.