Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace Array{...}(shape...)-like calls in base/a* #24755

Merged
merged 1 commit into from
Nov 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ argument or as a series of integer arguments.

Custom AbstractArray subtypes may choose which specific array type is best-suited to return
for the given element type and dimensionality. If they do not specialize this method, the
default is an `Array{element_type}(dims...)`.
default is an `Array{element_type}(uninitialized, dims...)`.

For example, `similar(1:10, 1, 4)` returns an uninitialized `Array{Int,2}` since ranges are
neither mutable nor support 2 dimensions:
Expand Down Expand Up @@ -525,7 +525,7 @@ similar(a::AbstractArray{T}, dims::DimOrInd...) where {T} = similar(a,
similar(a::AbstractArray, ::Type{T}, dims::DimOrInd...) where {T} = similar(a, T, to_shape(dims))
similar(a::AbstractArray, ::Type{T}, dims::NeedsShaping) where {T} = similar(a, T, to_shape(dims))
# similar creates an Array by default
similar(a::AbstractArray, ::Type{T}, dims::Dims{N}) where {T,N} = Array{T,N}(dims)
similar(a::AbstractArray, ::Type{T}, dims::Dims{N}) where {T,N} = Array{T,N}(uninitialized, dims)

to_shape(::Tuple{}) = ()
to_shape(dims::Dims) = dims
Expand All @@ -550,7 +550,7 @@ argument. `storagetype` might be a type or a function.
creates an array that "acts like" an `Array{Int}` (and might indeed be
backed by one), but which is indexed identically to `A`. If `A` has
conventional indexing, this will be identical to
`Array{Int}(size(A))`, but if `A` has unconventional indexing then the
`Array{Int}(uninitialized, size(A))`, but if `A` has unconventional indexing then the
indices of the result will match `A`.

similar(BitArray, (indices(A, 2),))
Expand Down Expand Up @@ -1078,21 +1078,21 @@ promote_eltype() = Bottom
promote_eltype(v1, vs...) = promote_type(eltype(v1), promote_eltype(vs...))

#TODO: ERROR CHECK
cat(catdim::Integer) = Array{Any,1}(0)
cat(catdim::Integer) = Vector{Any}()

typed_vcat(::Type{T}) where {T} = Array{T,1}(0)
typed_hcat(::Type{T}) where {T} = Array{T,1}(0)
typed_vcat(::Type{T}) where {T} = Vector{T}()
typed_hcat(::Type{T}) where {T} = Vector{T}()

## cat: special cases
vcat(X::T...) where {T} = T[ X[i] for i=1:length(X) ]
vcat(X::T...) where {T<:Number} = T[ X[i] for i=1:length(X) ]
hcat(X::T...) where {T} = T[ X[j] for i=1:1, j=1:length(X) ]
hcat(X::T...) where {T<:Number} = T[ X[j] for i=1:1, j=1:length(X) ]

vcat(X::Number...) = hvcat_fill(Vector{promote_typeof(X...)}(length(X)), X)
hcat(X::Number...) = hvcat_fill(Matrix{promote_typeof(X...)}(1,length(X)), X)
typed_vcat(::Type{T}, X::Number...) where {T} = hvcat_fill(Array{T,1}(length(X)), X)
typed_hcat(::Type{T}, X::Number...) where {T} = hvcat_fill(Array{T,2}(1,length(X)), X)
vcat(X::Number...) = hvcat_fill(Vector{promote_typeof(X...)}(uninitialized, length(X)), X)
hcat(X::Number...) = hvcat_fill(Matrix{promote_typeof(X...)}(uninitialized, 1,length(X)), X)
typed_vcat(::Type{T}, X::Number...) where {T} = hvcat_fill(Vector{T}(uninitialized, length(X)), X)
typed_hcat(::Type{T}, X::Number...) where {T} = hvcat_fill(Matrix{T}(uninitialized, 1,length(X)), X)

vcat(V::AbstractVector...) = typed_vcat(promote_eltype(V...), V...)
vcat(V::AbstractVector{T}...) where {T} = typed_vcat(T, V...)
Expand Down Expand Up @@ -1184,7 +1184,7 @@ cat_size(A::AbstractArray, d) = size(A, d)
cat_indices(A, d) = OneTo(1)
cat_indices(A::AbstractArray, d) = indices(A, d)

cat_similar(A, T, shape) = Array{T}(shape)
cat_similar(A, T, shape) = Array{T}(uninitialized, shape)
cat_similar(A::AbstractArray, T, shape) = similar(A, T, shape)

cat_shape(dims, shape::Tuple) = shape
Expand Down Expand Up @@ -1233,7 +1233,7 @@ end

function _cat(A, shape::NTuple{N}, catdims, X...) where N
offsets = zeros(Int, N)
inds = Vector{UnitRange{Int}}(N)
inds = Vector{UnitRange{Int}}(uninitialized, N)
concat = copy!(zeros(Bool, N), catdims)
for x in X
for i = 1:N
Expand Down Expand Up @@ -1457,13 +1457,13 @@ function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, as::AbstractVecOrMat..
end

hvcat(rows::Tuple{Vararg{Int}}) = []
typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}) where {T} = Array{T,1}(0)
typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}) where {T} = Vector{T}()

