1 | //! This module contains the parallel iterator types for hash maps |
2 | //! (`HashMap<K, V>`). You will rarely need to interact with it directly |
3 | //! unless you have need to name one of the iterator types. |
4 | |
5 | use std::collections::HashMap; |
6 | use std::hash::{BuildHasher, Hash}; |
7 | use std::marker::PhantomData; |
8 | |
9 | use crate::iter::plumbing::*; |
10 | use crate::iter::*; |
11 | |
12 | use crate::vec; |
13 | |
14 | /// Parallel iterator over a hash map |
15 | #[derive (Debug)] // std doesn't Clone |
16 | pub struct IntoIter<K: Hash + Eq + Send, V: Send> { |
17 | inner: vec::IntoIter<(K, V)>, |
18 | } |
19 | |
20 | into_par_vec! { |
21 | HashMap<K, V, S> => IntoIter<K, V>, |
22 | impl<K: Hash + Eq + Send, V: Send, S: BuildHasher> |
23 | } |
24 | |
25 | delegate_iterator! { |
26 | IntoIter<K, V> => (K, V), |
27 | impl<K: Hash + Eq + Send, V: Send> |
28 | } |
29 | |
30 | /// Parallel iterator over an immutable reference to a hash map |
31 | #[derive (Debug)] |
32 | pub struct Iter<'a, K: Hash + Eq + Sync, V: Sync> { |
33 | inner: vec::IntoIter<(&'a K, &'a V)>, |
34 | } |
35 | |
36 | impl<'a, K: Hash + Eq + Sync, V: Sync> Clone for Iter<'a, K, V> { |
37 | fn clone(&self) -> Self { |
38 | Iter { |
39 | inner: self.inner.clone(), |
40 | } |
41 | } |
42 | } |
43 | |
44 | into_par_vec! { |
45 | &'a HashMap<K, V, S> => Iter<'a, K, V>, |
46 | impl<'a, K: Hash + Eq + Sync, V: Sync, S: BuildHasher> |
47 | } |
48 | |
49 | delegate_iterator! { |
50 | Iter<'a, K, V> => (&'a K, &'a V), |
51 | impl<'a, K: Hash + Eq + Sync + 'a, V: Sync + 'a> |
52 | } |
53 | |
54 | /// Parallel iterator over a mutable reference to a hash map |
55 | #[derive (Debug)] |
56 | pub struct IterMut<'a, K: Hash + Eq + Sync, V: Send> { |
57 | inner: vec::IntoIter<(&'a K, &'a mut V)>, |
58 | } |
59 | |
60 | into_par_vec! { |
61 | &'a mut HashMap<K, V, S> => IterMut<'a, K, V>, |
62 | impl<'a, K: Hash + Eq + Sync, V: Send, S: BuildHasher> |
63 | } |
64 | |
65 | delegate_iterator! { |
66 | IterMut<'a, K, V> => (&'a K, &'a mut V), |
67 | impl<'a, K: Hash + Eq + Sync + 'a, V: Send + 'a> |
68 | } |
69 | |
70 | /// Draining parallel iterator that moves out of a hash map, |
71 | /// but keeps the total capacity. |
72 | #[derive (Debug)] |
73 | pub struct Drain<'a, K: Hash + Eq + Send, V: Send> { |
74 | inner: vec::IntoIter<(K, V)>, |
75 | marker: PhantomData<&'a mut HashMap<K, V>>, |
76 | } |
77 | |
78 | impl<'a, K: Hash + Eq + Send, V: Send, S: BuildHasher> ParallelDrainFull |
79 | for &'a mut HashMap<K, V, S> |
80 | { |
81 | type Iter = Drain<'a, K, V>; |
82 | type Item = (K, V); |
83 | |
84 | fn par_drain(self) -> Self::Iter { |
85 | let vec: Vec<_> = self.drain().collect(); |
86 | Drain { |
87 | inner: vec.into_par_iter(), |
88 | marker: PhantomData, |
89 | } |
90 | } |
91 | } |
92 | |
93 | delegate_iterator! { |
94 | Drain<'_, K, V> => (K, V), |
95 | impl<K: Hash + Eq + Send, V: Send> |
96 | } |
97 | |