@@ -10,9 +10,8 @@ use std::collections::hash_map::RandomState;
10
10
use std:: fmt;
11
11
use std:: hash:: BuildHasher ;
12
12
use std:: hash:: Hash ;
13
- use std:: hash:: Hasher ;
14
13
15
- use hashbrown:: raw :: RawTable ;
14
+ use hashbrown:: HashTable ;
16
15
use parking_lot:: RwLock ;
17
16
use parking_lot:: RwLockWriteGuard ;
18
17
@@ -21,7 +20,7 @@ const SHARDS: usize = 1 << SHARD_SHIFT;
21
20
22
21
pub struct ShardedSet < T , S = RandomState > {
23
22
build_hasher : S ,
24
- shards : [ RwLock < RawTable < T > > ; SHARDS ] ,
23
+ shards : [ RwLock < HashTable < T > > ; SHARDS ] ,
25
24
}
26
25
27
26
impl < T , S > fmt:: Debug for ShardedSet < T , S > {
@@ -111,15 +110,13 @@ impl<T, S: BuildHasher> ShardedSet<T, S> {
111
110
}
112
111
113
112
fn hash_one < B : BuildHasher , T : Hash > ( build_hasher : & B , x : T ) -> u64 {
114
- let mut hasher = build_hasher. build_hasher ( ) ;
115
- x. hash ( & mut hasher) ;
116
- hasher. finish ( )
113
+ build_hasher. hash_one ( & x)
117
114
}
118
115
119
116
pub struct InsertLock < ' a , T , S = RandomState > {
120
117
build_hasher : & ' a S ,
121
118
hash : u64 ,
122
- shard : RwLockWriteGuard < ' a , RawTable < T > > ,
119
+ shard : RwLockWriteGuard < ' a , HashTable < T > > ,
123
120
}
124
121
125
122
impl < ' a , T , S > fmt:: Debug for InsertLock < ' a , T , S > {
@@ -133,7 +130,7 @@ impl<T: Eq + Hash, S: BuildHasher> ShardedSet<T, S> {
133
130
/// hashbrown uses the upper 7 bits for disambiguation and the lower bits
134
131
/// for bucket indexing, we take the bits just above the top 7.
135
132
#[ inline( always) ]
136
- fn hash_and_shard < Q > ( & self , q : & Q ) -> ( u64 , & RwLock < RawTable < T > > )
133
+ fn hash_and_shard < Q > ( & self , q : & Q ) -> ( u64 , & RwLock < HashTable < T > > )
137
134
where
138
135
T : Borrow < Q > ,
139
136
Q : ?Sized + Hash ,
@@ -159,7 +156,7 @@ impl<T: Eq + Hash, S: BuildHasher> ShardedSet<T, S> {
159
156
write_lock
160
157
} else {
161
158
// Write contention. Try reading first to see if the entry already exists.
162
- if let Some ( t) = shard. read ( ) . get ( hash, |other| q == other. borrow ( ) ) {
159
+ if let Some ( t) = shard. read ( ) . find ( hash, |other| q == other. borrow ( ) ) {
163
160
// Already exists.
164
161
return Ok ( t. clone ( ) ) ;
165
162
}
@@ -170,7 +167,7 @@ impl<T: Eq + Hash, S: BuildHasher> ShardedSet<T, S> {
170
167
// checked in the write contention case above. We don't use an
171
168
// upgradable read lock because those are exclusive from one another
172
169
// just like write locks.
173
- if let Some ( t) = shard. get ( hash, |other| q == other. borrow ( ) ) {
170
+ if let Some ( t) = shard. find ( hash, |other| q == other. borrow ( ) ) {
174
171
return Ok ( t. clone ( ) ) ;
175
172
}
176
173
Err ( InsertLock {
@@ -189,15 +186,17 @@ impl<T: Eq + Hash, S: BuildHasher> ShardedSet<T, S> {
189
186
let ( hash, shard) = self . hash_and_shard ( q) ;
190
187
shard
191
188
. read ( )
192
- . get ( hash, |other| q == other. borrow ( ) )
193
- . map ( Clone :: clone )
189
+ . find ( hash, |other| q == other. borrow ( ) )
190
+ . cloned ( )
194
191
}
195
192
196
193
/// Unconditionally insert `t` without checking if it's in the set.
197
194
pub fn unchecked_insert ( & self , t : T ) {
198
195
let build_hasher = & self . build_hasher ;
199
196
let ( hash, shard) = self . hash_and_shard ( & t) ;
200
- shard. write ( ) . insert ( hash, t, |v| hash_one ( build_hasher, v) ) ;
197
+ shard
198
+ . write ( )
199
+ . insert_unique ( hash, t, |v| hash_one ( build_hasher, v) ) ;
201
200
}
202
201
}
203
202
@@ -207,6 +206,6 @@ impl<T: Sized + Hash, S: BuildHasher> InsertLock<'_, T, S> {
207
206
pub fn insert < Q : Into < T > > ( & mut self , q : Q ) {
208
207
let build_hasher = self . build_hasher ;
209
208
self . shard
210
- . insert ( self . hash , q. into ( ) , |v| hash_one ( build_hasher, v) ) ;
209
+ . insert_unique ( self . hash , q. into ( ) , |v| hash_one ( build_hasher, v) ) ;
211
210
}
212
211
}
0 commit comments