@@ -17,7 +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 , Map , FilterMap , Chain , Repeat , Zip , Extend , repeat } ;
20
+ use iter:: { Iterator , IteratorExt , FromIterator , Map , Chain , Extend } ;
21
21
use option:: Option :: { Some , None , mod} ;
22
22
use result:: Result :: { Ok , Err } ;
23
23
@@ -250,8 +250,8 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
250
250
/// }
251
251
/// ```
252
252
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
253
- pub fn iter < ' a > ( & ' a self ) -> SetItems < ' a , T > {
254
- SetItems { iter : self . map . keys ( ) }
253
+ pub fn iter < ' a > ( & ' a self ) -> Iter < ' a , T > {
254
+ Iter { iter : self . map . keys ( ) }
255
255
}
256
256
257
257
/// Creates a consuming iterator, that is, one that moves each value out
@@ -275,10 +275,10 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
275
275
/// }
276
276
/// ```
277
277
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
278
- pub fn into_iter ( self ) -> SetMoveItems < T > {
278
+ pub fn into_iter ( self ) -> IntoIter < T > {
279
279
fn first < A , B > ( ( a, _) : ( A , B ) ) -> A { a }
280
280
281
- SetMoveItems { iter : self . map . into_iter ( ) . map ( first) }
281
+ IntoIter { iter : self . map . into_iter ( ) . map ( first) }
282
282
}
283
283
284
284
/// Visit the values representing the difference.
@@ -304,14 +304,11 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
304
304
/// assert_eq!(diff, [4i].iter().map(|&x| x).collect());
305
305
/// ```
306
306
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
307
- pub fn difference < ' a > ( & ' a self , other : & ' a HashSet < T , H > ) -> SetAlgebraItems < ' a , T , H > {
308
- fn filter < ' a , T , S , H > ( ( other, elt) : ( & HashSet < T , H > , & ' a T ) ) -> Option < & ' a T > where
309
- T : Eq + Hash < S > , H : Hasher < S >
310
- {
311
- if !other. contains ( elt) { Some ( elt) } else { None }
307
+ pub fn difference < ' a > ( & ' a self , other : & ' a HashSet < T , H > ) -> Difference < ' a , T , H > {
308
+ Difference {
309
+ iter : self . iter ( ) ,
310
+ other : other,
312
311
}
313
-
314
- SetAlgebraItems { iter : repeat ( other) . zip ( self . iter ( ) ) . filter_map ( filter) }
315
312
}
316
313
317
314
/// Visit the values representing the symmetric difference.
@@ -336,8 +333,8 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
336
333
/// ```
337
334
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
338
335
pub fn symmetric_difference < ' a > ( & ' a self , other : & ' a HashSet < T , H > )
339
- -> SymDifferenceItems < ' a , T , H > {
340
- SymDifferenceItems { iter : self . difference ( other) . chain ( other. difference ( self ) ) }
336
+ -> SymmetricDifference < ' a , T , H > {
337
+ SymmetricDifference { iter : self . difference ( other) . chain ( other. difference ( self ) ) }
341
338
}
342
339
343
340
/// Visit the values representing the intersection.
@@ -358,14 +355,11 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
358
355
/// assert_eq!(diff, [2i, 3].iter().map(|&x| x).collect());
359
356
/// ```
360
357
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
361
- pub fn intersection < ' a > ( & ' a self , other : & ' a HashSet < T , H > ) -> SetAlgebraItems < ' a , T , H > {
362
- fn filter < ' a , T , S , H > ( ( other, elt) : ( & HashSet < T , H > , & ' a T ) ) -> Option < & ' a T > where
363
- T : Eq + Hash < S > , H : Hasher < S >
364
- {
365
- if other. contains ( elt) { Some ( elt) } else { None }
358
+ pub fn intersection < ' a > ( & ' a self , other : & ' a HashSet < T , H > ) -> Intersection < ' a , T , H > {
359
+ Intersection {
360
+ iter : self . iter ( ) ,
361
+ other : other,
366
362
}
367
-
368
- SetAlgebraItems { iter : repeat ( other) . zip ( self . iter ( ) ) . filter_map ( filter) }
369
363
}
370
364
371
365
/// Visit the values representing the union.
@@ -386,8 +380,8 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
386
380
/// assert_eq!(diff, [1i, 2, 3, 4].iter().map(|&x| x).collect());
387
381
/// ```
388
382
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
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 ) ) }
383
+ pub fn union < ' a > ( & ' a self , other : & ' a HashSet < T , H > ) -> Union < ' a , T , H > {
384
+ Union { iter : self . iter ( ) . chain ( other. difference ( self ) ) }
391
385
}
392
386
393
387
/// Return the number of elements in the set
@@ -625,12 +619,12 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> Default for HashSet<T, H> {
625
619
}
626
620
627
621
/// HashSet iterator
628
- pub struct SetItems < ' a , K : ' a > {
622
+ pub struct Iter < ' a , K : ' a > {
629
623
iter : Keys < ' a , K , ( ) >
630
624
}
631
625
632
626
/// HashSet move iterator
633
- pub struct SetMoveItems < K > {
627
+ pub struct IntoIter < K > {
634
628
iter : Map < ( K , ( ) ) , K , MoveEntries < K , ( ) > , fn ( ( K , ( ) ) ) -> K >
635
629
}
636
630
@@ -639,34 +633,38 @@ pub struct Drain<'a, K: 'a> {
639
633
iter : Map < ( K , ( ) ) , K , map:: Drain < ' a , K , ( ) > , fn ( ( K , ( ) ) ) -> K > ,
640
634
}
641
635
642
- // `Repeat` is used to feed the filter closure an explicit capture
643
- // of a reference to the other set
644
- /// Set operations iterator, used directly for intersection and difference
645
- pub struct SetAlgebraItems < ' a , T : ' a , H : ' a > {
646
- iter : FilterMap <
647
- ( & ' a HashSet < T , H > , & ' a T ) ,
648
- & ' a T ,
649
- Zip < Repeat < & ' a HashSet < T , H > > , SetItems < ' a , T > > ,
650
- for <' b > fn ( ( & HashSet < T , H > , & ' b T ) ) -> Option < & ' b T > ,
651
- >
636
+ /// Intersection iterator
637
+ pub struct Intersection < ' a , T : ' a , H : ' a > {
638
+ // iterator of the first set
639
+ iter : Iter < ' a , T > ,
640
+ // the second set
641
+ other : & ' a HashSet < T , H > ,
642
+ }
643
+
644
+ /// Difference iterator
645
+ pub struct Difference < ' a , T : ' a , H : ' a > {
646
+ // iterator of the first set
647
+ iter : Iter < ' a , T > ,
648
+ // the second set
649
+ other : & ' a HashSet < T , H > ,
652
650
}
653
651
654
652
/// Symmetric difference iterator.
655
- pub struct SymDifferenceItems < ' a , T : ' a , H : ' a > {
656
- iter : Chain < SetAlgebraItems < ' a , T , H > , SetAlgebraItems < ' a , T , H > >
653
+ pub struct SymmetricDifference < ' a , T : ' a , H : ' a > {
654
+ iter : Chain < Difference < ' a , T , H > , Difference < ' a , T , H > >
657
655
}
658
656
659
657
/// Set union iterator.
660
- pub struct UnionItems < ' a , T : ' a , H : ' a > {
661
- iter : Chain < SetItems < ' a , T > , SetAlgebraItems < ' a , T , H > >
658
+ pub struct Union < ' a , T : ' a , H : ' a > {
659
+ iter : Chain < Iter < ' a , T > , Difference < ' a , T , H > >
662
660
}
663
661
664
- impl < ' a , K > Iterator < & ' a K > for SetItems < ' a , K > {
662
+ impl < ' a , K > Iterator < & ' a K > for Iter < ' a , K > {
665
663
fn next ( & mut self ) -> Option < & ' a K > { self . iter . next ( ) }
666
664
fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
667
665
}
668
666
669
- impl < K > Iterator < K > for SetMoveItems < K > {
667
+ impl < K > Iterator < K > for IntoIter < K > {
670
668
fn next ( & mut self ) -> Option < K > { self . iter . next ( ) }
671
669
fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
672
670
}
@@ -676,17 +674,56 @@ impl<'a, K: 'a> Iterator<K> for Drain<'a, K> {
676
674
fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
677
675
}
678
676
679
- impl < ' a , T , H > Iterator < & ' a T > for SetAlgebraItems < ' a , T , H > {
680
- fn next ( & mut self ) -> Option < & ' a T > { self . iter . next ( ) }
681
- fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
677
+ impl < ' a , T , S , H > Iterator < & ' a T > for Intersection < ' a , T , H >
678
+ where T : Eq + Hash < S > , H : Hasher < S >
679
+ {
680
+ fn next ( & mut self ) -> Option < & ' a T > {
681
+ loop {
682
+ match self . iter . next ( ) {
683
+ None => return None ,
684
+ Some ( elt) => if self . other . contains ( elt) {
685
+ return Some ( elt)
686
+ } ,
687
+ }
688
+ }
689
+ }
690
+
691
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
692
+ let ( _, upper) = self . iter . size_hint ( ) ;
693
+ ( 0 , upper)
694
+ }
695
+ }
696
+
697
+ impl < ' a , T , S , H > Iterator < & ' a T > for Difference < ' a , T , H >
698
+ where T : Eq + Hash < S > , H : Hasher < S >
699
+ {
700
+ fn next ( & mut self ) -> Option < & ' a T > {
701
+ loop {
702
+ match self . iter . next ( ) {
703
+ None => return None ,
704
+ Some ( elt) => if !self . other . contains ( elt) {
705
+ return Some ( elt)
706
+ } ,
707
+ }
708
+ }
709
+ }
710
+
711
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
712
+ let ( _, upper) = self . iter . size_hint ( ) ;
713
+ ( 0 , upper)
714
+ }
682
715
}
683
716
684
- impl < ' a , T , H > Iterator < & ' a T > for SymDifferenceItems < ' a , T , H > {
717
+ impl < ' a , T , S , H > Iterator < & ' a T > for SymmetricDifference < ' a , T , H >
718
+ where T : Eq + Hash < S > , H : Hasher < S >
719
+ {
685
720
fn next ( & mut self ) -> Option < & ' a T > { self . iter . next ( ) }
686
721
fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
687
722
}
688
723
689
- impl < ' a , T , H > Iterator < & ' a T > for UnionItems < ' a , T , H > {
724
+ impl < ' a , T , S , H > Iterator < & ' a T > for Union < ' a , T , H >
725
+ where T : Eq + Hash < S > , H : Hasher < S >
726
+ {
690
727
fn next ( & mut self ) -> Option < & ' a T > { self . iter . next ( ) }
691
728
fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
692
729
}
0 commit comments