From bc6b77d37408024666ea182d9b8821323a04e6a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathieu=20Besan=C3=A7on?= Date: Tue, 3 Dec 2019 16:31:18 +0100 Subject: [PATCH] BitVector constructor from NTuple (#33792) --- base/bitarray.jl | 22 ++++++++++++++++++++++ test/bitarray.jl | 6 ++++++ 2 files changed, 28 insertions(+) diff --git a/base/bitarray.jl b/base/bitarray.jl index ce115afbd3aa10..258f055919ce22 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -77,6 +77,28 @@ const BitMatrix = BitArray{2} BitVector() = BitArray{1}(undef, 0) +""" + BitVector(nt::Tuple{Vararg{Bool}}) + +Construct a `BitVector` from a tuple of `Bool`. +# Examples +```julia-repl +julia> nt = (true, false, true, false) +(true, false, true, false) + +julia> BitVector(nt) +4-element BitArray{1}: + 1 + 0 + 1 + 0 +``` +""" +function BitVector(nt::Tuple{Vararg{Bool}}) + bv = BitVector(undef, length(nt)) + bv .= nt +end + ## utility functions ## length(B::BitArray) = B.len diff --git a/test/bitarray.jl b/test/bitarray.jl index cbc615ac0b19c6..ecea790f775a85 100644 --- a/test/bitarray.jl +++ b/test/bitarray.jl @@ -200,6 +200,12 @@ timesofar("utils") end end + @testset "constructor from NTuple" begin + for nt in ((true, false, false), NTuple{0,Bool}(), (false,), (true,)) + @test BitVector(nt) == BitVector(collect(nt)) + end + end + @testset "one" begin @test Array(one(BitMatrix(undef, 2,2))) == Matrix(I, 2, 2) @test_throws DimensionMismatch one(BitMatrix(undef, 2,3))