Skip to content

Commit f48283e

Browse files
authored
Merge pull request #24785 from Sacha0/digbitarr
replace BitArray(shape...) constructors/calls with BitArray(uninitialized, shape...)
2 parents c322fed + f34856c commit f48283e

File tree

12 files changed

+81
-62
lines changed

12 files changed

+81
-62
lines changed

NEWS.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ Language changes
8282
For example, `f() = (global sin = "gluttony"; nothing)` will now resolve which module
8383
contains `sin` eagerly, rather than delaying that decision until `f` is run. ([#22984]).
8484

85+
* Uninitialized `BitArray` constructors of the form `BitArray[{N}](shape...)` have been
86+
deprecated in favor of equivalents accepting `uninitialized` (an alias for
87+
`Uninitialized()`) as their first argument, as in
88+
`BitArray[{N}](uninitialized, shape...)`. For example, `BitVector(3)` is now
89+
`BitVector(uninitialized, 3)`, `BitMatrix((2, 4))` is now
90+
`BitMatrix(uninitialized, (2, 4))`, and `BitArray{3}(11, 13, 17)` is now
91+
`BitArray{3}(uninitialized, 11, 14, 17)` ([#24785]).
92+
8593
* Dispatch rules have been simplified:
8694
method matching is now determined exclusively by subtyping;
8795
the rule that method type parameters must also be captured has been removed.

base/bitarray.jl

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mutable struct BitArray{N} <: DenseArray{Bool, N}
1313
chunks::Vector{UInt64}
1414
len::Int
1515
dims::NTuple{N,Int}
16-
function BitArray{N}(dims::Vararg{Int,N}) where N
16+
function BitArray{N}(::Uninitialized, dims::Vararg{Int,N}) where N
1717
n = 1
1818
i = 1
1919
for d in dims
@@ -34,33 +34,35 @@ end
3434
# the first one is recognized by the help system; it would be nice
3535
# to fix this.
3636
"""
37-
BitArray(dims::Integer...)
38-
BitArray{N}(dims::NTuple{N,Int})
37+
BitArray(uninitialized, dims::Integer...)
38+
BitArray{N}(uninitialized, dims::NTuple{N,Int})
3939
4040
Construct an uninitialized [`BitArray`](@ref) with the given dimensions.
41-
Behaves identically to the [`Array`](@ref) constructor.
41+
Behaves identically to the [`Array`](@ref) constructor. See [`uninitialized`](@ref).
4242
4343
# Examples
4444
```julia-repl
45-
julia> BitArray(2, 2)
45+
julia> BitArray(uninitialized, 2, 2)
4646
2×2 BitArray{2}:
4747
false false
4848
false true
4949
50-
julia> BitArray((3, 1))
50+
julia> BitArray(uninitialized, (3, 1))
5151
3×1 BitArray{2}:
5252
false
5353
true
5454
false
5555
```
5656
"""
57-
BitArray(dims::Integer...) = BitArray(map(Int,dims))
58-
BitArray(dims::NTuple{N,Int}) where {N} = BitArray{N}(dims...)
57+
BitArray(::Uninitialized, dims::Integer...) = BitArray(uninitialized, map(Int,dims))
58+
BitArray{N}(::Uninitialized, dims::Integer...) where {N} = BitArray{N}(uninitialized, map(Int,dims))
59+
BitArray(::Uninitialized, dims::NTuple{N,Int}) where {N} = BitArray{N}(uninitialized, dims...)
60+
BitArray{N}(::Uninitialized, dims::NTuple{N,Int}) where {N} = BitArray{N}(uninitialized, dims...)
5961

6062
const BitVector = BitArray{1}
6163
const BitMatrix = BitArray{2}
6264

63-
BitVector() = BitArray{1}(0)
65+
BitVector() = BitArray{1}(uninitialized, 0)
6466

6567
## utility functions ##
6668

@@ -341,15 +343,17 @@ done(B::BitArray, i::Int) = i >= length(B)
341343

342344
## similar, fill!, copy! etc ##
343345

344-
similar(B::BitArray) = BitArray(size(B))
345-
similar(B::BitArray, dims::Int...) = BitArray(dims)
346-
similar(B::BitArray, dims::Dims) = BitArray(dims...)
346+
similar(B::BitArray) = BitArray(uninitialized, size(B))
347+
similar(B::BitArray, dims::Int...) = BitArray(uninitialized, dims)
348+
similar(B::BitArray, dims::Dims) = BitArray(uninitialized, dims...)
347349

348-
similar(B::BitArray, T::Type{Bool}, dims::Dims) = BitArray(dims)
350+
similar(B::BitArray, T::Type{Bool}, dims::Dims) = BitArray(uninitialized, dims)
349351
# changing type to a non-Bool returns an Array
350352
# (this triggers conversions like float(bitvector) etc.)
351353
similar(B::BitArray, T::Type, dims::Dims) = Array{T}(uninitialized, dims)
352354

355+
similar(::Type{T}, shape::Tuple) where {T<:BitArray} = T(uninitialized, to_shape(shape))
356+
353357
function fill!(B::BitArray, x)
354358
y = convert(Bool, x)
355359
isempty(B) && return B
@@ -376,7 +380,7 @@ julia> falses(2,3)
376380
false false false
377381
```
378382
"""
379-
falses(dims::Dims) = fill!(BitArray(dims), false)
383+
falses(dims::Dims) = fill!(BitArray(uninitialized, dims), false)
380384
falses(dims::Integer...) = falses(map(Int,dims))
381385
"""
382386
falses(A)
@@ -411,7 +415,7 @@ julia> trues(2,3)
411415
true true true
412416
```
413417
"""
414-
trues(dims::Dims) = fill!(BitArray(dims), true)
418+
trues(dims::Dims) = fill!(BitArray(uninitialized, dims), true)
415419
trues(dims::Integer...) = trues(map(Int,dims))
416420
"""
417421
trues(A)
@@ -490,7 +494,7 @@ reshape(B::BitArray, dims::Tuple{Vararg{Int}}) = _bitreshape(B, dims)
490494
function _bitreshape(B::BitArray, dims::NTuple{N,Int}) where N
491495
prod(dims) == length(B) ||
492496
throw(DimensionMismatch("new dimensions $(dims) must be consistent with array size $(length(B))"))
493-
Br = BitArray{N}(ntuple(i->0,Val(N))...)
497+
Br = BitArray{N}(uninitialized, ntuple(i->0,Val(N))...)
494498
Br.chunks = B.chunks
495499
Br.len = prod(dims)
496500
N != 1 && (Br.dims = dims)
@@ -512,7 +516,7 @@ end
512516

513517
convert(::Type{BitArray}, A::AbstractArray{T,N}) where {T,N} = convert(BitArray{N}, A)
514518
function convert(::Type{BitArray{N}}, A::AbstractArray{T,N}) where N where T
515-
B = BitArray(size(A))
519+
B = BitArray(uninitialized, size(A))
516520
Bc = B.chunks
517521
l = length(B)
518522
l == 0 && return B
@@ -537,7 +541,7 @@ function convert(::Type{BitArray{N}}, A::AbstractArray{T,N}) where N where T
537541
end
538542

539543
function convert(::Type{BitArray{N}}, A::Array{Bool,N}) where N
540-
B = BitArray(size(A))
544+
B = BitArray(uninitialized, size(A))
541545
Bc = B.chunks
542546
l = length(B)
543547
l == 0 && return B
@@ -595,7 +599,7 @@ gen_bitarray(isz::IteratorSize, itr) = gen_bitarray_from_itr(itr, start(itr))
595599

596600
# generic iterable with known shape
597601
function gen_bitarray(::HasShape, itr)
598-
B = BitArray(size(itr))
602+
B = BitArray(uninitialized, size(itr))
599603
for (I,x) in zip(CartesianRange(indices(itr)), itr)
600604
B[I] = x
601605
end
@@ -604,13 +608,12 @@ end
604608

605609
# generator with known shape or length
606610
function gen_bitarray(::HasShape, itr::Generator)
607-
B = BitArray(size(itr))
611+
B = BitArray(uninitialized, size(itr))
608612
return fill_bitarray_from_itr!(B, itr, start(itr))
609613
end
610614
function gen_bitarray(::HasLength, itr)
611-
n = length(itr)
612-
B = BitArray(n)
613-
return fill_bitarray_from_itr!(B, itr, start(itr))
615+
b = BitVector(uninitialized, length(itr))
616+
return fill_bitarray_from_itr!(b, itr, start(itr))
614617
end
615618

616619
gen_bitarray(::IsInfinite, itr) = throw(ArgumentError("infinite-size iterable used in BitArray constructor"))
@@ -619,7 +622,7 @@ gen_bitarray(::IsInfinite, itr) = throw(ArgumentError("infinite-size iterable u
619622
# use a Vector{Bool} cache for performance reasons
620623

621624
function gen_bitarray_from_itr(itr, st)
622-
B = empty!(BitArray(bitcache_size))
625+
B = empty!(BitVector(uninitialized, bitcache_size))
623626
C = Vector{Bool}(uninitialized, bitcache_size)
624627
Bc = B.chunks
625628
ind = 1
@@ -1050,7 +1053,7 @@ function splice!(B::BitVector, i::Integer)
10501053
return v
10511054
end
10521055

1053-
const _default_bit_splice = BitVector(0)
1056+
const _default_bit_splice = BitVector()
10541057

10551058
function splice!(B::BitVector, r::Union{UnitRange{Int}, Integer}, ins::AbstractArray = _default_bit_splice)
10561059
n = length(B)
@@ -1064,7 +1067,7 @@ function splice!(B::BitVector, r::Union{UnitRange{Int}, Integer}, ins::AbstractA
10641067

10651068
if (i_f > n)
10661069
append!(B, Bins)
1067-
return BitVector(0)
1070+
return BitVector()
10681071
end
10691072

10701073
v = B[r] # TODO: change to a copy if/when subscripting becomes an ArrayView
@@ -1094,7 +1097,7 @@ function splice!(B::BitVector, r::Union{UnitRange{Int}, Integer}, ins::AbstractA
10941097
end
10951098

10961099
function splice!(B::BitVector, r::Union{UnitRange{Int}, Integer}, ins)
1097-
Bins = BitArray(length(ins))
1100+
Bins = BitVector(uninitialized, length(ins))
10981101
i = 1
10991102
for x in ins
11001103
Bins[i] = Bool(x)
@@ -1221,7 +1224,7 @@ broadcast(::typeof(xor), x::Bool, B::BitArray) = broadcast(xor, B, x)
12211224
for f in (:&, :|, :xor)
12221225
@eval begin
12231226
function broadcast(::typeof($f), A::BitArray, B::BitArray)
1224-
F = BitArray(promote_shape(size(A),size(B))...)
1227+
F = BitArray(uninitialized, promote_shape(size(A),size(B))...)
12251228
Fc = F.chunks
12261229
Ac = A.chunks
12271230
Bc = B.chunks
@@ -1803,7 +1806,7 @@ function hcat(B::BitVector...)
18031806
length(B[j]) == height ||
18041807
throw(DimensionMismatch("dimensions must match"))
18051808
end
1806-
M = BitArray(height, length(B))
1809+
M = BitMatrix(uninitialized, height, length(B))
18071810
for j = 1:length(B)
18081811
copy_chunks!(M.chunks, (height*(j-1))+1, B[j].chunks, 1, height)
18091812
end
@@ -1815,7 +1818,7 @@ function vcat(V::BitVector...)
18151818
for Vk in V
18161819
n += length(Vk)
18171820
end
1818-
B = BitArray(n)
1821+
B = BitVector(uninitialized, n)
18191822
j = 1
18201823
for Vk in V
18211824
copy_chunks!(B.chunks, j, Vk.chunks, 1, length(Vk))
@@ -1837,7 +1840,7 @@ function hcat(A::Union{BitMatrix,BitVector}...)
18371840
throw(DimensionMismatch("row lengths must match"))
18381841
end
18391842

1840-
B = BitArray(nrows, ncols)
1843+
B = BitMatrix(uninitialized, nrows, ncols)
18411844

18421845
pos = 1
18431846
for k = 1:nargs
@@ -1857,7 +1860,7 @@ function vcat(A::BitMatrix...)
18571860
size(A[j], 2) == ncols ||
18581861
throw(DimensionMismatch("column lengths must match"))
18591862
end
1860-
B = BitArray(nrows, ncols)
1863+
B = BitMatrix(uninitialized, nrows, ncols)
18611864
Bc = B.chunks
18621865
nrowsA = [size(a, 1) for a in A]
18631866
Ac = [a.chunks for a in A]

base/deprecated.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,11 @@ end
18771877
@deprecate diagm(v::AbstractVector, k::Integer) diagm(k => v)
18781878
@deprecate diagm(x::Number) fill(x, 1, 1)
18791879

1880+
# deprecate BitArray{...}(shape...) constructors to BitArray{...}(uninitialized, shape...) equivalents
1881+
@deprecate BitArray{N}(dims::Vararg{Int,N}) where {N} BitArray{N}(uninitialized, dims)
1882+
@deprecate BitArray(dims::NTuple{N,Int}) where {N} BitArray(uninitialized, dims...)
1883+
@deprecate BitArray(dims::Integer...) BitArray(uninitialized, dims)
1884+
18801885
## deprecate full
18811886
export full
18821887
# full no-op fallback

base/pkg/cache.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ function prefetch(pkg::AbstractString, url::AbstractString, sha1s::Vector)
5858
end
5959
try
6060
LibGit2.set_remote_url(repo, "origin", normalized_url)
61-
in_cache = BitArray(map(sha1->LibGit2.iscommit(sha1, repo), sha1s))
61+
in_cache = BitVector(map(sha1->LibGit2.iscommit(sha1, repo), sha1s))
6262
if !all(in_cache)
6363
info("Updating cache of $pkg...")
6464
LibGit2.fetch(repo)
65-
in_cache = BitArray(map(sha1->LibGit2.iscommit(sha1, repo), sha1s))
65+
in_cache = BitVector(map(sha1->LibGit2.iscommit(sha1, repo), sha1s))
6666
end
6767
sha1s[.!in_cache]
6868
finally

base/random/misc.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ julia> bitrand(rng, 10)
3333
true
3434
```
3535
"""
36-
bitrand(r::AbstractRNG, dims::Dims) = rand!(r, BitArray(dims))
37-
bitrand(r::AbstractRNG, dims::Integer...) = rand!(r, BitArray(convert(Dims, dims)))
36+
bitrand(r::AbstractRNG, dims::Dims) = rand!(r, BitArray(uninitialized, dims))
37+
bitrand(r::AbstractRNG, dims::Integer...) = rand!(r, BitArray(uninitialized, convert(Dims, dims)))
3838

39-
bitrand(dims::Dims) = rand!(BitArray(dims))
40-
bitrand(dims::Integer...) = rand!(BitArray(convert(Dims, dims)))
39+
bitrand(dims::Dims) = rand!(BitArray(uninitialized, dims))
40+
bitrand(dims::Integer...) = rand!(BitArray(uninitialized, convert(Dims, dims)))
4141

4242

4343
## randstring (often useful for temporary filenames/dirnames)

doc/src/stdlib/arrays.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Base.getindex(::Type, ::Any...)
2020
Base.zeros
2121
Base.ones
2222
Base.BitArray
23-
Base.BitArray(::Integer...)
23+
Base.BitArray(::Uninitialized, ::Integer...)
2424
Base.BitArray(::Any)
2525
Base.trues
2626
Base.falses

stdlib/Mmap/src/Mmap.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ function mmap(io::IOStream, ::Type{<:BitArray}, dims::NTuple{N,Integer},
259259
throw(ArgumentError("the given file does not contain a valid BitArray of size $(join(dims, 'x')) (open with \"r+\" mode to override)"))
260260
end
261261
end
262-
B = BitArray{N}(ntuple(i->0,Val(N))...)
262+
B = BitArray{N}(uninitialized, ntuple(i->0,Val(N))...)
263263
B.chunks = chunks
264264
B.len = n
265265
if N != 1

test/TestHelpers.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,10 @@ function Base.similar(A::AbstractArray, T::Type, inds::Tuple{UnitRange,Vararg{Un
181181
OffsetArray(B, map(indsoffset, inds))
182182
end
183183

184-
Base.similar(f::Union{Function,Type}, shape::Tuple{UnitRange,Vararg{UnitRange}}) = OffsetArray(f(map(length, shape)), map(indsoffset, shape))
184+
Base.similar(f::Union{Function,Type}, shape::Tuple{UnitRange,Vararg{UnitRange}}) =
185+
OffsetArray(f(map(length, shape)), map(indsoffset, shape))
186+
Base.similar(::Type{T}, shape::Tuple{UnitRange,Vararg{UnitRange}}) where {T<:BitArray} =
187+
OffsetArray(T(uninitialized, map(length, shape)), map(indsoffset, shape))
185188

186189
Base.reshape(A::AbstractArray, inds::Tuple{UnitRange,Vararg{UnitRange}}) = OffsetArray(reshape(A, map(length, inds)), map(indsoffset, inds))
187190

0 commit comments

Comments
 (0)