Skip to content

Commit

Permalink
ensure the array constructors are generally using leaftypes
Browse files Browse the repository at this point in the history
this makes for faster dynamic dispatch and apply-type construction

ref #16128
  • Loading branch information
vtjnash committed Jul 18, 2016
1 parent b390d66 commit cde97db
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
30 changes: 15 additions & 15 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ end

## Basic functions ##

vect() = Array{Any}(0)
vect() = Array{Any,1}(0)
vect{T}(X::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.
copy!(Array{T}(length(X)), X)
copy!(Array{T,1}(length(X)), X)
end

size{T,N}(t::AbstractArray{T,N}, d) = d <= N ? size(t)[d] : 1
Expand Down Expand Up @@ -348,7 +348,7 @@ similar{T}(a::AbstractArray{T}, dims::DimOrInd...) = similar(a, T, to_shap
similar( a::AbstractArray, T::Type, dims::DimOrInd...) = similar(a, T, to_shape(dims))
similar( a::AbstractArray, T::Type, dims) = similar(a, T, to_shape(dims))
# similar creates an Array by default
similar( a::AbstractArray, T::Type, dims::Dims) = Array{T}(dims)
similar( a::AbstractArray, T::Type, dims::Dims) = Array{T,nfields(dims)}(dims)

to_shape(::Tuple{}) = ()
to_shape(dims::Dims) = dims
Expand Down Expand Up @@ -870,12 +870,12 @@ promote_eltype() = Bottom
promote_eltype(v1, vs...) = promote_type(eltype(v1), promote_eltype(vs...))

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

vcat() = Array{Any}(0)
hcat() = Array{Any}(0)
typed_vcat{T}(::Type{T}) = Array{T}(0)
typed_hcat{T}(::Type{T}) = Array{T}(0)
vcat() = Array{Any,1}(0)
hcat() = Array{Any,1}(0)
typed_vcat{T}(::Type{T}) = Array{T,1}(0)
typed_hcat{T}(::Type{T}) = Array{T,1}(0)

## cat: special cases
vcat{T}(X::T...) = T[ X[i] for i=1:length(X) ]
Expand All @@ -885,8 +885,8 @@ hcat{T<:Number}(X::T...) = T[ X[j] for i=1:1, j=1:length(X) ]

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

vcat(V::AbstractVector...) = typed_vcat(promote_eltype(V...), V...)
vcat{T}(V::AbstractVector{T}...) = typed_vcat(T, V...)
Expand Down Expand Up @@ -1104,13 +1104,13 @@ function typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}, as::AbstractMatrix.
end

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

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

a = Array{T}(nr, nc)
a = Array{T,2}(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 @@ -1153,13 +1153,13 @@ function typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}, xs::Number...)
if nr*nc != len
throw(ArgumentError("argument count $(len) does not match specified shape $((nr,nc))"))
end
hvcat_fill(Array{T}(nr, nc), xs)
hvcat_fill(Array{T,2}(nr, nc), xs)
end

# fallback definition of hvcat in terms of hcat and vcat
function hvcat(rows::Tuple{Vararg{Int}}, as...)
nbr = length(rows) # number of block rows
rs = Array{Any}(nbr)
rs = Array{Any,1}(nbr)
a = 1
for i = 1:nbr
rs[i] = hcat(as[a:a-1+rows[i]]...)
Expand All @@ -1170,7 +1170,7 @@ end

function typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}, as...)
nbr = length(rows) # number of block rows
rs = Array{Any}(nbr)
rs = Array{Any,1}(nbr)
a = 1
for i = 1:nbr
rs[i] = hcat(as[a:a-1+rows[i]]...)
Expand Down
50 changes: 25 additions & 25 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,38 +114,38 @@ end

## Constructors ##

similar(a::Array, T::Type, dims::Dims) = Array{T}(dims)
similar{T}(a::Array{T,1}) = Array{T}(size(a,1))
similar{T}(a::Array{T,2}) = Array{T}(size(a,1), size(a,2))
similar{T}(a::Array{T,1}, dims::Dims) = Array{T}(dims)
similar{T}(a::Array{T,1}, m::Int) = Array{T}(m)
similar{T}(a::Array{T,1}, S::Type) = Array{S}(size(a,1))
similar{T}(a::Array{T,2}, dims::Dims) = Array{T}(dims)
similar{T}(a::Array{T,2}, m::Int) = Array{T}(m)
similar{T}(a::Array{T,2}, S::Type) = Array{S}(size(a,1), size(a,2))
similar(a::Array, T::Type, dims::Dims) = Array{T,nfields(dims)}(dims)
similar{T}(a::Array{T,1}) = Array{T,1}(size(a,1))
similar{T}(a::Array{T,2}) = Array{T,2}(size(a,1), size(a,2))
similar{T}(a::Array{T,1}, dims::Dims) = Array{T,nfields(dims)}(dims)
similar{T}(a::Array{T,1}, m::Int) = Array{T,1}(m)
similar{T}(a::Array{T,1}, S::Type) = Array{S,1}(size(a,1))
similar{T}(a::Array{T,2}, dims::Dims) = Array{T,nfields(dims)}(dims)
similar{T}(a::Array{T,2}, m::Int) = Array{T,1}(m)
similar{T}(a::Array{T,2}, S::Type) = Array{S,2}(size(a,1), size(a,2))

