Skip to content

Commit

Permalink
Merge pull request #8599 from JuliaLang/jcb/sanecat
Browse files Browse the repository at this point in the history
WIP: Make [a, b] non-concatenating
  • Loading branch information
JeffBezanson committed Feb 9, 2015
2 parents 99b5f98 + e0b02f2 commit 075f84d
Show file tree
Hide file tree
Showing 67 changed files with 496 additions and 460 deletions.
17 changes: 17 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ New language features
Language changes
----------------

* `[x,y]` constructs a vector of `x` and `y` instead of concatenating them
([#3737], [#2488], [#8599]).

* `error(::Exception)` and `error(::Type{Exception})` have been deprecated
in favor of using an explicit `throw` ([#9690]).

Expand Down Expand Up @@ -941,6 +944,7 @@ Too numerous to mention.
[#2380]: https://github.com/JuliaLang/julia/issues/2380
[#2403]: https://github.com/JuliaLang/julia/issues/2403
[#2468]: https://github.com/JuliaLang/julia/issues/2468
[#2488]: https://github.com/JuliaLang/julia/issues/2488
[#2515]: https://github.com/JuliaLang/julia/issues/2515
[#2516]: https://github.com/JuliaLang/julia/issues/2516
[#2597]: https://github.com/JuliaLang/julia/issues/2597
Expand Down Expand Up @@ -986,6 +990,7 @@ Too numerous to mention.
[#3688]: https://github.com/JuliaLang/julia/issues/3688
[#3697]: https://github.com/JuliaLang/julia/issues/3697
[#3719]: https://github.com/JuliaLang/julia/issues/3719
[#3737]: https://github.com/JuliaLang/julia/issues/3737
[#3759]: https://github.com/JuliaLang/julia/issues/3759
[#3790]: https://github.com/JuliaLang/julia/issues/3790
[#3819]: https://github.com/JuliaLang/julia/issues/3819
Expand Down Expand Up @@ -1143,9 +1148,11 @@ Too numerous to mention.
[#8501]: https://github.com/JuliaLang/julia/issues/8501
[#8560]: https://github.com/JuliaLang/julia/issues/8560
[#8578]: https://github.com/JuliaLang/julia/issues/8578
[#8599]: https://github.com/JuliaLang/julia/issues/8599
[#8605]: https://github.com/JuliaLang/julia/issues/8605
[#8624]: https://github.com/JuliaLang/julia/issues/8624
[#8660]: https://github.com/JuliaLang/julia/issues/8660
[#8672]: https://github.com/JuliaLang/julia/issues/8672
[#8712]: https://github.com/JuliaLang/julia/issues/8712
[#8734]: https://github.com/JuliaLang/julia/issues/8734
[#8750]: https://github.com/JuliaLang/julia/issues/8750
Expand Down Expand Up @@ -1173,12 +1180,22 @@ Too numerous to mention.
[#9132]: https://github.com/JuliaLang/julia/issues/9132
[#9133]: https://github.com/JuliaLang/julia/issues/9133
[#9144]: https://github.com/JuliaLang/julia/issues/9144
[#9198]: https://github.com/JuliaLang/julia/issues/9198
[#9249]: https://github.com/JuliaLang/julia/issues/9249
[#9261]: https://github.com/JuliaLang/julia/issues/9261
[#9271]: https://github.com/JuliaLang/julia/issues/9271
[#9294]: https://github.com/JuliaLang/julia/issues/9294
[#9309]: https://github.com/JuliaLang/julia/issues/9309
[#9418]: https://github.com/JuliaLang/julia/issues/9418
[#9425]: https://github.com/JuliaLang/julia/issues/9425
[#9434]: https://github.com/JuliaLang/julia/issues/9434
[#9452]: https://github.com/JuliaLang/julia/issues/9452
[#9569]: https://github.com/JuliaLang/julia/issues/9569
[#9575]: https://github.com/JuliaLang/julia/issues/9575
[#9578]: https://github.com/JuliaLang/julia/issues/9578
[#9690]: https://github.com/JuliaLang/julia/issues/9690
[#9745]: https://github.com/JuliaLang/julia/issues/9745
[#9779]: https://github.com/JuliaLang/julia/issues/9779
[#9957]: https://github.com/JuliaLang/julia/issues/9957
[#10024]: https://github.com/JuliaLang/julia/issues/10024
[#10031]: https://github.com/JuliaLang/julia/issues/10031
2 changes: 1 addition & 1 deletion base/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ function keymap{D<:Dict}(keymaps::Array{D})
end

const escape_defaults = merge!(
AnyDict([char(i) => nothing for i=[1:26, 28:31]]), # Ignore control characters by default
AnyDict([char(i) => nothing for i=vcat(1:26, 28:31)]), # Ignore control characters by default
AnyDict( # And ignore other escape sequences by default
"\e*" => nothing,
"\e[*" => nothing,
Expand Down
2 changes: 1 addition & 1 deletion base/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function completions(string, pos)
# also search for packages
s = string[startpos:pos]
if dotpos <= startpos
for dir in [Pkg.dir(), LOAD_PATH, pwd()]
for dir in [Pkg.dir(); LOAD_PATH; pwd()]
isdir(dir) || continue
for pname in readdir(dir)
if pname[1] != '.' && pname != "METADATA" &&
Expand Down
160 changes: 85 additions & 75 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,45 @@ typealias AbstractVecOrMat{T} Union(AbstractVector{T}, AbstractMatrix{T})

## Basic functions ##

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

const _oldstyle_array_vcat_ = true

if _oldstyle_array_vcat_
function oldstyle_vcat_warning(n::Int)
if n == 1
before = "a"
after = "a;"
elseif n == 2
before = "a,b"
after = "a;b"
else
before = "a,b,..."
after = "a;b;..."
end
depwarn("[$before] concatenation is deprecated; use [$after] instead", :vect)
end
function vect(A::AbstractArray...)
oldstyle_vcat_warning(length(A))
vcat(A...)
end
function vect(X...)
for a in X
if typeof(a) <: AbstractArray
oldstyle_vcat_warning(length(X))
break
end
end
vcat(X...)
end
else
function vect(X...)
T = promote_typeof(X...)
T[ X[i] for i=1:length(X) ]
end
end

size{T,n}(t::AbstractArray{T,n}, d) = d <= n ? size(t)[d] : 1
size(x, d1::Integer, d2::Integer, dx::Integer...) = tuple(size(x, d1), size(x, d2, dx...)...)
eltype(x) = Any
Expand Down Expand Up @@ -488,7 +527,7 @@ function circshift{T,N}(a::AbstractArray{T,N}, shiftamts)
for i=1:N
s = size(a,i)
d = i<=length(shiftamts) ? shiftamts[i] : 0
I = tuple(I..., d==0 ? [1:s] : mod([-d:s-1-d], s).+1)
I = tuple(I..., d==0 ? [1:s;] : mod([-d:s-1-d;], s).+1)
end
a[(I::NTuple{N,Vector{Int}})...]
end
Expand Down Expand Up @@ -577,18 +616,12 @@ vcat{T}(X::T...) = T[ X[i] for i=1:length(X) ]
vcat{T<:Number}(X::T...) = T[ X[i] for i=1:length(X) ]

function vcat(X::Number...)
T = Bottom
for x in X
T = promote_type(T,typeof(x))
end
T = promote_typeof(X...)
hvcat_fill(Array(T,length(X)), X)
end

function hcat(X::Number...)
T = Bottom
for x in X
T = promote_type(T,typeof(x))
end
T = promote_typeof(X...)
hvcat_fill(Array(T,1,length(X)), X)
end

Expand Down Expand Up @@ -663,7 +696,13 @@ function vcat{T}(A::AbstractMatrix{T}...)
end

## cat: general case

function cat(catdims, X...)
T = promote_type(map(x->isa(x,AbstractArray) ? eltype(x) : typeof(x), X)...)
cat_t(catdims, T, X...)
end

function cat_t(catdims, typeC::Type, X...)
catdims = collect(catdims)
nargs = length(X)
ndimsX = Int[isa(a,AbstractArray) ? ndims(a) : 0 for a in X]
Expand All @@ -674,13 +713,11 @@ function cat(catdims, X...)
dims2cat[catdims[k]]=k
end

typeC = isa(X[1],AbstractArray) ? eltype(X[1]) : typeof(X[1])
dimsC = Int[d <= ndimsX[1] ? size(X[1],d) : 1 for d=1:ndimsC]
for k = 1:length(catdims)
catsizes[1,k] = dimsC[catdims[k]]
end
for i = 2:nargs
typeC = promote_type(typeC, isa(X[i], AbstractArray) ? eltype(X[i]) : typeof(X[i]))
for d = 1:ndimsC
currentdim = (d <= ndimsX[i] ? size(X[i],d) : 1)
if dims2cat[d]==0
Expand Down Expand Up @@ -711,57 +748,19 @@ end
vcat(X...) = cat(1, X...)
hcat(X...) = cat(2, X...)

cat{T}(catdims, A::AbstractArray{T}...) = cat_t(catdims, T, A...)

cat(catdims, A::AbstractArray...) =
cat_t(catdims, promote_eltype(A...), A...)

function cat_t(catdims, typeC, A::AbstractArray...)
catdims = collect(catdims)
nargs = length(A)
ndimsA = Int[ndims(a) for a in A]
ndimsC = max(maximum(ndimsA), maximum(catdims))
catsizes = zeros(Int,(nargs,length(catdims)))
dims2cat = zeros(Int,ndimsC)
for k = 1:length(catdims)
dims2cat[catdims[k]]=k
end
typed_vcat(T::Type, X...) = cat_t(1, T, X...)
typed_hcat(T::Type, X...) = cat_t(2, T, X...)

dimsC = Int[d <= ndimsA[1] ? size(A[1],d) : 1 for d=1:ndimsC]
for k = 1:length(catdims)
catsizes[1,k] = dimsC[catdims[k]]
end
for i = 2:nargs
for d = 1:ndimsC
currentdim = (d <= ndimsA[i] ? size(A[i],d) : 1)
if dims2cat[d]==0
dimsC[d] == currentdim || throw(DimensionMismatch("mismatch in dimension $(d)"))
else
dimsC[d] += currentdim
catsizes[i,dims2cat[d]] = currentdim
end
end
end
cat{T}(catdims, A::AbstractArray{T}...) = cat_t(catdims, T, A...)

C = similar(full(A[1]), typeC, tuple(dimsC...))
if length(catdims)>1
fill!(C,0)
end

offsets = zeros(Int,length(catdims))
for i=1:nargs
cat_one = [ dims2cat[d]==0 ? (1:dimsC[d]) : (offsets[dims2cat[d]]+(1:catsizes[i,dims2cat[d]])) for d=1:ndimsC]
C[cat_one...] = A[i]
for k = 1:length(catdims)
offsets[k] += catsizes[i,k]
end
end
return C
end
cat(catdims, A::AbstractArray...) = cat_t(catdims, promote_eltype(A...), A...)

vcat(A::AbstractArray...) = cat(1, A...)
hcat(A::AbstractArray...) = cat(2, A...)

typed_vcat(T::Type, A::AbstractArray...) = cat_t(1, T, A...)
typed_hcat(T::Type, A::AbstractArray...) = cat_t(2, T, A...)

# 2d horizontal and vertical concatenation

function hvcat(nbc::Integer, as...)
Expand Down Expand Up @@ -852,26 +851,49 @@ function hvcat_fill(a, xs)
a
end

function hvcat(rows::(Int...), xs::Number...)
function typed_hvcat(T::Type, rows::(Int...), xs::Number...)
nr = length(rows)
nc = rows[1]
#error check
for i = 2:nr
if nc != rows[i]
throw(ArgumentError("row $(i) has mismatched number of columns"))
end
end
len = length(xs)
T = typeof(xs[1])
for i=2:len
T = promote_type(T,typeof(xs[i]))
end
if nr*nc != len
throw(ArgumentError("argument count $(len) does not match specified shape $((nr,nc))"))
end
hvcat_fill(Array(T, nr, nc), xs)
end

function hvcat(rows::(Int...), xs::Number...)
T = promote_typeof(xs...)
typed_hvcat(T, rows, xs...)
end

# fallback definition of hvcat in terms of hcat and vcat
function hvcat(rows::(Int...), as...)
nbr = length(rows) # number of block rows
rs = cell(nbr)
a = 1
for i = 1:nbr
rs[i] = hcat(as[a:a-1+rows[i]]...)
a += rows[i]
end
vcat(rs...)
end

function typed_hvcat(T::Type, rows::(Int...), as...)
nbr = length(rows) # number of block rows
rs = cell(nbr)
a = 1
for i = 1:nbr
rs[i] = hcat(as[a:a-1+rows[i]]...)
a += rows[i]
end
T[rs...;]
end

## Reductions and scans ##

function isequal(A::AbstractArray, B::AbstractArray)
Expand Down Expand Up @@ -984,18 +1006,6 @@ end

## Other array functions ##

# fallback definition of hvcat in terms of hcat and vcat
function hvcat(rows::(Int...), as...)
nbr = length(rows) # number of block rows
rs = cell(nbr)
a = 1
for i = 1:nbr
rs[i] = hcat(as[a:a-1+rows[i]]...)
a += rows[i]
end
vcat(rs...)
end

function repmat(a::AbstractVecOrMat, m::Int, n::Int=1)
o, p = size(a,1), size(a,2)
b = similar(a, o*m, p*n)
Expand Down Expand Up @@ -1252,7 +1262,7 @@ function mapslices(f::Function, A::AbstractArray, dims::AbstractVector)

dimsA = [size(A)...]
ndimsA = ndims(A)
alldims = [1:ndimsA]
alldims = [1:ndimsA;]

otherdims = setdiff(alldims, dims)

Expand Down
6 changes: 6 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,15 @@ end

getindex(T::(Type...)) = Array(T,0)

if _oldstyle_array_vcat_
# T[a:b] and T[a:s:b] also construct typed ranges
function getindex{T<:Union(Char,Number)}(::Type{T}, r::Range)
depwarn("T[a:b] concatenation is deprecated; use T[a:b;] instead", :getindex)
copy!(Array(T,length(r)), r)
end

function getindex{T<:Union(Char,Number)}(::Type{T}, r1::Range, rs::Range...)
depwarn("T[a:b,...] concatenation is deprecated; use T[a:b;...] instead", :getindex)
a = Array(T,length(r1)+sum(length,rs))
o = 1
copy!(a, o, r1)
Expand All @@ -156,6 +159,7 @@ function getindex{T<:Union(Char,Number)}(::Type{T}, r1::Range, rs::Range...)
end
return a
end
end

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 @@ -239,6 +243,8 @@ convert{T,n}(::Type{Array{T,n}}, x::Array{T,n}) = x
convert{T,n,S}(::Type{Array{T}}, x::Array{S,n}) = convert(Array{T,n}, x)
convert{T,n,S}(::Type{Array{T,n}}, x::Array{S,n}) = copy!(similar(x,T), x)

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

function collect(T::Type, itr)
if applicable(length, itr)
# when length() isn't defined this branch might pollute the
Expand Down
2 changes: 1 addition & 1 deletion base/base64.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ end

# Based on code by Stefan Karpinski from https://github.com/hackerschool/WebSockets.jl (distributed under the same MIT license as Julia)

const b64chars = ['A':'Z','a':'z','0':'9','+','/']
const b64chars = ['A':'Z';'a':'z';'0':'9';'+';'/']

const base64_pad = uint8('=')

Expand Down
Loading

0 comments on commit 075f84d

Please sign in to comment.