Skip to content

Commit

Permalink
Merge pull request #17214 from JuliaLang/jb/misc
Browse files Browse the repository at this point in the history
some generally-applicable changes from #16622
  • Loading branch information
JeffBezanson authored Jul 1, 2016
2 parents fb2e585 + c779caf commit 7a95d57
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 15 deletions.
24 changes: 17 additions & 7 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,28 @@ promote_rule{T,n,S}(::Type{Array{T,n}}, ::Type{Array{S,n}}) = Array{promote_type

## copying iterators to containers

# make a collection similar to `c` and appropriate for collecting `itr`
_similar_for(c::AbstractArray, T, itr, ::SizeUnknown) = similar(c, T, 0)
_similar_for(c::AbstractArray, T, itr, ::HasLength) = similar(c, T, Int(length(itr)::Integer))
_similar_for(c::AbstractArray, T, itr, ::HasShape) = similar(c, T, convert(Dims,size(itr)))
_similar_for(c, T, itr, isz) = similar(c, T)

"""
collect(element_type, collection)
Return an array of type `Array{element_type,1}` of all items in a collection.
"""
collect{T}(::Type{T}, itr) = collect(Generator(T, itr))
collect{T}(::Type{T}, itr) = _collect(T, itr, iteratorsize(itr))

_collect{T}(::Type{T}, itr, isz::HasLength) = copy!(Array{T,1}(Int(length(itr)::Integer)), itr)
_collect{T}(::Type{T}, itr, isz::HasShape) = copy!(Array{T}(convert(Dims,size(itr))), itr)
function _collect{T}(::Type{T}, itr, isz::SizeUnknown)
a = Array{T,1}(0)
for x in itr
push!(a,x)
end
return a
end

# make a collection similar to `c` and appropriate for collecting `itr`
_similar_for(c::AbstractArray, T, itr, ::SizeUnknown) = similar(c, T, 0)
_similar_for(c::AbstractArray, T, itr, ::HasLength) = similar(c, T, Int(length(itr)::Integer))
_similar_for(c::AbstractArray, T, itr, ::HasShape) = similar(c, T, convert(Dims,size(itr)))
_similar_for(c, T, itr, isz) = similar(c, T)

"""
collect(collection)
Expand Down
2 changes: 1 addition & 1 deletion base/generator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ immutable Generator{I,F}
iter::I
end

Generator(f, c1, c...) = Generator(a->f(a...), zip(c1, c...))
Generator(f, I1, I2, Is...) = Generator(a->f(a...), zip(I1, I2, Is...))

Generator{T,I}(::Type{T}, iter::I) = Generator{I,Type{T}}(T, iter)

Expand Down
7 changes: 6 additions & 1 deletion base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,12 @@ function tmerge(typea::ANY, typeb::ANY)
if isa(typea, DataType) && isa(typeb, DataType) && length(typea.parameters) == length(typeb.parameters) && !isvatuple(typea) && !isvatuple(typeb)
return typejoin(typea, typeb)
end
return Tuple
if isa(typea, Union) || isa(typeb, Union) || (isa(typea,DataType) && length(typea.parameters)>3) ||
(isa(typeb,DataType) && length(typeb.parameters)>3)
# widen tuples faster (see #6704), but not too much, to make sure we can infer
# e.g. (t::Union{Tuple{Bool},Tuple{Bool,Int}})[1]
return Tuple
end
end
u = Union{typea, typeb}
if length(u.types) > MAX_TYPEUNION_LEN || type_too_complex(u, 0)
Expand Down
8 changes: 5 additions & 3 deletions base/iterator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function advance_filter(pred, itr, st)
end
s=t
end
v, (true,)
v, (true, v, s)
end

done(f::Filter, s) = s[1]
Expand Down Expand Up @@ -479,7 +479,9 @@ flatten(itr) = Flatten(itr)

eltype{I}(::Type{Flatten{I}}) = eltype(eltype(I))
iteratorsize{I}(::Type{Flatten{I}}) = SizeUnknown()
iteratoreltype{I}(::Type{Flatten{I}}) = iteratoreltype(eltype(I))
iteratoreltype{I}(::Type{Flatten{I}}) = _flatteneltype(I, iteratoreltype(I))
_flatteneltype(I, ::HasEltype) = iteratoreltype(eltype(I))
_flatteneltype(I, et) = EltypeUnknown()

function start(f::Flatten)
local inner, s2
Expand All @@ -496,7 +498,7 @@ function start(f::Flatten)
return s, inner, s2
end

function next(f::Flatten, state)
@inline function next(f::Flatten, state)
s, inner, s2 = state
val, s2 = next(inner, s2)
while done(inner, s2) && !done(f.it, s)
Expand Down
8 changes: 5 additions & 3 deletions src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2380,10 +2380,12 @@ static jl_value_t *inst_tuple_w_(jl_value_t *t, jl_value_t **env, size_t n,
int i;
for(i=0; i < ntp; i++) {
jl_value_t *elt = jl_svecref(tp, i);
iparams[i] = (jl_value_t*)inst_type_w_(elt, env, n, stack, 0);
jl_value_t *pi = (jl_value_t*)inst_type_w_(elt, env, n, stack, 0);
if (jl_is_typevar(pi) && !((jl_tvar_t*)pi)->bound)
pi = ((jl_tvar_t*)pi)->ub;
iparams[i] = pi;
if (ip_heap)
jl_gc_wb(ip_heap, iparams[i]);
jl_value_t *pi = iparams[i];
jl_gc_wb(ip_heap, pi);
check_tuple_parameter(pi, i, ntp);
if (cacheable && !jl_is_leaf_type(pi)) {
cacheable = 0;
Expand Down
4 changes: 4 additions & 0 deletions test/functional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ end
# typed `collect`
@test collect(Float64, Filter(isodd, [1,2,3,4]))[1] === 1.0

@test isa(collect(Any, [1,2]), Vector{Any})

# enumerate (issue #6284)
let b = IOBuffer("1\n2\n3\n"), a = []
for (i,x) in enumerate(eachline(b))
Expand Down Expand Up @@ -374,6 +376,8 @@ import Base.flatten
@test eltype(flatten(UnitRange{Int8}[1:2, 3:4])) == Int8
@test_throws ArgumentError collect(flatten(Any[]))

@test Base.iteratoreltype(Base.Flatten((i for i=1:2) for j=1:1)) == Base.EltypeUnknown()

# foreach
let
a = []
Expand Down

0 comments on commit 7a95d57

Please sign in to comment.