18
18
19
19
use std:: cmp;
20
20
use std:: container:: { Container , Mutable , Map , Set } ;
21
+ use std:: iterator:: { Iterator , IteratorUtil , ZipIterator , Counter } ;
21
22
use std:: uint;
22
23
use std:: util:: replace;
24
+ use std:: vec:: { VecIterator , VecMutIterator , VecRevIterator , VecMutRevIterator } ;
23
25
24
26
#[ allow( missing_doc) ]
25
27
pub struct SmallIntMap < T > {
@@ -168,6 +170,40 @@ impl<V> SmallIntMap<V> {
168
170
pub fn get < ' a > ( & ' a self , key : & uint ) -> & ' a V {
169
171
self . find ( key) . expect ( "key not present" )
170
172
}
173
+
174
+ /// An iterator visiting all key-value pairs in ascending order by the keys.
175
+ /// Iterator element type is (uint, &'r V)
176
+ pub fn iter < ' r > ( & ' r self ) -> SmallIntMapIterator < ' r , V > {
177
+ SmallIntMapIterator {
178
+ iter : Counter :: new ( 0 , 1 ) . zip ( self . v . iter ( ) )
179
+ }
180
+ }
181
+
182
+ /// An iterator visiting all key-value pairs in ascending order by the keys,
183
+ /// with mutable references to the values
184
+ /// Iterator element type is (uint, &'r mut V)
185
+ pub fn mut_iter < ' r > ( & ' r mut self ) -> SmallIntMapMutIterator < ' r , V > {
186
+ SmallIntMapMutIterator {
187
+ iter : Counter :: new ( 0 , 1 ) . zip ( self . v . mut_iter ( ) )
188
+ }
189
+ }
190
+
191
+ /// An iterator visiting all key-value pairs in descending order by the keys.
192
+ /// Iterator element type is (uint, &'r V)
193
+ pub fn rev_iter < ' r > ( & ' r self ) -> SmallIntMapRevIterator < ' r , V > {
194
+ SmallIntMapRevIterator {
195
+ iter : Counter :: new ( self . len ( ) as int - 1 , -1 ) . zip ( self . v . rev_iter ( ) )
196
+ }
197
+ }
198
+
199
+ /// An iterator visiting all key-value pairs in descending order by the keys,
200
+ /// with mutable references to the values
201
+ /// Iterator element type is (uint, &'r mut V)
202
+ pub fn mut_rev_iter < ' r > ( & ' r mut self ) -> SmallIntMapMutRevIterator < ' r , V > {
203
+ SmallIntMapMutRevIterator {
204
+ iter : Counter :: new ( self . len ( ) as int - 1 , -1 ) . zip ( self . v . mut_rev_iter ( ) )
205
+ }
206
+ }
171
207
}
172
208
173
209
impl < V : Copy > SmallIntMap < V > {
@@ -186,6 +222,95 @@ impl<V:Copy> SmallIntMap<V> {
186
222
}
187
223
}
188
224
225
+
226
+ macro_rules! iterator {
227
+ /* FIXME: #4375 Cannot attach documentation/attributes to a macro generated struct.
228
+ (struct $name:ident -> $ptr:ty, $elem:ty) => {
229
+ pub struct $name<'self, T> {
230
+ priv ptr: $ptr,
231
+ priv end: $ptr,
232
+ priv lifetime: $elem // FIXME: #5922
233
+ }
234
+ };*/
235
+ ( impl $name: ident -> $elem: ty) => {
236
+ impl <' self , T > Iterator <( uint, $elem) > for $name<' self , T > {
237
+ #[ inline]
238
+ pub fn next( & mut self ) -> Option <( uint, $elem) > {
239
+ for self . iter. advance |( idx, elem) | {
240
+ match elem {
241
+ & None => { }
242
+ & Some ( ref e) => { return Some ( ( idx as uint, e) ) }
243
+ }
244
+ }
245
+
246
+ None
247
+ }
248
+ }
249
+ }
250
+ }
251
+
252
+ macro_rules! mut_iterator {
253
+ /* FIXME: #4375 Cannot attach documentation/attributes to a macro generated struct.
254
+ (struct $name:ident -> $ptr:ty, $elem:ty) => {
255
+ pub struct $name<'self, T> {
256
+ priv ptr: $ptr,
257
+ priv end: $ptr,
258
+ priv lifetime: $elem // FIXME: #5922
259
+ }
260
+ };*/
261
+ ( impl $name: ident -> $elem: ty) => {
262
+ impl <' self , T > Iterator <( uint, $elem) > for $name<' self , T > {
263
+ #[ inline]
264
+ pub fn next( & mut self ) -> Option <( uint, $elem) > {
265
+ for self . iter. advance |( idx, elem) | {
266
+ match elem {
267
+ & None => { }
268
+ & Some ( ref mut e) => { return Some ( ( idx as uint, e) ) }
269
+ }
270
+ }
271
+
272
+ None
273
+ }
274
+ }
275
+ }
276
+ }
277
+
278
+ pub struct SmallIntMapIterator < ' self , T > {
279
+ priv iter : ZipIterator < int ,
280
+ Counter < int > ,
281
+ & ' self Option < T > ,
282
+ VecIterator < ' self , Option < T > > >
283
+ }
284
+
285
+ iterator ! { impl SmallIntMapIterator -> & ' self T }
286
+
287
+ pub struct SmallIntMapMutIterator < ' self , T > {
288
+ priv iter : ZipIterator < int ,
289
+ Counter < int > ,
290
+ & ' self mut Option < T > ,
291
+ VecMutIterator < ' self , Option < T > > >
292
+ }
293
+
294
+ mut_iterator ! { impl SmallIntMapMutIterator -> & ' self mut T }
295
+
296
+ pub struct SmallIntMapRevIterator < ' self , T > {
297
+ priv iter : ZipIterator < int ,
298
+ Counter < int > ,
299
+ & ' self Option < T > ,
300
+ VecRevIterator < ' self , Option < T > > >
301
+ }
302
+
303
+ iterator ! { impl SmallIntMapRevIterator -> & ' self T }
304
+
305
+ pub struct SmallIntMapMutRevIterator < ' self , T > {
306
+ priv iter : ZipIterator < int ,
307
+ Counter < int > ,
308
+ & ' self mut Option < T > ,
309
+ VecMutRevIterator < ' self , Option < T > > >
310
+ }
311
+
312
+ mut_iterator ! { impl SmallIntMapMutRevIterator -> & ' self mut T }
313
+
189
314
/// A set implemented on top of the SmallIntMap type. This set is always a set
190
315
/// of integers, and the space requirements are on the order of the highest
191
316
/// valued integer in the set.
@@ -281,8 +406,57 @@ impl SmallIntSet {
281
406
282
407
/// Visit all values in order
283
408
pub fn each ( & self , f : & fn ( & uint ) -> bool ) -> bool { self . map . each_key ( f) }
409
+
410
+ /// An iterator visiting all set members in ascending order.
411
+ /// Iterator element type is uint
412
+ pub fn iter < ' r > ( & ' r self ) -> SmallIntSetIterator < ' r > {
413
+ SmallIntSetIterator {
414
+ iter : self . map . iter ( )
415
+ }
416
+ }
417
+
418
+ /// An iterator visiting all set members in descending order.
419
+ /// Iterator element type is uint
420
+ pub fn rev_iter < ' r > ( & ' r mut self ) -> SmallIntSetRevIterator < ' r > {
421
+ SmallIntSetRevIterator {
422
+ iter : self . map . rev_iter ( )
423
+ }
424
+ }
425
+
426
+ }
427
+
428
+ pub struct SmallIntSetIterator < ' self > {
429
+ priv iter : SmallIntMapIterator < ' self , ( ) >
430
+ }
431
+
432
+ pub struct SmallIntSetRevIterator < ' self > {
433
+ priv iter : SmallIntMapRevIterator < ' self , ( ) >
434
+ }
435
+
436
+ impl < ' self > Iterator < uint > for SmallIntSetIterator < ' self > {
437
+ #[ inline]
438
+ pub fn next ( & mut self ) -> Option < uint > {
439
+ let next_opt = self . iter . next ( ) ;
440
+ match next_opt {
441
+ None => { None }
442
+ Some ( ( idx, _) ) => { Some ( idx) }
443
+ }
444
+ }
284
445
}
285
446
447
+ impl < ' self > Iterator < uint > for SmallIntSetRevIterator < ' self > {
448
+ #[ inline]
449
+ pub fn next ( & mut self ) -> Option < uint > {
450
+ let next_opt = self . iter . next ( ) ;
451
+ match next_opt {
452
+ None => { None }
453
+ Some ( ( idx, _) ) => { Some ( idx) }
454
+ }
455
+ }
456
+ }
457
+
458
+
459
+
286
460
#[ cfg( test) ]
287
461
mod tests {
288
462
@@ -375,6 +549,82 @@ mod tests {
375
549
assert_eq ! ( m. pop( & 1 ) , Some ( 2 ) ) ;
376
550
assert_eq ! ( m. pop( & 1 ) , None ) ;
377
551
}
552
+
553
+ #[ test]
554
+ fn test_iterator ( ) {
555
+ let mut a = SmallIntMap :: new ( ) ;
556
+
557
+ assert ! ( a. insert( 0 , 1 ) ) ;
558
+ assert ! ( a. insert( 1 , 2 ) ) ;
559
+ assert ! ( a. insert( 2 , 5 ) ) ;
560
+ assert ! ( a. insert( 3 , 10 ) ) ;
561
+ assert ! ( a. insert( 4 , 11 ) ) ;
562
+
563
+ let mut it = a. iter ( ) ;
564
+ assert_eq ! ( it. next( ) . unwrap( ) , ( 0 , & 1 ) ) ;
565
+ assert_eq ! ( it. next( ) . unwrap( ) , ( 1 , & 2 ) ) ;
566
+ assert_eq ! ( it. next( ) . unwrap( ) , ( 2 , & 5 ) ) ;
567
+ assert_eq ! ( it. next( ) . unwrap( ) , ( 3 , & 10 ) ) ;
568
+ assert_eq ! ( it. next( ) . unwrap( ) , ( 4 , & 11 ) ) ;
569
+ assert ! ( it. next( ) . is_none( ) ) ;
570
+ }
571
+
572
+ #[ test]
573
+ fn test_mut_iterator ( ) {
574
+ let mut a = SmallIntMap :: new ( ) ;
575
+
576
+ assert ! ( a. insert( 0 , 1 ) ) ;
577
+ assert ! ( a. insert( 1 , 1 ) ) ;
578
+ assert ! ( a. insert( 2 , 1 ) ) ;
579
+ assert ! ( a. insert( 3 , 1 ) ) ;
580
+ assert ! ( a. insert( 4 , 1 ) ) ;
581
+
582
+ for a. mut_iter( ) . advance |( _, v) | {
583
+ * v += 1 ;
584
+ }
585
+
586
+ assert ! ( a. iter( ) . all( |( _, v) | * v == 2 ) ) ;
587
+ }
588
+
589
+ #[ test]
590
+ fn test_rev_iterator ( ) {
591
+ let mut a = SmallIntMap : : new( ) ;
592
+
593
+ assert ! ( a. insert( 0 , 1 ) ) ;
594
+ assert ! ( a. insert( 1 , 2 ) ) ;
595
+ assert ! ( a. insert( 2 , 5 ) ) ;
596
+ assert ! ( a. insert( 3 , 10 ) ) ;
597
+ assert ! ( a. insert( 4 , 11 ) ) ;
598
+
599
+ let mut b = SmallIntMap :: new( ) ;
600
+
601
+ assert ! ( b. insert( 0 , 11 ) ) ;
602
+ assert ! ( b. insert( 1 , 10 ) ) ;
603
+ assert ! ( b. insert( 2 , 5 ) ) ;
604
+ assert ! ( b. insert( 3 , 2 ) ) ;
605
+ assert ! ( b. insert( 4 , 1 ) ) ;
606
+
607
+ let ( a_it, b_it) = ( a. iter( ) , b. rev_iter( ) ) ;
608
+
609
+ assert ! ( a_it. zip( b_it) . all( |( ( _ , v1) , ( _, v2) ) | * v1 == * v2) ) ;
610
+ }
611
+
612
+ #[ test]
613
+ fn test_mut_rev_iterator( ) {
614
+ let mut a = SmallIntMap :: new( ) ;
615
+
616
+ assert ! ( a. insert( 0 , 5 ) ) ;
617
+ assert ! ( a. insert( 1 , 4 ) ) ;
618
+ assert ! ( a. insert( 2 , 3 ) ) ;
619
+ assert ! ( a. insert( 3 , 2 ) ) ;
620
+ assert ! ( a. insert( 4 , 1 ) ) ;
621
+
622
+ for a. mut_rev_iter( ) . advance |( i, v) | {
623
+ * v += i as int;
624
+ }
625
+
626
+ assert ! ( a. iter( ) . all( |( _, v) | * v == 5 ) ) ;
627
+ }
378
628
}
379
629
380
630
#[ cfg( test) ]
@@ -535,4 +785,41 @@ mod test_set {
535
785
}
536
786
assert_eq ! ( i, expected. len( ) ) ;
537
787
}
788
+
789
+ #[ test]
790
+ fn test_iterator ( ) {
791
+ let mut a = SmallIntSet : : new( ) ;
792
+
793
+ assert ! ( a. insert( 0 ) ) ;
794
+ assert ! ( a. insert( 1 ) ) ;
795
+ assert ! ( a. insert( 2 ) ) ;
796
+ assert ! ( a. insert( 3 ) ) ;
797
+ assert ! ( a. insert( 4 ) ) ;
798
+
799
+ let mut it = a. iter( ) ;
800
+ assert_eq ! ( it. next( ) . unwrap( ) , 0 ) ;
801
+ assert_eq ! ( it. next( ) . unwrap( ) , 1 ) ;
802
+ assert_eq ! ( it. next( ) . unwrap( ) , 2 ) ;
803
+ assert_eq ! ( it. next( ) . unwrap( ) , 3 ) ;
804
+ assert_eq ! ( it. next( ) . unwrap( ) , 4 ) ;
805
+ assert ! ( it. next( ) . is_none( ) ) ;
806
+ }
807
+
808
+ #[ test]
809
+ fn test_rev_iterator( ) {
810
+ let mut a = SmallIntSet :: new( ) ;
811
+
812
+ assert ! ( a. insert( 0 ) ) ;
813
+ assert ! ( a. insert( 1 ) ) ;
814
+ assert ! ( a. insert( 2 ) ) ;
815
+ assert ! ( a. insert( 3 ) ) ;
816
+ assert ! ( a. insert( 4 ) ) ;
817
+
818
+ let a_it = a. rev_iter( ) ;
819
+
820
+ assert ! ( do a_it. enumerate( ) . all |( i, v2 ) | {
821
+ i + v2 == 4
822
+ } ) ;
823
+ }
824
+
538
825
}
0 commit comments