function hvcat(rows::Tuple{Vararg{Int}}, xs::T...) where T<:Number
nr = length(rows)
nc = rows[1]

a = Array{T,2}(nr, nc)
a = Matrix{T}(uninitialized, nr, nc)
if length(a) != length(xs)
throw(ArgumentError("argument count does not match specified shape (expected $(length(a)), got $(length(xs)))"))
end
Expand Down Expand Up @@ -1507,12 +1507,12 @@ function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, xs::Number...) where T
if nr*nc != len
throw(ArgumentError("argument count $(len) does not match specified shape $((nr,nc))"))
end
hvcat_fill(Array{T,2}(nr, nc), xs)
hvcat_fill(Matrix{T}(uninitialized, nr, nc), xs)
end

function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, as...) where T
nbr = length(rows) # number of block rows
rs = Array{Any,1}(nbr)
rs = Vector{Any}(uninitialized, nbr)
a = 1
for i = 1:nbr
rs[i] = typed_hcat(T, as[a:a-1+rows[i]]...)
Expand Down
76 changes: 38 additions & 38 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ eltype(x) = eltype(typeof(x))

import Core: arraysize, arrayset, arrayref

vect() = Array{Any,1}(0)
vect() = Vector{Any}()
vect(X::T...) where {T} = T[ X[i] for i = 1:length(X) ]

function vect(X...)
T = promote_typeof(X...)
#T[ X[i] for i=1:length(X) ]
# TODO: this is currently much faster. should figure out why. not clear.
return copy!(Array{T,1}(length(X)), X)
return copy!(Vector{T}(uninitialized, length(X)), X)
end

size(a::Array, d) = arraysize(a, d)
Expand Down Expand Up @@ -244,13 +244,13 @@ end

## Constructors ##

similar(a::Array{T,1}) where {T} = Array{T,1}(size(a,1))
similar(a::Array{T,2}) where {T} = Array{T,2}(size(a,1), size(a,2))
similar(a::Array{T,1}, S::Type) where {T} = Array{S,1}(size(a,1))
similar(a::Array{T,2}, S::Type) where {T} = Array{S,2}(size(a,1), size(a,2))
similar(a::Array{T}, m::Int) where {T} = Array{T,1}(m)
similar(a::Array, T::Type, dims::Dims{N}) where {N} = Array{T,N}(dims)
similar(a::Array{T}, dims::Dims{N}) where {T,N} = Array{T,N}(dims)
similar(a::Array{T,1}) where {T} = Vector{T}(uninitialized, size(a,1))
similar(a::Array{T,2}) where {T} = Matrix{T}(uninitialized, size(a,1), size(a,2))
similar(a::Array{T,1}, S::Type) where {T} = Vector{S}(uninitialized, size(a,1))
similar(a::Array{T,2}, S::Type) where {T} = Matrix{S}(uninitialized, size(a,1), size(a,2))
similar(a::Array{T}, m::Int) where {T} = Vector{T}(uninitialized, m)
similar(a::Array, T::Type, dims::Dims{N}) where {N} = Array{T,N}(uninitialized, dims)
similar(a::Array{T}, dims::Dims{N}) where {T,N} = Array{T,N}(uninitialized, dims)

