@@ -56,14 +56,18 @@ pub trait RawEntryApiV1<K, V, S>: private::Sealed {
56
56
///
57
57
/// for k in ["a", "b", "c", "d", "e", "f"] {
58
58
/// let hash = compute_hash(map.hasher(), k);
59
- /// let v = map.get(k).cloned();
60
- /// let kv = v.as_ref().map(|v| (&k, v));
59
+ /// let i = map.get_index_of(k);
60
+ /// let v = map.get(k);
61
+ /// let kv = map.get_key_value(k);
62
+ /// let ikv = map.get_full(k);
61
63
///
62
64
/// println!("Key: {} and value: {:?}", k, v);
63
65
///
64
66
/// assert_eq!(map.raw_entry_v1().from_key(k), kv);
65
67
/// assert_eq!(map.raw_entry_v1().from_hash(hash, |q| *q == k), kv);
66
68
/// assert_eq!(map.raw_entry_v1().from_key_hashed_nocheck(hash, k), kv);
69
+ /// assert_eq!(map.raw_entry_v1().from_hash_full(hash, |q| *q == k), ikv);
70
+ /// assert_eq!(map.raw_entry_v1().index_from_hash(hash, |q| *q == k), i);
67
71
/// }
68
72
/// ```
69
73
fn raw_entry_v1 ( & self ) -> RawEntryBuilder < ' _ , K , V , S > ;
@@ -116,6 +120,7 @@ pub trait RawEntryApiV1<K, V, S>: private::Sealed {
116
120
/// match map.raw_entry_mut_v1().from_key("a") {
117
121
/// RawEntryMut::Vacant(_) => unreachable!(),
118
122
/// RawEntryMut::Occupied(mut view) => {
123
+ /// assert_eq!(view.index(), 0);
119
124
/// assert_eq!(view.get(), &100);
120
125
/// let v = view.get_mut();
121
126
/// let new_v = (*v) * 10;
@@ -132,6 +137,7 @@ pub trait RawEntryApiV1<K, V, S>: private::Sealed {
132
137
/// match map.raw_entry_mut_v1().from_key_hashed_nocheck(hash, "c") {
133
138
/// RawEntryMut::Vacant(_) => unreachable!(),
134
139
/// RawEntryMut::Occupied(view) => {
140
+ /// assert_eq!(view.index(), 2);
135
141
/// assert_eq!(view.shift_remove_entry(), ("c", 300));
136
142
/// }
137
143
/// }
@@ -144,6 +150,7 @@ pub trait RawEntryApiV1<K, V, S>: private::Sealed {
144
150
/// match map.raw_entry_mut_v1().from_hash(hash, |q| *q == key) {
145
151
/// RawEntryMut::Occupied(_) => unreachable!(),
146
152
/// RawEntryMut::Vacant(view) => {
153
+ /// assert_eq!(view.index(), 2);
147
154
/// let (k, value) = view.insert("d", 4000);
148
155
/// assert_eq!((*k, *value), ("d", 4000));
149
156
/// *value = 40000;
@@ -155,6 +162,7 @@ pub trait RawEntryApiV1<K, V, S>: private::Sealed {
155
162
/// match map.raw_entry_mut_v1().from_hash(hash, |q| *q == key) {
156
163
/// RawEntryMut::Vacant(_) => unreachable!(),
157
164
/// RawEntryMut::Occupied(view) => {
165
+ /// assert_eq!(view.index(), 2);
158
166
/// assert_eq!(view.swap_remove_entry(), ("d", 40000));
159
167
/// }
160
168
/// }
@@ -205,19 +213,39 @@ impl<'a, K, V, S> RawEntryBuilder<'a, K, V, S> {
205
213
{
206
214
let hash = HashValue ( hash as usize ) ;
207
215
let i = self . map . core . get_index_of ( hash, key) ?;
208
- Some ( self . map . core . entries [ i ] . refs ( ) )
216
+ self . map . get_index ( i )
209
217
}
210
218
211
219
/// Access an entry by hash.
212
- pub fn from_hash < F > ( self , hash : u64 , mut is_match : F ) -> Option < ( & ' a K , & ' a V ) >
220
+ pub fn from_hash < F > ( self , hash : u64 , is_match : F ) -> Option < ( & ' a K , & ' a V ) >
221
+ where
222
+ F : FnMut ( & K ) -> bool ,
223
+ {
224
+ let map = self . map ;
225
+ let i = self . index_from_hash ( hash, is_match) ?;
226
+ map. get_index ( i)
227
+ }
228
+
229
+ /// Access an entry by hash, including its index.
230
+ pub fn from_hash_full < F > ( self , hash : u64 , is_match : F ) -> Option < ( usize , & ' a K , & ' a V ) >
231
+ where
232
+ F : FnMut ( & K ) -> bool ,
233
+ {
234
+ let map = self . map ;
235
+ let i = self . index_from_hash ( hash, is_match) ?;
236
+ let ( key, value) = map. get_index ( i) ?;
237
+ Some ( ( i, key, value) )
238
+ }
239
+
240
+ /// Access the index of an entry by hash.
241
+ pub fn index_from_hash < F > ( self , hash : u64 , mut is_match : F ) -> Option < usize >
213
242
where
214
243
F : FnMut ( & K ) -> bool ,
215
244
{
216
245
let hash = HashValue ( hash as usize ) ;
217
246
let entries = & * self . map . core . entries ;
218
247
let eq = move |& i: & usize | is_match ( & entries[ i] . key ) ;
219
- let i = * self . map . core . indices . get ( hash. get ( ) , eq) ?;
220
- Some ( entries[ i] . refs ( ) )
248
+ self . map . core . indices . get ( hash. get ( ) , eq) . copied ( )
221
249
}
222
250
}
223
251
@@ -294,6 +322,15 @@ impl<K: fmt::Debug, V: fmt::Debug, S> fmt::Debug for RawEntryMut<'_, K, V, S> {
294
322
}
295
323
296
324
impl < ' a , K , V , S > RawEntryMut < ' a , K , V , S > {
325
+ /// Return the index where the key-value pair exists or may be inserted.
326
+ #[ inline]
327
+ pub fn index ( & self ) -> usize {
328
+ match self {
329
+ Self :: Occupied ( entry) => entry. index ( ) ,
330
+ Self :: Vacant ( entry) => entry. index ( ) ,
331
+ }
332
+ }
333
+
297
334
/// Inserts the given default key and value in the entry if it is vacant and returns mutable
298
335
/// references to them. Otherwise mutable references to an already existent pair are returned.
299
336
pub fn or_insert ( self , default_key : K , default_value : V ) -> ( & ' a mut K , & ' a mut V )
0 commit comments