@@ -973,7 +973,7 @@ cycle(xs) = Cycle(xs)
973973
974974eltype (:: Type{Cycle{I}} ) where {I} = eltype (I)
975975IteratorEltype (:: Type{Cycle{I}} ) where {I} = IteratorEltype (I)
976- IteratorSize (:: Type{Cycle{I}} ) where {I} = IsInfinite ()
976+ IteratorSize (:: Type{Cycle{I}} ) where {I} = IsInfinite () # XXX : this is false if iterator ever becomes empty
977977
978978iterate (it:: Cycle ) = iterate (it. xs)
979979isdone (it:: Cycle ) = isdone (it. xs)
@@ -1422,43 +1422,30 @@ julia> sum(a) # Sum the remaining elements
142214227
14231423```
14241424"""
1425- mutable struct Stateful{T, VS, N <: Integer }
1425+ mutable struct Stateful{T, VS}
14261426 itr:: T
14271427 # A bit awkward right now, but adapted to the new iteration protocol
14281428 nextvalstate:: Union{VS, Nothing}
1429-
1430- # Number of remaining elements, if itr is HasLength or HasShape.
1431- # if not, store -1 - number_of_consumed_elements.
1432- # This allows us to defer calculating length until asked for.
1433- # See PR #45924
1434- remaining:: N
14351429 @inline function Stateful {<:Any, Any} (itr:: T ) where {T}
1436- itl = iterlength (itr)
1437- new {T, Any, typeof(itl)} (itr, iterate (itr), itl)
1430+ return new {T, Any} (itr, iterate (itr))
14381431 end
14391432 @inline function Stateful (itr:: T ) where {T}
14401433 VS = approx_iter_type (T)
1441- itl = iterlength (itr)
1442- return new {T, VS, typeof(itl)} (itr, iterate (itr):: VS , itl)
1434+ return new {T, VS} (itr, iterate (itr):: VS )
14431435 end
14441436end
14451437
1446- function iterlength (it):: Signed
1447- if IteratorSize (it) isa Union{HasShape, HasLength}
1448- return length (it)
1449- else
1450- - 1
1451- end
1438+ function reset! (s:: Stateful )
1439+ setfield! (s, :nextvalstate , iterate (s. itr)) # bypass convert call of setproperty!
1440+ return s
14521441end
1453-
1454- function reset! (s:: Stateful{T,VS} , itr:: T = s. itr) where {T,VS}
1442+ function reset! (s:: Stateful{T} , itr:: T ) where {T}
14551443 s. itr = itr
1456- itl = iterlength (itr)
1457- setfield! (s, :nextvalstate , iterate (itr))
1458- s. remaining = itl
1459- s
1444+ reset! (s)
1445+ return s
14601446end
14611447
1448+
14621449# Try to find an appropriate type for the (value, state tuple),
14631450# by doing a recursive unrolling of the iteration protocol up to
14641451# fixpoint.
@@ -1480,7 +1467,6 @@ end
14801467
14811468Stateful (x:: Stateful ) = x
14821469convert (:: Type{Stateful} , itr) = Stateful (itr)
1483-
14841470@inline isdone (s:: Stateful , st= nothing ) = s. nextvalstate === nothing
14851471
14861472@inline function popfirst! (s:: Stateful )
@@ -1490,8 +1476,6 @@ convert(::Type{Stateful}, itr) = Stateful(itr)
14901476 else
14911477 val, state = vs
14921478 Core. setfield! (s, :nextvalstate , iterate (s. itr, state))
1493- rem = s. remaining
1494- s. remaining = rem - typeof (rem)(1 )
14951479 return val
14961480 end
14971481end
@@ -1501,20 +1485,10 @@ end
15011485 return ns != = nothing ? ns[1 ] : sentinel
15021486end
15031487@inline iterate (s:: Stateful , state= nothing ) = s. nextvalstate === nothing ? nothing : (popfirst! (s), nothing )
1504- IteratorSize (:: Type{<:Stateful{T}} ) where {T} = IteratorSize (T) isa HasShape ? HasLength () : IteratorSize (T )
1488+ IteratorSize (:: Type{<:Stateful{T}} ) where {T} = IteratorSize (T) isa IsInfinite ? IsInfinite () : SizeUnknown ( )
15051489eltype (:: Type{<:Stateful{T}} ) where {T} = eltype (T)
15061490IteratorEltype (:: Type{<:Stateful{T}} ) where {T} = IteratorEltype (T)
15071491
1508- function length (s:: Stateful )
1509- rem = s. remaining
1510- # If rem is actually remaining length, return it.
1511- # else, rem is number of consumed elements.
1512- if rem >= 0
1513- rem
1514- else
1515- length (s. itr) - (typeof (rem)(1 ) - rem)
1516- end
1517- end
15181492end # if statement several hundred lines above
15191493
15201494"""
0 commit comments