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

WIP: Make [a, b] non-concatenating #8599

Merged
merged 15 commits into from
Feb 9, 2015
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
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to check: in this PR, is [1:s;] equivalent to [1:s...] (or maybe [(1:s)...], if the attachment of the elipses is ambiguous)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

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