Skip to content

Commit

Permalink
Use new where syntax for a couple of misc methods (JuliaLang#21415)
Browse files Browse the repository at this point in the history
* Use new where syntax for eltype

* New where syntax for some size methods

* New where syntax for copy mehods
  • Loading branch information
musm authored and JeffBezanson committed Apr 18, 2017
1 parent 972619b commit 7f26246
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 40 deletions.
6 changes: 3 additions & 3 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ julia> size(A,3,2)
(4, 3)
```
"""
size{T,N}(t::AbstractArray{T,N}, d) = d <= N ? size(t)[d] : 1
size{N}(x, d1::Integer, d2::Integer, dx::Vararg{Integer, N}) =
size(t::AbstractArray{T,N}, d) where {T,N} = d <= N ? size(t)[d] : 1
size(x, d1::Integer, d2::Integer, dx::Vararg{Integer, N}) where {N} =
(size(x, d1), size(x, d2), ntuple(k->size(x, dx[k]), Val{N})...)

"""
Expand Down Expand Up @@ -91,7 +91,7 @@ julia> extrema(b)
linearindices(A) = (@_inline_meta; OneTo(_length(A)))
linearindices(A::AbstractVector) = (@_inline_meta; indices1(A))
eltype(::Type{<:AbstractArray{E}}) where {E} = E
elsize{T}(::AbstractArray{T}) = sizeof(T)
elsize(::AbstractArray{T}) where {T} = sizeof(T)

"""
ndims(A::AbstractArray) -> Integer
Expand Down
14 changes: 7 additions & 7 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@ 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) = (@_inline_meta; _size((), a))
_size{_,N}(out::NTuple{N}, A::Array{_,N}) = out
function _size{_,M,N}(out::NTuple{M}, A::Array{_,N})
_size(out::NTuple{N}, A::Array{_,N}) where {_,N} = out
function _size(out::NTuple{M}, A::Array{_,N}) where _ where M where N
@_inline_meta
_size((out..., size(A,M+1)), A)
end

asize_from(a::Array, n) = n > ndims(a) ? () : (arraysize(a,n), asize_from(a, n+1)...)

length(a::Array) = arraylen(a)
elsize{T}(a::Array{T}) = isbits(T) ? sizeof(T) : sizeof(Ptr)
elsize(a::Array{T}) where {T} = isbits(T) ? sizeof(T) : sizeof(Ptr)
sizeof(a::Array) = elsize(a) * length(a)

function isassigned(a::Array, i::Int...)
Expand All @@ -94,15 +94,15 @@ end

## copy ##

function unsafe_copy!{T}(dest::Ptr{T}, src::Ptr{T}, n)
function unsafe_copy!(dest::Ptr{T}, src::Ptr{T}, n) where T
# Do not use this to copy data between pointer arrays.
# It can't be made safe no matter how carefully you checked.
ccall(:memmove, Ptr{Void}, (Ptr{Void}, Ptr{Void}, UInt),
dest, src, n*sizeof(T))
return dest
end

function unsafe_copy!{T}(dest::Array{T}, doffs, src::Array{T}, soffs, n)
function unsafe_copy!(dest::Array{T}, doffs, src::Array{T}, soffs, n) where T
if isbits(T)
unsafe_copy!(pointer(dest, doffs), pointer(src, soffs), n)
else
Expand All @@ -121,9 +121,9 @@ function copy!{T}(dest::Array{T}, doffs::Integer, src::Array{T}, soffs::Integer,
unsafe_copy!(dest, doffs, src, soffs, n)
end

copy!{T}(dest::Array{T}, src::Array{T}) = copy!(dest, 1, src, 1, length(src))
copy!(dest::Array{T}, src::Array{T}) where {T} = copy!(dest, 1, src, 1, length(src))

copy{T<:Array}(a::T) = ccall(:jl_array_copy, Ref{T}, (Any,), a)
copy(a::T) where {T<:Array} = ccall(:jl_array_copy, Ref{T}, (Any,), a)

function reinterpret{T,S}(::Type{T}, a::Array{S,1})
nel = Int(div(length(a)*sizeof(S),sizeof(T)))
Expand Down
6 changes: 3 additions & 3 deletions base/associative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ length(v::Union{KeyIterator,ValueIterator}) = length(v.dict)
isempty(v::Union{KeyIterator,ValueIterator}) = isempty(v.dict)
_tt1{A,B}(::Type{Pair{A,B}}) = A
_tt2{A,B}(::Type{Pair{A,B}}) = B
eltype{D}(::Type{KeyIterator{D}}) = _tt1(eltype(D))
eltype{D}(::Type{ValueIterator{D}}) = _tt2(eltype(D))
eltype(::Type{KeyIterator{D}}) where {D} = _tt1(eltype(D))
eltype(::Type{ValueIterator{D}}) where {D} = _tt2(eltype(D))

start(v::Union{KeyIterator,ValueIterator}) = start(v.dict)
done(v::Union{KeyIterator,ValueIterator}, state) = done(v.dict, state)
Expand Down Expand Up @@ -318,7 +318,7 @@ function filter(f, d::Associative)
return df
end

eltype{K,V}(::Type{Associative{K,V}}) = Pair{K,V}
eltype(::Type{Associative{K,V}}) where {K,V} = Pair{K,V}

function isequal(l::Associative, r::Associative)
l === r && return true
Expand Down
2 changes: 1 addition & 1 deletion base/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ function notify_error(c::Channel, err)
end
notify_error(c::Channel) = notify_error(c, get(c.excp))

eltype{T}(::Type{Channel{T}}) = T
eltype(::Type{Channel{T}}) where {T} = T

show(io::IO, c::Channel) = print(io, "$(typeof(c))(sz_max:$(c.sz_max),sz_curr:$(n_avail(c)))")

Expand Down
2 changes: 1 addition & 1 deletion base/dft.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ abstract type Plan{T} end
import Base: show, summary, size, ndims, length, eltype,
*, A_mul_B!, inv, \, A_ldiv_B!

eltype{T}(::Type{Plan{T}}) = T
eltype(::Type{Plan{T}}) where {T} = T

# size(p) should return the size of the input array for p
size(p::Plan, d) = size(p)[d]
Expand Down
34 changes: 17 additions & 17 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ size(e::Enumerate) = size(e.itr)
end
@inline done(e::Enumerate, state) = done(e.itr, state[2])

eltype{I}(::Type{Enumerate{I}}) = Tuple{Int, eltype(I)}
eltype(::Type{Enumerate{I}}) where {I} = Tuple{Int, eltype(I)}

iteratorsize{I}(::Type{Enumerate{I}}) = iteratorsize(I)
iteratoreltype{I}(::Type{Enumerate{I}}) = iteratoreltype(I)
Expand Down Expand Up @@ -135,7 +135,7 @@ size(v::IndexValue) = size(v.itr)
end
@inline done(v::IndexValue, state) = done(v.itr, state)

eltype{I,A}(::Type{IndexValue{I,A}}) = Tuple{eltype(I), eltype(A)}
eltype(::Type{IndexValue{I,A}}) where {I,A} = Tuple{eltype(I), eltype(A)}

iteratorsize{I}(::Type{IndexValue{I}}) = iteratorsize(I)
iteratoreltype{I}(::Type{IndexValue{I}}) = iteratoreltype(I)
Expand All @@ -158,7 +158,7 @@ zip(a) = Zip1(a)
length(z::Zip1) = length(z.a)
size(z::Zip1) = size(z.a)
indices(z::Zip1) = indices(z.a)
eltype{I}(::Type{Zip1{I}}) = Tuple{eltype(I)}
eltype(::Type{Zip1{I}}) where {I} = Tuple{eltype(I)}
@inline start(z::Zip1) = start(z.a)
@inline function next(z::Zip1, st)
n = next(z.a,st)
Expand All @@ -177,7 +177,7 @@ zip(a, b) = Zip2(a, b)
length(z::Zip2) = _min_length(z.a, z.b, iteratorsize(z.a), iteratorsize(z.b))
size(z::Zip2) = promote_shape(size(z.a), size(z.b))
indices(z::Zip2) = promote_shape(indices(z.a), indices(z.b))
eltype{I1,I2}(::Type{Zip2{I1,I2}}) = Tuple{eltype(I1), eltype(I2)}
eltype(::Type{Zip2{I1,I2}}) where {I1,I2} = Tuple{eltype(I1), eltype(I2)}
@inline start(z::Zip2) = (start(z.a), start(z.b))
@inline function next(z::Zip2, st)
n1 = next(z.a,st[1])
Expand Down Expand Up @@ -228,7 +228,7 @@ zip(a, b, c...) = Zip(a, zip(b, c...))
length(z::Zip) = _min_length(z.a, z.z, iteratorsize(z.a), iteratorsize(z.z))
size(z::Zip) = promote_shape(size(z.a), size(z.z))
indices(z::Zip) = promote_shape(indices(z.a), indices(z.z))
eltype{I,Z}(::Type{Zip{I,Z}}) = tuple_type_cons(eltype(I), eltype(Z))
eltype(::Type{Zip{I,Z}}) where {I,Z} = tuple_type_cons(eltype(I), eltype(Z))
@inline start(z::Zip) = tuple(start(z.a), start(z.z))
@inline function next(z::Zip, st)
n1 = next(z.a, st[1])
Expand Down Expand Up @@ -277,7 +277,7 @@ end

done(f::Filter, s) = s[1]

eltype{F,I}(::Type{Filter{F,I}}) = eltype(I)
eltype(::Type{Filter{F,I}}) where {F,I} = eltype(I)
iteratoreltype{F,I}(::Type{Filter{F,I}}) = iteratoreltype(I)
iteratorsize(::Type{<:Filter}) = SizeUnknown()

Expand All @@ -299,7 +299,7 @@ start(i::Rest) = i.st
next(i::Rest, st) = next(i.itr, st)
done(i::Rest, st) = done(i.itr, st)

eltype{I}(::Type{Rest{I}}) = eltype(I)
eltype(::Type{Rest{I}}) where {I} = eltype(I)
iteratoreltype{I,S}(::Type{Rest{I,S}}) = iteratoreltype(I)
rest_iteratorsize(a) = SizeUnknown()
rest_iteratorsize(::IsInfinite) = IsInfinite()
Expand All @@ -322,7 +322,7 @@ countfrom(start::Number, step::Number) = Count(promote(start, step)...)
countfrom(start::Number) = Count(start, oneunit(start))
countfrom() = Count(1, 1)

eltype{S}(::Type{Count{S}}) = S
eltype(::Type{Count{S}}) where {S} = S

start(it::Count) = it.start
next(it::Count, state) = (state, state + it.step)
Expand Down Expand Up @@ -365,7 +365,7 @@ julia> collect(Iterators.take(a,3))
take(xs, n::Integer) = Take(xs, Int(n))
take(xs::Take, n::Integer) = Take(xs.xs, min(Int(n), xs.n))

eltype{I}(::Type{Take{I}}) = eltype(I)
eltype(::Type{Take{I}}) where {I} = eltype(I)
iteratoreltype{I}(::Type{Take{I}}) = iteratoreltype(I)
take_iteratorsize(a) = HasLength()
take_iteratorsize(::SizeUnknown) = SizeUnknown()
Expand Down Expand Up @@ -420,7 +420,7 @@ drop(xs, n::Integer) = Drop(xs, Int(n))
drop(xs::Take, n::Integer) = Take(drop(xs.xs, Int(n)), max(0, xs.n - Int(n)))
drop(xs::Drop, n::Integer) = Drop(xs.xs, Int(n) + xs.n)

eltype{I}(::Type{Drop{I}}) = eltype(I)
eltype(::Type{Drop{I}}) where {I} = eltype(I)
iteratoreltype{I}(::Type{Drop{I}}) = iteratoreltype(I)
drop_iteratorsize(::SizeUnknown) = SizeUnknown()
drop_iteratorsize(::Union{HasShape, HasLength}) = HasLength()
Expand Down Expand Up @@ -456,7 +456,7 @@ An iterator that cycles through `iter` forever.
"""
cycle(xs) = Cycle(xs)

eltype{I}(::Type{Cycle{I}}) = eltype(I)
eltype(::Type{Cycle{I}}) where {I} = eltype(I)
iteratoreltype{I}(::Type{Cycle{I}}) = iteratoreltype(I)
iteratorsize{I}(::Type{Cycle{I}}) = IsInfinite()

Expand Down Expand Up @@ -503,7 +503,7 @@ julia> collect(a)
"""
repeated(x, n::Integer) = take(repeated(x), Int(n))

eltype{O}(::Type{Repeated{O}}) = O
eltype(::Type{Repeated{O}}) where {O} = O

start(it::Repeated) = nothing
next(it::Repeated, state) = (it.x, nothing)
Expand Down Expand Up @@ -552,7 +552,7 @@ struct Prod1{I} <: AbstractProdIterator
end
product(a) = Prod1(a)

eltype{I}(::Type{Prod1{I}}) = Tuple{eltype(I)}
eltype(::Type{Prod1{I}}) where {I} = Tuple{eltype(I)}
size(p::Prod1) = _prod_size(p.a, iteratorsize(p.a))
indices(p::Prod1) = _prod_indices(p.a, iteratorsize(p.a))

Expand Down Expand Up @@ -588,7 +588,7 @@ julia> collect(Iterators.product(1:2,3:5))
"""
product(a, b) = Prod2(a, b)

eltype{I1,I2}(::Type{Prod2{I1,I2}}) = Tuple{eltype(I1), eltype(I2)}
eltype(::Type{Prod2{I1,I2}}) where {I1,I2} = Tuple{eltype(I1), eltype(I2)}

iteratoreltype{I1,I2}(::Type{Prod2{I1,I2}}) = and_iteratoreltype(iteratoreltype(I1),iteratoreltype(I2))
iteratorsize{I1,I2}(::Type{Prod2{I1,I2}}) = prod_iteratorsize(iteratorsize(I1),iteratorsize(I2))
Expand Down Expand Up @@ -625,7 +625,7 @@ struct Prod{I1, I2<:AbstractProdIterator} <: AbstractProdIterator
end
product(a, b, c...) = Prod(a, product(b, c...))

eltype{I1,I2}(::Type{Prod{I1,I2}}) = tuple_type_cons(eltype(I1), eltype(I2))
eltype(::Type{Prod{I1,I2}}) where {I1,I2} = tuple_type_cons(eltype(I1), eltype(I2))

iteratoreltype{I1,I2}(::Type{Prod{I1,I2}}) = and_iteratoreltype(iteratoreltype(I1),iteratoreltype(I2))
iteratorsize{I1,I2}(::Type{Prod{I1,I2}}) = prod_iteratorsize(iteratorsize(I1),iteratorsize(I2))
Expand Down Expand Up @@ -667,7 +667,7 @@ julia> collect(Iterators.flatten((1:2, 8:9)))
"""
flatten(itr) = Flatten(itr)

eltype{I}(::Type{Flatten{I}}) = eltype(eltype(I))
eltype(::Type{Flatten{I}}) where {I} = eltype(eltype(I))
iteratorsize{I}(::Type{Flatten{I}}) = SizeUnknown()
iteratoreltype{I}(::Type{Flatten{I}}) = _flatteneltype(I, iteratoreltype(I))
_flatteneltype(I, ::HasEltype) = iteratoreltype(eltype(I))
Expand Down Expand Up @@ -725,7 +725,7 @@ mutable struct PartitionIterator{T}
n::Int
end

eltype{T}(::Type{PartitionIterator{T}}) = Vector{eltype(T)}
eltype(::Type{PartitionIterator{T}}) where {T} = Vector{eltype(T)}

function length(itr::PartitionIterator)
l = length(itr.c)
Expand Down
2 changes: 1 addition & 1 deletion base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ module IteratorsMD
@inline maxt(a::Tuple, b::Tuple{}) = a
@inline maxt(a::Tuple, b::Tuple) = (max(a[1], b[1]), maxt(tail(a), tail(b))...)

eltype{I}(::Type{CartesianRange{I}}) = I
eltype(::Type{CartesianRange{I}}) where {I} = I
iteratorsize(::Type{<:CartesianRange}) = Base.HasShape()

@inline function start(iter::CartesianRange{<:CartesianIndex})
Expand Down
2 changes: 1 addition & 1 deletion base/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Nullable{Int64}
Nullable{T}(value::T, hasvalue::Bool=true) = Nullable{T}(value, hasvalue)
Nullable() = Nullable{Union{}}()

eltype{T}(::Type{Nullable{T}}) = T
eltype(::Type{Nullable{T}}) where {T} = T

convert(::Type{Nullable{T}}, x::Nullable{T}) where {T} = x
convert(::Type{Nullable }, x::Nullable ) = x
Expand Down
2 changes: 1 addition & 1 deletion base/number.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ size(x::Number) = ()
size(x::Number,d) = convert(Int,d)<1 ? throw(BoundsError()) : 1
indices(x::Number) = ()
indices(x::Number,d) = convert(Int,d)<1 ? throw(BoundsError()) : OneTo(1)
eltype{T<:Number}(::Type{T}) = T
eltype(::Type{T}) where {T<:Number} = T
ndims(x::Number) = 0
ndims(::Type{<:Number}) = 0
length(x::Number) = 1
Expand Down
4 changes: 2 additions & 2 deletions base/permuteddimsarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ function PermutedDimsArray(data::AbstractArray{T,N}, perm) where {T,N}
end

Base.parent(A::PermutedDimsArray) = A.parent
Base.size{T,N,perm}(A::PermutedDimsArray{T,N,perm}) = genperm(size(parent(A)), perm)
Base.indices{T,N,perm}(A::PermutedDimsArray{T,N,perm}) = genperm(indices(parent(A)), perm)
Base.size(A::PermutedDimsArray{T,N,perm}) where {T,N,perm} = genperm(size(parent(A)), perm)
Base.indices(A::PermutedDimsArray{T,N,perm}) where {T,N,perm} = genperm(indices(parent(A)), perm)

Base.unsafe_convert{T}(::Type{Ptr{T}}, A::PermutedDimsArray{T}) = Base.unsafe_convert(Ptr{T}, parent(A))

Expand Down
2 changes: 1 addition & 1 deletion base/pointer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ remains referenced for the whole time that the `Ptr` will be used.
pointer_from_objref(x::ANY) = ccall(:jl_value_ptr, Ptr{Void}, (Any,), x)
data_pointer_from_objref(x::ANY) = pointer_from_objref(x)::Ptr{Void}

eltype{T}(::Type{Ptr{T}}) = T
eltype(::Type{Ptr{T}}) where {T} = T

## limited pointer arithmetic & comparison ##

Expand Down
2 changes: 1 addition & 1 deletion base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function Set(g::Generator)
return Set{T}(g)
end

eltype{T}(::Type{Set{T}}) = T
eltype(::Type{Set{T}}) where {T} = T
similar{T}(s::Set{T}) = Set{T}()
similar(s::Set, T::Type) = Set{T}()

Expand Down
2 changes: 1 addition & 1 deletion base/twiceprecision.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ nbitslen(::Type{Float32}, len, offset) = min(12, nbitslen(len, offset))
nbitslen(::Type{Float16}, len, offset) = min(5, nbitslen(len, offset))
nbitslen(len, offset) = len < 2 ? 0 : ceil(Int, log2(max(offset-1, len-offset)))

eltype{T}(::Type{TwicePrecision{T}}) = T
eltype(::Type{TwicePrecision{T}}) where {T} = T

promote_rule{R,S}(::Type{TwicePrecision{R}}, ::Type{TwicePrecision{S}}) =
TwicePrecision{promote_type(R,S)}
Expand Down

0 comments on commit 7f26246

Please sign in to comment.