11//! Rayon extensions for `HashMap`.
22
33use crate :: hash_map:: HashMap ;
4+ use crate :: raw:: { AllocRef , Global } ;
45use core:: fmt;
56use core:: hash:: { BuildHasher , Hash } ;
67use rayon:: iter:: plumbing:: UnindexedConsumer ;
@@ -15,11 +16,13 @@ use rayon::iter::{FromParallelIterator, IntoParallelIterator, ParallelExtend, Pa
1516/// [`par_iter`]: /hashbrown/struct.HashMap.html#method.par_iter
1617/// [`HashMap`]: /hashbrown/struct.HashMap.html
1718/// [`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 > ,
2021}
2122
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+ {
2326 type Item = ( & ' a K , & ' a V ) ;
2427
2528 #[ 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> {
3639 }
3740}
3841
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 > {
4043 #[ cfg_attr( feature = "inline-more" , inline) ]
4144 fn clone ( & self ) -> Self {
4245 ParIter { map : self . map }
4346 }
4447}
4548
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+ {
4752 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
4853 self . map . iter ( ) . fmt ( f)
4954 }
@@ -56,11 +61,13 @@ impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for Pa
5661///
5762/// [`par_keys`]: /hashbrown/struct.HashMap.html#method.par_keys
5863/// [`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 > ,
6166}
6267
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+ {
6471 type Item = & ' a K ;
6572
6673 #[ 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> {
7481 }
7582}
7683
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 > {
7885 #[ cfg_attr( feature = "inline-more" , inline) ]
7986 fn clone ( & self ) -> Self {
8087 ParKeys { map : self . map }
8188 }
8289}
8390
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+ {
8594 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
8695 self . map . keys ( ) . fmt ( f)
8796 }
@@ -94,11 +103,13 @@ impl<K: fmt::Debug + Eq + Hash, V, S: BuildHasher> fmt::Debug for ParKeys<'_, K,
94103///
95104/// [`par_values`]: /hashbrown/struct.HashMap.html#method.par_values
96105/// [`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 > ,
99108}
100109
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+ {
102113 type Item = & ' a V ;
103114
104115 #[ 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>
112123 }
113124}
114125
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 > {
116127 #[ cfg_attr( feature = "inline-more" , inline) ]
117128 fn clone ( & self ) -> Self {
118129 ParValues { map : self . map }
119130 }
120131}
121132
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+ {
123136 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
124137 self . map . values ( ) . fmt ( f)
125138 }
@@ -134,11 +147,13 @@ impl<K: Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for ParValues<'_, K
134147/// [`par_iter_mut`]: /hashbrown/struct.HashMap.html#method.par_iter_mut
135148/// [`HashMap`]: /hashbrown/struct.HashMap.html
136149/// [`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 > ,
139152}
140153
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+ {
142157 type Item = ( & ' a K , & ' a mut V ) ;
143158
144159 #[ cfg_attr( feature = "inline-more" , inline) ]
@@ -155,8 +170,8 @@ impl<'a, K: Send + Sync, V: Send, S: Send> ParallelIterator for ParIterMut<'a, K
155170 }
156171}
157172
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 >
160175{
161176 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
162177 self . map . iter ( ) . fmt ( f)
@@ -170,11 +185,13 @@ impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug
170185///
171186/// [`par_values_mut`]: /hashbrown/struct.HashMap.html#method.par_values_mut
172187/// [`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 > ,
175190}
176191
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+ {
178195 type Item = & ' a mut V ;
179196
180197 #[ cfg_attr( feature = "inline-more" , inline) ]
@@ -188,7 +205,9 @@ impl<'a, K: Send, V: Send, S: Send> ParallelIterator for ParValuesMut<'a, K, V,
188205 }
189206}
190207
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+ {
192211 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
193212 self . map . values ( ) . fmt ( f)
194213 }
@@ -203,11 +222,13 @@ impl<K: Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for ParValuesMut<'_
203222/// [`into_par_iter`]: /hashbrown/struct.HashMap.html#method.into_par_iter
204223/// [`HashMap`]: /hashbrown/struct.HashMap.html
205224/// [`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 > ,
208227}
209228
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+ {
211232 type Item = ( K , V ) ;
212233
213234 #[ cfg_attr( feature = "inline-more" , inline) ]
@@ -219,7 +240,9 @@ impl<K: Send, V: Send, S: Send> ParallelIterator for IntoParIter<K, V, S> {
219240 }
220241}
221242
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+ {
223246 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
224247 self . map . iter ( ) . fmt ( f)
225248 }
@@ -232,11 +255,13 @@ impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for In
232255///
233256/// [`par_drain`]: /hashbrown/struct.HashMap.html#method.par_drain
234257/// [`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 > ,
237260}
238261
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+ {
240265 type Item = ( K , V ) ;
241266
242267 #[ cfg_attr( feature = "inline-more" , inline) ]
@@ -248,48 +273,49 @@ impl<K: Send, V: Send, S: Send> ParallelIterator for ParDrain<'_, K, V, S> {
248273 }
249274}
250275
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 >
253278{
254279 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
255280 self . map . iter ( ) . fmt ( f)
256281 }
257282}
258283
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 > {
260285 /// Visits (potentially in parallel) immutably borrowed keys in an arbitrary order.
261286 #[ 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 > {
263288 ParKeys { map : self }
264289 }
265290
266291 /// Visits (potentially in parallel) immutably borrowed values in an arbitrary order.
267292 #[ 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 > {
269294 ParValues { map : self }
270295 }
271296}
272297
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 > {
274299 /// Visits (potentially in parallel) mutably borrowed values in an arbitrary order.
275300 #[ 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 > {
277302 ParValuesMut { map : self }
278303 }
279304
280305 /// Consumes (potentially in parallel) all values in an arbitrary order,
281306 /// while preserving the map's allocated memory for reuse.
282307 #[ 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 > {
284309 ParDrain { map : self }
285310 }
286311}
287312
288- impl < K , V , S > HashMap < K , V , S >
313+ impl < K , V , S , A > HashMap < K , V , S , A >
289314where
290315 K : Eq + Hash + Sync ,
291316 V : PartialEq + Sync ,
292317 S : BuildHasher + Sync ,
318+ A : AllocRef + Clone + Sync ,
293319{
294320 /// Returns `true` if the map is equal to another,
295321 /// i.e. both maps contain the same keys mapped to the same values.
@@ -303,29 +329,35 @@ where
303329 }
304330}
305331
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+ {
307335 type Item = ( K , V ) ;
308- type Iter = IntoParIter < K , V , S > ;
336+ type Iter = IntoParIter < K , V , S , A > ;
309337
310338 #[ cfg_attr( feature = "inline-more" , inline) ]
311339 fn into_par_iter ( self ) -> Self :: Iter {
312340 IntoParIter { map : self }
313341 }
314342}
315343
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+ {
317347 type Item = ( & ' a K , & ' a V ) ;
318- type Iter = ParIter < ' a , K , V , S > ;
348+ type Iter = ParIter < ' a , K , V , S , A > ;
319349
320350 #[ cfg_attr( feature = "inline-more" , inline) ]
321351 fn into_par_iter ( self ) -> Self :: Iter {
322352 ParIter { map : self }
323353 }
324354}
325355
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+ {
327359 type Item = ( & ' a K , & ' a mut V ) ;
328- type Iter = ParIterMut < ' a , K , V , S > ;
360+ type Iter = ParIterMut < ' a , K , V , S , A > ;
329361
330362 #[ cfg_attr( feature = "inline-more" , inline) ]
331363 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
337369/// hashmap. If multiple pairs correspond to the same key, then the
338370/// ones produced earlier in the parallel iterator will be
339371/// 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 >
341373where
342374 K : Eq + Hash + Send ,
343375 V : Send ,
@@ -354,11 +386,12 @@ where
354386}
355387
356388/// 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 >
358390where
359391 K : Eq + Hash + Send ,
360392 V : Send ,
361393 S : BuildHasher ,
394+ A : AllocRef + Clone ,
362395{
363396 fn par_extend < I > ( & mut self , par_iter : I )
364397 where
@@ -369,11 +402,12 @@ where
369402}
370403
371404/// 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 >
373406where
374407 K : Copy + Eq + Hash + Sync ,
375408 V : Copy + Sync ,
376409 S : BuildHasher ,
410+ A : AllocRef + Clone ,
377411{
378412 fn par_extend < I > ( & mut self , par_iter : I )
379413 where
@@ -384,12 +418,13 @@ where
384418}
385419
386420// 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 )
388422where
389423 K : Eq + Hash ,
390424 S : BuildHasher ,
391425 I : IntoParallelIterator ,
392- HashMap < K , V , S > : Extend < I :: Item > ,
426+ A : AllocRef + Clone ,
427+ HashMap < K , V , S , A > : Extend < I :: Item > ,
393428{
394429 let ( list, len) = super :: helpers:: collect ( par_iter) ;
395430
0 commit comments