@@ -2,11 +2,12 @@ use crate::{
2
2
adaptors:: map:: { MapSpecialCase , MapSpecialCaseFn } ,
3
3
MinMaxResult ,
4
4
} ;
5
- use std :: cmp :: Ordering ;
5
+ use core :: hash :: BuildHasher ;
6
6
use std:: collections:: HashMap ;
7
7
use std:: hash:: Hash ;
8
8
use std:: iter:: Iterator ;
9
9
use std:: ops:: { Add , Mul } ;
10
+ use std:: { cmp:: Ordering , hash:: RandomState } ;
10
11
11
12
/// A wrapper to allow for an easy [`into_grouping_map_by`](crate::Itertools::into_grouping_map_by)
12
13
pub type MapForGrouping < I , F > = MapSpecialCase < I , GroupingMapFn < F > > ;
@@ -36,18 +37,19 @@ pub(crate) fn new_map_for_grouping<K, I: Iterator, F: FnMut(&I::Item) -> K>(
36
37
}
37
38
38
39
/// Creates a new `GroupingMap` from `iter`
39
- pub fn new < I , K , V > ( iter : I ) -> GroupingMap < I >
40
+ pub fn new < I , K , V , S > ( iter : I , hash_builder : S ) -> GroupingMap < I , S >
40
41
where
41
42
I : Iterator < Item = ( K , V ) > ,
42
43
K : Hash + Eq ,
44
+ S : BuildHasher ,
43
45
{
44
- GroupingMap { iter }
46
+ GroupingMap { iter, hash_builder }
45
47
}
46
48
47
49
/// `GroupingMapBy` is an intermediate struct for efficient group-and-fold operations.
48
50
///
49
51
/// See [`GroupingMap`] for more informations.
50
- pub type GroupingMapBy < I , F > = GroupingMap < MapForGrouping < I , F > > ;
52
+ pub type GroupingMapBy < I , F , S = RandomState > = GroupingMap < MapForGrouping < I , F > , S > ;
51
53
52
54
/// `GroupingMap` is an intermediate struct for efficient group-and-fold operations.
53
55
/// It groups elements by their key and at the same time fold each group
@@ -56,14 +58,19 @@ pub type GroupingMapBy<I, F> = GroupingMap<MapForGrouping<I, F>>;
56
58
/// No method on this struct performs temporary allocations.
57
59
#[ derive( Clone , Debug ) ]
58
60
#[ must_use = "GroupingMap is lazy and do nothing unless consumed" ]
59
- pub struct GroupingMap < I > {
61
+ pub struct GroupingMap < I , S = RandomState >
62
+ where
63
+ S : BuildHasher ,
64
+ {
60
65
iter : I ,
66
+ hash_builder : S ,
61
67
}
62
68
63
- impl < I , K , V > GroupingMap < I >
69
+ impl < I , K , V , S > GroupingMap < I , S >
64
70
where
65
71
I : Iterator < Item = ( K , V ) > ,
66
72
K : Hash + Eq ,
73
+ S : BuildHasher ,
67
74
{
68
75
/// This is the generic way to perform any operation on a `GroupingMap`.
69
76
/// It's suggested to use this method only to implement custom operations
@@ -105,11 +112,11 @@ where
105
112
/// assert_eq!(lookup[&3], 7);
106
113
/// assert_eq!(lookup.len(), 3); // The final keys are only 0, 1 and 2
107
114
/// ```
108
- pub fn aggregate < FO , R > ( self , mut operation : FO ) -> HashMap < K , R >
115
+ pub fn aggregate < FO , R > ( self , mut operation : FO ) -> HashMap < K , R , S >
109
116
where
110
117
FO : FnMut ( Option < R > , & K , V ) -> Option < R > ,
111
118
{
112
- let mut destination_map = HashMap :: new ( ) ;
119
+ let mut destination_map = HashMap :: with_hasher ( self . hash_builder ) ;
113
120
114
121
self . iter . for_each ( |( key, val) | {
115
122
let acc = destination_map. remove ( & key) ;
@@ -154,7 +161,7 @@ where
154
161
/// assert_eq!(lookup[&2].acc, 2 + 5);
155
162
/// assert_eq!(lookup.len(), 3);
156
163
/// ```
157
- pub fn fold_with < FI , FO , R > ( self , mut init : FI , mut operation : FO ) -> HashMap < K , R >
164
+ pub fn fold_with < FI , FO , R > ( self , mut init : FI , mut operation : FO ) -> HashMap < K , R , S >
158
165
where
159
166
FI : FnMut ( & K , & V ) -> R ,
160
167
FO : FnMut ( R , & K , V ) -> R ,
@@ -190,7 +197,7 @@ where
190
197
/// assert_eq!(lookup[&2], 2 + 5);
191
198
/// assert_eq!(lookup.len(), 3);
192
199
/// ```
193
- pub fn fold < FO , R > ( self , init : R , operation : FO ) -> HashMap < K , R >
200
+ pub fn fold < FO , R > ( self , init : R , operation : FO ) -> HashMap < K , R , S >
194
201
where
195
202
R : Clone ,
196
203
FO : FnMut ( R , & K , V ) -> R ,
@@ -225,7 +232,7 @@ where
225
232
/// assert_eq!(lookup[&2], 2 + 5);
226
233
/// assert_eq!(lookup.len(), 3);
227
234
/// ```
228
- pub fn reduce < FO > ( self , mut operation : FO ) -> HashMap < K , V >
235
+ pub fn reduce < FO > ( self , mut operation : FO ) -> HashMap < K , V , S >
229
236
where
230
237
FO : FnMut ( V , & K , V ) -> V ,
231
238
{
@@ -239,7 +246,7 @@ where
239
246
240
247
/// See [`.reduce()`](GroupingMap::reduce).
241
248
#[ deprecated( note = "Use .reduce() instead" , since = "0.13.0" ) ]
242
- pub fn fold_first < FO > ( self , operation : FO ) -> HashMap < K , V >
249
+ pub fn fold_first < FO > ( self , operation : FO ) -> HashMap < K , V , S >
243
250
where
244
251
FO : FnMut ( V , & K , V ) -> V ,
245
252
{
@@ -264,11 +271,11 @@ where
264
271
/// assert_eq!(lookup[&2], vec![2, 5].into_iter().collect::<HashSet<_>>());
265
272
/// assert_eq!(lookup.len(), 3);
266
273
/// ```
267
- pub fn collect < C > ( self ) -> HashMap < K , C >
274
+ pub fn collect < C > ( self ) -> HashMap < K , C , S >
268
275
where
269
276
C : Default + Extend < V > ,
270
277
{
271
- let mut destination_map = HashMap :: new ( ) ;
278
+ let mut destination_map = HashMap :: with_hasher ( self . hash_builder ) ;
272
279
273
280
self . iter . for_each ( |( key, val) | {
274
281
destination_map
@@ -298,7 +305,7 @@ where
298
305
/// assert_eq!(lookup[&2], 8);
299
306
/// assert_eq!(lookup.len(), 3);
300
307
/// ```
301
- pub fn max ( self ) -> HashMap < K , V >
308
+ pub fn max ( self ) -> HashMap < K , V , S >
302
309
where
303
310
V : Ord ,
304
311
{
@@ -324,7 +331,7 @@ where
324
331
/// assert_eq!(lookup[&2], 5);
325
332
/// assert_eq!(lookup.len(), 3);
326
333
/// ```
327
- pub fn max_by < F > ( self , mut compare : F ) -> HashMap < K , V >
334
+ pub fn max_by < F > ( self , mut compare : F ) -> HashMap < K , V , S >
328
335
where
329
336
F : FnMut ( & K , & V , & V ) -> Ordering ,
330
337
{
@@ -353,7 +360,7 @@ where
353
360
/// assert_eq!(lookup[&2], 5);
354
361
/// assert_eq!(lookup.len(), 3);
355
362
/// ```
356
- pub fn max_by_key < F , CK > ( self , mut f : F ) -> HashMap < K , V >
363
+ pub fn max_by_key < F , CK > ( self , mut f : F ) -> HashMap < K , V , S >
357
364
where
358
365
F : FnMut ( & K , & V ) -> CK ,
359
366
CK : Ord ,
@@ -379,7 +386,7 @@ where
379
386
/// assert_eq!(lookup[&2], 5);
380
387
/// assert_eq!(lookup.len(), 3);
381
388
/// ```
382
- pub fn min ( self ) -> HashMap < K , V >
389
+ pub fn min ( self ) -> HashMap < K , V , S >
383
390
where
384
391
V : Ord ,
385
392
{
@@ -405,7 +412,7 @@ where
405
412
/// assert_eq!(lookup[&2], 8);
406
413
/// assert_eq!(lookup.len(), 3);
407
414
/// ```
408
- pub fn min_by < F > ( self , mut compare : F ) -> HashMap < K , V >
415
+ pub fn min_by < F > ( self , mut compare : F ) -> HashMap < K , V , S >
409
416
where
410
417
F : FnMut ( & K , & V , & V ) -> Ordering ,
411
418
{
@@ -434,7 +441,7 @@ where
434
441
/// assert_eq!(lookup[&2], 8);
435
442
/// assert_eq!(lookup.len(), 3);
436
443
/// ```
437
- pub fn min_by_key < F , CK > ( self , mut f : F ) -> HashMap < K , V >
444
+ pub fn min_by_key < F , CK > ( self , mut f : F ) -> HashMap < K , V , S >
438
445
where
439
446
F : FnMut ( & K , & V ) -> CK ,
440
447
CK : Ord ,
@@ -469,7 +476,7 @@ where
469
476
/// assert_eq!(lookup[&2], OneElement(5));
470
477
/// assert_eq!(lookup.len(), 3);
471
478
/// ```
472
- pub fn minmax ( self ) -> HashMap < K , MinMaxResult < V > >
479
+ pub fn minmax ( self ) -> HashMap < K , MinMaxResult < V > , S >
473
480
where
474
481
V : Ord ,
475
482
{
@@ -499,7 +506,7 @@ where
499
506
/// assert_eq!(lookup[&2], OneElement(5));
500
507
/// assert_eq!(lookup.len(), 3);
501
508
/// ```
502
- pub fn minmax_by < F > ( self , mut compare : F ) -> HashMap < K , MinMaxResult < V > >
509
+ pub fn minmax_by < F > ( self , mut compare : F ) -> HashMap < K , MinMaxResult < V > , S >
503
510
where
504
511
F : FnMut ( & K , & V , & V ) -> Ordering ,
505
512
{
@@ -550,7 +557,7 @@ where
550
557
/// assert_eq!(lookup[&2], OneElement(5));
551
558
/// assert_eq!(lookup.len(), 3);
552
559
/// ```
553
- pub fn minmax_by_key < F , CK > ( self , mut f : F ) -> HashMap < K , MinMaxResult < V > >
560
+ pub fn minmax_by_key < F , CK > ( self , mut f : F ) -> HashMap < K , MinMaxResult < V > , S >
554
561
where
555
562
F : FnMut ( & K , & V ) -> CK ,
556
563
CK : Ord ,
@@ -577,7 +584,7 @@ where
577
584
/// assert_eq!(lookup[&2], 5 + 8);
578
585
/// assert_eq!(lookup.len(), 3);
579
586
/// ```
580
- pub fn sum ( self ) -> HashMap < K , V >
587
+ pub fn sum ( self ) -> HashMap < K , V , S >
581
588
where
582
589
V : Add < V , Output = V > ,
583
590
{
@@ -603,7 +610,7 @@ where
603
610
/// assert_eq!(lookup[&2], 5 * 8);
604
611
/// assert_eq!(lookup.len(), 3);
605
612
/// ```
606
- pub fn product ( self ) -> HashMap < K , V >
613
+ pub fn product ( self ) -> HashMap < K , V , S >
607
614
where
608
615
V : Mul < V , Output = V > ,
609
616
{
0 commit comments