Skip to content

Commit

Permalink
make size of a product iterator concatenate the sizes of its compon…
Browse files Browse the repository at this point in the history
…ents

this removes an extra layer of wrapping with `IteratorND`

add some inline declarations for `Generator` iteration
  • Loading branch information
JeffBezanson committed May 13, 2016
1 parent 5173d65 commit e7d9b64
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
5 changes: 3 additions & 2 deletions base/generator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ Generator(f, c1, c...) = Generator(a->f(a...), zip(c1, c...))

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

start(g::Generator) = start(g.iter)
done(g::Generator, s) = done(g.iter, s)
start(g::Generator) = (@_inline_meta; start(g.iter))
done(g::Generator, s) = (@_inline_meta; done(g.iter, s))
function next(g::Generator, s)
@_inline_meta
v, s2 = next(g.iter, s)
g.f(v), s2
end
Expand Down
11 changes: 7 additions & 4 deletions base/iterator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,16 @@ iteratorsize{I1,I2}(::Type{Prod{I1,I2}}) = prod_iteratorsize(iteratorsize(I1),it
((x[1][1],x[1][2]...), x[2])
end

prod_iteratorsize(::Union{HasLength,HasShape}, ::Union{HasLength,HasShape}) = HasLength()
prod_iteratorsize(::Union{HasLength,HasShape}, ::Union{HasLength,HasShape}) = HasShape()
prod_iteratorsize(a, ::IsInfinite) = IsInfinite() # products can have an infinite last iterator (which moves slowest)
prod_iteratorsize(a, b) = SizeUnknown()

_size(p::Prod2) = (length(p.a), length(p.b))
_size(p::Prod) = (length(p.a), _size(p.b)...)
it_size(it) = it_size(it, iteratorsize(it))
it_size(it, ::HasLength) = (length(it),)
it_size(it, ::HasShape) = size(it)

size(p::Prod2) = (it_size(p.a)..., it_size(p.b)...)
size(p::Prod) = (it_size(p.a)..., size(p.b)...)

"""
IteratorND(iter, dims)
Expand All @@ -396,7 +400,6 @@ immutable IteratorND{I,N}
end
new{I,N}(iter, shape)
end
(::Type{IteratorND}){I<:AbstractProdIterator}(p::I) = IteratorND(p, _size(p))
end

start(i::IteratorND) = start(i.iter)
Expand Down
2 changes: 1 addition & 1 deletion src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2009,7 +2009,7 @@
`(call (top Generator) (-> ,argname (block ,@splat ,expr))
,(if (length= ranges 1)
(car ranges)
`(call (top IteratorND) (call (top product) ,@ranges))))))))
`(call (top product) ,@ranges)))))))

'comprehension
(lambda (e) (expand-forms
Expand Down

0 comments on commit e7d9b64

Please sign in to comment.