# T[x...] constructs Array{T,1}
function getindex(T::Type, vals...)
a = Array{T}(length(vals))
a = Array{T,1}(length(vals))
@inbounds for i = 1:length(vals)
a[i] = vals[i]
end
return a
end

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

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

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 @@ -195,7 +195,7 @@ convert{T,n}(::Type{Array{T}}, x::Array{T,n}) = x
convert{T,n}(::Type{Array{T,n}}, x::Array{T,n}) = x

convert{T,n,S}(::Type{Array{T}}, x::AbstractArray{S, n}) = convert(Array{T, n}, x)
convert{T,n,S}(::Type{Array{T,n}}, x::AbstractArray{S,n}) = copy!(Array{T}(size(x)), x)
convert{T,n,S}(::Type{Array{T,n}}, x::AbstractArray{S,n}) = copy!(Array{T,n}(size(x)), x)

promote_rule{T,n,S}(::Type{Array{T,n}}, ::Type{Array{S,n}}) = Array{promote_type(T,S),n}

Expand Down Expand Up @@ -647,7 +647,7 @@ function vcat{T}(arrays::Vector{T}...)
for a in arrays
n += length(a)
end
arr = Array{T}(n)
arr = Array{T,1}(n)
ptr = pointer(arr)
if isbits(T)
elsz = Core.sizeof(T)
Expand Down Expand Up @@ -763,13 +763,13 @@ findlast(testf::Function, A) = findprev(testf, A, length(A))
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}(0)
tmpI = Array{Int,1}(0)
for (i,a) = enumerate(A)
if testf(a)
push!(tmpI, i)
end
end
I = Array{Int}(length(tmpI))
I = Array{Int,1}(length(tmpI))
copy!(I, tmpI)
return I
end
Expand All @@ -787,8 +787,8 @@ function find(A)
return I
end

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

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

Expand All @@ -811,7 +811,7 @@ function findnz{T}(A::AbstractMatrix{T})
nnzA = countnz(A)
I = zeros(Int, nnzA)
J = zeros(Int, nnzA)
NZs = Array{T}(nnzA)
NZs = Array{T,1}(nnzA)
count = 1
if nnzA > 0
for j=1:size(A,2), i=1:size(A,1)
Expand Down Expand Up @@ -874,7 +874,7 @@ function indexin(a::AbstractArray, b::AbstractArray)
end

function findin(a, b)
ind = Array{Int}(0)
ind = Array{Int,1}(0)
bset = Set(b)
@inbounds for (i,ai) in enumerate(a)
ai in bset && push!(ind, i)
Expand Down Expand Up @@ -969,7 +969,7 @@ end
function setdiff(a, b)
args_type = promote_type(eltype(a), eltype(b))
bset = Set(b)
ret = Array{args_type}(0)
ret = Array{args_type,1}(0)
seen = Set{eltype(a)}()
for a_elem in a
if !in(a_elem, seen) && !in(a_elem, bset)
Expand Down
4 changes: 2 additions & 2 deletions base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ typealias NTuple{N,T} Tuple{Vararg{T,N}}
(::Type{Array{T,2}}){T}() = Array{T,2}(0, 0)

# TODO: possibly turn these into deprecations
Array{T,N}(::Type{T}, d::NTuple{N,Int}) = Array{T}(d)
Array{T}(::Type{T}, d::Int...) = Array{T}(d)
Array{T,N}(::Type{T}, d::NTuple{N,Int}) = Array{T,N}(d)
Array{T}(::Type{T}, d::Int...) = Array(T, d)
Array{T}(::Type{T}, m::Int) = Array{T,1}(m)
Array{T}(::Type{T}, m::Int,n::Int) = Array{T,2}(m,n)
Array{T}(::Type{T}, m::Int,n::Int,o::Int) = Array{T,3}(m,n,o)
Expand Down
2 changes: 1 addition & 1 deletion base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ include("array.jl")
(::Type{Matrix})(m::Integer, n::Integer) = Matrix{Any}(Int(m), Int(n))

# TODO: possibly turn these into deprecations
Array{T}(::Type{T}, d::Integer...) = Array{T}(convert(Tuple{Vararg{Int}}, d))
Array{T}(::Type{T}, d::Integer...) = Array(T, convert(Tuple{Vararg{Int}}, d))
Array{T}(::Type{T}, m::Integer) = Array{T,1}(Int(m))
Array{T}(::Type{T}, m::Integer,n::Integer) = Array{T,2}(Int(m),Int(n))
Array{T}(::Type{T}, m::Integer,n::Integer,o::Integer) = Array{T,3}(Int(m),Int(n),Int(o))
Expand Down

0 comments on commit cde97db

Please sign in to comment.