Skip to content

Commit e7d9b64

Browse files
committed
make size of a product iterator concatenate the sizes of its components
this removes an extra layer of wrapping with `IteratorND` add some inline declarations for `Generator` iteration
1 parent 5173d65 commit e7d9b64

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

base/generator.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ Generator(f, c1, c...) = Generator(a->f(a...), zip(c1, c...))
1515

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

18-
start(g::Generator) = start(g.iter)
19-
done(g::Generator, s) = done(g.iter, s)
18+
start(g::Generator) = (@_inline_meta; start(g.iter))
19+
done(g::Generator, s) = (@_inline_meta; done(g.iter, s))
2020
function next(g::Generator, s)
21+
@_inline_meta
2122
v, s2 = next(g.iter, s)
2223
g.f(v), s2
2324
end

base/iterator.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,16 @@ iteratorsize{I1,I2}(::Type{Prod{I1,I2}}) = prod_iteratorsize(iteratorsize(I1),it
370370
((x[1][1],x[1][2]...), x[2])
371371
end
372372

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

377-
_size(p::Prod2) = (length(p.a), length(p.b))
378-
_size(p::Prod) = (length(p.a), _size(p.b)...)
377+
it_size(it) = it_size(it, iteratorsize(it))
378+
it_size(it, ::HasLength) = (length(it),)
379+
it_size(it, ::HasShape) = size(it)
380+
381+
size(p::Prod2) = (it_size(p.a)..., it_size(p.b)...)
382+
size(p::Prod) = (it_size(p.a)..., size(p.b)...)
379383

380384
"""
381385
IteratorND(iter, dims)
@@ -396,7 +400,6 @@ immutable IteratorND{I,N}
396400
end
397401
new{I,N}(iter, shape)
398402
end
399-
(::Type{IteratorND}){I<:AbstractProdIterator}(p::I) = IteratorND(p, _size(p))
400403
end
401404

402405
start(i::IteratorND) = start(i.iter)

src/julia-syntax.scm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2009,7 +2009,7 @@
20092009
`(call (top Generator) (-> ,argname (block ,@splat ,expr))
20102010
,(if (length= ranges 1)
20112011
(car ranges)
2012-
`(call (top IteratorND) (call (top product) ,@ranges))))))))
2012+
`(call (top product) ,@ranges)))))))
20132013

20142014
'comprehension
20152015
(lambda (e) (expand-forms

0 commit comments

Comments
 (0)