Debian Codegraph @

src/map/rayon.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 //! Parallel iterator types for [`OrderMap`] with [`rayon`].
2 //!
3 //! You will rarely need to interact with this module directly unless you need to name one of the
4 //! iterator types.
5
6 pub use indexmap::map::rayon::{
7 IntoParIter, ParDrain, ParIter, ParIterMut, ParKeys, ParValues, ParValuesMut,
8 };
9
10 use super::OrderMap;
11 use core::cmp::Ordering;
12 use core::hash::{BuildHasher, Hash};
13 use core::ops::RangeBounds;
14 use rayon::prelude::*;
15
16 impl<K, V, S> IntoParallelIterator for OrderMap<K, V, S>
17 where
18 K: Send,
19 V: Send,
20 {
21 type Item = (K, V);
22 type Iter = IntoParIter<K, V>;
23
24 fn into_par_iter(self) -> Self::Iter {
25 self.inner.into_par_iter()
26 }
27 }
28
29 impl<'a, K, V, S> IntoParallelIterator for &'a OrderMap<K, V, S>
30 where
31 K: Sync,
32 V: Sync,
33 {
34 type Item = (&'a K, &'a V);
35 type Iter = ParIter<'a, K, V>;
36
37 fn into_par_iter(self) -> Self::Iter {
38 self.inner.par_iter()
39 }
40 }
41
42 impl<'a, K, V, S> IntoParallelIterator for &'a mut OrderMap<K, V, S>
43 where
44 K: Sync + Send,
45 V: Send,
46 {
47 type Item = (&'a K, &'a mut V);
48 type Iter = ParIterMut<'a, K, V>;
49
50 fn into_par_iter(self) -> Self::Iter {
51 self.inner.par_iter_mut()
52 }
53 }
54
55 impl<'a, K, V, S> ParallelDrainRange<usize> for &'a mut OrderMap<K, V, S>
56 where
57 K: Send,
58 V: Send,
59 {
60 type Item = (K, V);
61 type Iter = ParDrain<'a, K, V>;
62
63 fn par_drain<R: RangeBounds<usize>>(self, range: R) -> Self::Iter {
64 self.inner.par_drain(range)
65 }
66 }
67
68 /// Parallel iterator methods and other parallel methods.
69 ///
70 /// The following methods **require crate feature `"rayon"`**.
71 ///
72 /// See also the `IntoParallelIterator` implementations.
73 impl<K, V, S> OrderMap<K, V, S>
74 where
75 K: Sync,
76 V: Sync,
77 {
78 /// Return a parallel iterator over the keys of the map.
79 ///
80 /// While parallel iterators can process items in any order, their relative order
81 /// in the map is still preserved for operations like `reduce` and `collect`.
82 pub fn par_keys(&self) -> ParKeys<'_, K, V> {
83 self.inner.par_keys()
84 }
85
86 /// Return a parallel iterator over the values of the map.
87 ///
88 /// While parallel iterators can process items in any order, their relative order
89 /// in the map is still preserved for operations like `reduce` and `collect`.
90 pub fn par_values(&self) -> ParValues<'_, K, V> {
91 self.inner.par_values()
92 }
93 }
94
95 impl<K, V, S> OrderMap<K, V, S>
96 where
97 K: PartialEq + Sync,
98 V: Sync,
99 {
100 /// Returns `true` if `self` contains all of the same key-value pairs as `other`,
101 /// in the same indexed order, determined in parallel.
102 pub fn par_eq<S2>(&self, other: &OrderMap<K, V, S2>) -> bool
103 where
104 V: PartialEq,
105 {
106 self.len() == other.len() && self.par_iter().eq(other)
107 }
108 }
109
110 impl<K, V, S> OrderMap<K, V, S>
111 where
112 K: Send,
113 V: Send,
114 {
115 /// Return a parallel iterator over mutable references to the values of the map
116 ///
117 /// While parallel iterators can process items in any order, their relative order
118 /// in the map is still preserved for operations like `reduce` and `collect`.
119 pub fn par_values_mut(&mut self) -> ParValuesMut<'_, K, V> {
120 self.inner.par_values_mut()
121 }
122 }
123
124 impl<K, V, S> OrderMap<K, V, S>
125 where
126 K: Send,
127 V: Send,
128 {
129 /// Sort the map's key-value pairs in parallel, by the default ordering of the keys.
130 pub fn par_sort_keys(&mut self)
131 where
132 K: Ord,
133 {
134 self.inner.par_sort_keys();
135 }
136
137 /// Sort the map's key-value pairs in place and in parallel, using the comparison
138 /// function `cmp`.
139 ///
140 /// The comparison function receives two key and value pairs to compare (you
141 /// can sort by keys or values or their combination as needed).
142 pub fn par_sort_by<F>(&mut self, cmp: F)
143 where
144 F: Fn(&K, &V, &K, &V) -> Ordering + Sync,
145 {
146 self.inner.par_sort_by(cmp);
147 }
148
149 /// Sort the key-value pairs of the map in parallel and return a by-value parallel
150 /// iterator of the key-value pairs with the result.
151 pub fn par_sorted_by<F>(self, cmp: F) -> IntoParIter<K, V>
152 where
153 F: Fn(&K, &V, &K, &V) -> Ordering + Sync,
154 {
155 self.inner.par_sorted_by(cmp)
156 }
157
158 /// Sort the map's key-value pairs in place and in parallel, using a sort-key extraction
159 /// function.
160 pub fn par_sort_by_key<T, F>(&mut self, sort_key: F)
161 where
162 T: Ord,
163 F: Fn(&K, &V) -> T + Sync,
164 {
165 self.inner.par_sort_by_key(sort_key)
166 }
167
168 /// Sort the map's key-value pairs in parallel, by the default ordering of the keys.
169 pub fn par_sort_unstable_keys(&mut self)
170 where
171 K: Ord,
172 {
173 self.inner.par_sort_unstable_keys();
174 }
175
176 /// Sort the map's key-value pairs in place and in parallel, using the comparison
177 /// function `cmp`.
178 ///
179 /// The comparison function receives two key and value pairs to compare (you
180 /// can sort by keys or values or their combination as needed).
181 pub fn par_sort_unstable_by<F>(&mut self, cmp: F)
182 where
183 F: Fn(&K, &V, &K, &V) -> Ordering + Sync,
184 {
185 self.inner.par_sort_unstable_by(cmp);
186 }
187
188 /// Sort the key-value pairs of the map in parallel and return a by-value parallel
189 /// iterator of the key-value pairs with the result.
190 pub fn par_sorted_unstable_by<F>(self, cmp: F) -> IntoParIter<K, V>
191 where
192 F: Fn(&K, &V, &K, &V) -> Ordering + Sync,
193 {
194 self.inner.par_sorted_unstable_by(cmp)
195 }
196
197 /// Sort the map's key-value pairs in place and in parallel, using a sort-key extraction
198 /// function.
199 pub fn par_sort_unstable_by_key<T, F>(&mut self, sort_key: F)
200 where
201 T: Ord,
202 F: Fn(&K, &V) -> T + Sync,
203 {
204 self.inner.par_sort_unstable_by_key(sort_key)
205 }
206
207 /// Sort the map's key-value pairs in place and in parallel, using a sort-key extraction
208 /// function.
209 pub fn par_sort_by_cached_key<T, F>(&mut self, sort_key: F)
210 where
211 T: Ord + Send,
212 F: Fn(&K, &V) -> T + Sync,
213 {
214 self.inner.par_sort_by_cached_key(sort_key)
215 }
216 }
217
218 impl<K, V, S> FromParallelIterator<(K, V)> for OrderMap<K, V, S>
219 where
220 K: Eq + Hash + Send,
221 V: Send,
222 S: BuildHasher + Default + Send,
223 {
224 fn from_par_iter<I>(iter: I) -> Self
225 where
226 I: IntoParallelIterator<Item = (K, V)>,
227 {
228 Self {
229 inner: <_>::from_par_iter(iter),
230 }
231 }
232 }
233
234 impl<K, V, S> ParallelExtend<(K, V)> for OrderMap<K, V, S>
235 where
236 K: Eq + Hash + Send,
237 V: Send,
238 S: BuildHasher + Send,
239 {
240 fn par_extend<I>(&mut self, iter: I)
241 where
242 I: IntoParallelIterator<Item = (K, V)>,
243 {
244 self.inner.par_extend(iter);
245 }
246 }
247
248 impl<'a, K: 'a, V: 'a, S> ParallelExtend<(&'a K, &'a V)> for OrderMap<K, V, S>
249 where
250 K: Copy + Eq + Hash + Send + Sync,
251 V: Copy + Send + Sync,
252 S: BuildHasher + Send,
253 {
254 fn par_extend<I>(&mut self, iter: I)
255 where
256 I: IntoParallelIterator<Item = (&'a K, &'a V)>,
257 {
258 self.inner.par_extend(iter);
259 }
260 }
261
•262 #[cfg(all(test, feature = "std"))]
263 mod tests {
264 use super::*;
265 use std::vec::Vec;
266
267 #[test]
268 fn insert_order() {
269 let insert = [0, 4, 2, 12, 8, 7, 11, 5, 3, 17, 19, 22, 23];
270 let mut map = OrderMap::new();
271
272 for &elt in &insert {
273 map.insert(elt, ());
274 }
275
276 assert_eq!(map.par_keys().count(), map.len());
277 assert_eq!(map.par_keys().count(), insert.len());
278 insert.par_iter().zip(map.par_keys()).for_each(|(a, b)| {
279 assert_eq!(a, b);
280 });
281 (0..insert.len())
282 .into_par_iter()
283 .zip(map.par_keys())
284 .for_each(|(i, k)| {
285 assert_eq!(map.get_index(i).unwrap().0, k);
286 });
287 }
288
289 #[test]
290 fn partial_eq_and_eq() {
291 let mut map_a = OrderMap::new();
292 map_a.insert(1, "1");
293 map_a.insert(2, "2");
294 let mut map_b = map_a.clone();
295 assert!(map_a.par_eq(&map_b));
296 map_b.swap_remove(&1);
297 assert!(!map_a.par_eq(&map_b));
298 map_b.insert(3, "3");
299 assert!(!map_a.par_eq(&map_b));
300 }
301
302 #[test]
303 fn extend() {
304 let mut map = OrderMap::new();
305 map.par_extend(vec![(&1, &2), (&3, &4)]);
306 map.par_extend(vec![(5, 6)]);
307 assert_eq!(
308 map.into_par_iter().collect::<Vec<_>>(),
309 vec![(1, 2), (3, 4), (5, 6)]
310 );
311 }
312
313 #[test]
314 fn keys() {
315 let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
316 let map: OrderMap<_, _> = vec.into_par_iter().collect();
317 let keys: Vec<_> = map.par_keys().copied().collect();
318 assert_eq!(keys.len(), 3);
319 assert!(keys.contains(&1));
320 assert!(keys.contains(&2));
321 assert!(keys.contains(&3));
322 }
323
324 #[test]
325 fn values() {
326 let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
327 let map: OrderMap<_, _> = vec.into_par_iter().collect();
328 let values: Vec<_> = map.par_values().copied().collect();
329 assert_eq!(values.len(), 3);
330 assert!(values.contains(&'a'));
331 assert!(values.contains(&'b'));
332 assert!(values.contains(&'c'));
333 }
334
335 #[test]
336 fn values_mut() {
337 let vec = vec![(1, 1), (2, 2), (3, 3)];
338 let mut map: OrderMap<_, _> = vec.into_par_iter().collect();
339 map.par_values_mut().for_each(|value| *value *= 2);
340 let values: Vec<_> = map.par_values().copied().collect();
341 assert_eq!(values.len(), 3);
342 assert!(values.contains(&2));
343 assert!(values.contains(&4));
344 assert!(values.contains(&6));
345 }
346 }