@@ -973,7 +973,7 @@ cycle(xs) = Cycle(xs)
973
973
974
974
eltype (:: Type{Cycle{I}} ) where {I} = eltype (I)
975
975
IteratorEltype (:: 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
977
977
978
978
iterate (it:: Cycle ) = iterate (it. xs)
979
979
isdone (it:: Cycle ) = isdone (it. xs)
@@ -1422,43 +1422,30 @@ julia> sum(a) # Sum the remaining elements
1422
1422
7
1423
1423
```
1424
1424
"""
1425
- mutable struct Stateful{T, VS, N <: Integer }
1425
+ mutable struct Stateful{T, VS}
1426
1426
itr:: T
1427
1427
# A bit awkward right now, but adapted to the new iteration protocol
1428
1428
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
1435
1429
@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))
1438
1431
end
1439
1432
@inline function Stateful (itr:: T ) where {T}
1440
1433
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 )
1443
1435
end
1444
1436
end
1445
1437
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
1452
1441
end
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}
1455
1443
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
1460
1446
end
1461
1447
1448
+
1462
1449
# Try to find an appropriate type for the (value, state tuple),
1463
1450
# by doing a recursive unrolling of the iteration protocol up to
1464
1451
# fixpoint.
@@ -1480,7 +1467,6 @@ end
1480
1467
1481
1468
Stateful (x:: Stateful ) = x
1482
1469
convert (:: Type{Stateful} , itr) = Stateful (itr)
1483
-
1484
1470
@inline isdone (s:: Stateful , st= nothing ) = s. nextvalstate === nothing
1485
1471
1486
1472
@inline function popfirst! (s:: Stateful )
@@ -1490,8 +1476,6 @@ convert(::Type{Stateful}, itr) = Stateful(itr)
1490
1476
else
1491
1477
val, state = vs
1492
1478
Core. setfield! (s, :nextvalstate , iterate (s. itr, state))
1493
- rem = s. remaining
1494
- s. remaining = rem - typeof (rem)(1 )
1495
1479
return val
1496
1480
end
1497
1481
end
@@ -1501,20 +1485,10 @@ end
1501
1485
return ns != = nothing ? ns[1 ] : sentinel
1502
1486
end
1503
1487
@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 ( )
1505
1489
eltype (:: Type{<:Stateful{T}} ) where {T} = eltype (T)
1506
1490
IteratorEltype (:: Type{<:Stateful{T}} ) where {T} = IteratorEltype (T)
1507
1491
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
1518
1492
end # if statement several hundred lines above
1519
1493
1520
1494
"""
0 commit comments