1- use std:: hash:: Hash ;
1+ use core:: hash:: BuildHasher ;
2+ use std:: hash:: { Hash , RandomState } ;
23
34mod private {
5+ use core:: hash:: BuildHasher ;
46 use std:: collections:: HashMap ;
57 use std:: fmt;
68 use std:: hash:: Hash ;
79
810 #[ derive( Clone ) ]
911 #[ 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+ {
1116 pub ( crate ) iter : I ,
12- pub ( crate ) meta : Meta < Key , F > ,
17+ pub ( crate ) meta : Meta < Key , F , S > ,
1318 }
1419
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 >
1621 where
1722 I : Iterator + fmt:: Debug ,
1823 V : fmt:: Debug + Hash + Eq ,
24+ S : BuildHasher ,
1925 {
2026 debug_fmt_fields ! ( DuplicatesBy , iter, meta. used) ;
2127 }
2228
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 {
2531 Self {
2632 iter,
2733 meta : Meta {
28- used : HashMap :: new ( ) ,
34+ used : HashMap :: with_hasher ( hash_builder ) ,
2935 pending : 0 ,
3036 key_method,
3137 } ,
@@ -34,15 +40,16 @@ mod private {
3440 }
3541
3642 #[ 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 > ,
3945 pending : usize ,
4046 key_method : F ,
4147 }
4248
43- impl < Key , F > Meta < Key , F >
49+ impl < Key , F , S > Meta < Key , F , S >
4450 where
4551 Key : Eq + Hash ,
52+ S : BuildHasher ,
4653 {
4754 /// Takes an item and returns it back to the caller if it's the second time we see it.
4855 /// Otherwise the item is consumed and None is returned
@@ -68,11 +75,12 @@ mod private {
6875 }
6976 }
7077
71- impl < I , Key , F > Iterator for DuplicatesBy < I , Key , F >
78+ impl < I , Key , F , S > Iterator for DuplicatesBy < I , Key , F , S >
7279 where
7380 I : Iterator ,
7481 Key : Eq + Hash ,
7582 F : KeyMethod < Key , I :: Item > ,
83+ S : BuildHasher ,
7684 {
7785 type Item = I :: Item ;
7886
@@ -102,11 +110,12 @@ mod private {
102110 }
103111 }
104112
105- impl < I , Key , F > DoubleEndedIterator for DuplicatesBy < I , Key , F >
113+ impl < I , Key , F , S > DoubleEndedIterator for DuplicatesBy < I , Key , F , S >
106114 where
107115 I : DoubleEndedIterator ,
108116 Key : Eq + Hash ,
109117 F : KeyMethod < Key , I :: Item > ,
118+ S : BuildHasher ,
110119 {
111120 fn next_back ( & mut self ) -> Option < Self :: Item > {
112121 let Self { iter, meta } = self ;
@@ -189,28 +198,35 @@ mod private {
189198/// An iterator adapter to filter for duplicate elements.
190199///
191200/// 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 >
196209where
197210 Key : Eq + Hash ,
198211 F : FnMut ( & I :: Item ) -> Key ,
199212 I : Iterator ,
213+ S : BuildHasher ,
200214{
201- DuplicatesBy :: new ( iter, private:: ByFn ( f) )
215+ DuplicatesBy :: new ( iter, private:: ByFn ( f) , hash_builder )
202216}
203217
204218/// An iterator adapter to filter out duplicate elements.
205219///
206220/// 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 > ;
208223
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 >
211226where
212227 I : Iterator ,
213228 I :: Item : Eq + Hash ,
229+ S : BuildHasher ,
214230{
215- Duplicates :: new ( iter, private:: ById )
231+ Duplicates :: new ( iter, private:: ById , hash_builder )
216232}
0 commit comments