@@ -17,8 +17,7 @@ use default::Default;
17
17
use fmt:: Show ;
18
18
use fmt;
19
19
use hash:: { Hash , Hasher , RandomSipHasher } ;
20
- use iter:: { Iterator , IteratorExt , FromIterator , FilterMap , Chain , Repeat , Zip , Extend , repeat} ;
21
- use iter;
20
+ use iter:: { Iterator , IteratorExt , FromIterator , Map , FilterMap , Chain , Repeat , Zip , Extend , repeat} ;
22
21
use option:: Option :: { Some , None , mod} ;
23
22
use result:: Result :: { Ok , Err } ;
24
23
@@ -252,7 +251,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
252
251
/// ```
253
252
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
254
253
pub fn iter < ' a > ( & ' a self ) -> SetItems < ' a , T > {
255
- self . map . keys ( )
254
+ SetItems { iter : self . map . keys ( ) }
256
255
}
257
256
258
257
/// Creates a consuming iterator, that is, one that moves each value out
@@ -279,7 +278,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
279
278
pub fn into_iter ( self ) -> SetMoveItems < T > {
280
279
fn first < A , B > ( ( a, _) : ( A , B ) ) -> A { a }
281
280
282
- self . map . into_iter ( ) . map ( first)
281
+ SetMoveItems { iter : self . map . into_iter ( ) . map ( first) }
283
282
}
284
283
285
284
/// Visit the values representing the difference.
@@ -312,7 +311,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
312
311
if !other. contains ( elt) { Some ( elt) } else { None }
313
312
}
314
313
315
- repeat ( other) . zip ( self . iter ( ) ) . filter_map ( filter)
314
+ SetAlgebraItems { iter : repeat ( other) . zip ( self . iter ( ) ) . filter_map ( filter) }
316
315
}
317
316
318
317
/// Visit the values representing the symmetric difference.
@@ -337,8 +336,8 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
337
336
/// ```
338
337
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
339
338
pub fn symmetric_difference < ' a > ( & ' a self , other : & ' a HashSet < T , H > )
340
- -> Chain < SetAlgebraItems < ' a , T , H > , SetAlgebraItems < ' a , T , H > > {
341
- self . difference ( other) . chain ( other. difference ( self ) )
339
+ -> SymDifferenceItems < ' a , T , H > {
340
+ SymDifferenceItems { iter : self . difference ( other) . chain ( other. difference ( self ) ) }
342
341
}
343
342
344
343
/// Visit the values representing the intersection.
@@ -366,7 +365,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
366
365
if other. contains ( elt) { Some ( elt) } else { None }
367
366
}
368
367
369
- repeat ( other) . zip ( self . iter ( ) ) . filter_map ( filter)
368
+ SetAlgebraItems { iter : repeat ( other) . zip ( self . iter ( ) ) . filter_map ( filter) }
370
369
}
371
370
372
371
/// Visit the values representing the union.
@@ -387,9 +386,8 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
387
386
/// assert_eq!(diff, [1i, 2, 3, 4].iter().map(|&x| x).collect());
388
387
/// ```
389
388
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
390
- pub fn union < ' a > ( & ' a self , other : & ' a HashSet < T , H > )
391
- -> Chain < SetItems < ' a , T > , SetAlgebraItems < ' a , T , H > > {
392
- self . iter ( ) . chain ( other. difference ( self ) )
389
+ pub fn union < ' a > ( & ' a self , other : & ' a HashSet < T , H > ) -> UnionItems < ' a , T , H > {
390
+ UnionItems { iter : self . iter ( ) . chain ( other. difference ( self ) ) }
393
391
}
394
392
395
393
/// Return the number of elements in the set
@@ -617,20 +615,61 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> Default for HashSet<T, H> {
617
615
}
618
616
619
617
/// HashSet iterator
620
- pub type SetItems < ' a , K > = Keys < ' a , K , ( ) > ;
618
+ pub struct SetItems < ' a , K : ' a > {
619
+ iter : Keys < ' a , K , ( ) >
620
+ }
621
621
622
622
/// HashSet move iterator
623
- pub type SetMoveItems < K > = iter:: Map < ( K , ( ) ) , K , MoveEntries < K , ( ) > , fn ( ( K , ( ) ) ) -> K > ;
623
+ pub struct SetMoveItems < K > {
624
+ iter : Map < ( K , ( ) ) , K , MoveEntries < K , ( ) > , fn ( ( K , ( ) ) ) -> K >
625
+ }
624
626
625
627
// `Repeat` is used to feed the filter closure an explicit capture
626
628
// of a reference to the other set
627
- /// Set operations iterator
628
- pub type SetAlgebraItems < ' a , T , H > = FilterMap <
629
- ( & ' a HashSet < T , H > , & ' a T ) ,
630
- & ' a T ,
631
- Zip < Repeat < & ' a HashSet < T , H > > , SetItems < ' a , T > > ,
632
- for <' b > fn ( ( & HashSet < T , H > , & ' b T ) ) -> Option < & ' b T > ,
633
- > ;
629
+ /// Set operations iterator, used directly for intersection and difference
630
+ pub struct SetAlgebraItems < ' a , T : ' a , H : ' a > {
631
+ iter : FilterMap <
632
+ ( & ' a HashSet < T , H > , & ' a T ) ,
633
+ & ' a T ,
634
+ Zip < Repeat < & ' a HashSet < T , H > > , SetItems < ' a , T > > ,
635
+ for <' b > fn ( ( & HashSet < T , H > , & ' b T ) ) -> Option < & ' b T > ,
636
+ >
637
+ }
638
+
639
+ /// Symmetric difference iterator.
640
+ pub struct SymDifferenceItems < ' a , T : ' a , H : ' a > {
641
+ iter : Chain < SetAlgebraItems < ' a , T , H > , SetAlgebraItems < ' a , T , H > >
642
+ }
643
+
644
+ /// Set union iterator.
645
+ pub struct UnionItems < ' a , T : ' a , H : ' a > {
646
+ iter : Chain < SetItems < ' a , T > , SetAlgebraItems < ' a , T , H > >
647
+ }
648
+
649
+ impl < ' a , K > Iterator < & ' a K > for SetItems < ' a , K > {
650
+ fn next ( & mut self ) -> Option < & ' a K > { self . iter . next ( ) }
651
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
652
+ }
653
+
654
+ impl < K > Iterator < K > for SetMoveItems < K > {
655
+ fn next ( & mut self ) -> Option < K > { self . iter . next ( ) }
656
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
657
+ }
658
+
659
+ impl < ' a , T , H > Iterator < & ' a T > for SetAlgebraItems < ' a , T , H > {
660
+ fn next ( & mut self ) -> Option < & ' a T > { self . iter . next ( ) }
661
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
662
+ }
663
+
664
+ impl < ' a , T , H > Iterator < & ' a T > for SymDifferenceItems < ' a , T , H > {
665
+ fn next ( & mut self ) -> Option < & ' a T > { self . iter . next ( ) }
666
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
667
+ }
668
+
669
+ impl < ' a , T , H > Iterator < & ' a T > for UnionItems < ' a , T , H > {
670
+ fn next ( & mut self ) -> Option < & ' a T > { self . iter . next ( ) }
671
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
672
+ }
634
673
635
674
#[ cfg( test) ]
636
675
mod test_set {
0 commit comments