Skip to content

Commit

Permalink
Merge pull request #10380 from JuliaLang/jb/tupleoverhaul
Browse files Browse the repository at this point in the history
WIP: redesign of tuples and tuple types
  • Loading branch information
JeffBezanson committed Apr 17, 2015
2 parents 58cef56 + a93e9a1 commit ef06211
Show file tree
Hide file tree
Showing 87 changed files with 3,129 additions and 3,626 deletions.
2 changes: 1 addition & 1 deletion base/Enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ macro enum(T,syms...)
else
throw(ArgumentError("invalid type expression for enum $T"))
end
vals = Array((Symbol,Integer),0)
vals = Array(Tuple{Symbol,Integer},0)
lo = hi = 0
i = -1
enumT = typeof(i)
Expand Down
4 changes: 2 additions & 2 deletions base/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@ function complete_methods(ex_org::Expr)
found ? push!(args_ex, method_type_of_arg(val)) : push!(args_ex, Any)
end
out = UTF8String[]
t_in = tuple(args_ex...) # Input types
t_in = Tuple{args_ex...} # Input types
for method in methods(func)
# Check if the method's type signature intersects the input types
typeintersect(method.sig[1 : min(length(args_ex), end)], t_in) != None &&
typeintersect(Tuple{method.sig.parameters[1 : min(length(args_ex), end)]...}, t_in) != None &&
push!(out,string(method))
end
return out
Expand Down
46 changes: 23 additions & 23 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ reshape(a::AbstractArray, dims::Int...) = reshape(a, dims)
vec(a::AbstractArray) = reshape(a,length(a))
vec(a::AbstractVector) = a

_sub(::(), ::()) = ()
_sub(t::Tuple, ::()) = t
_sub(::Tuple{}, ::Tuple{}) = ()
_sub(t::Tuple, ::Tuple{}) = t
_sub(t::Tuple, s::Tuple) = _sub(tail(t), tail(s))

function squeeze(A::AbstractArray, dims::Dims)
Expand Down Expand Up @@ -526,10 +526,10 @@ end

## get (getindex with a default value) ##

typealias RangeVecIntList{A<:AbstractVector{Int}} Union((Union(Range, AbstractVector{Int})...), AbstractVector{UnitRange{Int}}, AbstractVector{Range{Int}}, AbstractVector{A})
typealias RangeVecIntList{A<:AbstractVector{Int}} Union(Tuple{Union(Range, AbstractVector{Int}),...}, AbstractVector{UnitRange{Int}}, AbstractVector{Range{Int}}, AbstractVector{A})

get(A::AbstractArray, i::Integer, default) = in_bounds(length(A), i) ? A[i] : default
get(A::AbstractArray, I::(), default) = similar(A, typeof(default), 0)
get(A::AbstractArray, I::Tuple{}, default) = similar(A, typeof(default), 0)
get(A::AbstractArray, I::Dims, default) = in_bounds(size(A), I...) ? A[I...] : default

function get!{T}(X::AbstractArray{T}, A::AbstractArray, I::Union(Range, AbstractVector{Int}), default::T)
Expand Down Expand Up @@ -727,7 +727,7 @@ function hvcat(nbc::Integer, as...)
hvcat(ntuple(nbr, i->nbc), as...)
end

function hvcat{T}(rows::(Int...), as::AbstractMatrix{T}...)
function hvcat{T}(rows::Tuple{Int,...}, as::AbstractMatrix{T}...)
nbr = length(rows) # number of block rows

nc = 0
Expand Down Expand Up @@ -770,9 +770,9 @@ function hvcat{T}(rows::(Int...), as::AbstractMatrix{T}...)
out
end

hvcat(rows::(Int...)) = []
hvcat(rows::Tuple{Int,...}) = []

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

Expand Down Expand Up @@ -805,7 +805,7 @@ function hvcat_fill(a, xs)
a
end

function typed_hvcat(T::Type, rows::(Int...), xs::Number...)
function typed_hvcat(T::Type, rows::Tuple{Int,...}, xs::Number...)
nr = length(rows)
nc = rows[1]
for i = 2:nr
Expand All @@ -820,13 +820,13 @@ function typed_hvcat(T::Type, rows::(Int...), xs::Number...)
hvcat_fill(Array(T, nr, nc), xs)
end

