Skip to content

Commit

Permalink
more redesign
Browse files Browse the repository at this point in the history
  • Loading branch information
Jutho committed Feb 18, 2015
1 parent b34f1d2 commit 79f48c6
Show file tree
Hide file tree
Showing 3 changed files with 371 additions and 237 deletions.
265 changes: 177 additions & 88 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -615,50 +615,183 @@ catrange(catdim, x) = 1
catndims(x::AbstractArray) = ndims(x)
catndims(x) = 1

cat_eltypeof{T}(X::T...) = T
cat_eltypeof{T}(X::AbstractArray{T}...) = T
cat_eltypeof(x, X...) = promote_type(cat_eltypeof(x), cat_eltypeof(X...))
vcatsize()=(0,)
vcatsize(x::AbstractArray) = size(x)
vcatsize(x) = (1,)
vcatsize(X::Number...) = (length(X),)
stagedfunction vcatsize(X::AbstractVector...)
quote
$(Expr(:meta,:inline))
s1 = 0
for x in X
s1 += length(x)
end
(s1,)
end
end
stagedfunction vcatsize(X::AbstractMatrix...)
quote
$(Expr(:meta,:inline))
s1 = 0
for x in X
s1 += size(x,1)
end
(s1, size(X[1],2))
end
end
stagedfunction vcatsize(X...)
quote
$(Expr(:meta,:inline))
sz = vcatsize(X[1])
for k = 2:length(X)
sz = vcatsize_add(sz, vcatsize(X[k]))
end
sz
end
end

cat_containertypeof(X...) = Array
cat_container(T, sz, ::Type{Array}) = Array(T, sz)
stagedfunction vcatsize_add{N1,N2}(sz::NTuple{N1,Int}, sz2::NTuple{N2,Int})
newsize = Expr(:tuple, :(sz[1]+sz2[1]), [:(sz[$i]) for i=2:N1]..., [1 for i=N1+1:N2]...)
quote
$(Expr(:meta,:inline))
$newsize
end
end

stagedfunction vcatsize{N,T}(catlength, x::AbstractArray{T,N})
Expr(:tuple,:(catlength),[:(size(x,$d)) for d=2:N]...)
end
vcatsize(catlength, x) = catlength

stagedfunction hcatsize{N,T}(catlength, x::AbstractArray{T,N})
Expr(:tuple,:(size(x,1)),:(catlength),[:(size(x,$d)) for d=3:N]...)
end
hcatsize(catlength, x) = (1,catlength)

function catsize(n, catdim, catndims, x)
if catndims == 1
return (n,)
elseif catndims == 2
n1 = catdim==1 ? n : catlength(1, x)
n2 = catdim==2 ? n : catlength(2, x)
return (n1,n2)
elseif catndims == 3
n1 = catdim==1 ? n : catlength(1, x)
n2 = catdim==2 ? n : catlength(2, x)
n3 = catdim==3 ? n : catlength(3, x)
return (n1,n2,n3)
elseif catndims == 4
n1 = catdim==1 ? n : catlength(1, x)
n2 = catdim==2 ? n : catlength(2, x)
n3 = catdim==3 ? n : catlength(3, x)
n4 = catdim==4 ? n : catlength(4, x)
return (n1,n2,n3,n4)
else
return ntuple(d->(d==catdim ? n : catlength(d, x)), catndims)
hcatsize(x::AbstractArray) = size(x)
hcatsize(x) = (1,1)
hcatsize(x::Number, X::Number...) = (1,length(X)+1)
hcatsize(x::AbstractVector, X::AbstractVector...) = (length(x), length(X)+1)
stagedfunction hcatsize(x::AbstractVecOrMat, X::AbstractVecOrMat...)
quote
$(Expr(:meta,:inline))
s1 = size(x,1)
s2 = size(x,2)
for Xk in X
s2 += size(Xk,2)
end
(s1, s2)
end
end
stagedfunction hcatsize(x,X...)
quote
$(Expr(:meta,:inline))
sz = hcatsize(x)
for Xk in X
sz = hcatsize_add(sz, hcatsize(Xk))
end
sz
end
end