# T[x...] constructs Array{T,1}
"""
Expand All @@ -275,26 +275,26 @@ julia> getindex(Int8, 1, 2, 3)
```
"""
function getindex(::Type{T}, vals...) where T
a = Array{T,1}(length(vals))
a = Vector{T}(uninitialized, length(vals))
@inbounds for i = 1:length(vals)
a[i] = vals[i]
end
return a
end

getindex(::Type{T}) where {T} = (@_inline_meta; Array{T,1}(0))
getindex(::Type{T}, x) where {T} = (@_inline_meta; a = Array{T,1}(1); @inbounds a[1] = x; a)
getindex(::Type{T}, x, y) where {T} = (@_inline_meta; a = Array{T,1}(2); @inbounds (a[1] = x; a[2] = y); a)
getindex(::Type{T}, x, y, z) where {T} = (@_inline_meta; a = Array{T,1}(3); @inbounds (a[1] = x; a[2] = y; a[3] = z); a)
getindex(::Type{T}) where {T} = (@_inline_meta; Vector{T}())
getindex(::Type{T}, x) where {T} = (@_inline_meta; a = Vector{T}(uninitialized, 1); @inbounds a[1] = x; a)
getindex(::Type{T}, x, y) where {T} = (@_inline_meta; a = Vector{T}(uninitialized, 2); @inbounds (a[1] = x; a[2] = y); a)
getindex(::Type{T}, x, y, z) where {T} = (@_inline_meta; a = Vector{T}(uninitialized, 3); @inbounds (a[1] = x; a[2] = y; a[3] = z); a)

function getindex(::Type{Any}, @nospecialize vals...)
a = Array{Any,1}(length(vals))
a = Vector{Any}(uninitialized, length(vals))
@inbounds for i = 1:length(vals)
a[i] = vals[i]
end
return a
end
getindex(::Type{Any}) = Array{Any,1}(0)
getindex(::Type{Any}) = Vector{Any}()

function fill!(a::Union{Array{UInt8}, Array{Int8}}, x::Integer)
ccall(:memset, Ptr{Void}, (Ptr{Void}, Cint, Csize_t), a, x, length(a))
Expand Down Expand Up @@ -329,8 +329,8 @@ julia> fill(1.0, (5,5))
If `x` is an object reference, all elements will refer to the same object. `fill(Foo(),
dims)` will return an array filled with the result of evaluating `Foo()` once.
"""
fill(v, dims::Dims) = fill!(Array{typeof(v)}(dims), v)
fill(v, dims::Integer...) = fill!(Array{typeof(v)}(dims...), v)
fill(v, dims::Dims) = fill!(Array{typeof(v)}(uninitialized, dims), v)
fill(v, dims::Integer...) = fill!(Array{typeof(v)}(uninitialized, dims...), v)

"""
zeros([A::AbstractArray,] [T=eltype(A)::Type,] [dims=size(A)::Tuple])
Expand Down Expand Up @@ -420,7 +420,7 @@ function ones end