function hvcat(rows::(Int...), xs::Number...)
function hvcat(rows::Tuple{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...)
function hvcat(rows::Tuple{Int,...}, as...)
nbr = length(rows) # number of block rows
rs = cell(nbr)
a = 1
Expand All @@ -837,7 +837,7 @@ function hvcat(rows::(Int...), as...)
vcat(rs...)
end

function typed_hvcat(T::Type, rows::(Int...), as...)
function typed_hvcat(T::Type, rows::Tuple{Int,...}, as...)
nbr = length(rows) # number of block rows
rs = cell(nbr)
a = 1
Expand Down Expand Up @@ -1021,7 +1021,7 @@ end
sub2ind{T<:Integer}(dims, I::AbstractVector{T}...) =
[ sub2ind(dims, map(X->X[i], I)...)::Int for i=1:length(I[1]) ]

function ind2sub(dims::(Integer,Integer...), ind::Int)
function ind2sub(dims::Tuple{Integer,Integer,...}, ind::Int)
ndims = length(dims)
stride = dims[1]
for i=2:ndims-1
Expand All @@ -1038,17 +1038,17 @@ function ind2sub(dims::(Integer,Integer...), ind::Int)
return tuple(ind, sub...)
end

ind2sub(dims::(Integer...), ind::Integer) = ind2sub(dims, Int(ind))
ind2sub(dims::(), ind::Integer) = ind==1 ? () : throw(BoundsError())
ind2sub(dims::(Integer,), ind::Int) = (ind,)
ind2sub(dims::(Integer,Integer), ind::Int) =
ind2sub(dims::Tuple{Integer,...}, ind::Integer) = ind2sub(dims, Int(ind))
ind2sub(dims::Tuple{}, ind::Integer) = ind==1 ? () : throw(BoundsError())
ind2sub(dims::Tuple{Integer,}, ind::Int) = (ind,)
ind2sub(dims::Tuple{Integer,Integer}, ind::Int) =
(rem(ind-1,dims[1])+1, div(ind-1,dims[1])+1)
ind2sub(dims::(Integer,Integer,Integer), ind::Int) =
ind2sub(dims::Tuple{Integer,Integer,Integer}, ind::Int) =
(rem(ind-1,dims[1])+1, div(rem(ind-1,dims[1]*dims[2]), dims[1])+1,
div(rem(ind-1,dims[1]*dims[2]*dims[3]), dims[1]*dims[2])+1)
ind2sub(a::AbstractArray, ind::Integer) = ind2sub(size(a), Int(ind))

function ind2sub{T<:Integer}(dims::(Integer,Integer...), ind::AbstractVector{T})
function ind2sub{T<:Integer}(dims::Tuple{Integer,Integer,...}, ind::AbstractVector{T})
n = length(dims)
l = length(ind)
t = ntuple(n, x->Array(Int, l))
Expand Down Expand Up @@ -1131,7 +1131,7 @@ end
## iteration utilities ##

# slow, but useful
function cartesianmap(body, t::(Int...), it...)
function cartesianmap(body, t::Tuple{Int,...}, it...)
idx = length(t)-length(it)
if idx == 1
for i = 1:t[1]
Expand All @@ -1154,23 +1154,23 @@ function cartesianmap(body, t::(Int...), it...)
end
end

cartesianmap(body, t::()) = (body(); nothing)
cartesianmap(body, t::Tuple{}) = (body(); nothing)

function cartesianmap(body, t::(Int,))
function cartesianmap(body, t::Tuple{Int,})
for i = 1:t[1]
body(i)
end
end

function cartesianmap(body, t::(Int,Int))
function cartesianmap(body, t::Tuple{Int,Int})
for j = 1:t[2]
for i = 1:t[1]
body(i,j)
end
end
end

function cartesianmap(body, t::(Int,Int,Int))
function cartesianmap(body, t::Tuple{Int,Int,Int})
for k = 1:t[3]
for j = 1:t[2]
for i = 1:t[1]
Expand Down
27 changes: 12 additions & 15 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ typealias DenseVector{T} DenseArray{T,1}
typealias DenseMatrix{T} DenseArray{T,2}
typealias DenseVecOrMat{T} Union(DenseVector{T}, DenseMatrix{T})

typealias StridedArray{T,N,A<:DenseArray,I<:(RangeIndex...)} Union(DenseArray{T,N}, SubArray{T,N,A,I})
typealias StridedVector{T,A<:DenseArray,I<:(RangeIndex...)} Union(DenseArray{T,1}, SubArray{T,1,A,I})
typealias StridedMatrix{T,A<:DenseArray,I<:(RangeIndex...)} Union(DenseArray{T,2}, SubArray{T,2,A,I})
typealias StridedArray{T,N,A<:DenseArray,I<:Tuple{RangeIndex,...}} Union(DenseArray{T,N}, SubArray{T,N,A,I})
typealias StridedVector{T,A<:DenseArray,I<:Tuple{RangeIndex,...}} Union(DenseArray{T,1}, SubArray{T,1,A,I})
typealias StridedMatrix{T,A<:DenseArray,I<:Tuple{RangeIndex,...}} Union(DenseArray{T,2}, SubArray{T,2,A,I})
typealias StridedVecOrMat{T} Union(StridedVector{T}, StridedMatrix{T})

call{T}(::Type{Vector{T}}, m::Integer) = Array{T}(m)
Expand Down Expand Up @@ -41,9 +41,14 @@ end
cconvert{P<:Ptr,T<:Ptr}(::Union(Type{Ptr{P}},Type{Ref{P}}), a::Array{T}) = a
cconvert{P<:Ptr}(::Union(Type{Ptr{P}},Type{Ref{P}}), a::Array) = Ref{P}(a)

size(a::Array) = arraysize(a)
size(a::Array, d) = arraysize(a, d)
size(a::Vector) = (arraysize(a,1),)
size(a::Matrix) = (arraysize(a,1), arraysize(a,2))
size{_}(a::Array{_,3}) = (arraysize(a,1), arraysize(a,2), arraysize(a,3))
size{_}(a::Array{_,4}) = (arraysize(a,1), arraysize(a,2), arraysize(a,3), arraysize(a,4))
asize_from(a::Array, n) = n > ndims(a) ? () : (arraysize(a,n), asize_from(a, n+1)...)
size{_,N}(a::Array{_,N}) = asize_from(a, 1)::NTuple{N,Int}

length(a::Array) = arraylen(a)
elsize{T}(a::Array{T}) = isbits(T) ? sizeof(T) : sizeof(Ptr)
sizeof(a::Array) = elsize(a) * length(a)
Expand Down Expand Up @@ -148,7 +153,7 @@ similar{T}(a::Array{T,2}, m::Int) = Array(T, m)
similar{T}(a::Array{T,2}, S) = Array(S, size(a,1), size(a,2))

# T[x...] constructs Array{T,1}
function getindex(T::NonTupleType, vals...)
function getindex(T::Type, vals...)
a = Array(T,length(vals))
@inbounds for i = 1:length(vals)
a[i] = vals[i]
Expand All @@ -164,14 +169,6 @@ function getindex(::Type{Any}, vals::ANY...)
return a
end

function getindex(T::(Type...), vals::Tuple...)
a = Array(T,length(vals))
@inbounds for i = 1:length(vals)
a[i] = vals[i]
end
return a
end

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)
Expand Down Expand Up @@ -218,7 +215,7 @@ fill(v, dims::Dims) = fill!(Array(typeof(v), dims), v)
fill(v, dims::Integer...) = fill!(Array(typeof(v), dims...), v)