stagedfunction hvcatsize{N,T}(nr, nc, x::AbstractArray{T,N})
Expr(:tuple,:(nr),:(nc),[:(size(x,$d)) for d=3:N]...)
stagedfunction hcatsize_add{N1,N2}(sz::NTuple{N1,Int}, sz2::NTuple{N2, Int})
newsize = Expr(:tuple, :(sz[1]), :(sz[2]+sz2[2]), [:(sz[$i]) for i=3:N1]..., [1 for i=N1+1:N2]...)
quote
$(Expr(:meta,:inline))
$newsize
end
end

catsize(catdim, x::AbstractArray) = size(x, (1:catdim)...)
catsize(catdim, x) = tuple(ones(Int,catdim)...)
catsize(catdim, x::Number, X::Number...) = tuple(ones(Int,catdim-1)...,length(X)+1)
catsize(catdim, x, X...) = begin
sz = catsize(catdim, x)
for Xk in X
sz = catsize_add(catdim, sz, catsize(catdim, Xk))
end
sz
end

stagedfunction catsize_add{N1,N2}(catdim, sz::NTuple{N1,Int}, sz2::NTuple{N2,Int})
newsize = Expr(:tuple, [:($i==catdim ? (sz[$i]+sz2[$i]) : sz[$i]) for i=1:N1]..., [1 for i=N1+1:N2]...)
quote
$(Expr(:meta,:inline))
$newsize
end
end
hvcatsize(nr, nc, x) = (nr, nc)


function hvcatsize(rows::(Integer...), x::AbstractVecOrMat, X::AbstractVecOrMat...)
nr = size(x,1)
nc = size(x,2)
for i = 2:rows[1]
nc += size(X[i-1], 2)
end
k = rows[1]
for i = 2:length(rows)
nr += size(X[k], 1)
k += rows[i]
end
return (nr, nc)
end
hvcatsize(rows::(Integer...), x::Number, X::Number...) = (length(rows), rows[1])
function hvcatsize(rows::(Integer...), x, X...)
rowsz = hcatsize(x)
k=1
for j = 2:row[1]
rowsz = hcatsize_add(rowsz, X[k])
k+=1
end
sz = rowsz
for i = 2:nbr
rowsz = hcatsize(X[k])
k+=1
for j = 2:row[i]
rowsz = hcatsize_add(rowsz, X[k])
k+=1
end
sz = vcatsize_add(sz, rowsz)
end
sz
end



cat_eltypeof{T}(x::T, X::T...) = T
cat_eltypeof{T}(x::AbstractArray{T}, X::AbstractArray{T}...) = T
cat_eltypeof(x, X...) = promote_type(cat_eltypeof(x), cat_eltypeof(X...))

cat_containertypeof(X...) = Array
cat_container(T, sz, ::Type{Array}) = Array(T, sz)

# stagedfunction vcatsize{N,T}(catlength, x::AbstractArray{T,N})
# Expr(:tuple,:(catlength),[:(size(x,$d)) for d=2:N]...)
# end
# vcatsize(catlength, x) = catlength
#
# stagedfunction hcatsize{N,T}(catlength, x::AbstractArray{T,N})
# Expr(:tuple,:(size(x,1)),:(catlength),[:(size(x,$d)) for d=3:N]...)
# end
# hcatsize(catlength, x) = (1,catlength)
#
# function catsize(n, catdim, catndims, x)
# if catndims == 1
# return (n,)
# elseif catndims == 2
# n1 = catdim==1 ? n : catlength(1, x)
# n2 = catdim==2 ? n : catlength(2, x)
# return (n1,n2)
# elseif catndims == 3
# n1 = catdim==1 ? n : catlength(1, x)
# n2 = catdim==2 ? n : catlength(2, x)
# n3 = catdim==3 ? n : catlength(3, x)
# return (n1,n2,n3)
# elseif catndims == 4
# n1 = catdim==1 ? n : catlength(1, x)
# n2 = catdim==2 ? n : catlength(2, x)
# n3 = catdim==3 ? n : catlength(3, x)
# n4 = catdim==4 ? n : catlength(4, x)
# return (n1,n2,n3,n4)
# else
# return ntuple(d->(d==catdim ? n : catlength(d, x)), catndims)
# end
# end
#
# stagedfunction hvcatsize{N,T}(nr, nc, x::AbstractArray{T,N})
# Expr(:tuple,:(nr),:(nc),[:(size(x,$d)) for d=3:N]...)
# end
# hvcatsize(nr, nc, x) = (nr, nc)

