1
- use std:: hash:: Hash ;
1
+ use core:: hash:: BuildHasher ;
2
+ use std:: hash:: { Hash , RandomState } ;
2
3
3
4
mod private {
5
+ use core:: hash:: BuildHasher ;
4
6
use std:: collections:: HashMap ;
5
7
use std:: fmt;
6
8
use std:: hash:: Hash ;
7
9
8
10
#[ derive( Clone ) ]
9
11
#[ must_use = "iterator adaptors are lazy and do nothing unless consumed" ]
10
- pub struct DuplicatesBy < I : Iterator , Key , F > {
12
+ pub struct DuplicatesBy < I : Iterator , Key , F , S >
13
+ where
14
+ S : BuildHasher ,
15
+ {
11
16
pub ( crate ) iter : I ,
12
- pub ( crate ) meta : Meta < Key , F > ,
17
+ pub ( crate ) meta : Meta < Key , F , S > ,
13
18
}
14
19
15
- impl < I , V , F > fmt:: Debug for DuplicatesBy < I , V , F >
20
+ impl < I , V , F , S > fmt:: Debug for DuplicatesBy < I , V , F , S >
16
21
where
17
22
I : Iterator + fmt:: Debug ,
18
23
V : fmt:: Debug + Hash + Eq ,
24
+ S : BuildHasher ,
19
25
{
20
26
debug_fmt_fields ! ( DuplicatesBy , iter, meta. used) ;
21
27
}
22
28
23
- impl < I : Iterator , Key : Eq + Hash , F > DuplicatesBy < I , Key , F > {
24
- pub ( crate ) fn new ( iter : I , key_method : F ) -> Self {
29
+ impl < I : Iterator , Key : Eq + Hash , F , S : BuildHasher > DuplicatesBy < I , Key , F , S > {
30
+ pub ( crate ) fn new ( iter : I , key_method : F , hash_builder : S ) -> Self {
25
31
Self {
26
32
iter,
27
33
meta : Meta {
28
- used : HashMap :: new ( ) ,
34
+ used : HashMap :: with_hasher ( hash_builder ) ,
29
35
pending : 0 ,
30
36
key_method,
31
37
} ,
@@ -34,15 +40,16 @@ mod private {
34
40
}
35
41
36
42
#[ derive( Clone ) ]
37
- pub struct Meta < Key , F > {
38
- used : HashMap < Key , bool > ,
43
+ pub struct Meta < Key , F , S > {
44
+ used : HashMap < Key , bool , S > ,
39
45
pending : usize ,
40
46
key_method : F ,
41
47
}
42
48
43
- impl < Key , F > Meta < Key , F >
49
+ impl < Key , F , S > Meta < Key , F , S >
44
50
where
45
51
Key : Eq + Hash ,
52
+ S : BuildHasher ,
46
53
{
47
54
/// Takes an item and returns it back to the caller if it's the second time we see it.
48
55
/// Otherwise the item is consumed and None is returned
@@ -68,11 +75,12 @@ mod private {
68
75
}
69
76
}
70
77
71
- impl < I , Key , F > Iterator for DuplicatesBy < I , Key , F >
78
+ impl < I , Key , F , S > Iterator for DuplicatesBy < I , Key , F , S >
72
79
where
73
80
I : Iterator ,
74
81
Key : Eq + Hash ,
75
82
F : KeyMethod < Key , I :: Item > ,
83
+ S : BuildHasher ,
76
84
{
77
85
type Item = I :: Item ;
78
86
@@ -102,11 +110,12 @@ mod private {
102
110
}
103
111
}
104
112
105
- impl < I , Key , F > DoubleEndedIterator for DuplicatesBy < I , Key , F >
113
+ impl < I , Key , F , S > DoubleEndedIterator for DuplicatesBy < I , Key , F , S >
106
114
where
107
115
I : DoubleEndedIterator ,
108
116
Key : Eq + Hash ,
109
117
F : KeyMethod < Key , I :: Item > ,
118
+ S : BuildHasher ,
110
119
{
111
120
fn next_back ( & mut self ) -> Option < Self :: Item > {
112
121
let Self { iter, meta } = self ;
@@ -189,28 +198,35 @@ mod private {
189
198
/// An iterator adapter to filter for duplicate elements.
190
199
///
191
200
/// See [`.duplicates_by()`](crate::Itertools::duplicates_by) for more information.
192
- pub type DuplicatesBy < I , V , F > = private:: DuplicatesBy < I , V , private:: ByFn < F > > ;
193
-
194
- /// Create a new `DuplicatesBy` iterator.
195
- pub fn duplicates_by < I , Key , F > ( iter : I , f : F ) -> DuplicatesBy < I , Key , F >
201
+ pub type DuplicatesBy < I , V , F , S = RandomState > = private:: DuplicatesBy < I , V , private:: ByFn < F > , S > ;
202
+
203
+ /// Create a new `DuplicatesBy` iterator with a specified hash builder.
204
+ pub fn duplicates_by_with_hasher < I , Key , F , S > (
205
+ iter : I ,
206
+ f : F ,
207
+ hash_builder : S ,
208
+ ) -> DuplicatesBy < I , Key , F , S >
196
209
where
197
210
Key : Eq + Hash ,
198
211
F : FnMut ( & I :: Item ) -> Key ,
199
212
I : Iterator ,
213
+ S : BuildHasher ,
200
214
{
201
- DuplicatesBy :: new ( iter, private:: ByFn ( f) )
215
+ DuplicatesBy :: new ( iter, private:: ByFn ( f) , hash_builder )
202
216
}
203
217
204
218
/// An iterator adapter to filter out duplicate elements.
205
219
///
206
220
/// See [`.duplicates()`](crate::Itertools::duplicates) for more information.
207
- pub type Duplicates < I > = private:: DuplicatesBy < I , <I as Iterator >:: Item , private:: ById > ;
221
+ pub type Duplicates < I , S = RandomState > =
222
+ private:: DuplicatesBy < I , <I as Iterator >:: Item , private:: ById , S > ;
208
223
209
- /// Create a new `Duplicates` iterator.
210
- pub fn duplicates < I > ( iter : I ) -> Duplicates < I >
224
+ /// Create a new `Duplicates` iterator with a specified hash builder .
225
+ pub fn duplicates_with_hasher < I , S > ( iter : I , hash_builder : S ) -> Duplicates < I , S >
211
226
where
212
227
I : Iterator ,
213
228
I :: Item : Eq + Hash ,
229
+ S : BuildHasher ,
214
230
{
215
- Duplicates :: new ( iter, private:: ById )
231
+ Duplicates :: new ( iter, private:: ById , hash_builder )
216
232
}
0 commit comments