@@ -256,6 +256,54 @@ where
256
256
}
257
257
}
258
258
259
+ fn fold < B , G > ( mut self , init : B , mut f : G ) -> B
260
+ where
261
+ Self : Sized ,
262
+ G : FnMut ( B , Self :: Item ) -> B ,
263
+ {
264
+ let mut acc = init;
265
+ let mut left = self . left . next ( ) ;
266
+ let mut right = self . right . next ( ) ;
267
+
268
+ loop {
269
+ match ( left, right) {
270
+ ( Some ( l) , Some ( r) ) => match self . cmp_fn . merge ( l, r) {
271
+ ( None , Some ( r) , x) => {
272
+ acc = f ( acc, x) ;
273
+ left = self . left . next ( ) ;
274
+ right = Some ( r) ;
275
+ }
276
+ ( Some ( l) , None , x) => {
277
+ acc = f ( acc, x) ;
278
+ left = Some ( l) ;
279
+ right = self . right . next ( ) ;
280
+ }
281
+ ( None , None , x) => {
282
+ acc = f ( acc, x) ;
283
+ left = self . left . next ( ) ;
284
+ right = self . right . next ( ) ;
285
+ }
286
+ ( Some ( _) , Some ( _) , _) => unreachable ! ( ) ,
287
+ } ,
288
+ ( Some ( l) , None ) => {
289
+ self . left . put_back ( l) ;
290
+ acc = self . left . fold ( acc, |acc, x| f ( acc, F :: left ( x) ) ) ;
291
+ break ;
292
+ }
293
+ ( None , Some ( r) ) => {
294
+ self . right . put_back ( r) ;
295
+ acc = self . right . fold ( acc, |acc, x| f ( acc, F :: right ( x) ) ) ;
296
+ break ;
297
+ }
298
+ ( None , None ) => {
299
+ break ;
300
+ }
301
+ }
302
+ }
303
+
304
+ acc
305
+ }
306
+
259
307
fn size_hint ( & self ) -> SizeHint {
260
308
F :: size_hint ( self . left . size_hint ( ) , self . right . size_hint ( ) )
261
309
}
0 commit comments