stagedfunction vcat_fill!{T,N}(C::AbstractArray{T,N}, catrange, x)
ranges = [:(:) for d=2:N]
Expand Down Expand Up @@ -699,16 +832,8 @@ end

## vcat
function typed_vcat(T::Type, X...)
n::Int = catlength(1, X[1])
N::Int = catndims(X[1])
k = 1
for i = 2:length(X)
x = X[i]
n += catlength(1, x)
Ni = catndims(x)
Ni > N && (k=i;N=Ni)
end
C=cat_container(T, vcatsize(n,X[k]), cat_containertypeof(X...))
sz = vcatsize(X...)
C = cat_container(T, vcatsize(X...), cat_containertypeof(X...))
offset = 0
for i = 1:length(X)
x = X[i]
Expand All @@ -723,16 +848,7 @@ vcat(X...) = typed_vcat(cat_eltypeof(X...), X...)

## hcat
function typed_hcat(T::Type, X...)
n::Int = catlength(2, X[1])
N::Int = catndims(X[1])
k = 1
for i = 2:length(X)
x = X[i]
n += catlength(2, x)
Ni = catndims(x)
Ni > N && (k=i;N=Ni)
end
C=cat_container(T, hcatsize(n,X[k]), cat_containertypeof(X...))
C=cat_container(T, hcatsize(X...), cat_containertypeof(X...))
offset = 0
for i = 1:length(X)
x = X[i]
Expand All @@ -747,13 +863,7 @@ hcat(X...) = typed_hcat(cat_eltypeof(X...), X...)

## cat: general case
function typed_cat(T::Type, catdim::Integer, X...)
n::Int = catlength(catdim, X[1])
N::Int = max(catdim, catndims(X[1]))
for i = 2:length(X)
n += catlength(catdim,X[i])
N = max(N, catndims(X[i]))
end
C=cat_container(T, catsize(n, catdim, N, X[1]), cat_containertypeof(X...))
C=cat_container(T, catsize(catdim, X...), cat_containertypeof(X...))
offset = 0
for i = 1:length(X)
x = X[i]
Expand All @@ -770,25 +880,7 @@ cat(catdim::Integer, X...) = typed_cat(cat_eltypeof(X...), catdim, X...)

## hvcat: 2d horizontal and vertical concatenation
function typed_hvcat(T::Type, rows::(Int...), X...)
nbr = length(rows) # number of block rows
nc::Int = 0
for k = 1:rows[1]
nc += catlength(2,X[k])
end
nr::Int = 0
k = 1
for i = 1:nbr
nr += catlength(1,X[k])
k += rows[i]
end
k = 1
N::Int = catndims(X[k])
for i = 2:length(X)
Ni = catndims(X[i])
Ni > N && (k=i;N=Ni)
end

C = cat_container(T, hvcatsize(nr, nc, X[k]), cat_containertypeof(X...))
C = cat_container(T, hvcatsize(rows,X...), cat_containertypeof(X...))
rowoffset = 0
k = 1
for nbc in rows
Expand Down Expand Up @@ -827,10 +919,7 @@ end
function typed_dcat(T::Type, catdims, X...)
dims = Int[d for d in catdims]
M = length(dims)
N::Int = maximum(dims)
for i = 1:length(X)
N = max(N,catndims(X[i]))
end
N = maximum(dims)

catsizes = zeros(Int, N)
for i = 1:length(X)
Expand Down
Loading

0 comments on commit 79f48c6

Please sign in to comment.