Skip to content

Commit bc6b77d

Browse files
matbesanconKristofferC
authored andcommitted
BitVector constructor from NTuple (#33792)
1 parent 93c747c commit bc6b77d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

base/bitarray.jl

+22
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,28 @@ const BitMatrix = BitArray{2}
7777

7878
BitVector() = BitArray{1}(undef, 0)
7979

80+
"""
81+
BitVector(nt::Tuple{Vararg{Bool}})
82+
83+
Construct a `BitVector` from a tuple of `Bool`.
84+
# Examples
85+
```julia-repl
86+
julia> nt = (true, false, true, false)
87+
(true, false, true, false)
88+
89+
julia> BitVector(nt)
90+
4-element BitArray{1}:
91+
1
92+
0
93+
1
94+
0
95+
```
96+
"""
97+
function BitVector(nt::Tuple{Vararg{Bool}})
98+
bv = BitVector(undef, length(nt))
99+
bv .= nt
100+
end
101+
80102
## utility functions ##
81103

82104
length(B::BitArray) = B.len

test/bitarray.jl

+6
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ timesofar("utils")
200200
end
201201
end
202202

203+
@testset "constructor from NTuple" begin
204+
for nt in ((true, false, false), NTuple{0,Bool}(), (false,), (true,))
205+
@test BitVector(nt) == BitVector(collect(nt))
206+
end
207+
end
208+
203209
@testset "one" begin
204210
@test Array(one(BitMatrix(undef, 2,2))) == Matrix(I, 2, 2)
205211
@test_throws DimensionMismatch one(BitMatrix(undef, 2,3))

0 commit comments

Comments
 (0)