Skip to content

Commit

Permalink
Add function to support prehashed keys
Browse files Browse the repository at this point in the history
  • Loading branch information
joaovbsevero committed Dec 11, 2023
1 parent f2e6212 commit f67ea05
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1824,6 +1824,53 @@ where
(k_ref, v_ref)
}

/// Inserts a key-value pair into the map using pre-generated key hash value.
///
/// This method is intended to be used by monads that overwrite the default
/// hash method of the inner type.
///
/// No value is returned from this function, this functions intent is to be
/// a fast insertion path for container types.
///
/// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
/// [`std::collections`]: https://doc.rust-lang.org/std/collections/index.html
/// [module-level documentation]: https://doc.rust-lang.org/std/collections/index.html#insert-and-complex-keys
///
/// # Examples
///
/// ```
/// use hashbrown::HashMap;
/// use std::hash::{BuildHasher, Hash, Hasher};
///
/// let mut map = HashMap::new();
/// let key: i32 = 37;
///
/// let hasher = map.hasher();
/// let mut state = hasher.build_hasher();
/// key.hash(&mut state);
/// let hash = state.finish();
///
/// map.insert_with_hash(hash, key, 'a');
/// assert_eq!(map[&37], 'a');
///
/// map.insert_with_hash(hash, key, 'b');
/// assert_eq!(map[&37], 'b');
/// ```
pub fn insert_with_hash(&mut self, hash: u64, k: K, mut v: V) {
let hasher = make_hasher::<_, V, S>(&self.hash_builder);
match self
.table
.find_or_find_insert_slot(hash, equivalent_key(&k), hasher)
{
Ok(bucket) => unsafe {
mem::swap(&mut bucket.as_mut().1, &mut v);
},
Err(slot) => unsafe {
self.table.insert_in_slot(hash, slot, (k, v));
},
};
}

/// Tries to insert a key-value pair into the map, and returns
/// a mutable reference to the value in the entry.
///
Expand Down

0 comments on commit f67ea05

Please sign in to comment.