cell(dims::Integer...) = Array(Any, dims...)
cell(dims::(Integer...)) = Array(Any, convert((Int...), dims))
cell(dims::Tuple{Integer,...}) = Array(Any, convert(Tuple{Int,...}, dims))

for (fname, felt) in ((:zeros,:zero), (:ones,:one))
@eval begin
Expand Down Expand Up @@ -1289,7 +1286,7 @@ function indcopy(sz::Dims, I::Vector)
dst, src
end

function indcopy(sz::Dims, I::(RangeIndex...))
function indcopy(sz::Dims, I::Tuple{RangeIndex,...})
n = length(I)
s = sz[n]
for i = n+1:length(sz)
Expand Down
63 changes: 50 additions & 13 deletions base/base.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const NonTupleType = Union(DataType,UnionType,TypeConstructor)

typealias Callable Union(Function,DataType)

const Bottom = Union()
Expand Down Expand Up @@ -39,20 +37,37 @@ call{T}(::Type{T}, args...) = convert(T, args...)::T

convert{T}(::Type{T}, x::T) = x

convert(::(), ::()) = ()
convert(::Type{Tuple{}}, ::Tuple{}) = ()
convert(::Type{Tuple}, x::Tuple) = x
convert{T}(::Type{Tuple{T,...}}, x::Tuple) = cnvt_all(T, x...)
cnvt_all(T) = ()
cnvt_all(T, x, rest...) = tuple(convert(T,x), cnvt_all(T, rest...)...)

stagedfunction tuple_type_head{T<:Tuple}(::Type{T})
T.parameters[1]
end

isvarargtype(t::ANY) = isa(t,DataType)&&is((t::DataType).name,Vararg.name)
isvatuple(t::DataType) = (n = length(t.parameters); n > 0 && isvarargtype(t.parameters[n]))
unwrapva(t::ANY) = isvarargtype(t) ? t.parameters[1] : t

stagedfunction tuple_type_tail{T<:Tuple}(::Type{T})
if isvatuple(T) && length(T.parameters) == 1
return T
end
Tuple{argtail(T.parameters...)...}
end

argtail(x, rest...) = rest
tail(x::Tuple) = argtail(x...)

convert(T::(Type, Type...), x::(Any, Any...)) =
tuple(convert(T[1],x[1]), convert(tail(T), tail(x))...)
convert(T::(Any, Any...), x::(Any, Any...)) =
tuple(convert(T[1],x[1]), convert(tail(T), tail(x))...)
convert{T<:Tuple{Any,Any,...}}(::Type{T}, x::Tuple{Any, Any, ...}) =
tuple(convert(tuple_type_head(T),x[1]), convert(tuple_type_tail(T), tail(x))...)