for (fname, felt) in ((:zeros, :zero), (:ones, :one))
@eval begin
$fname(::Type{T}, dims::NTuple{N, Any}) where {T, N} = fill!(Array{T,N}(Dims(dims)), $felt(T))
$fname(::Type{T}, dims::NTuple{N, Any}) where {T, N} = fill!(Array{T,N}(uninitialized, Dims(dims)), $felt(T))
$fname(dims::Tuple) = ($fname)(Float64, dims)
$fname(::Type{T}, dims...) where {T} = $fname(T, dims)
$fname(dims...) = $fname(dims)
Expand Down Expand Up @@ -477,10 +477,10 @@ julia> collect(Float64, 1:2:5)
"""
collect(::Type{T}, itr) where {T} = _collect(T, itr, iteratorsize(itr))

_collect(::Type{T}, itr, isz::HasLength) where {T} = copy!(Array{T,1}(Int(length(itr)::Integer)), itr)
_collect(::Type{T}, itr, isz::HasLength) where {T} = copy!(Vector{T}(uninitialized, Int(length(itr)::Integer)), itr)
_collect(::Type{T}, itr, isz::HasShape) where {T} = copy!(similar(Array{T}, indices(itr)), itr)
function _collect(::Type{T}, itr, isz::SizeUnknown) where T
a = Array{T,1}(0)
a = Vector{T}()
for x in itr
push!(a,x)
end
Expand Down Expand Up @@ -532,9 +532,9 @@ end

_collect_indices(::Tuple{}, A) = copy!(Array{eltype(A)}(), A)
_collect_indices(indsA::Tuple{Vararg{OneTo}}, A) =
copy!(Array{eltype(A)}(length.(indsA)), A)
copy!(Array{eltype(A)}(uninitialized, length.(indsA)), A)
function _collect_indices(indsA, A)
B = Array{eltype(A)}(length.(indsA))
B = Array{eltype(A)}(uninitialized, length.(indsA))
copy!(B, CartesianRange(indices(B)), A, CartesianRange(indsA))
end

Expand All @@ -554,14 +554,14 @@ else
end
end

_array_for(::Type{T}, itr, ::HasLength) where {T} = Array{T,1}(Int(length(itr)::Integer))
_array_for(::Type{T}, itr, ::HasLength) where {T} = Vector{T}(uninitialized, Int(length(itr)::Integer))
_array_for(::Type{T}, itr, ::HasShape) where {T} = similar(Array{T}, indices(itr))::Array{T}

function collect(itr::Generator)
isz = iteratorsize(itr.iter)
et = @default_eltype(typeof(itr))
if isa(isz, SizeUnknown)
return grow_to!(Array{et,1}(0), itr)
return grow_to!(Vector{et}(), itr)
else
st = start(itr)
if done(itr,st)
Expand Down Expand Up @@ -1439,8 +1439,8 @@ end

# concatenations of homogeneous combinations of vectors, horizontal and vertical

vcat() = Array{Any,1}(0)
hcat() = Array{Any,1}(0)
vcat() = Vector{Any}()
hcat() = Vector{Any}()

function hcat(V::Vector{T}...) where T
height = length(V[1])
Expand All @@ -1457,7 +1457,7 @@ function vcat(arrays::Vector{T}...) where T
for a in arrays
n += length(a)
end
arr = Array{T,1}(n)
arr = Vector{T}(uninitialized, n)
ptr = pointer(arr)
if isbits(T)
elsz = Core.sizeof(T)
Expand Down Expand Up @@ -1752,14 +1752,14 @@ julia> find(isodd, [2, 4])
function find(testf::Function, A)
# use a dynamic-length array to store the indexes, then copy to a non-padded
# array for the return
tmpI = Array{Int,1}(0)
tmpI = Vector{Int}()
inds = _index_remapper(A)
for (i,a) = enumerate(A)
if testf(a)
push!(tmpI, inds[i])
end
end
I = Array{Int,1}(length(tmpI))
I = Vector{Int}(uninitialized, length(tmpI))
copy!(I, tmpI)
return I
end
Expand Down Expand Up @@ -1790,7 +1790,7 @@ julia> find(falses(3))
"""
function find(A)
nnzA = count(t -> t != 0, A)
I = Vector{Int}(nnzA)
I = Vector{Int}(uninitialized, nnzA)
cnt = 1
inds = _index_remapper(A)
warned = false
Expand All @@ -1807,8 +1807,8 @@ function find(A)
return I
end

