From 95239726572a5044eecadd4f9c23e6844ec1a1c8 Mon Sep 17 00:00:00 2001 From: Martin Holters Date: Mon, 11 Apr 2016 09:57:11 +0200 Subject: [PATCH] Fix falses(A) and trues(A) Make falses(A::BitArray) and trues(A::BitArray) not change their arguments by implementing falses(A::AbstractArray) and trues(A::AbstractArray) and replacing the completely generic case with more specific ones for ::Dims and ::Interger... arguments. Update documentation accordingly and move it inline while at it. --- base/bitarray.jl | 29 +++++++++++++++++++++++++++-- base/docs/helpdb/Base.jl | 14 -------------- doc/manual/arrays.rst | 2 ++ doc/stdlib/arrays.rst | 12 ++++++++++++ test/bitarray.jl | 20 ++++++++++++++++++++ 5 files changed, 61 insertions(+), 16 deletions(-) diff --git a/base/bitarray.jl b/base/bitarray.jl index 4e4d2384f9bfd..30d32eaf6b0b9 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -235,8 +235,33 @@ function fill!(B::BitArray, x) return B end -falses(args...) = fill!(BitArray(args...), false) -trues(args...) = fill!(BitArray(args...), true) +""" + falses(dims) + +Create a `BitArray` with all values set to `false`. +""" +falses(dims::Dims) = fill!(BitArray(dims), false) +falses(dims::Integer...) = falses(dims) +""" + falses(A) + +Create a `BitArray` with all values set to `false` of the same shape as `A`. +""" +falses(A::AbstractArray) = falses(size(A)) + +""" + trues(dims) + +Create a `BitArray` with all values set to `true`. +""" +trues(dims::Dims) = fill!(BitArray(dims), true) +trues(dims::Integer...) = trues(dims) +""" + trues(A) + +Create a `BitArray` with all values set to `true` of the same shape as `A`. +""" +trues(A::AbstractArray) = trues(size(A)) function one(x::BitMatrix) m, n = size(x) diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index 25fe67e3b63f4..cf4a4f4e783e6 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -1124,13 +1124,6 @@ results to `r`. """ any! -""" - falses(dims) - -Create a `BitArray` with all values set to `false`. -""" -falses - """ filter!(function, collection) @@ -7825,13 +7818,6 @@ x == fld(x,y)*y + mod(x,y) """ mod -""" - trues(dims) - -Create a `BitArray` with all values set to `true`. -""" -trues - """ qr(A [,pivot=Val{false}][;thin=true]) -> Q, R, [p] diff --git a/doc/manual/arrays.rst b/doc/manual/arrays.rst index 0e4b8e90f1dbd..baf8542668419 100644 --- a/doc/manual/arrays.rst +++ b/doc/manual/arrays.rst @@ -76,7 +76,9 @@ Function Description ``type`` not specified :func:`ones(A) ` an array of all ones of same element type and shape of ``A`` :func:`trues(dims...) ` a ``Bool`` array with all values ``true`` +:func:`trues(A) ` a ``Bool`` array with all values ``true`` and the shape of ``A`` :func:`falses(dims...) ` a ``Bool`` array with all values ``false`` +:func:`falses(A) ` a ``Bool`` array with all values ``false`` and the shape of ``A`` :func:`reshape(A, dims...) ` an array with the same data as the given array, but with different dimensions. :func:`copy(A) ` copy ``A`` diff --git a/doc/stdlib/arrays.rst b/doc/stdlib/arrays.rst index 1764117ffec5c..0a6e60cd8e439 100644 --- a/doc/stdlib/arrays.rst +++ b/doc/stdlib/arrays.rst @@ -191,12 +191,24 @@ Constructors Create a ``BitArray`` with all values set to ``true``\ . +.. function:: trues(A) + + .. Docstring generated from Julia source + + 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``\ . +.. function:: falses(A) + + .. Docstring generated from Julia source + + Create a ``BitArray`` with all values set to ``false`` of the same shape as ``A``\ . + .. function:: fill(x, dims) .. Docstring generated from Julia source diff --git a/test/bitarray.jl b/test/bitarray.jl index d57ec2b7fc221..fefb2868cae81 100644 --- a/test/bitarray.jl +++ b/test/bitarray.jl @@ -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