@@ -65,7 +65,7 @@ use cmp::{Eq, TotalOrd, Ordering, Less, Equal, Greater};
65
65
use cmp;
66
66
use iterator:: * ;
67
67
use libc:: c_void;
68
- use num:: Zero ;
68
+ use num:: { Integer , Zero , CheckedAdd , Saturating } ;
69
69
use option:: { None , Option , Some } ;
70
70
use ptr:: to_unsafe_ptr;
71
71
use ptr;
@@ -209,6 +209,7 @@ pub struct SplitIterator<'self, T> {
209
209
}
210
210
211
211
impl < ' self , T > Iterator < & ' self [ T ] > for SplitIterator < ' self , T > {
212
+ #[ inline]
212
213
fn next ( & mut self ) -> Option < & ' self [ T ] > {
213
214
if self . finished { return None ; }
214
215
@@ -230,6 +231,21 @@ impl<'self, T> Iterator<&'self [T]> for SplitIterator<'self, T> {
230
231
}
231
232
}
232
233
}
234
+
235
+ #[ inline]
236
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
237
+ if self . finished {
238
+ return ( 0 , Some ( 0 ) )
239
+ }
240
+ // if the predicate doesn't match anything, we yield one slice
241
+ // if it matches every element, we yield N+1 empty slices where
242
+ // N is either the number of elements or the number of splits.
243
+ match ( self . v . len ( ) , self . n ) {
244
+ ( 0 , _) => ( 1 , Some ( 1 ) ) ,
245
+ ( _, 0 ) => ( 1 , Some ( 1 ) ) ,
246
+ ( l, n) => ( 1 , cmp:: min ( l, n) . checked_add ( & 1 u) )
247
+ }
248
+ }
233
249
}
234
250
235
251
/// An iterator over the slices of a vector separated by elements that
@@ -242,6 +258,7 @@ pub struct RSplitIterator<'self, T> {
242
258
}
243
259
244
260
impl < ' self , T > Iterator < & ' self [ T ] > for RSplitIterator < ' self , T > {
261
+ #[ inline]
245
262
fn next ( & mut self ) -> Option < & ' self [ T ] > {
246
263
if self . finished { return None ; }
247
264
@@ -263,6 +280,18 @@ impl<'self, T> Iterator<&'self [T]> for RSplitIterator<'self, T> {
263
280
}
264
281
}
265
282
}
283
+
284
+ #[ inline]
285
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
286
+ if self . finished {
287
+ return ( 0 , Some ( 0 ) )
288
+ }
289
+ match ( self . v . len ( ) , self . n ) {
290
+ ( 0 , _) => ( 1 , Some ( 1 ) ) ,
291
+ ( _, 0 ) => ( 1 , Some ( 1 ) ) ,
292
+ ( l, n) => ( 1 , cmp:: min ( l, n) . checked_add ( & 1 u) )
293
+ }
294
+ }
266
295
}
267
296
268
297
// Appending
@@ -453,6 +482,7 @@ pub struct WindowIter<'self, T> {
453
482
}
454
483
455
484
impl < ' self , T > Iterator < & ' self [ T ] > for WindowIter < ' self , T > {
485
+ #[ inline]
456
486
fn next ( & mut self ) -> Option < & ' self [ T ] > {
457
487
if self . size > self . v . len ( ) {
458
488
None
@@ -462,6 +492,16 @@ impl<'self, T> Iterator<&'self [T]> for WindowIter<'self, T> {
462
492
ret
463
493
}
464
494
}
495
+
496
+ #[ inline]
497
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
498
+ if self . size > self . v . len ( ) {
499
+ ( 0 , Some ( 0 ) )
500
+ } else {
501
+ let x = self . v . len ( ) - self . size ;
502
+ ( x. saturating_add ( 1 ) , x. checked_add ( & 1 u) )
503
+ }
504
+ }
465
505
}
466
506
467
507
/// An iterator over a vector in (non-overlapping) chunks (`size`
@@ -476,6 +516,7 @@ pub struct ChunkIter<'self, T> {
476
516
}
477
517
478
518
impl < ' self , T > Iterator < & ' self [ T ] > for ChunkIter < ' self , T > {
519
+ #[ inline]
479
520
fn next ( & mut self ) -> Option < & ' self [ T ] > {
480
521
if self . v . len ( ) == 0 {
481
522
None
@@ -487,9 +528,21 @@ impl<'self, T> Iterator<&'self [T]> for ChunkIter<'self, T> {
487
528
Some ( fst)
488
529
}
489
530
}
531
+
532
+ #[ inline]
533
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
534
+ if self . v . len ( ) == 0 {
535
+ ( 0 , Some ( 0 ) )
536
+ } else {
537
+ let ( n, rem) = self . v . len ( ) . div_rem ( & self . size ) ;
538
+ let n = if rem > 0 { n+1 } else { n } ;
539
+ ( n, Some ( n) )
540
+ }
541
+ }
490
542
}
491
543
492
544
impl < ' self , T > DoubleEndedIterator < & ' self [ T ] > for ChunkIter < ' self , T > {
545
+ #[ inline]
493
546
fn next_back ( & mut self ) -> Option < & ' self [ T ] > {
494
547
if self . v . len ( ) == 0 {
495
548
None
@@ -2223,6 +2276,7 @@ impl<'self, T> RandomAccessIterator<&'self T> for VecIterator<'self, T> {
2223
2276
exact
2224
2277
}
2225
2278
2279
+ #[ inline]
2226
2280
fn idx ( & self , index : uint ) -> Option < & ' self T > {
2227
2281
unsafe {
2228
2282
if index < self . indexable ( ) {
@@ -2268,6 +2322,7 @@ pub struct MoveIterator<T> {
2268
2322
}
2269
2323
2270
2324
impl < T > Iterator < T > for MoveIterator < T > {
2325
+ #[ inline]
2271
2326
fn next ( & mut self ) -> Option < T > {
2272
2327
// this is peculiar, but is required for safety with respect
2273
2328
// to dtors. It traverses the first half of the vec, and
@@ -2285,6 +2340,12 @@ impl<T> Iterator<T> for MoveIterator<T> {
2285
2340
2286
2341
self . v . pop_opt ( )
2287
2342
}
2343
+
2344
+ #[ inline]
2345
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
2346
+ let l = self . v . len ( ) ;
2347
+ ( l, Some ( l) )
2348
+ }
2288
2349
}
2289
2350
2290
2351
/// An iterator that moves out of a vector in reverse order.
@@ -2294,9 +2355,16 @@ pub struct MoveRevIterator<T> {
2294
2355
}
2295
2356
2296
2357
impl < T > Iterator < T > for MoveRevIterator < T > {
2358
+ #[ inline]
2297
2359
fn next ( & mut self ) -> Option < T > {
2298
2360
self . v . pop_opt ( )
2299
2361
}
2362
+
2363
+ #[ inline]
2364
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
2365
+ let l = self . v . len ( ) ;
2366
+ ( l, Some ( l) )
2367
+ }
2300
2368
}
2301
2369
2302
2370
impl < A > FromIterator < A > for ~[ A ] {
0 commit comments