@@ -1016,44 +1016,56 @@ mutable struct Stateful{T, VS}
1016
1016
# A bit awkward right now, but adapted to the new iteration protocol
1017
1017
nextvalstate:: Union{VS, Nothing}
1018
1018
taken:: Int
1019
- # Try to find an appropriate type for the (value, state tuple),
1020
- # by doing a recursive unrolling of the iteration protocol up to
1021
- # fixpoint.
1022
- function fixpoint_iter_type (itrT:: Type , valT:: Type , stateT:: Type )
1023
- nextvalstate = Base. _return_type (next, Tuple{itrT, stateT})
1024
- nextvalstate <: Tuple{Any, Any} || return Any
1025
- nextvalstate = Tuple{
1026
- typejoin (valT, fieldtype (nextvalstate, 1 )),
1027
- typejoin (stateT, fieldtype (nextvalstate, 2 ))}
1028
- return (Tuple{valT, stateT} == nextvalstate ? nextvalstate :
1029
- fixpoint_iter_type (itrT,
1030
- fieldtype (nextvalstate, 1 ),
1031
- fieldtype (nextvalstate, 2 )))
1032
- end
1033
- function Stateful (itr:: T ) where {T}
1019
+ @inline function Stateful (itr:: T ) where {T}
1034
1020
state = start (itr)
1035
1021
VS = fixpoint_iter_type (T, Union{}, typeof (state))
1036
- vs = done (itr, state) ? nothing : next (itr, state):: VS
1037
- new {T, VS} (itr, vs, 0 )
1022
+ if done (itr, state)
1023
+ new {T, VS} (itr, nothing , 0 )
1024
+ else
1025
+ new {T, VS} (itr, next (itr, state):: VS , 0 )
1026
+ end
1038
1027
end
1039
1028
end
1040
1029
1030
+ # Try to find an appropriate type for the (value, state tuple),
1031
+ # by doing a recursive unrolling of the iteration protocol up to
1032
+ # fixpoint.
1033
+ function fixpoint_iter_type (itrT:: Type , valT:: Type , stateT:: Type )
1034
+ nextvalstate = Base. _return_type (next, Tuple{itrT, stateT})
1035
+ nextvalstate <: Tuple{Any, Any} || return Any
1036
+ nextvalstate = Tuple{
1037
+ typejoin (valT, fieldtype (nextvalstate, 1 )),
1038
+ typejoin (stateT, fieldtype (nextvalstate, 2 ))}
1039
+ return (Tuple{valT, stateT} == nextvalstate ? nextvalstate :
1040
+ fixpoint_iter_type (itrT,
1041
+ fieldtype (nextvalstate, 1 ),
1042
+ fieldtype (nextvalstate, 2 )))
1043
+ end
1044
+
1041
1045
convert (:: Type{Stateful} , itr) = Stateful (itr)
1042
1046
1043
- isempty (s:: Stateful ) = s. nextvalstate === nothing
1047
+ @inline isempty (s:: Stateful ) = s. nextvalstate === nothing
1044
1048
1045
- function popfirst! (s:: Stateful )
1046
- isempty (s) && throw (EOFError ())
1047
- val, state = s. nextvalstate
1048
- s. nextvalstate = done (s. itr, state) ? nothing : next (s. itr, state)
1049
- s. taken += 1
1050
- val
1049
+ @inline function popfirst! (s:: Stateful )
1050
+ vs = s. nextvalstate
1051
+ if vs === nothing
1052
+ throw (EOFError ())
1053
+ else
1054
+ val, state = vs
1055
+ if done (s. itr, state)
1056
+ s. nextvalstate = nothing
1057
+ else
1058
+ s. nextvalstate = next (s. itr, state)
1059
+ end
1060
+ s. taken += 1
1061
+ return val
1062
+ end
1051
1063
end
1052
1064
1053
- peek (s:: Stateful , sentinel= nothing ) = s. nextvalstate != = nothing ? s. nextvalstate[1 ] : sentinel
1054
- start (s:: Stateful ) = nothing
1055
- next (s:: Stateful , state) = popfirst! (s), nothing
1056
- done (s:: Stateful , state) = isempty (s)
1065
+ @inline peek (s:: Stateful , sentinel= nothing ) = s. nextvalstate != = nothing ? s. nextvalstate[1 ] : sentinel
1066
+ @inline start (s:: Stateful ) = nothing
1067
+ @inline next (s:: Stateful , state) = popfirst! (s), nothing
1068
+ @inline done (s:: Stateful , state) = isempty (s)
1057
1069
IteratorSize (:: Type{Stateful{VS,T}} where VS) where {T} =
1058
1070
isa (IteratorSize (T), SizeUnknown) ? SizeUnknown () : HasLength ()
1059
1071
eltype (:: Type{Stateful{VS, T}} where VS) where {T} = eltype (T)
0 commit comments