convert{T}(::Type{(T...)}, x::Tuple) = cnvt_all(T, x...)
cnvt_all(T) = ()
cnvt_all(T, x, rest...) = tuple(convert(T,x), cnvt_all(T, rest...)...)
oftype(x,c) = convert(typeof(x),c)

unsigned(x::Int) = reinterpret(UInt, x)
signed(x::UInt) = reinterpret(Int, x)

# conversions used by ccall
ptr_arg_cconvert{T}(::Type{Ptr{T}}, x) = cconvert(T, x)
Expand All @@ -66,6 +81,8 @@ unsafe_convert{P<:Ptr}(::Type{P}, x::Ptr) = convert(P, x)

reinterpret{T,S}(::Type{T}, x::S) = box(T,unbox(S,x))

sizeof(x) = Core.sizeof(x)

abstract IO

type ErrorException <: Exception
Expand Down Expand Up @@ -220,7 +237,7 @@ function precompile(f::ANY, args::Tuple)
f = f.name.module.call
end
if isgeneric(f)
ccall(:jl_compile_hint, Void, (Any, Any), f, args)
ccall(:jl_compile_hint, Void, (Any, Any), f, Tuple{args...})
end
end

Expand All @@ -247,7 +264,7 @@ end

call{T,N}(::Type{Array{T}}, d::NTuple{N,Int}) =
ccall(:jl_new_array, Array{T,N}, (Any,Any), Array{T,N}, d)
call{T}(::Type{Array{T}}, d::Integer...) = Array{T}(convert((Int...), d))
call{T}(::Type{Array{T}}, d::Integer...) = Array{T}(convert(Tuple{Int,...}, d))

call{T}(::Type{Array{T}}, m::Integer) =
ccall(:jl_alloc_array_1d, Array{T,1}, (Any,Int), Array{T,1}, m)
Expand All @@ -258,11 +275,31 @@ call{T}(::Type{Array{T}}, m::Integer, n::Integer, o::Integer) =

# TODO: possibly turn these into deprecations
Array{T,N}(::Type{T}, d::NTuple{N,Int}) = Array{T}(d)
Array{T}(::Type{T}, d::Integer...) = Array{T}(convert((Int...), d))
Array{T}(::Type{T}, d::Integer...) = Array{T}(convert(Tuple{Int,...}, d))
Array{T}(::Type{T}, m::Integer) = Array{T}(m)
Array{T}(::Type{T}, m::Integer,n::Integer) = Array{T}(m,n)
Array{T}(::Type{T}, m::Integer,n::Integer,o::Integer) = Array{T}(m,n,o)

# SimpleVector

function getindex(v::SimpleVector, i::Int)
if !(1 <= i <= length(v))
throw(BoundsError())
end
unsafe_load(convert(Ptr{Any},data_pointer_from_objref(v)) + i*sizeof(Ptr))
end

length(v::SimpleVector) = v.length
endof(v::SimpleVector) = v.length
start(v::SimpleVector) = 1
next(v::SimpleVector,i) = (v[i],i+1)
done(v::SimpleVector,i) = (i > v.length)
isempty(v::SimpleVector) = (v.length == 0)

map(f, v::SimpleVector) = Any[ f(v[i]) for i = 1:length(v) ]

getindex(v::SimpleVector, I::AbstractArray) = Any[ v[i] for i in I ]

immutable Nullable{T}
isnull::Bool
value::T
Expand Down
4 changes: 2 additions & 2 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,7 @@ function cat(catdim::Integer, X::Union(BitArray, Integer)...)
end
end
# just integers and no BitArrays -> general case
has_bitarray || return invoke(cat, (Integer, Any...), catdim, X...)
has_bitarray || return invoke(cat, Tuple{Integer, Any, ...}, catdim, X...)
dimsX = map((a->isa(a,BitArray) ? size(a) : (1,)), X)
ndimsX = map((a->isa(a,BitArray) ? ndims(a) : 1), X)
d_max = maximum(ndimsX)
Expand Down Expand Up @@ -1842,7 +1842,7 @@ function cat(catdim::Integer, X::Union(BitArray, Integer)...)
end

ndimsC = max(catdim, d_max)
dimsC = ntuple(ndimsC, compute_dims)::(Int...)
dimsC = ntuple(ndimsC, compute_dims)::Tuple{Int,...}
typeC = promote_type(map(x->isa(x,BitArray) ? eltype(x) : typeof(x), X)...)
if !has_integer || typeC == Bool
C = BitArray(dimsC)
Expand Down
Loading

0 comments on commit ef06211

Please sign in to comment.