find(x::Bool) = x ? [1] : Array{Int,1}(0)
find(testf::Function, x::Number) = !testf(x) ? Array{Int,1}(0) : [1]
find(x::Bool) = x ? [1] : Vector{Int}()
find(testf::Function, x::Number) = !testf(x) ? Vector{Int}() : [1]

findn(A::AbstractVector) = find(A)

Expand Down Expand Up @@ -1876,7 +1876,7 @@ function findnz(A::AbstractMatrix{T}) where T
nnzA = count(t -> t != 0, A)
I = zeros(Int, nnzA)
J = zeros(Int, nnzA)
NZs = Array{T,1}(nnzA)
NZs = Vector{T}(uninitialized, nnzA)
cnt = 1
if nnzA > 0
for j=indices(A,2), i=indices(A,1)
Expand Down Expand Up @@ -2225,7 +2225,7 @@ function filter!(f, a::AbstractVector)
end

function filter(f, a::Vector)
r = Vector{eltype(a)}(0)
r = Vector{eltype(a)}()
for ai in a
if f(ai)
push!(r, ai)
Expand All @@ -2238,7 +2238,7 @@ end
# These are moderately efficient, preserve order, and remove dupes.

function intersect(v1, vs...)
ret = Vector{promote_eltype(v1, vs...)}(0)
ret = Vector{promote_eltype(v1, vs...)}()
for v_elem in v1
inall = true
for vsi in vs
Expand All @@ -2254,7 +2254,7 @@ function intersect(v1, vs...)
end

function union(vs...)
ret = Vector{promote_eltype(vs...)}(0)
ret = Vector{promote_eltype(vs...)}()
seen = Set()
for v in vs
for v_elem in v
Expand Down Expand Up @@ -2287,7 +2287,7 @@ julia> setdiff([1,2,3],[3,4,5])
function setdiff(a, b)
args_type = promote_type(eltype(a), eltype(b))
bset = Set(b)
ret = Array{args_type,1}(0)
ret = Vector{args_type}()
seen = Set{eltype(a)}()
for a_elem in a
if !in(a_elem, seen) && !in(a_elem, bset)
Expand Down
6 changes: 3 additions & 3 deletions base/associative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ Dict{Int64,String} with 2 entries:
```
"""
function filter!(f, d::Associative)
badkeys = Vector{keytype(d)}(0)
badkeys = Vector{keytype(d)}()
try
for (k,v) in d
# don't delete!(d, k) here, since associative types
Expand All @@ -368,7 +368,7 @@ end
function filter!_dict_deprecation(e, f, d::Associative)
if isa(e, MethodError) && e.f === f
depwarn("In `filter!(f, dict)`, `f` is now passed a single pair instead of two arguments.", :filter!)
badkeys = Vector{keytype(d)}(0)
badkeys = Vector{keytype(d)}()
for (k,v) in d
# don't delete!(d, k) here, since associative types
# may not support mutation during iteration
Expand Down Expand Up @@ -495,7 +495,7 @@ See [`Dict`](@ref) for further help.
mutable struct ObjectIdDict <: Associative{Any,Any}
ht::Vector{Any}
ndel::Int
ObjectIdDict() = new(Vector{Any}(32), 0)
ObjectIdDict() = new(Vector{Any}(uninitialized, 32), 0)

function ObjectIdDict(itr)
d = ObjectIdDict()
Expand Down
2 changes: 1 addition & 1 deletion base/asyncmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ end

# Special handling for some types.
function asyncmap(f, s::AbstractString; kwargs...)
s2 = Array{Char,1}(length(s))
s2 = Vector{Char}(uninitialized, length(s))
asyncmap!(f, s2, s; kwargs...)
return convert(String, s2)
end
Expand Down