1
1
//! Rayon extensions for `HashMap`.
2
2
3
3
use crate :: hash_map:: HashMap ;
4
+ use crate :: raw:: { AllocRef , Global } ;
4
5
use core:: fmt;
5
6
use core:: hash:: { BuildHasher , Hash } ;
6
7
use rayon:: iter:: plumbing:: UnindexedConsumer ;
@@ -15,11 +16,13 @@ use rayon::iter::{FromParallelIterator, IntoParallelIterator, ParallelExtend, Pa
15
16
/// [`par_iter`]: /hashbrown/struct.HashMap.html#method.par_iter
16
17
/// [`HashMap`]: /hashbrown/struct.HashMap.html
17
18
/// [`IntoParallelRefIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelRefIterator.html
18
- pub struct ParIter < ' a , K , V , S > {
19
- map : & ' a HashMap < K , V , S > ,
19
+ pub struct ParIter < ' a , K , V , S , A : AllocRef + Clone = Global > {
20
+ map : & ' a HashMap < K , V , S , A > ,
20
21
}
21
22
22
- impl < ' a , K : Sync , V : Sync , S : Sync > ParallelIterator for ParIter < ' a , K , V , S > {
23
+ impl < ' a , K : Sync , V : Sync , S : Sync , A : AllocRef + Clone + Sync > ParallelIterator
24
+ for ParIter < ' a , K , V , S , A >
25
+ {
23
26
type Item = ( & ' a K , & ' a V ) ;
24
27
25
28
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -36,14 +39,16 @@ impl<'a, K: Sync, V: Sync, S: Sync> ParallelIterator for ParIter<'a, K, V, S> {
36
39
}
37
40
}
38
41
39
- impl < K , V , S > Clone for ParIter < ' _ , K , V , S > {
42
+ impl < K , V , S , A : AllocRef + Clone > Clone for ParIter < ' _ , K , V , S , A > {
40
43
#[ cfg_attr( feature = "inline-more" , inline) ]
41
44
fn clone ( & self ) -> Self {
42
45
ParIter { map : self . map }
43
46
}
44
47
}
45
48
46
- impl < K : fmt:: Debug + Eq + Hash , V : fmt:: Debug , S : BuildHasher > fmt:: Debug for ParIter < ' _ , K , V , S > {
49
+ impl < K : fmt:: Debug + Eq + Hash , V : fmt:: Debug , S : BuildHasher , A : AllocRef + Clone > fmt:: Debug
50
+ for ParIter < ' _ , K , V , S , A >
51
+ {
47
52
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
48
53
self . map . iter ( ) . fmt ( f)
49
54
}
@@ -56,11 +61,13 @@ impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for Pa
56
61
///
57
62
/// [`par_keys`]: /hashbrown/struct.HashMap.html#method.par_keys
58
63
/// [`HashMap`]: /hashbrown/struct.HashMap.html
59
- pub struct ParKeys < ' a , K , V , S > {
60
- map : & ' a HashMap < K , V , S > ,
64
+ pub struct ParKeys < ' a , K , V , S , A : AllocRef + Clone > {
65
+ map : & ' a HashMap < K , V , S , A > ,
61
66
}
62
67
63
- impl < ' a , K : Sync , V : Sync , S : Sync > ParallelIterator for ParKeys < ' a , K , V , S > {
68
+ impl < ' a , K : Sync , V : Sync , S : Sync , A : AllocRef + Clone + Sync > ParallelIterator
69
+ for ParKeys < ' a , K , V , S , A >
70
+ {
64
71
type Item = & ' a K ;
65
72
66
73
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -74,14 +81,16 @@ impl<'a, K: Sync, V: Sync, S: Sync> ParallelIterator for ParKeys<'a, K, V, S> {
74
81
}
75
82
}
76
83
77
- impl < K , V , S > Clone for ParKeys < ' _ , K , V , S > {
84
+ impl < K , V , S , A : AllocRef + Clone > Clone for ParKeys < ' _ , K , V , S , A > {
78
85
#[ cfg_attr( feature = "inline-more" , inline) ]
79
86
fn clone ( & self ) -> Self {
80
87
ParKeys { map : self . map }
81
88
}
82
89
}
83
90
84
- impl < K : fmt:: Debug + Eq + Hash , V , S : BuildHasher > fmt:: Debug for ParKeys < ' _ , K , V , S > {
91
+ impl < K : fmt:: Debug + Eq + Hash , V , S : BuildHasher , A : AllocRef + Clone > fmt:: Debug
92
+ for ParKeys < ' _ , K , V , S , A >
93
+ {
85
94
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
86
95
self . map . keys ( ) . fmt ( f)
87
96
}
@@ -94,11 +103,13 @@ impl<K: fmt::Debug + Eq + Hash, V, S: BuildHasher> fmt::Debug for ParKeys<'_, K,
94
103
///
95
104
/// [`par_values`]: /hashbrown/struct.HashMap.html#method.par_values
96
105
/// [`HashMap`]: /hashbrown/struct.HashMap.html
97
- pub struct ParValues < ' a , K , V , S > {
98
- map : & ' a HashMap < K , V , S > ,
106
+ pub struct ParValues < ' a , K , V , S , A : AllocRef + Clone = Global > {
107
+ map : & ' a HashMap < K , V , S , A > ,
99
108
}
100
109
101
- impl < ' a , K : Sync , V : Sync , S : Sync > ParallelIterator for ParValues < ' a , K , V , S > {
110
+ impl < ' a , K : Sync , V : Sync , S : Sync , A : AllocRef + Clone + Sync > ParallelIterator
111
+ for ParValues < ' a , K , V , S , A >
112
+ {
102
113
type Item = & ' a V ;
103
114
104
115
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -112,14 +123,16 @@ impl<'a, K: Sync, V: Sync, S: Sync> ParallelIterator for ParValues<'a, K, V, S>
112
123
}
113
124
}
114
125
115
- impl < K , V , S > Clone for ParValues < ' _ , K , V , S > {
126
+ impl < K , V , S , A : AllocRef + Clone > Clone for ParValues < ' _ , K , V , S , A > {
116
127
#[ cfg_attr( feature = "inline-more" , inline) ]
117
128
fn clone ( & self ) -> Self {
118
129
ParValues { map : self . map }
119
130
}
120
131
}
121
132
122
- impl < K : Eq + Hash , V : fmt:: Debug , S : BuildHasher > fmt:: Debug for ParValues < ' _ , K , V , S > {
133
+ impl < K : Eq + Hash , V : fmt:: Debug , S : BuildHasher , A : AllocRef + Clone > fmt:: Debug
134
+ for ParValues < ' _ , K , V , S , A >
135
+ {
123
136
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
124
137
self . map . values ( ) . fmt ( f)
125
138
}
@@ -134,11 +147,13 @@ impl<K: Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for ParValues<'_, K
134
147
/// [`par_iter_mut`]: /hashbrown/struct.HashMap.html#method.par_iter_mut
135
148
/// [`HashMap`]: /hashbrown/struct.HashMap.html
136
149
/// [`IntoParallelRefMutIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelRefMutIterator.html
137
- pub struct ParIterMut < ' a , K , V , S > {
138
- map : & ' a mut HashMap < K , V , S > ,
150
+ pub struct ParIterMut < ' a , K , V , S , A : AllocRef + Clone > {
151
+ map : & ' a mut HashMap < K , V , S , A > ,
139
152
}
140
153
141
- impl < ' a , K : Send + Sync , V : Send , S : Send > ParallelIterator for ParIterMut < ' a , K , V , S > {
154
+ impl < ' a , K : Send + Sync , V : Send , S : Send , A : AllocRef + Clone + Sync > ParallelIterator
155
+ for ParIterMut < ' a , K , V , S , A >
156
+ {
142
157
type Item = ( & ' a K , & ' a mut V ) ;
143
158
144
159
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -155,8 +170,8 @@ impl<'a, K: Send + Sync, V: Send, S: Send> ParallelIterator for ParIterMut<'a, K
155
170
}
156
171
}
157
172
158
- impl < K : fmt:: Debug + Eq + Hash , V : fmt:: Debug , S : BuildHasher > fmt:: Debug
159
- for ParIterMut < ' _ , K , V , S >
173
+ impl < K : fmt:: Debug + Eq + Hash , V : fmt:: Debug , S : BuildHasher , A : AllocRef + Clone > fmt:: Debug
174
+ for ParIterMut < ' _ , K , V , S , A >
160
175
{
161
176
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
162
177
self . map . iter ( ) . fmt ( f)
@@ -170,11 +185,13 @@ impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug
170
185
///
171
186
/// [`par_values_mut`]: /hashbrown/struct.HashMap.html#method.par_values_mut
172
187
/// [`HashMap`]: /hashbrown/struct.HashMap.html
173
- pub struct ParValuesMut < ' a , K , V , S > {
174
- map : & ' a mut HashMap < K , V , S > ,
188
+ pub struct ParValuesMut < ' a , K , V , S , A : AllocRef + Clone = Global > {
189
+ map : & ' a mut HashMap < K , V , S , A > ,
175
190
}
176
191
177
- impl < ' a , K : Send , V : Send , S : Send > ParallelIterator for ParValuesMut < ' a , K , V , S > {
192
+ impl < ' a , K : Send , V : Send , S : Send , A : AllocRef + Clone + Send > ParallelIterator
193
+ for ParValuesMut < ' a , K , V , S , A >
194
+ {
178
195
type Item = & ' a mut V ;
179
196
180
197
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -188,7 +205,9 @@ impl<'a, K: Send, V: Send, S: Send> ParallelIterator for ParValuesMut<'a, K, V,
188
205
}
189
206
}
190
207
191
- impl < K : Eq + Hash , V : fmt:: Debug , S : BuildHasher > fmt:: Debug for ParValuesMut < ' _ , K , V , S > {
208
+ impl < K : Eq + Hash , V : fmt:: Debug , S : BuildHasher , A : AllocRef + Clone > fmt:: Debug
209
+ for ParValuesMut < ' _ , K , V , S , A >
210
+ {
192
211
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
193
212
self . map . values ( ) . fmt ( f)
194
213
}
@@ -203,11 +222,13 @@ impl<K: Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for ParValuesMut<'_
203
222
/// [`into_par_iter`]: /hashbrown/struct.HashMap.html#method.into_par_iter
204
223
/// [`HashMap`]: /hashbrown/struct.HashMap.html
205
224
/// [`IntoParallelIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelIterator.html
206
- pub struct IntoParIter < K , V , S > {
207
- map : HashMap < K , V , S > ,
225
+ pub struct IntoParIter < K , V , S , A : AllocRef + Clone = Global > {
226
+ map : HashMap < K , V , S , A > ,
208
227
}
209
228
210
- impl < K : Send , V : Send , S : Send > ParallelIterator for IntoParIter < K , V , S > {
229
+ impl < K : Send , V : Send , S : Send , A : AllocRef + Clone + Send > ParallelIterator
230
+ for IntoParIter < K , V , S , A >
231
+ {
211
232
type Item = ( K , V ) ;
212
233
213
234
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -219,7 +240,9 @@ impl<K: Send, V: Send, S: Send> ParallelIterator for IntoParIter<K, V, S> {
219
240
}
220
241
}
221
242
222
- impl < K : fmt:: Debug + Eq + Hash , V : fmt:: Debug , S : BuildHasher > fmt:: Debug for IntoParIter < K , V , S > {
243
+ impl < K : fmt:: Debug + Eq + Hash , V : fmt:: Debug , S : BuildHasher , A : AllocRef + Clone > fmt:: Debug
244
+ for IntoParIter < K , V , S , A >
245
+ {
223
246
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
224
247
self . map . iter ( ) . fmt ( f)
225
248
}
@@ -232,11 +255,13 @@ impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for In
232
255
///
233
256
/// [`par_drain`]: /hashbrown/struct.HashMap.html#method.par_drain
234
257
/// [`HashMap`]: /hashbrown/struct.HashMap.html
235
- pub struct ParDrain < ' a , K , V , S > {
236
- map : & ' a mut HashMap < K , V , S > ,
258
+ pub struct ParDrain < ' a , K , V , S , A : AllocRef + Clone = Global > {
259
+ map : & ' a mut HashMap < K , V , S , A > ,
237
260
}
238
261
239
- impl < K : Send , V : Send , S : Send > ParallelIterator for ParDrain < ' _ , K , V , S > {
262
+ impl < K : Send , V : Send , S : Send , A : AllocRef + Clone + Sync > ParallelIterator
263
+ for ParDrain < ' _ , K , V , S , A >
264
+ {
240
265
type Item = ( K , V ) ;
241
266
242
267
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -248,48 +273,49 @@ impl<K: Send, V: Send, S: Send> ParallelIterator for ParDrain<'_, K, V, S> {
248
273
}
249
274
}
250
275
251
- impl < K : fmt:: Debug + Eq + Hash , V : fmt:: Debug , S : BuildHasher > fmt:: Debug
252
- for ParDrain < ' _ , K , V , S >
276
+ impl < K : fmt:: Debug + Eq + Hash , V : fmt:: Debug , S : BuildHasher , A : AllocRef + Clone > fmt:: Debug
277
+ for ParDrain < ' _ , K , V , S , A >
253
278
{
254
279
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
255
280
self . map . iter ( ) . fmt ( f)
256
281
}
257
282
}
258
283
259
- impl < K : Sync , V : Sync , S : Sync > HashMap < K , V , S > {
284
+ impl < K : Sync , V : Sync , S : Sync , A : AllocRef + Clone + Sync > HashMap < K , V , S , A > {
260
285
/// Visits (potentially in parallel) immutably borrowed keys in an arbitrary order.
261
286
#[ cfg_attr( feature = "inline-more" , inline) ]
262
- pub fn par_keys ( & self ) -> ParKeys < ' _ , K , V , S > {
287
+ pub fn par_keys ( & self ) -> ParKeys < ' _ , K , V , S , A > {
263
288
ParKeys { map : self }
264
289
}
265
290
266
291
/// Visits (potentially in parallel) immutably borrowed values in an arbitrary order.
267
292
#[ cfg_attr( feature = "inline-more" , inline) ]
268
- pub fn par_values ( & self ) -> ParValues < ' _ , K , V , S > {
293
+ pub fn par_values ( & self ) -> ParValues < ' _ , K , V , S , A > {
269
294
ParValues { map : self }
270
295
}
271
296
}
272
297
273
- impl < K : Send , V : Send , S : Send > HashMap < K , V , S > {
298
+ impl < K : Send , V : Send , S : Send , A : AllocRef + Clone + Sync > HashMap < K , V , S , A > {
274
299
/// Visits (potentially in parallel) mutably borrowed values in an arbitrary order.
275
300
#[ cfg_attr( feature = "inline-more" , inline) ]
276
- pub fn par_values_mut ( & mut self ) -> ParValuesMut < ' _ , K , V , S > {
301
+ pub fn par_values_mut ( & mut self ) -> ParValuesMut < ' _ , K , V , S , A > {
277
302
ParValuesMut { map : self }
278
303
}
279
304
280
305
/// Consumes (potentially in parallel) all values in an arbitrary order,
281
306
/// while preserving the map's allocated memory for reuse.
282
307
#[ cfg_attr( feature = "inline-more" , inline) ]
283
- pub fn par_drain ( & mut self ) -> ParDrain < ' _ , K , V , S > {
308
+ pub fn par_drain ( & mut self ) -> ParDrain < ' _ , K , V , S , A > {
284
309
ParDrain { map : self }
285
310
}
286
311
}
287
312
288
- impl < K , V , S > HashMap < K , V , S >
313
+ impl < K , V , S , A > HashMap < K , V , S , A >
289
314
where
290
315
K : Eq + Hash + Sync ,
291
316
V : PartialEq + Sync ,
292
317
S : BuildHasher + Sync ,
318
+ A : AllocRef + Clone + Sync ,
293
319
{
294
320
/// Returns `true` if the map is equal to another,
295
321
/// i.e. both maps contain the same keys mapped to the same values.
@@ -303,29 +329,35 @@ where
303
329
}
304
330
}
305
331
306
- impl < K : Send , V : Send , S : Send > IntoParallelIterator for HashMap < K , V , S > {
332
+ impl < K : Send , V : Send , S : Send , A : AllocRef + Clone + Send > IntoParallelIterator
333
+ for HashMap < K , V , S , A >
334
+ {
307
335
type Item = ( K , V ) ;
308
- type Iter = IntoParIter < K , V , S > ;
336
+ type Iter = IntoParIter < K , V , S , A > ;
309
337
310
338
#[ cfg_attr( feature = "inline-more" , inline) ]
311
339
fn into_par_iter ( self ) -> Self :: Iter {
312
340
IntoParIter { map : self }
313
341
}
314
342
}
315
343
316
- impl < ' a , K : Sync , V : Sync , S : Sync > IntoParallelIterator for & ' a HashMap < K , V , S > {
344
+ impl < ' a , K : Sync , V : Sync , S : Sync , A : AllocRef + Clone + Sync > IntoParallelIterator
345
+ for & ' a HashMap < K , V , S , A >
346
+ {
317
347
type Item = ( & ' a K , & ' a V ) ;
318
- type Iter = ParIter < ' a , K , V , S > ;
348
+ type Iter = ParIter < ' a , K , V , S , A > ;
319
349
320
350
#[ cfg_attr( feature = "inline-more" , inline) ]
321
351
fn into_par_iter ( self ) -> Self :: Iter {
322
352
ParIter { map : self }
323
353
}
324
354
}
325
355
326
- impl < ' a , K : Send + Sync , V : Send , S : Send > IntoParallelIterator for & ' a mut HashMap < K , V , S > {
356
+ impl < ' a , K : Send + Sync , V : Send , S : Send , A : AllocRef + Clone + Sync > IntoParallelIterator
357
+ for & ' a mut HashMap < K , V , S , A >
358
+ {
327
359
type Item = ( & ' a K , & ' a mut V ) ;
328
- type Iter = ParIterMut < ' a , K , V , S > ;
360
+ type Iter = ParIterMut < ' a , K , V , S , A > ;
329
361
330
362
#[ cfg_attr( feature = "inline-more" , inline) ]
331
363
fn into_par_iter ( self ) -> Self :: Iter {
@@ -337,7 +369,7 @@ impl<'a, K: Send + Sync, V: Send, S: Send> IntoParallelIterator for &'a mut Hash
337
369
/// hashmap. If multiple pairs correspond to the same key, then the
338
370
/// ones produced earlier in the parallel iterator will be
339
371
/// overwritten, just as with a sequential iterator.
340
- impl < K , V , S > FromParallelIterator < ( K , V ) > for HashMap < K , V , S >
372
+ impl < K , V , S > FromParallelIterator < ( K , V ) > for HashMap < K , V , S , Global >
341
373
where
342
374
K : Eq + Hash + Send ,
343
375
V : Send ,
@@ -354,11 +386,12 @@ where
354
386
}
355
387
356
388
/// Extend a hash map with items from a parallel iterator.
357
- impl < K , V , S > ParallelExtend < ( K , V ) > for HashMap < K , V , S >
389
+ impl < K , V , S , A > ParallelExtend < ( K , V ) > for HashMap < K , V , S , A >
358
390
where
359
391
K : Eq + Hash + Send ,
360
392
V : Send ,
361
393
S : BuildHasher ,
394
+ A : AllocRef + Clone ,
362
395
{
363
396
fn par_extend < I > ( & mut self , par_iter : I )
364
397
where
@@ -369,11 +402,12 @@ where
369
402
}
370
403
371
404
/// Extend a hash map with copied items from a parallel iterator.
372
- impl < ' a , K , V , S > ParallelExtend < ( & ' a K , & ' a V ) > for HashMap < K , V , S >
405
+ impl < ' a , K , V , S , A > ParallelExtend < ( & ' a K , & ' a V ) > for HashMap < K , V , S , A >
373
406
where
374
407
K : Copy + Eq + Hash + Sync ,
375
408
V : Copy + Sync ,
376
409
S : BuildHasher ,
410
+ A : AllocRef + Clone ,
377
411
{
378
412
fn par_extend < I > ( & mut self , par_iter : I )
379
413
where
@@ -384,12 +418,13 @@ where
384
418
}
385
419
386
420
// This is equal to the normal `HashMap` -- no custom advantage.
387
- fn extend < K , V , S , I > ( map : & mut HashMap < K , V , S > , par_iter : I )
421
+ fn extend < K , V , S , A , I > ( map : & mut HashMap < K , V , S , A > , par_iter : I )
388
422
where
389
423
K : Eq + Hash ,
390
424
S : BuildHasher ,
391
425
I : IntoParallelIterator ,
392
- HashMap < K , V , S > : Extend < I :: Item > ,
426
+ A : AllocRef + Clone ,
427
+ HashMap < K , V , S , A > : Extend < I :: Item > ,
393
428
{
394
429
let ( list, len) = super :: helpers:: collect ( par_iter) ;
395
430
0 commit comments