@@ -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
@@ -617,58 +611,101 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> Default for HashSet<T, H> {
617
611
}
618
612
619
613
/// HashSet iterator
620
- pub struct SetItems < ' a , K : ' a > {
614
+ pub struct Iter < ' a , K : ' a > {
621
615
iter : Keys < ' a , K , ( ) >
622
616
}
623
617
624
618
/// HashSet move iterator
625
- pub struct SetMoveItems < K > {
619
+ pub struct IntoIter < K > {
626
620
iter : Map < ( K , ( ) ) , K , MoveEntries < K , ( ) > , fn ( ( K , ( ) ) ) -> K >
627
621
}
628
622
629
- // `Repeat` is used to feed the filter closure an explicit capture
630
- // of a reference to the other set
631
- /// Set operations iterator, used directly for intersection and difference
632
- pub struct SetAlgebraItems < ' a , T : ' a , H : ' a > {
633
- iter : FilterMap <
634
- ( & ' a HashSet < T , H > , & ' a T ) ,
635
- & ' a T ,
636
- Zip < Repeat < & ' a HashSet < T , H > > , SetItems < ' a , T > > ,
637
- for <' b > fn ( ( & HashSet < T , H > , & ' b T ) ) -> Option < & ' b T > ,
638
- >
623
+ /// Intersection iterator
624
+ pub struct Intersection < ' a , T : ' a , H : ' a > {
625
+ // iterator of the first set
626
+ iter : Iter < ' a , T > ,
627
+ // the second set
628
+ other : & ' a HashSet < T , H > ,
629
+ }
630
+
631
+ /// Difference iterator
632
+ pub struct Difference < ' a , T : ' a , H : ' a > {
633
+ // iterator of the first set
634
+ iter : Iter < ' a , T > ,
635
+ // the second set
636
+ other : & ' a HashSet < T , H > ,
639
637
}
640
638
641
639
/// Symmetric difference iterator.
642
- pub struct SymDifferenceItems < ' a , T : ' a , H : ' a > {
643
- iter : Chain < SetAlgebraItems < ' a , T , H > , SetAlgebraItems < ' a , T , H > >
640
+ pub struct SymmetricDifference < ' a , T : ' a , H : ' a > {
641
+ iter : Chain < Difference < ' a , T , H > , Difference < ' a , T , H > >
644
642
}
645
643
646
644
/// Set union iterator.
647
- pub struct UnionItems < ' a , T : ' a , H : ' a > {
648
- iter : Chain < SetItems < ' a , T > , SetAlgebraItems < ' a , T , H > >
645
+ pub struct Union < ' a , T : ' a , H : ' a > {
646
+ iter : Chain < Iter < ' a , T > , Difference < ' a , T , H > >
649
647
}
650
648
651
- impl < ' a , K > Iterator < & ' a K > for SetItems < ' a , K > {
649
+ impl < ' a , K > Iterator < & ' a K > for Iter < ' a , K > {
652
650
fn next ( & mut self ) -> Option < & ' a K > { self . iter . next ( ) }
653
651
fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
654
652
}
655
653
656
- impl < K > Iterator < K > for SetMoveItems < K > {
654
+ impl < K > Iterator < K > for IntoIter < K > {
657
655
fn next ( & mut self ) -> Option < K > { self . iter . next ( ) }
658
656
fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
659
657
}
660
658
661
- impl < ' a , T , H > Iterator < & ' a T > for SetAlgebraItems < ' a , T , H > {
662
- fn next ( & mut self ) -> Option < & ' a T > { self . iter . next ( ) }
663
- fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
659
+ impl < ' a , T , S , H > Iterator < & ' a T > for Intersection < ' a , T , H >
660
+ where T : Eq + Hash < S > , H : Hasher < S >
661
+ {
662
+ fn next ( & mut self ) -> Option < & ' a T > {
663
+ loop {
664
+ match self . iter . next ( ) {
665
+ None => return None ,
666
+ Some ( elt) => if self . other . contains ( elt) {
667
+ return Some ( elt)
668
+ } ,
669
+ }
670
+ }
671
+ }
672
+
673
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
674
+ let ( _, upper) = self . iter . size_hint ( ) ;
675
+ ( 0 , upper)
676
+ }
677
+ }
678
+
679
+ impl < ' a , T , S , H > Iterator < & ' a T > for Difference < ' a , T , H >
680
+ where T : Eq + Hash < S > , H : Hasher < S >
681
+ {
682
+ fn next ( & mut self ) -> Option < & ' a T > {
683
+ loop {
684
+ match self . iter . next ( ) {
685
+ None => return None ,
686
+ Some ( elt) => if !self . other . contains ( elt) {
687
+ return Some ( elt)
688
+ } ,
689
+ }
690
+ }
691
+ }
692
+
693
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) {
694
+ let ( _, upper) = self . iter . size_hint ( ) ;
695
+ ( 0 , upper)
696
+ }
664
697
}
665
698
666
- impl < ' a , T , H > Iterator < & ' a T > for SymDifferenceItems < ' a , T , H > {
699
+ impl < ' a , T , S , H > Iterator < & ' a T > for SymmetricDifference < ' a , T , H >
700
+ where T : Eq + Hash < S > , H : Hasher < S >
701
+ {
667
702
fn next ( & mut self ) -> Option < & ' a T > { self . iter . next ( ) }
668
703
fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
669
704
}
670
705
671
- impl < ' a , T , H > Iterator < & ' a T > for UnionItems < ' a , T , H > {
706
+ impl < ' a , T , S , H > Iterator < & ' a T > for Union < ' a , T , H >
707
+ where T : Eq + Hash < S > , H : Hasher < S >
708
+ {
672
709
fn next ( & mut self ) -> Option < & ' a T > { self . iter . next ( ) }
673
710
fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
674
711
}
0 commit comments