@@ -10,7 +10,7 @@ pub use self::multi_product::*;
10
10
11
11
use std:: fmt;
12
12
use std:: mem:: replace;
13
- use std:: iter:: { Fuse , Peekable , FromIterator } ;
13
+ use std:: iter:: { Fuse , Peekable , FromIterator , FusedIterator } ;
14
14
use std:: marker:: PhantomData ;
15
15
use crate :: size_hint;
16
16
@@ -310,13 +310,13 @@ pub fn cartesian_product<I, J>(mut i: I, j: J) -> Product<I, J>
310
310
}
311
311
}
312
312
313
-
314
313
impl < I , J > Iterator for Product < I , J >
315
314
where I : Iterator ,
316
315
J : Clone + Iterator ,
317
316
I :: Item : Clone
318
317
{
319
318
type Item = ( I :: Item , J :: Item ) ;
319
+
320
320
fn next ( & mut self ) -> Option < ( I :: Item , J :: Item ) > {
321
321
let elt_b = match self . b . next ( ) {
322
322
None => {
@@ -607,18 +607,18 @@ impl<I, J, F> Iterator for MergeBy<I, J, F>
607
607
}
608
608
609
609
#[ derive( Clone , Debug ) ]
610
- pub struct CoalesceCore < I >
610
+ pub struct CoalesceCore < I , T >
611
611
where I : Iterator
612
612
{
613
613
iter : I ,
614
- last : Option < I :: Item > ,
614
+ last : Option < T > ,
615
615
}
616
616
617
- impl < I > CoalesceCore < I >
617
+ impl < I , T > CoalesceCore < I , T >
618
618
where I : Iterator
619
619
{
620
- fn next_with < F > ( & mut self , mut f : F ) -> Option < I :: Item >
621
- where F : FnMut ( I :: Item , I :: Item ) -> Result < I :: Item , ( I :: Item , I :: Item ) >
620
+ fn next_with < F > ( & mut self , mut f : F ) -> Option < T >
621
+ where F : FnMut ( T , I :: Item ) -> Result < T , ( T , T ) >
622
622
{
623
623
// this fuses the iterator
624
624
let mut last = match self . last . take ( ) {
@@ -652,7 +652,7 @@ impl<I> CoalesceCore<I>
652
652
pub struct Coalesce < I , F >
653
653
where I : Iterator
654
654
{
655
- iter : CoalesceCore < I > ,
655
+ iter : CoalesceCore < I , I :: Item > ,
656
656
f : F ,
657
657
}
658
658
@@ -705,7 +705,7 @@ impl<I, F> Iterator for Coalesce<I, F>
705
705
pub struct DedupBy < I , Pred >
706
706
where I : Iterator
707
707
{
708
- iter : CoalesceCore < I > ,
708
+ iter : CoalesceCore < I , I :: Item > ,
709
709
dedup_pred : Pred ,
710
710
}
711
711
@@ -718,7 +718,7 @@ pub struct DedupEq;
718
718
719
719
impl < T : PartialEq > DedupPredicate < T > for DedupEq {
720
720
fn dedup_pair ( & mut self , a : & T , b : & T ) -> bool {
721
- a== b
721
+ a == b
722
722
}
723
723
}
724
724
@@ -803,6 +803,79 @@ impl<I, Pred> Iterator for DedupBy<I, Pred>
803
803
}
804
804
}
805
805
806
+ /// An iterator adaptor that removes repeated duplicates, while keeping a count of how many
807
+ /// repeated elements were present. This will determine equality using a comparison function.
808
+ ///
809
+ /// See [`.dedup_by_with_count()`](../trait.Itertools.html#method.dedup_by_with_count) or
810
+ /// [`.dedup_with_count()`](../trait.Itertools.html#method.dedup_with_count) for more information.
811
+ #[ must_use = "iterator adaptors are lazy and do nothing unless consumed" ]
812
+ pub struct DedupByWithCount < I , Pred >
813
+ where I : Iterator
814
+ {
815
+ iter : CoalesceCore < I , ( usize , I :: Item ) > ,
816
+ dedup_pred : Pred ,
817
+ }
818
+
819
+ /// An iterator adaptor that removes repeated duplicates, while keeping a count of how many
820
+ /// repeated elements were present.
821
+ ///
822
+ /// See [`.dedup_with_count()`](../trait.Itertools.html#method.dedup_with_count) for more information.
823
+ pub type DedupWithCount < I > = DedupByWithCount < I , DedupEq > ;
824
+
825
+ /// Create a new `DedupByWithCount`.
826
+ pub fn dedup_by_with_count < I , Pred > ( mut iter : I , dedup_pred : Pred ) -> DedupByWithCount < I , Pred >
827
+ where I : Iterator ,
828
+ {
829
+ DedupByWithCount {
830
+ iter : CoalesceCore {
831
+ last : iter. next ( ) . map ( |v| ( 1 , v) ) ,
832
+ iter,
833
+ } ,
834
+ dedup_pred,
835
+ }
836
+ }
837
+
838
+ /// Create a new `DedupWithCount`.
839
+ pub fn dedup_with_count < I > ( iter : I ) -> DedupWithCount < I >
840
+ where I : Iterator
841
+ {
842
+ dedup_by_with_count ( iter, DedupEq )
843
+ }
844
+
845
+ impl < I , Pred > fmt:: Debug for DedupByWithCount < I , Pred >
846
+ where I : Iterator + fmt:: Debug ,
847
+ I :: Item : fmt:: Debug ,
848
+ {
849
+ debug_fmt_fields ! ( Dedup , iter) ;
850
+ }
851
+
852
+ impl < I : Clone , Pred : Clone > Clone for DedupByWithCount < I , Pred >
853
+ where I : Iterator ,
854
+ I :: Item : Clone ,
855
+ {
856
+ clone_fields ! ( iter, dedup_pred) ;
857
+ }
858
+
859
+ impl < I , Pred > Iterator for DedupByWithCount < I , Pred >
860
+ where I : Iterator ,
861
+ Pred : DedupPredicate < I :: Item > ,
862
+ {
863
+ type Item = ( usize , I :: Item ) ;
864
+
865
+ fn next ( & mut self ) -> Option < ( usize , I :: Item ) > {
866
+ let ref mut dedup_pred = self . dedup_pred ;
867
+ self . iter . next_with ( |( c, x) , y| {
868
+ if dedup_pred. dedup_pair ( & x, & y) { Ok ( ( c + 1 , x) ) } else { Err ( ( ( c, x) , ( 1 , y) ) ) }
869
+ } )
870
+ }
871
+
872
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
873
+ self . iter . size_hint ( )
874
+ }
875
+ }
876
+
877
+ impl < I : Iterator , Pred : DedupPredicate < I :: Item > > FusedIterator for DedupByWithCount < I , Pred > { }
878
+
806
879
/// An iterator adaptor that borrows from a `Clone`-able iterator
807
880
/// to only pick off elements while the predicate returns `true`.
808
881
///
0 commit comments