1 | use super::core::IndexMapCore; |
2 | use super::{Bucket, Entries, IndexMap, Slice}; |
3 | |
4 | use alloc::vec::{self, Vec}; |
5 | use core::fmt; |
6 | use core::hash::{BuildHasher, Hash}; |
7 | use core::iter::FusedIterator; |
8 | use core::ops::{Index, RangeBounds}; |
9 | use core::slice; |
10 | |
11 | impl<'a, K, V, S> IntoIterator for &'a IndexMap<K, V, S> { |
12 | type Item = (&'a K, &'a V); |
13 | type IntoIter = Iter<'a, K, V>; |
14 | |
15 | fn into_iter(self) -> Self::IntoIter { |
16 | self.iter() |
17 | } |
18 | } |
19 | |
20 | impl<'a, K, V, S> IntoIterator for &'a mut IndexMap<K, V, S> { |
21 | type Item = (&'a K, &'a mut V); |
22 | type IntoIter = IterMut<'a, K, V>; |
23 | |
24 | fn into_iter(self) -> Self::IntoIter { |
25 | self.iter_mut() |
26 | } |
27 | } |
28 | |
29 | impl<K, V, S> IntoIterator for IndexMap<K, V, S> { |
30 | type Item = (K, V); |
31 | type IntoIter = IntoIter<K, V>; |
32 | |
33 | fn into_iter(self) -> Self::IntoIter { |
34 | IntoIter::new(self.into_entries()) |
35 | } |
36 | } |
37 | |
38 | /// An iterator over the entries of an [`IndexMap`]. |
39 | /// |
40 | /// This `struct` is created by the [`IndexMap::iter`] method. |
41 | /// See its documentation for more. |
42 | pub struct Iter<'a, K, V> { |
43 | iter: slice::Iter<'a, Bucket<K, V>>, |
44 | } |
45 | |
46 | impl<'a, K, V> Iter<'a, K, V> { |
47 | pub(super) fn new(entries: &'a [Bucket<K, V>]) -> Self { |
48 | Self { |
49 | iter: entries.iter(), |
50 | } |
51 | } |
52 | |
53 | /// Returns a slice of the remaining entries in the iterator. |
54 | pub fn as_slice(&self) -> &'a Slice<K, V> { |
55 | Slice::from_slice(self.iter.as_slice()) |
56 | } |
57 | } |
58 | |
59 | impl<'a, K, V> Iterator for Iter<'a, K, V> { |
60 | type Item = (&'a K, &'a V); |
61 | |
62 | iterator_methods!(Bucket::refs); |
63 | } |
64 | |
65 | impl<K, V> DoubleEndedIterator for Iter<'_, K, V> { |
66 | double_ended_iterator_methods!(Bucket::refs); |
67 | } |
68 | |
69 | impl<K, V> ExactSizeIterator for Iter<'_, K, V> { |
70 | fn len(&self) -> usize { |
71 | self.iter.len() |
72 | } |
73 | } |
74 | |
75 | impl<K, V> FusedIterator for Iter<'_, K, V> {} |
76 | |
77 | // FIXME(#26925) Remove in favor of `#[derive(Clone)]` |
78 | impl<K, V> Clone for Iter<'_, K, V> { |
79 | fn clone(&self) -> Self { |
80 | Iter { |
81 | iter: self.iter.clone(), |
82 | } |
83 | } |
84 | } |
85 | |
86 | impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Iter<'_, K, V> { |
87 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
88 | f.debug_list().entries(self.clone()).finish() |
89 | } |
90 | } |
91 | |
92 | impl<K, V> Default for Iter<'_, K, V> { |
93 | fn default() -> Self { |
94 | Self { iter: [].iter() } |
95 | } |
96 | } |
97 | |
98 | /// A mutable iterator over the entries of an [`IndexMap`]. |
99 | /// |
100 | /// This `struct` is created by the [`IndexMap::iter_mut`] method. |
101 | /// See its documentation for more. |
102 | pub struct IterMut<'a, K, V> { |
103 | iter: slice::IterMut<'a, Bucket<K, V>>, |
104 | } |
105 | |
106 | impl<'a, K, V> IterMut<'a, K, V> { |
107 | pub(super) fn new(entries: &'a mut [Bucket<K, V>]) -> Self { |
108 | Self { |
109 | iter: entries.iter_mut(), |
110 | } |
111 | } |
112 | |
113 | /// Returns a slice of the remaining entries in the iterator. |
114 | pub fn as_slice(&self) -> &Slice<K, V> { |
115 | Slice::from_slice(self.iter.as_slice()) |
116 | } |
117 | |
118 | /// Returns a mutable slice of the remaining entries in the iterator. |
119 | /// |
120 | /// To avoid creating `&mut` references that alias, this is forced to consume the iterator. |
121 | pub fn into_slice(self) -> &'a mut Slice<K, V> { |
122 | Slice::from_mut_slice(self.iter.into_slice()) |
123 | } |
124 | } |
125 | |
126 | impl<'a, K, V> Iterator for IterMut<'a, K, V> { |
127 | type Item = (&'a K, &'a mut V); |
128 | |
129 | iterator_methods!(Bucket::ref_mut); |
130 | } |
131 | |
132 | impl<K, V> DoubleEndedIterator for IterMut<'_, K, V> { |
133 | double_ended_iterator_methods!(Bucket::ref_mut); |
134 | } |
135 | |
136 | impl<K, V> ExactSizeIterator for IterMut<'_, K, V> { |
137 | fn len(&self) -> usize { |
138 | self.iter.len() |
139 | } |
140 | } |
141 | |
142 | impl<K, V> FusedIterator for IterMut<'_, K, V> {} |
143 | |
144 | impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> { |
145 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
146 | let iter: impl Iterator = self.iter.as_slice().iter().map(Bucket::refs); |
147 | f.debug_list().entries(iter).finish() |
148 | } |
149 | } |
150 | |
151 | impl<K, V> Default for IterMut<'_, K, V> { |
152 | fn default() -> Self { |
153 | Self { |
154 | iter: [].iter_mut(), |
155 | } |
156 | } |
157 | } |
158 | |
159 | /// A mutable iterator over the entries of an [`IndexMap`]. |
160 | /// |
161 | /// This `struct` is created by the [`MutableKeys::iter_mut2`][super::MutableKeys::iter_mut2] method. |
162 | /// See its documentation for more. |
163 | pub struct IterMut2<'a, K, V> { |
164 | iter: slice::IterMut<'a, Bucket<K, V>>, |
165 | } |
166 | |
167 | impl<'a, K, V> IterMut2<'a, K, V> { |
168 | pub(super) fn new(entries: &'a mut [Bucket<K, V>]) -> Self { |
169 | Self { |
170 | iter: entries.iter_mut(), |
171 | } |
172 | } |
173 | |
174 | /// Returns a slice of the remaining entries in the iterator. |
175 | pub fn as_slice(&self) -> &Slice<K, V> { |
176 | Slice::from_slice(self.iter.as_slice()) |
177 | } |
178 | |
179 | /// Returns a mutable slice of the remaining entries in the iterator. |
180 | /// |
181 | /// To avoid creating `&mut` references that alias, this is forced to consume the iterator. |
182 | pub fn into_slice(self) -> &'a mut Slice<K, V> { |
183 | Slice::from_mut_slice(self.iter.into_slice()) |
184 | } |
185 | } |
186 | |
187 | impl<'a, K, V> Iterator for IterMut2<'a, K, V> { |
188 | type Item = (&'a mut K, &'a mut V); |
189 | |
190 | iterator_methods!(Bucket::muts); |
191 | } |
192 | |
193 | impl<K, V> DoubleEndedIterator for IterMut2<'_, K, V> { |
194 | double_ended_iterator_methods!(Bucket::muts); |
195 | } |
196 | |
197 | impl<K, V> ExactSizeIterator for IterMut2<'_, K, V> { |
198 | fn len(&self) -> usize { |
199 | self.iter.len() |
200 | } |
201 | } |
202 | |
203 | impl<K, V> FusedIterator for IterMut2<'_, K, V> {} |
204 | |
205 | impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut2<'_, K, V> { |
206 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
207 | let iter: impl Iterator = self.iter.as_slice().iter().map(Bucket::refs); |
208 | f.debug_list().entries(iter).finish() |
209 | } |
210 | } |
211 | |
212 | impl<K, V> Default for IterMut2<'_, K, V> { |
213 | fn default() -> Self { |
214 | Self { |
215 | iter: [].iter_mut(), |
216 | } |
217 | } |
218 | } |
219 | |
220 | /// An owning iterator over the entries of an [`IndexMap`]. |
221 | /// |
222 | /// This `struct` is created by the [`IndexMap::into_iter`] method |
223 | /// (provided by the [`IntoIterator`] trait). See its documentation for more. |
224 | #[derive (Clone)] |
225 | pub struct IntoIter<K, V> { |
226 | iter: vec::IntoIter<Bucket<K, V>>, |
227 | } |
228 | |
229 | impl<K, V> IntoIter<K, V> { |
230 | pub(super) fn new(entries: Vec<Bucket<K, V>>) -> Self { |
231 | Self { |
232 | iter: entries.into_iter(), |
233 | } |
234 | } |
235 | |
236 | /// Returns a slice of the remaining entries in the iterator. |
237 | pub fn as_slice(&self) -> &Slice<K, V> { |
238 | Slice::from_slice(self.iter.as_slice()) |
239 | } |
240 | |
241 | /// Returns a mutable slice of the remaining entries in the iterator. |
242 | pub fn as_mut_slice(&mut self) -> &mut Slice<K, V> { |
243 | Slice::from_mut_slice(self.iter.as_mut_slice()) |
244 | } |
245 | } |
246 | |
247 | impl<K, V> Iterator for IntoIter<K, V> { |
248 | type Item = (K, V); |
249 | |
250 | iterator_methods!(Bucket::key_value); |
251 | } |
252 | |
253 | impl<K, V> DoubleEndedIterator for IntoIter<K, V> { |
254 | double_ended_iterator_methods!(Bucket::key_value); |
255 | } |
256 | |
257 | impl<K, V> ExactSizeIterator for IntoIter<K, V> { |
258 | fn len(&self) -> usize { |
259 | self.iter.len() |
260 | } |
261 | } |
262 | |
263 | impl<K, V> FusedIterator for IntoIter<K, V> {} |
264 | |
265 | impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> { |
266 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
267 | let iter: impl Iterator = self.iter.as_slice().iter().map(Bucket::refs); |
268 | f.debug_list().entries(iter).finish() |
269 | } |
270 | } |
271 | |
272 | impl<K, V> Default for IntoIter<K, V> { |
273 | fn default() -> Self { |
274 | Self { |
275 | iter: Vec::new().into_iter(), |
276 | } |
277 | } |
278 | } |
279 | |
280 | /// A draining iterator over the entries of an [`IndexMap`]. |
281 | /// |
282 | /// This `struct` is created by the [`IndexMap::drain`] method. |
283 | /// See its documentation for more. |
284 | pub struct Drain<'a, K, V> { |
285 | iter: vec::Drain<'a, Bucket<K, V>>, |
286 | } |
287 | |
288 | impl<'a, K, V> Drain<'a, K, V> { |
289 | pub(super) fn new(iter: vec::Drain<'a, Bucket<K, V>>) -> Self { |
290 | Self { iter } |
291 | } |
292 | |
293 | /// Returns a slice of the remaining entries in the iterator. |
294 | pub fn as_slice(&self) -> &Slice<K, V> { |
295 | Slice::from_slice(self.iter.as_slice()) |
296 | } |
297 | } |
298 | |
299 | impl<K, V> Iterator for Drain<'_, K, V> { |
300 | type Item = (K, V); |
301 | |
302 | iterator_methods!(Bucket::key_value); |
303 | } |
304 | |
305 | impl<K, V> DoubleEndedIterator for Drain<'_, K, V> { |
306 | double_ended_iterator_methods!(Bucket::key_value); |
307 | } |
308 | |
309 | impl<K, V> ExactSizeIterator for Drain<'_, K, V> { |
310 | fn len(&self) -> usize { |
311 | self.iter.len() |
312 | } |
313 | } |
314 | |
315 | impl<K, V> FusedIterator for Drain<'_, K, V> {} |
316 | |
317 | impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Drain<'_, K, V> { |
318 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
319 | let iter: impl Iterator = self.iter.as_slice().iter().map(Bucket::refs); |
320 | f.debug_list().entries(iter).finish() |
321 | } |
322 | } |
323 | |
324 | /// An iterator over the keys of an [`IndexMap`]. |
325 | /// |
326 | /// This `struct` is created by the [`IndexMap::keys`] method. |
327 | /// See its documentation for more. |
328 | pub struct Keys<'a, K, V> { |
329 | iter: slice::Iter<'a, Bucket<K, V>>, |
330 | } |
331 | |
332 | impl<'a, K, V> Keys<'a, K, V> { |
333 | pub(super) fn new(entries: &'a [Bucket<K, V>]) -> Self { |
334 | Self { |
335 | iter: entries.iter(), |
336 | } |
337 | } |
338 | } |
339 | |
340 | impl<'a, K, V> Iterator for Keys<'a, K, V> { |
341 | type Item = &'a K; |
342 | |
343 | iterator_methods!(Bucket::key_ref); |
344 | } |
345 | |
346 | impl<K, V> DoubleEndedIterator for Keys<'_, K, V> { |
347 | double_ended_iterator_methods!(Bucket::key_ref); |
348 | } |
349 | |
350 | impl<K, V> ExactSizeIterator for Keys<'_, K, V> { |
351 | fn len(&self) -> usize { |
352 | self.iter.len() |
353 | } |
354 | } |
355 | |
356 | impl<K, V> FusedIterator for Keys<'_, K, V> {} |
357 | |
358 | // FIXME(#26925) Remove in favor of `#[derive(Clone)]` |
359 | impl<K, V> Clone for Keys<'_, K, V> { |
360 | fn clone(&self) -> Self { |
361 | Keys { |
362 | iter: self.iter.clone(), |
363 | } |
364 | } |
365 | } |
366 | |
367 | impl<K: fmt::Debug, V> fmt::Debug for Keys<'_, K, V> { |
368 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
369 | f.debug_list().entries(self.clone()).finish() |
370 | } |
371 | } |
372 | |
373 | impl<K, V> Default for Keys<'_, K, V> { |
374 | fn default() -> Self { |
375 | Self { iter: [].iter() } |
376 | } |
377 | } |
378 | |
379 | /// Access [`IndexMap`] keys at indexed positions. |
380 | /// |
381 | /// While [`Index<usize> for IndexMap`][values] accesses a map's values, |
382 | /// indexing through [`IndexMap::keys`] offers an alternative to access a map's |
383 | /// keys instead. |
384 | /// |
385 | /// [values]: IndexMap#impl-Index<usize>-for-IndexMap<K,+V,+S> |
386 | /// |
387 | /// Since `Keys` is also an iterator, consuming items from the iterator will |
388 | /// offset the effective indexes. Similarly, if `Keys` is obtained from |
389 | /// [`Slice::keys`], indexes will be interpreted relative to the position of |
390 | /// that slice. |
391 | /// |
392 | /// # Examples |
393 | /// |
394 | /// ``` |
395 | /// use indexmap::IndexMap; |
396 | /// |
397 | /// let mut map = IndexMap::new(); |
398 | /// for word in "Lorem ipsum dolor sit amet" .split_whitespace() { |
399 | /// map.insert(word.to_lowercase(), word.to_uppercase()); |
400 | /// } |
401 | /// |
402 | /// assert_eq!(map[0], "LOREM" ); |
403 | /// assert_eq!(map.keys()[0], "lorem" ); |
404 | /// assert_eq!(map[1], "IPSUM" ); |
405 | /// assert_eq!(map.keys()[1], "ipsum" ); |
406 | /// |
407 | /// map.reverse(); |
408 | /// assert_eq!(map.keys()[0], "amet" ); |
409 | /// assert_eq!(map.keys()[1], "sit" ); |
410 | /// |
411 | /// map.sort_keys(); |
412 | /// assert_eq!(map.keys()[0], "amet" ); |
413 | /// assert_eq!(map.keys()[1], "dolor" ); |
414 | /// |
415 | /// // Advancing the iterator will offset the indexing |
416 | /// let mut keys = map.keys(); |
417 | /// assert_eq!(keys[0], "amet" ); |
418 | /// assert_eq!(keys.next().map(|s| &**s), Some("amet" )); |
419 | /// assert_eq!(keys[0], "dolor" ); |
420 | /// assert_eq!(keys[1], "ipsum" ); |
421 | /// |
422 | /// // Slices may have an offset as well |
423 | /// let slice = &map[2..]; |
424 | /// assert_eq!(slice[0], "IPSUM" ); |
425 | /// assert_eq!(slice.keys()[0], "ipsum" ); |
426 | /// ``` |
427 | /// |
428 | /// ```should_panic |
429 | /// use indexmap::IndexMap; |
430 | /// |
431 | /// let mut map = IndexMap::new(); |
432 | /// map.insert("foo" , 1); |
433 | /// println!("{:?}" , map.keys()[10]); // panics! |
434 | /// ``` |
435 | impl<'a, K, V> Index<usize> for Keys<'a, K, V> { |
436 | type Output = K; |
437 | |
438 | /// Returns a reference to the key at the supplied `index`. |
439 | /// |
440 | /// ***Panics*** if `index` is out of bounds. |
441 | fn index(&self, index: usize) -> &K { |
442 | &self.iter.as_slice()[index].key |
443 | } |
444 | } |
445 | |
446 | /// An owning iterator over the keys of an [`IndexMap`]. |
447 | /// |
448 | /// This `struct` is created by the [`IndexMap::into_keys`] method. |
449 | /// See its documentation for more. |
450 | pub struct IntoKeys<K, V> { |
451 | iter: vec::IntoIter<Bucket<K, V>>, |
452 | } |
453 | |
454 | impl<K, V> IntoKeys<K, V> { |
455 | pub(super) fn new(entries: Vec<Bucket<K, V>>) -> Self { |
456 | Self { |
457 | iter: entries.into_iter(), |
458 | } |
459 | } |
460 | } |
461 | |
462 | impl<K, V> Iterator for IntoKeys<K, V> { |
463 | type Item = K; |
464 | |
465 | iterator_methods!(Bucket::key); |
466 | } |
467 | |
468 | impl<K, V> DoubleEndedIterator for IntoKeys<K, V> { |
469 | double_ended_iterator_methods!(Bucket::key); |
470 | } |
471 | |
472 | impl<K, V> ExactSizeIterator for IntoKeys<K, V> { |
473 | fn len(&self) -> usize { |
474 | self.iter.len() |
475 | } |
476 | } |
477 | |
478 | impl<K, V> FusedIterator for IntoKeys<K, V> {} |
479 | |
480 | impl<K: fmt::Debug, V> fmt::Debug for IntoKeys<K, V> { |
481 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
482 | let iter: impl Iterator = self.iter.as_slice().iter().map(Bucket::key_ref); |
483 | f.debug_list().entries(iter).finish() |
484 | } |
485 | } |
486 | |
487 | impl<K, V> Default for IntoKeys<K, V> { |
488 | fn default() -> Self { |
489 | Self { |
490 | iter: Vec::new().into_iter(), |
491 | } |
492 | } |
493 | } |
494 | |
495 | /// An iterator over the values of an [`IndexMap`]. |
496 | /// |
497 | /// This `struct` is created by the [`IndexMap::values`] method. |
498 | /// See its documentation for more. |
499 | pub struct Values<'a, K, V> { |
500 | iter: slice::Iter<'a, Bucket<K, V>>, |
501 | } |
502 | |
503 | impl<'a, K, V> Values<'a, K, V> { |
504 | pub(super) fn new(entries: &'a [Bucket<K, V>]) -> Self { |
505 | Self { |
506 | iter: entries.iter(), |
507 | } |
508 | } |
509 | } |
510 | |
511 | impl<'a, K, V> Iterator for Values<'a, K, V> { |
512 | type Item = &'a V; |
513 | |
514 | iterator_methods!(Bucket::value_ref); |
515 | } |
516 | |
517 | impl<K, V> DoubleEndedIterator for Values<'_, K, V> { |
518 | double_ended_iterator_methods!(Bucket::value_ref); |
519 | } |
520 | |
521 | impl<K, V> ExactSizeIterator for Values<'_, K, V> { |
522 | fn len(&self) -> usize { |
523 | self.iter.len() |
524 | } |
525 | } |
526 | |
527 | impl<K, V> FusedIterator for Values<'_, K, V> {} |
528 | |
529 | // FIXME(#26925) Remove in favor of `#[derive(Clone)]` |
530 | impl<K, V> Clone for Values<'_, K, V> { |
531 | fn clone(&self) -> Self { |
532 | Values { |
533 | iter: self.iter.clone(), |
534 | } |
535 | } |
536 | } |
537 | |
538 | impl<K, V: fmt::Debug> fmt::Debug for Values<'_, K, V> { |
539 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
540 | f.debug_list().entries(self.clone()).finish() |
541 | } |
542 | } |
543 | |
544 | impl<K, V> Default for Values<'_, K, V> { |
545 | fn default() -> Self { |
546 | Self { iter: [].iter() } |
547 | } |
548 | } |
549 | |
550 | /// A mutable iterator over the values of an [`IndexMap`]. |
551 | /// |
552 | /// This `struct` is created by the [`IndexMap::values_mut`] method. |
553 | /// See its documentation for more. |
554 | pub struct ValuesMut<'a, K, V> { |
555 | iter: slice::IterMut<'a, Bucket<K, V>>, |
556 | } |
557 | |
558 | impl<'a, K, V> ValuesMut<'a, K, V> { |
559 | pub(super) fn new(entries: &'a mut [Bucket<K, V>]) -> Self { |
560 | Self { |
561 | iter: entries.iter_mut(), |
562 | } |
563 | } |
564 | } |
565 | |
566 | impl<'a, K, V> Iterator for ValuesMut<'a, K, V> { |
567 | type Item = &'a mut V; |
568 | |
569 | iterator_methods!(Bucket::value_mut); |
570 | } |
571 | |
572 | impl<K, V> DoubleEndedIterator for ValuesMut<'_, K, V> { |
573 | double_ended_iterator_methods!(Bucket::value_mut); |
574 | } |
575 | |
576 | impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> { |
577 | fn len(&self) -> usize { |
578 | self.iter.len() |
579 | } |
580 | } |
581 | |
582 | impl<K, V> FusedIterator for ValuesMut<'_, K, V> {} |
583 | |
584 | impl<K, V: fmt::Debug> fmt::Debug for ValuesMut<'_, K, V> { |
585 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
586 | let iter: impl Iterator = self.iter.as_slice().iter().map(Bucket::value_ref); |
587 | f.debug_list().entries(iter).finish() |
588 | } |
589 | } |
590 | |
591 | impl<K, V> Default for ValuesMut<'_, K, V> { |
592 | fn default() -> Self { |
593 | Self { |
594 | iter: [].iter_mut(), |
595 | } |
596 | } |
597 | } |
598 | |
599 | /// An owning iterator over the values of an [`IndexMap`]. |
600 | /// |
601 | /// This `struct` is created by the [`IndexMap::into_values`] method. |
602 | /// See its documentation for more. |
603 | pub struct IntoValues<K, V> { |
604 | iter: vec::IntoIter<Bucket<K, V>>, |
605 | } |
606 | |
607 | impl<K, V> IntoValues<K, V> { |
608 | pub(super) fn new(entries: Vec<Bucket<K, V>>) -> Self { |
609 | Self { |
610 | iter: entries.into_iter(), |
611 | } |
612 | } |
613 | } |
614 | |
615 | impl<K, V> Iterator for IntoValues<K, V> { |
616 | type Item = V; |
617 | |
618 | iterator_methods!(Bucket::value); |
619 | } |
620 | |
621 | impl<K, V> DoubleEndedIterator for IntoValues<K, V> { |
622 | double_ended_iterator_methods!(Bucket::value); |
623 | } |
624 | |
625 | impl<K, V> ExactSizeIterator for IntoValues<K, V> { |
626 | fn len(&self) -> usize { |
627 | self.iter.len() |
628 | } |
629 | } |
630 | |
631 | impl<K, V> FusedIterator for IntoValues<K, V> {} |
632 | |
633 | impl<K, V: fmt::Debug> fmt::Debug for IntoValues<K, V> { |
634 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
635 | let iter: impl Iterator = self.iter.as_slice().iter().map(Bucket::value_ref); |
636 | f.debug_list().entries(iter).finish() |
637 | } |
638 | } |
639 | |
640 | impl<K, V> Default for IntoValues<K, V> { |
641 | fn default() -> Self { |
642 | Self { |
643 | iter: Vec::new().into_iter(), |
644 | } |
645 | } |
646 | } |
647 | |
648 | /// A splicing iterator for `IndexMap`. |
649 | /// |
650 | /// This `struct` is created by [`IndexMap::splice()`]. |
651 | /// See its documentation for more. |
652 | pub struct Splice<'a, I, K, V, S> |
653 | where |
654 | I: Iterator<Item = (K, V)>, |
655 | K: Hash + Eq, |
656 | S: BuildHasher, |
657 | { |
658 | map: &'a mut IndexMap<K, V, S>, |
659 | tail: IndexMapCore<K, V>, |
660 | drain: vec::IntoIter<Bucket<K, V>>, |
661 | replace_with: I, |
662 | } |
663 | |
664 | impl<'a, I, K, V, S> Splice<'a, I, K, V, S> |
665 | where |
666 | I: Iterator<Item = (K, V)>, |
667 | K: Hash + Eq, |
668 | S: BuildHasher, |
669 | { |
670 | pub(super) fn new<R>(map: &'a mut IndexMap<K, V, S>, range: R, replace_with: I) -> Self |
671 | where |
672 | R: RangeBounds<usize>, |
673 | { |
674 | let (tail: IndexMapCore, drain: IntoIter>) = map.core.split_splice(range); |
675 | Self { |
676 | map, |
677 | tail, |
678 | drain, |
679 | replace_with, |
680 | } |
681 | } |
682 | } |
683 | |
684 | impl<I, K, V, S> Drop for Splice<'_, I, K, V, S> |
685 | where |
686 | I: Iterator<Item = (K, V)>, |
687 | K: Hash + Eq, |
688 | S: BuildHasher, |
689 | { |
690 | fn drop(&mut self) { |
691 | // Finish draining unconsumed items. We don't strictly *have* to do this |
692 | // manually, since we already split it into separate memory, but it will |
693 | // match the drop order of `vec::Splice` items this way. |
694 | let _ = self.drain.nth(usize::MAX); |
695 | |
696 | // Now insert all the new items. If a key matches an existing entry, it |
697 | // keeps the original position and only replaces the value, like `insert`. |
698 | while let Some((key: K, value: V)) = self.replace_with.next() { |
699 | // Since the tail is disjoint, we can try to update it first, |
700 | // or else insert (update or append) the primary map. |
701 | let hash: HashValue = self.map.hash(&key); |
702 | if let Some(i: usize) = self.tail.get_index_of(hash, &key) { |
703 | self.tail.as_entries_mut()[i].value = value; |
704 | } else { |
705 | self.map.core.insert_full(hash, key, value); |
706 | } |
707 | } |
708 | |
709 | // Finally, re-append the tail |
710 | self.map.core.append_unchecked(&mut self.tail); |
711 | } |
712 | } |
713 | |
714 | impl<I, K, V, S> Iterator for Splice<'_, I, K, V, S> |
715 | where |
716 | I: Iterator<Item = (K, V)>, |
717 | K: Hash + Eq, |
718 | S: BuildHasher, |
719 | { |
720 | type Item = (K, V); |
721 | |
722 | fn next(&mut self) -> Option<Self::Item> { |
723 | self.drain.next().map(Bucket::key_value) |
724 | } |
725 | |
726 | fn size_hint(&self) -> (usize, Option<usize>) { |
727 | self.drain.size_hint() |
728 | } |
729 | } |
730 | |
731 | impl<I, K, V, S> DoubleEndedIterator for Splice<'_, I, K, V, S> |
732 | where |
733 | I: Iterator<Item = (K, V)>, |
734 | K: Hash + Eq, |
735 | S: BuildHasher, |
736 | { |
737 | fn next_back(&mut self) -> Option<Self::Item> { |
738 | self.drain.next_back().map(Bucket::key_value) |
739 | } |
740 | } |
741 | |
742 | impl<I, K, V, S> ExactSizeIterator for Splice<'_, I, K, V, S> |
743 | where |
744 | I: Iterator<Item = (K, V)>, |
745 | K: Hash + Eq, |
746 | S: BuildHasher, |
747 | { |
748 | fn len(&self) -> usize { |
749 | self.drain.len() |
750 | } |
751 | } |
752 | |
753 | impl<I, K, V, S> FusedIterator for Splice<'_, I, K, V, S> |
754 | where |
755 | I: Iterator<Item = (K, V)>, |
756 | K: Hash + Eq, |
757 | S: BuildHasher, |
758 | { |
759 | } |
760 | |
761 | impl<'a, I, K, V, S> fmt::Debug for Splice<'a, I, K, V, S> |
762 | where |
763 | I: fmt::Debug + Iterator<Item = (K, V)>, |
764 | K: fmt::Debug + Hash + Eq, |
765 | V: fmt::Debug, |
766 | S: BuildHasher, |
767 | { |
768 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
769 | // Follow `vec::Splice` in only printing the drain and replacement |
770 | f&mut DebugStruct<'_, '_>.debug_struct("Splice" ) |
771 | .field("drain" , &self.drain) |
772 | .field(name:"replace_with" , &self.replace_with) |
773 | .finish() |
774 | } |
775 | } |
776 | |