@@ -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,14 +82,12 @@ 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 > {
@@ -112,19 +114,22 @@ impl<K, V> DoubleEndedIterator for Buckets<'_, K, V> {
112
114
fn next_back ( & mut self ) -> Option < Self :: Item > {
113
115
match self . tail . next_back ( ) {
114
116
next @ Some ( _) => next,
115
- 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
+ }
116
123
}
117
124
}
118
125
119
126
fn nth_back ( & mut self , mut n : usize ) -> Option < Self :: Item > {
120
- if n < self . tail . len ( ) {
121
- return self . tail . nth_back ( n) ;
122
- }
123
- if self . tail . len ( ) > 0 {
127
+ if n >= self . tail . len ( ) {
124
128
n -= self . tail . len ( ) ;
125
129
self . tail = [ ] . iter ( ) ;
130
+ mem:: swap ( & mut self . head , & mut self . tail ) ;
126
131
}
127
- self . head . nth_back ( n)
132
+ self . tail . nth_back ( n)
128
133
}
129
134
130
135
fn rfold < Acc , F > ( self , mut acc : Acc , mut f : F ) -> Acc
@@ -196,7 +201,12 @@ impl<'a, K, V> Iterator for BucketsMut<'a, K, V> {
196
201
fn next ( & mut self ) -> Option < Self :: Item > {
197
202
match self . head . next ( ) {
198
203
next @ Some ( _) => next,
199
- 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
+ }
200
210
}
201
211
}
202
212
@@ -210,14 +220,12 @@ impl<'a, K, V> Iterator for BucketsMut<'a, K, V> {
210
220
}
211
221
212
222
fn nth ( & mut self , mut n : usize ) -> Option < Self :: Item > {
213
- if n < self . head . len ( ) {
214
- return self . head . nth ( n) ;
215
- }
216
- if self . head . len ( ) > 0 {
223
+ if n >= self . head . len ( ) {
217
224
n -= self . head . len ( ) ;
218
225
self . head = [ ] . iter_mut ( ) ;
226
+ mem:: swap ( & mut self . head , & mut self . tail ) ;
219
227
}
220
- self . tail . nth ( n)
228
+ self . head . nth ( n)
221
229
}
222
230
223
231
fn last ( mut self ) -> Option < Self :: Item > {
@@ -244,19 +252,22 @@ impl<K, V> DoubleEndedIterator for BucketsMut<'_, K, V> {
244
252
fn next_back ( & mut self ) -> Option < Self :: Item > {
245
253
match self . tail . next_back ( ) {
246
254
next @ Some ( _) => next,
247
- 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
+ }
248
261
}
249
262
}
250
263
251
264
fn nth_back ( & mut self , mut n : usize ) -> Option < Self :: Item > {
252
- if n < self . tail . len ( ) {
253
- return self . tail . nth_back ( n) ;
254
- }
255
- if self . tail . len ( ) > 0 {
265
+ if n >= self . tail . len ( ) {
256
266
n -= self . tail . len ( ) ;
257
267
self . tail = [ ] . iter_mut ( ) ;
268
+ mem:: swap ( & mut self . head , & mut self . tail ) ;
258
269
}
259
- self . head . nth_back ( n)
270
+ self . tail . nth_back ( n)
260
271
}
261
272
262
273
fn rfold < Acc , F > ( self , acc : Acc , mut f : F ) -> Acc
0 commit comments