@@ -2,11 +2,10 @@ use super::core::RingMapCore;
2
2
use super :: { Bucket , Entries , RingMap } ;
3
3
4
4
use alloc:: collections:: vec_deque:: { self , VecDeque } ;
5
- use core:: fmt;
6
5
use core:: hash:: { BuildHasher , Hash } ;
7
6
use core:: iter:: FusedIterator ;
8
7
use core:: ops:: { Index , RangeBounds } ;
9
- use core:: slice;
8
+ use core:: { fmt , mem , slice} ;
10
9
11
10
impl < ' a , K , V , S > IntoIterator for & ' a RingMap < K , V , S > {
12
11
type Item = ( & ' a K , & ' a V ) ;
@@ -64,7 +63,12 @@ impl<'a, K, V> Iterator for Buckets<'a, K, V> {
64
63
fn next ( & mut self ) -> Option < Self :: Item > {
65
64
match self . head . next ( ) {
66
65
next @ Some ( _) => next,
67
- None => self . tail . next ( ) ,
66
+ None => {
67
+ // Swap so the rest is found on the first branch next time.
68
+ // (Like `VecDeque` does in its own iterators.)
69
+ mem:: swap ( & mut self . head , & mut self . tail ) ;
70
+ self . head . next ( )
71
+ }
68
72
}
69
73
}
70
74
@@ -78,20 +82,26 @@ impl<'a, K, V> Iterator for Buckets<'a, K, V> {
78
82
}
79
83
80
84
fn nth ( & mut self , mut n : usize ) -> Option < Self :: Item > {
81
- if n < self . head . len ( ) {
82
- return self . head . nth ( n) ;
83
- }
84
- if self . head . len ( ) > 0 {
85
+ if n >= self . head . len ( ) {
85
86
n -= self . head . len ( ) ;
86
87
self . head = [ ] . iter ( ) ;
88
+ mem:: swap ( & mut self . head , & mut self . tail ) ;
87
89
}
88
- self . tail . nth ( n)
90
+ self . head . nth ( n)
89
91
}
90
92
91
93
fn last ( mut self ) -> Option < Self :: Item > {
92
94
self . next_back ( )
93
95
}
94
96
97
+ fn fold < Acc , F > ( self , mut acc : Acc , mut f : F ) -> Acc
98
+ where
99
+ F : FnMut ( Acc , Self :: Item ) -> Acc ,
100
+ {
101
+ acc = self . head . fold ( acc, & mut f) ;
102
+ self . tail . fold ( acc, & mut f)
103
+ }
104
+
95
105
fn collect < C > ( self ) -> C
96
106
where
97
107
C : FromIterator < Self :: Item > ,
@@ -104,19 +114,30 @@ impl<K, V> DoubleEndedIterator for Buckets<'_, K, V> {
104
114
fn next_back ( & mut self ) -> Option < Self :: Item > {
105
115
match self . tail . next_back ( ) {
106
116
next @ Some ( _) => next,
107
- None => self . head . next_back ( ) ,
117
+ None => {
118
+ // Swap so the rest is found on the first branch next time.
119
+ // (Like `VecDeque` does in its own iterators.)
120
+ mem:: swap ( & mut self . head , & mut self . tail ) ;
121
+ self . tail . next_back ( )
122
+ }
108
123
}
109
124
}
110
125
111
126
fn nth_back ( & mut self , mut n : usize ) -> Option < Self :: Item > {
112
- if n < self . tail . len ( ) {
113
- return self . tail . nth_back ( n) ;
114
- }
115
- if self . tail . len ( ) > 0 {
127
+ if n >= self . tail . len ( ) {
116
128
n -= self . tail . len ( ) ;
117
129
self . tail = [ ] . iter ( ) ;
130
+ mem:: swap ( & mut self . head , & mut self . tail ) ;
118
131
}
119
- self . head . nth_back ( n)
132
+ self . tail . nth_back ( n)
133
+ }
134
+
135
+ fn rfold < Acc , F > ( self , mut acc : Acc , mut f : F ) -> Acc
136
+ where
137
+ F : FnMut ( Acc , Self :: Item ) -> Acc ,
138
+ {
139
+ acc = self . tail . rfold ( acc, & mut f) ;
140
+ self . head . rfold ( acc, & mut f)
120
141
}
121
142
}
122
143
@@ -180,7 +201,12 @@ impl<'a, K, V> Iterator for BucketsMut<'a, K, V> {
180
201
fn next ( & mut self ) -> Option < Self :: Item > {
181
202
match self . head . next ( ) {
182
203
next @ Some ( _) => next,
183
- None => self . tail . next ( ) ,
204
+ None => {
205
+ // Swap so the rest is found on the first branch next time.
206
+ // (Like `VecDeque` does in its own iterators.)
207
+ mem:: swap ( & mut self . head , & mut self . tail ) ;
208
+ self . head . next ( )
209
+ }
184
210
}
185
211
}
186
212
@@ -194,20 +220,26 @@ impl<'a, K, V> Iterator for BucketsMut<'a, K, V> {
194
220
}
195
221
196
222
fn nth ( & mut self , mut n : usize ) -> Option < Self :: Item > {
197
- if n < self . head . len ( ) {
198
- return self . head . nth ( n) ;
199
- }
200
- if self . head . len ( ) > 0 {
223
+ if n >= self . head . len ( ) {
201
224
n -= self . head . len ( ) ;
202
225
self . head = [ ] . iter_mut ( ) ;
226
+ mem:: swap ( & mut self . head , & mut self . tail ) ;
203
227
}
204
- self . tail . nth ( n)
228
+ self . head . nth ( n)
205
229
}
206
230
207
231
fn last ( mut self ) -> Option < Self :: Item > {
208
232
self . next_back ( )
209
233
}
210
234
235
+ fn fold < Acc , F > ( self , acc : Acc , mut f : F ) -> Acc
236
+ where
237
+ F : FnMut ( Acc , Self :: Item ) -> Acc ,
238
+ {
239
+ let acc = self . head . fold ( acc, & mut f) ;
240
+ self . tail . fold ( acc, & mut f)
241
+ }
242
+
211
243
fn collect < C > ( self ) -> C
212
244
where
213
245
C : FromIterator < Self :: Item > ,
@@ -220,19 +252,30 @@ impl<K, V> DoubleEndedIterator for BucketsMut<'_, K, V> {
220
252
fn next_back ( & mut self ) -> Option < Self :: Item > {
221
253
match self . tail . next_back ( ) {
222
254
next @ Some ( _) => next,
223
- None => self . head . next_back ( ) ,
255
+ None => {
256
+ // Swap so the rest is found on the first branch next time.
257
+ // (Like `VecDeque` does in its own iterators.)
258
+ mem:: swap ( & mut self . head , & mut self . tail ) ;
259
+ self . tail . next_back ( )
260
+ }
224
261
}
225
262
}
226
263
227
264
fn nth_back ( & mut self , mut n : usize ) -> Option < Self :: Item > {
228
- if n < self . tail . len ( ) {
229
- return self . tail . nth_back ( n) ;
230
- }
231
- if self . tail . len ( ) > 0 {
265
+ if n >= self . tail . len ( ) {
232
266
n -= self . tail . len ( ) ;
233
267
self . tail = [ ] . iter_mut ( ) ;
268
+ mem:: swap ( & mut self . head , & mut self . tail ) ;
234
269
}
235
- self . head . nth_back ( n)
270
+ self . tail . nth_back ( n)
271
+ }
272
+
273
+ fn rfold < Acc , F > ( self , acc : Acc , mut f : F ) -> Acc
274
+ where
275
+ F : FnMut ( Acc , Self :: Item ) -> Acc ,
276
+ {
277
+ let acc = self . tail . rfold ( acc, & mut f) ;
278
+ self . head . rfold ( acc, & mut f)
236
279
}
237
280
}
238
281
0 commit comments