Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to support pre-hashed keys #491

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading