@@ -18,10 +18,28 @@ pub struct Iter<K: Clone, V: Borrow<usize>, InnerIter: Iterator<Item = (K, V)>>
1818 pub ( crate ) iter : InnerIter ,
1919 pub ( crate ) duplicate : Option < <InnerIter as Iterator >:: Item > ,
2020 pub ( crate ) duplicate_index : usize ,
21+ pub ( crate ) duplicate_back : Option < <InnerIter as Iterator >:: Item > ,
22+ pub ( crate ) duplicate_index_back : usize ,
2123 pub ( crate ) len : usize ,
2224 pub ( crate ) _ghost : PhantomData < * const ( K , V ) > ,
2325}
2426
27+ impl < K : Clone , V : Borrow < usize > , InnerIter : Iterator < Item = ( K , V ) > + ExactSizeIterator >
28+ Iter < K , V , InnerIter >
29+ {
30+ pub ( crate ) fn new ( iter : InnerIter , len : usize ) -> Self {
31+ Iter {
32+ iter,
33+ duplicate : None ,
34+ duplicate_index : 0 ,
35+ duplicate_back : None ,
36+ duplicate_index_back : 0 ,
37+ len,
38+ _ghost : PhantomData ,
39+ }
40+ }
41+ }
42+
2543impl < K : Clone , V : Borrow < usize > , InnerIter : Iterator < Item = ( K , V ) > + Clone > Clone
2644 for Iter < K , V , InnerIter >
2745where
3250 iter : self . iter . clone ( ) ,
3351 duplicate : self . duplicate . clone ( ) ,
3452 duplicate_index : self . duplicate_index ,
53+ duplicate_back : self . duplicate_back . clone ( ) ,
54+ duplicate_index_back : self . duplicate_index_back ,
3555 len : self . len ,
3656 _ghost : PhantomData ,
3757 }
@@ -50,14 +70,24 @@ impl<K: Clone, V: Borrow<usize>, InnerIter: Iterator<Item = (K, V)>> Iterator
5070 if let Some ( ( key, count) ) = self . duplicate . as_ref ( ) {
5171 self . duplicate_index += 1 ;
5272 let key = key. clone ( ) ;
53- if & self . duplicate_index >= count. borrow ( ) {
73+ if self . duplicate_index >= * count. borrow ( ) {
5474 self . duplicate = None ;
5575 self . duplicate_index = 0 ;
5676 }
5777 self . len -= 1 ;
5878 Some ( key)
5979 } else {
60- None
80+ if let Some ( ( key, count) ) = self . duplicate_back . as_ref ( ) {
81+ self . duplicate_index_back += 1 ;
82+ let key = key. clone ( ) ;
83+ if self . duplicate_index_back >= * count. borrow ( ) {
84+ self . duplicate_back = None ;
85+ }
86+ self . len -= 1 ;
87+ Some ( key)
88+ } else {
89+ None
90+ }
6191 }
6292 }
6393
@@ -87,3 +117,35 @@ impl<K: Clone, V: Borrow<usize>, InnerIter: Iterator<Item = (K, V)>> ExactSizeIt
87117 self . len
88118 }
89119}
120+
121+ impl < K : Clone , V : Borrow < usize > , InnerIter : Iterator < Item = ( K , V ) > + DoubleEndedIterator >
122+ DoubleEndedIterator for Iter < K , V , InnerIter >
123+ {
124+ fn next_back ( & mut self ) -> Option < Self :: Item > {
125+ if self . duplicate_back . is_none ( ) {
126+ self . duplicate_back = self . iter . next_back ( ) ;
127+ }
128+ if let Some ( ( key, count) ) = self . duplicate_back . as_ref ( ) {
129+ self . duplicate_index_back += 1 ;
130+ let key = key. clone ( ) ;
131+ if self . duplicate_index_back >= * count. borrow ( ) {
132+ self . duplicate_back = None ;
133+ self . duplicate_index_back = 0 ;
134+ }
135+ self . len -= 1 ;
136+ Some ( key)
137+ } else {
138+ if let Some ( ( key, count) ) = self . duplicate . as_ref ( ) {
139+ self . duplicate_index += 1 ;
140+ let key = key. clone ( ) ;
141+ if self . duplicate_index >= * count. borrow ( ) {
142+ self . duplicate = None ;
143+ }
144+ self . len -= 1 ;
145+ Some ( key)
146+ } else {
147+ None
148+ }
149+ }
150+ }
151+ }
0 commit comments