From 1f982acb6ea5ed0fda8096b4bbe43d8258d315b4 Mon Sep 17 00:00:00 2001 From: chethega Date: Mon, 22 Oct 2018 20:17:32 +0200 Subject: [PATCH] special case flatten of AbstractArray --- base/iterators.jl | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/base/iterators.jl b/base/iterators.jl index b513d78c89d34..388e19ef6c7ad 100644 --- a/base/iterators.jl +++ b/base/iterators.jl @@ -909,6 +909,33 @@ length(f::Flatten{Tuple{}}) = 0 iterate(f, (x[2], x[1])) end +@propagate_inbounds function iterate(f::Flatten{<:AbstractArray}) + it = f.it + length(it) == 0 && return nothing + idx = firstindex(it) + sn = iterate(it[idx]) + while sn === nothing + idx += 1 + idx <= lastindex(it) || return nothing + sn = iterate(it[idx]) + end + elem, s_inner = sn + return (elem, (idx, s_inner)) +end + +@propagate_inbounds function iterate(f::Flatten{<:AbstractArray}, state) + idx, s_inner = state + it = f.it + sn = iterate(it[idx], s_inner) + while sn === nothing + idx += 1 + idx <= lastindex(it) || return nothing + sn = iterate(it[idx]) + end + elem, s_inner = sn + return (elem, (idx, s_inner)) +end + reverse(f::Flatten) = Flatten(reverse(itr) for itr in reverse(f.it)) """