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,11 @@ 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 for ParIter < ' a , K , V , S , A > {
23
24
type Item = ( & ' a K , & ' a V ) ;
24
25
25
26
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -38,14 +39,14 @@ impl<'a, K: Sync, V: Sync, S: Sync> ParallelIterator for ParIter<'a, K, V, S> {
38
39
}
39
40
}
40
41
41
- 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 > {
42
43
#[ cfg_attr( feature = "inline-more" , inline) ]
43
44
fn clone ( & self ) -> Self {
44
45
ParIter { map : self . map }
45
46
}
46
47
}
47
48
48
- 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 for ParIter < ' _ , K , V , S , A > {
49
50
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
50
51
self . map . iter ( ) . fmt ( f)
51
52
}
@@ -58,11 +59,11 @@ impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for Pa
58
59
///
59
60
/// [`par_keys`]: /hashbrown/struct.HashMap.html#method.par_keys
60
61
/// [`HashMap`]: /hashbrown/struct.HashMap.html
61
- pub struct ParKeys < ' a , K , V , S > {
62
- map : & ' a HashMap < K , V , S > ,
62
+ pub struct ParKeys < ' a , K , V , S , A : AllocRef + Clone > {
63
+ map : & ' a HashMap < K , V , S , A > ,
63
64
}
64
65
65
- impl < ' a , K : Sync , V : Sync , S : Sync > ParallelIterator for ParKeys < ' a , K , V , S > {
66
+ impl < ' a , K : Sync , V : Sync , S : Sync , A : AllocRef + Clone + Sync > ParallelIterator for ParKeys < ' a , K , V , S , A > {
66
67
type Item = & ' a K ;
67
68
68
69
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -78,14 +79,14 @@ impl<'a, K: Sync, V: Sync, S: Sync> ParallelIterator for ParKeys<'a, K, V, S> {
78
79
}
79
80
}
80
81
81
- impl < K , V , S > Clone for ParKeys < ' _ , K , V , S > {
82
+ impl < K , V , S , A : AllocRef + Clone > Clone for ParKeys < ' _ , K , V , S , A > {
82
83
#[ cfg_attr( feature = "inline-more" , inline) ]
83
84
fn clone ( & self ) -> Self {
84
85
ParKeys { map : self . map }
85
86
}
86
87
}
87
88
88
- impl < K : fmt:: Debug + Eq + Hash , V , S : BuildHasher > fmt:: Debug for ParKeys < ' _ , K , V , S > {
89
+ impl < K : fmt:: Debug + Eq + Hash , V , S : BuildHasher , A : AllocRef + Clone > fmt:: Debug for ParKeys < ' _ , K , V , S , A > {
89
90
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
90
91
self . map . keys ( ) . fmt ( f)
91
92
}
@@ -98,11 +99,11 @@ impl<K: fmt::Debug + Eq + Hash, V, S: BuildHasher> fmt::Debug for ParKeys<'_, K,
98
99
///
99
100
/// [`par_values`]: /hashbrown/struct.HashMap.html#method.par_values
100
101
/// [`HashMap`]: /hashbrown/struct.HashMap.html
101
- pub struct ParValues < ' a , K , V , S > {
102
- map : & ' a HashMap < K , V , S > ,
102
+ pub struct ParValues < ' a , K , V , S , A : AllocRef + Clone = Global > {
103
+ map : & ' a HashMap < K , V , S , A > ,
103
104
}
104
105
105
- impl < ' a , K : Sync , V : Sync , S : Sync > ParallelIterator for ParValues < ' a , K , V , S > {
106
+ impl < ' a , K : Sync , V : Sync , S : Sync , A : AllocRef + Clone + Sync > ParallelIterator for ParValues < ' a , K , V , S , A > {
106
107
type Item = & ' a V ;
107
108
108
109
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -118,14 +119,14 @@ impl<'a, K: Sync, V: Sync, S: Sync> ParallelIterator for ParValues<'a, K, V, S>
118
119
}
119
120
}
120
121
121
- impl < K , V , S > Clone for ParValues < ' _ , K , V , S > {
122
+ impl < K , V , S , A : AllocRef + Clone > Clone for ParValues < ' _ , K , V , S , A > {
122
123
#[ cfg_attr( feature = "inline-more" , inline) ]
123
124
fn clone ( & self ) -> Self {
124
125
ParValues { map : self . map }
125
126
}
126
127
}
127
128
128
- impl < K : Eq + Hash , V : fmt:: Debug , S : BuildHasher > fmt:: Debug for ParValues < ' _ , K , V , S > {
129
+ impl < K : Eq + Hash , V : fmt:: Debug , S : BuildHasher , A : AllocRef + Clone > fmt:: Debug for ParValues < ' _ , K , V , S , A > {
129
130
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
130
131
self . map . values ( ) . fmt ( f)
131
132
}
@@ -140,11 +141,11 @@ impl<K: Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for ParValues<'_, K
140
141
/// [`par_iter_mut`]: /hashbrown/struct.HashMap.html#method.par_iter_mut
141
142
/// [`HashMap`]: /hashbrown/struct.HashMap.html
142
143
/// [`IntoParallelRefMutIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelRefMutIterator.html
143
- pub struct ParIterMut < ' a , K , V , S > {
144
- map : & ' a mut HashMap < K , V , S > ,
144
+ pub struct ParIterMut < ' a , K , V , S , A : AllocRef + Clone > {
145
+ map : & ' a mut HashMap < K , V , S , A > ,
145
146
}
146
147
147
- impl < ' a , K : Send + Sync , V : Send , S : Send > ParallelIterator for ParIterMut < ' a , K , V , S > {
148
+ impl < ' a , K : Send + Sync , V : Send , S : Send , A : AllocRef + Clone + Sync > ParallelIterator for ParIterMut < ' a , K , V , S , A > {
148
149
type Item = ( & ' a K , & ' a mut V ) ;
149
150
150
151
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -163,8 +164,8 @@ impl<'a, K: Send + Sync, V: Send, S: Send> ParallelIterator for ParIterMut<'a, K
163
164
}
164
165
}
165
166
166
- impl < K : fmt:: Debug + Eq + Hash , V : fmt:: Debug , S : BuildHasher > fmt:: Debug
167
- for ParIterMut < ' _ , K , V , S >
167
+ impl < K : fmt:: Debug + Eq + Hash , V : fmt:: Debug , S : BuildHasher , A : AllocRef + Clone > fmt:: Debug
168
+ for ParIterMut < ' _ , K , V , S , A >
168
169
{
169
170
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
170
171
self . map . iter ( ) . fmt ( f)
@@ -178,11 +179,11 @@ impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug
178
179
///
179
180
/// [`par_values_mut`]: /hashbrown/struct.HashMap.html#method.par_values_mut
180
181
/// [`HashMap`]: /hashbrown/struct.HashMap.html
181
- pub struct ParValuesMut < ' a , K , V , S > {
182
- map : & ' a mut HashMap < K , V , S > ,
182
+ pub struct ParValuesMut < ' a , K , V , S , A : AllocRef + Clone = Global > {
183
+ map : & ' a mut HashMap < K , V , S , A > ,
183
184
}
184
185
185
- impl < ' a , K : Send , V : Send , S : Send > ParallelIterator for ParValuesMut < ' a , K , V , S > {
186
+ impl < ' a , K : Send , V : Send , S : Send , A : AllocRef + Clone + Send > ParallelIterator for ParValuesMut < ' a , K , V , S , A > {
186
187
type Item = & ' a mut V ;
187
188
188
189
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -198,7 +199,7 @@ impl<'a, K: Send, V: Send, S: Send> ParallelIterator for ParValuesMut<'a, K, V,
198
199
}
199
200
}
200
201
201
- impl < K : Eq + Hash , V : fmt:: Debug , S : BuildHasher > fmt:: Debug for ParValuesMut < ' _ , K , V , S > {
202
+ impl < K : Eq + Hash , V : fmt:: Debug , S : BuildHasher , A : AllocRef + Clone > fmt:: Debug for ParValuesMut < ' _ , K , V , S , A > {
202
203
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
203
204
self . map . values ( ) . fmt ( f)
204
205
}
@@ -213,11 +214,11 @@ impl<K: Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for ParValuesMut<'_
213
214
/// [`into_par_iter`]: /hashbrown/struct.HashMap.html#method.into_par_iter
214
215
/// [`HashMap`]: /hashbrown/struct.HashMap.html
215
216
/// [`IntoParallelIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelIterator.html
216
- pub struct IntoParIter < K , V , S > {
217
- map : HashMap < K , V , S > ,
217
+ pub struct IntoParIter < K , V , S , A : AllocRef + Clone = Global > {
218
+ map : HashMap < K , V , S , A > ,
218
219
}
219
220
220
- impl < K : Send , V : Send , S : Send > ParallelIterator for IntoParIter < K , V , S > {
221
+ impl < K : Send , V : Send , S : Send , A : AllocRef + Clone + Send > ParallelIterator for IntoParIter < K , V , S , A > {
221
222
type Item = ( K , V ) ;
222
223
223
224
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -229,7 +230,7 @@ impl<K: Send, V: Send, S: Send> ParallelIterator for IntoParIter<K, V, S> {
229
230
}
230
231
}
231
232
232
- impl < K : fmt:: Debug + Eq + Hash , V : fmt:: Debug , S : BuildHasher > fmt:: Debug for IntoParIter < K , V , S > {
233
+ impl < K : fmt:: Debug + Eq + Hash , V : fmt:: Debug , S : BuildHasher , A : AllocRef + Clone > fmt:: Debug for IntoParIter < K , V , S , A > {
233
234
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
234
235
self . map . iter ( ) . fmt ( f)
235
236
}
@@ -242,11 +243,11 @@ impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for In
242
243
///
243
244
/// [`par_drain`]: /hashbrown/struct.HashMap.html#method.par_drain
244
245
/// [`HashMap`]: /hashbrown/struct.HashMap.html
245
- pub struct ParDrain < ' a , K , V , S > {
246
- map : & ' a mut HashMap < K , V , S > ,
246
+ pub struct ParDrain < ' a , K , V , S , A : AllocRef + Clone = Global > {
247
+ map : & ' a mut HashMap < K , V , S , A > ,
247
248
}
248
249
249
- impl < K : Send , V : Send , S : Send > ParallelIterator for ParDrain < ' _ , K , V , S > {
250
+ impl < K : Send , V : Send , S : Send , A : AllocRef + Clone + Sync > ParallelIterator for ParDrain < ' _ , K , V , S , A > {
250
251
type Item = ( K , V ) ;
251
252
252
253
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -258,48 +259,49 @@ impl<K: Send, V: Send, S: Send> ParallelIterator for ParDrain<'_, K, V, S> {
258
259
}
259
260
}
260
261
261
- impl < K : fmt:: Debug + Eq + Hash , V : fmt:: Debug , S : BuildHasher > fmt:: Debug
262
- for ParDrain < ' _ , K , V , S >
262
+ impl < K : fmt:: Debug + Eq + Hash , V : fmt:: Debug , S : BuildHasher , A : AllocRef + Clone > fmt:: Debug
263
+ for ParDrain < ' _ , K , V , S , A >
263
264
{
264
265
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
265
266
self . map . iter ( ) . fmt ( f)
266
267
}
267
268
}
268
269
269
- impl < K : Sync , V : Sync , S : Sync > HashMap < K , V , S > {
270
+ impl < K : Sync , V : Sync , S : Sync , A : AllocRef + Clone + Sync > HashMap < K , V , S , A > {
270
271
/// Visits (potentially in parallel) immutably borrowed keys in an arbitrary order.
271
272
#[ cfg_attr( feature = "inline-more" , inline) ]
272
- pub fn par_keys ( & self ) -> ParKeys < ' _ , K , V , S > {
273
+ pub fn par_keys ( & self ) -> ParKeys < ' _ , K , V , S , A > {
273
274
ParKeys { map : self }
274
275
}
275
276
276
277
/// Visits (potentially in parallel) immutably borrowed values in an arbitrary order.
277
278
#[ cfg_attr( feature = "inline-more" , inline) ]
278
- pub fn par_values ( & self ) -> ParValues < ' _ , K , V , S > {
279
+ pub fn par_values ( & self ) -> ParValues < ' _ , K , V , S , A > {
279
280
ParValues { map : self }
280
281
}
281
282
}
282
283
283
- impl < K : Send , V : Send , S : Send > HashMap < K , V , S > {
284
+ impl < K : Send , V : Send , S : Send , A : AllocRef + Clone + Sync > HashMap < K , V , S , A > {
284
285
/// Visits (potentially in parallel) mutably borrowed values in an arbitrary order.
285
286
#[ cfg_attr( feature = "inline-more" , inline) ]
286
- pub fn par_values_mut ( & mut self ) -> ParValuesMut < ' _ , K , V , S > {
287
+ pub fn par_values_mut ( & mut self ) -> ParValuesMut < ' _ , K , V , S , A > {
287
288
ParValuesMut { map : self }
288
289
}
289
290
290
291
/// Consumes (potentially in parallel) all values in an arbitrary order,
291
292
/// while preserving the map's allocated memory for reuse.
292
293
#[ cfg_attr( feature = "inline-more" , inline) ]
293
- pub fn par_drain ( & mut self ) -> ParDrain < ' _ , K , V , S > {
294
+ pub fn par_drain ( & mut self ) -> ParDrain < ' _ , K , V , S , A > {
294
295
ParDrain { map : self }
295
296
}
296
297
}
297
298
298
- impl < K , V , S > HashMap < K , V , S >
299
+ impl < K , V , S , A > HashMap < K , V , S , A >
299
300
where
300
301
K : Eq + Hash + Sync ,
301
302
V : PartialEq + Sync ,
302
303
S : BuildHasher + Sync ,
304
+ A : AllocRef + Clone + Sync ,
303
305
{
304
306
/// Returns `true` if the map is equal to another,
305
307
/// i.e. both maps contain the same keys mapped to the same values.
@@ -313,29 +315,29 @@ where
313
315
}
314
316
}
315
317
316
- impl < K : Send , V : Send , S : Send > IntoParallelIterator for HashMap < K , V , S > {
318
+ impl < K : Send , V : Send , S : Send , A : AllocRef + Clone + Send > IntoParallelIterator for HashMap < K , V , S , A > {
317
319
type Item = ( K , V ) ;
318
- type Iter = IntoParIter < K , V , S > ;
320
+ type Iter = IntoParIter < K , V , S , A > ;
319
321
320
322
#[ cfg_attr( feature = "inline-more" , inline) ]
321
323
fn into_par_iter ( self ) -> Self :: Iter {
322
324
IntoParIter { map : self }
323
325
}
324
326
}
325
327
326
- impl < ' a , K : Sync , V : Sync , S : Sync > IntoParallelIterator for & ' a HashMap < K , V , S > {
328
+ impl < ' a , K : Sync , V : Sync , S : Sync , A : AllocRef + Clone + Sync > IntoParallelIterator for & ' a HashMap < K , V , S , A > {
327
329
type Item = ( & ' a K , & ' a V ) ;
328
- type Iter = ParIter < ' a , K , V , S > ;
330
+ type Iter = ParIter < ' a , K , V , S , A > ;
329
331
330
332
#[ cfg_attr( feature = "inline-more" , inline) ]
331
333
fn into_par_iter ( self ) -> Self :: Iter {
332
334
ParIter { map : self }
333
335
}
334
336
}
335
337
336
- impl < ' a , K : Send + Sync , V : Send , S : Send > IntoParallelIterator for & ' a mut HashMap < K , V , S > {
338
+ impl < ' a , K : Send + Sync , V : Send , S : Send , A : AllocRef + Clone + Sync > IntoParallelIterator for & ' a mut HashMap < K , V , S , A > {
337
339
type Item = ( & ' a K , & ' a mut V ) ;
338
- type Iter = ParIterMut < ' a , K , V , S > ;
340
+ type Iter = ParIterMut < ' a , K , V , S , A > ;
339
341
340
342
#[ cfg_attr( feature = "inline-more" , inline) ]
341
343
fn into_par_iter ( self ) -> Self :: Iter {
@@ -347,7 +349,7 @@ impl<'a, K: Send + Sync, V: Send, S: Send> IntoParallelIterator for &'a mut Hash
347
349
/// hashmap. If multiple pairs correspond to the same key, then the
348
350
/// ones produced earlier in the parallel iterator will be
349
351
/// overwritten, just as with a sequential iterator.
350
- impl < K , V , S > FromParallelIterator < ( K , V ) > for HashMap < K , V , S >
352
+ impl < K , V , S > FromParallelIterator < ( K , V ) > for HashMap < K , V , S , Global >
351
353
where
352
354
K : Eq + Hash + Send ,
353
355
V : Send ,
@@ -364,11 +366,12 @@ where
364
366
}
365
367
366
368
/// Extend a hash map with items from a parallel iterator.
367
- impl < K , V , S > ParallelExtend < ( K , V ) > for HashMap < K , V , S >
369
+ impl < K , V , S , A > ParallelExtend < ( K , V ) > for HashMap < K , V , S , A >
368
370
where
369
371
K : Eq + Hash + Send ,
370
372
V : Send ,
371
373
S : BuildHasher ,
374
+ A : AllocRef + Clone ,
372
375
{
373
376
fn par_extend < I > ( & mut self , par_iter : I )
374
377
where
@@ -379,11 +382,12 @@ where
379
382
}
380
383
381
384
/// Extend a hash map with copied items from a parallel iterator.
382
- impl < ' a , K , V , S > ParallelExtend < ( & ' a K , & ' a V ) > for HashMap < K , V , S >
385
+ impl < ' a , K , V , S , A > ParallelExtend < ( & ' a K , & ' a V ) > for HashMap < K , V , S , A >
383
386
where
384
387
K : Copy + Eq + Hash + Sync ,
385
388
V : Copy + Sync ,
386
389
S : BuildHasher ,
390
+ A : AllocRef + Clone ,
387
391
{
388
392
fn par_extend < I > ( & mut self , par_iter : I )
389
393
where
@@ -394,12 +398,13 @@ where
394
398
}
395
399
396
400
// This is equal to the normal `HashMap` -- no custom advantage.
397
- fn extend < K , V , S , I > ( map : & mut HashMap < K , V , S > , par_iter : I )
401
+ fn extend < K , V , S , A , I > ( map : & mut HashMap < K , V , S , A > , par_iter : I )
398
402
where
399
403
K : Eq + Hash ,
400
404
S : BuildHasher ,
401
405
I : IntoParallelIterator ,
402
- HashMap < K , V , S > : Extend < I :: Item > ,
406
+ A : AllocRef + Clone ,
407
+ HashMap < K , V , S , A > : Extend < I :: Item > ,
403
408
{
404
409
let ( list, len) = super :: helpers:: collect ( par_iter) ;
405
410
0 commit comments