Skip to content

Commit

Permalink
BitVector constructor from NTuple (#33792)
Browse files Browse the repository at this point in the history
  • Loading branch information
matbesancon authored and KristofferC committed Apr 11, 2020
1 parent 93c747c commit bc6b77d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
22 changes: 22 additions & 0 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit bc6b77d

Please sign in to comment.