@@ -28,9 +28,9 @@ use std::marker::PhantomData;
28
28
#[ derive( Clone , Debug ) ]
29
29
#[ must_use = "iterator adaptors are lazy and do nothing unless consumed" ]
30
30
pub struct Interleave < I , J > {
31
- a : Fuse < I > ,
32
- b : Fuse < J > ,
33
- flag : bool ,
31
+ i : Fuse < I > ,
32
+ j : Fuse < J > ,
33
+ next_coming_from_j : bool ,
34
34
}
35
35
36
36
/// Create an iterator that interleaves elements in `i` and `j`.
45
45
J : IntoIterator < Item = I :: Item > ,
46
46
{
47
47
Interleave {
48
- a : i. into_iter ( ) . fuse ( ) ,
49
- b : j. into_iter ( ) . fuse ( ) ,
50
- flag : false ,
48
+ i : i. into_iter ( ) . fuse ( ) ,
49
+ j : j. into_iter ( ) . fuse ( ) ,
50
+ next_coming_from_j : false ,
51
51
}
52
52
}
53
53
@@ -59,45 +59,49 @@ where
59
59
type Item = I :: Item ;
60
60
#[ inline]
61
61
fn next ( & mut self ) -> Option < Self :: Item > {
62
- self . flag = !self . flag ;
63
- if self . flag {
64
- match self . a . next ( ) {
65
- None => self . b . next ( ) ,
62
+ self . next_coming_from_j = !self . next_coming_from_j ;
63
+ if self . next_coming_from_j {
64
+ match self . i . next ( ) {
65
+ None => self . j . next ( ) ,
66
66
r => r,
67
67
}
68
68
} else {
69
- match self . b . next ( ) {
70
- None => self . a . next ( ) ,
69
+ match self . j . next ( ) {
70
+ None => self . i . next ( ) ,
71
71
r => r,
72
72
}
73
73
}
74
74
}
75
75
76
76
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
77
- size_hint:: add ( self . a . size_hint ( ) , self . b . size_hint ( ) )
77
+ size_hint:: add ( self . i . size_hint ( ) , self . j . size_hint ( ) )
78
78
}
79
79
80
80
fn fold < B , F > ( self , mut init : B , mut f : F ) -> B
81
81
where
82
82
F : FnMut ( B , Self :: Item ) -> B ,
83
83
{
84
- let Self { mut a, mut b, flag } = self ;
85
- if flag {
86
- match b. next ( ) {
84
+ let Self {
85
+ mut i,
86
+ mut j,
87
+ next_coming_from_j,
88
+ } = self ;
89
+ if next_coming_from_j {
90
+ match j. next ( ) {
87
91
Some ( y) => init = f ( init, y) ,
88
- None => return a . fold ( init, f) ,
92
+ None => return i . fold ( init, f) ,
89
93
}
90
94
}
91
- let res = a . try_fold ( init, |mut acc, x| {
95
+ let res = i . try_fold ( init, |mut acc, x| {
92
96
acc = f ( acc, x) ;
93
- match b . next ( ) {
97
+ match j . next ( ) {
94
98
Some ( y) => Ok ( f ( acc, y) ) ,
95
99
None => Err ( acc) ,
96
100
}
97
101
} ) ;
98
102
match res {
99
- Ok ( acc) => b . fold ( acc, f) ,
100
- Err ( acc) => a . fold ( acc, f) ,
103
+ Ok ( acc) => j . fold ( acc, f) ,
104
+ Err ( acc) => i . fold ( acc, f) ,
101
105
}
102
106
}
103
107
}
@@ -123,21 +127,21 @@ where
123
127
I : Iterator ,
124
128
J : Iterator < Item = I :: Item > ,
125
129
{
126
- it0 : I ,
127
- it1 : J ,
128
- phase : bool , // false ==> it0, true ==> it1
130
+ i : I ,
131
+ j : J ,
132
+ next_coming_from_j : bool ,
129
133
}
130
134
131
135
/// Create a new `InterleaveShortest` iterator.
132
- pub fn interleave_shortest < I , J > ( a : I , b : J ) -> InterleaveShortest < I , J >
136
+ pub fn interleave_shortest < I , J > ( i : I , j : J ) -> InterleaveShortest < I , J >
133
137
where
134
138
I : Iterator ,
135
139
J : Iterator < Item = I :: Item > ,
136
140
{
137
141
InterleaveShortest {
138
- it0 : a ,
139
- it1 : b ,
140
- phase : false ,
142
+ i ,
143
+ j ,
144
+ next_coming_from_j : false ,
141
145
}
142
146
}
143
147
@@ -150,26 +154,26 @@ where
150
154
151
155
#[ inline]
152
156
fn next ( & mut self ) -> Option < Self :: Item > {
153
- let e = if self . phase {
154
- self . it1 . next ( )
157
+ let e = if self . next_coming_from_j {
158
+ self . j . next ( )
155
159
} else {
156
- self . it0 . next ( )
160
+ self . i . next ( )
157
161
} ;
158
162
if e. is_some ( ) {
159
- self . phase = !self . phase ;
163
+ self . next_coming_from_j = !self . next_coming_from_j ;
160
164
}
161
165
e
162
166
}
163
167
164
168
#[ inline]
165
169
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
166
170
let ( curr_hint, next_hint) = {
167
- let it0_hint = self . it0 . size_hint ( ) ;
168
- let it1_hint = self . it1 . size_hint ( ) ;
169
- if self . phase {
170
- ( it1_hint , it0_hint )
171
+ let i_hint = self . i . size_hint ( ) ;
172
+ let j_hint = self . j . size_hint ( ) ;
173
+ if self . next_coming_from_j {
174
+ ( j_hint , i_hint )
171
175
} else {
172
- ( it0_hint , it1_hint )
176
+ ( i_hint , j_hint )
173
177
}
174
178
} ;
175
179
let ( curr_lower, curr_upper) = curr_hint;
@@ -201,19 +205,19 @@ where
201
205
F : FnMut ( B , Self :: Item ) -> B ,
202
206
{
203
207
let Self {
204
- mut it0 ,
205
- mut it1 ,
206
- phase ,
208
+ mut i ,
209
+ mut j ,
210
+ next_coming_from_j ,
207
211
} = self ;
208
- if phase {
209
- match it1 . next ( ) {
212
+ if next_coming_from_j {
213
+ match j . next ( ) {
210
214
Some ( y) => init = f ( init, y) ,
211
215
None => return init,
212
216
}
213
217
}
214
- let res = it0 . try_fold ( init, |mut acc, x| {
218
+ let res = i . try_fold ( init, |mut acc, x| {
215
219
acc = f ( acc, x) ;
216
- match it1 . next ( ) {
220
+ match j . next ( ) {
217
221
Some ( y) => Ok ( f ( acc, y) ) ,
218
222
None => Err ( acc) ,
219
223
}
0 commit comments