Skip to content

Commit

Permalink
special case flatten of AbstractArray
Browse files Browse the repository at this point in the history
  • Loading branch information
chethega committed Oct 22, 2018
1 parent 2e2b14c commit ea7bef8
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,33 @@ length(f::Flatten{Tuple{}}) = 0
iterate(f, (x[2], x[1]))
end

@inline function iterate(f::Flatten{<:AbstractArray})
it = f.it
length(it)== 0 && return nothing
idx = firstindex(it)
sn = iterate(@inbounds it[idx])
while sn === nothing
idx += 1
idx <= lastindex(it) || return nothing
sn = iterate(@inbounds 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))

"""
Expand Down

0 comments on commit ea7bef8

Please sign in to comment.