pub struct HashMap<K, V, H = BuildHasherDefault<DefaultHasher>, A: Allocator = Global> { /* private fields */ }Expand description
A HashMap implementation which uses a modified form of RobinHood/Hopscotch
probing. This implementation is efficient, roughly 2x the performance of
the default hash map when using a the same fast hasher, and a lot more
when using the default hasher. It has also been benchmarked to be as fast/
faster than other hashmaps which are known to be very efficient. The
implementation is also fully safe.
§Limitations
This hash map does not store keys, so if that is required, then another map would be better. This does, however, save space when the key is large.
Currently, moving the table does not use a custom allocator, which would further improve the implementation.
The number of elements in the map must be a power of two.
§Threading
This version is not thread-safe. [LeapMap] is a thread-safe version of
the map.
Implementations§
Source§impl<K, V> HashMap<K, V, BuildHasherDefault<DefaultHasher>, Global>
impl<K, V> HashMap<K, V, BuildHasherDefault<DefaultHasher>, Global>
Sourcepub fn new() -> HashMap<K, V, BuildHasherDefault<DefaultHasher>, Global>
pub fn new() -> HashMap<K, V, BuildHasherDefault<DefaultHasher>, Global>
Creates the hash map with space for the default number of elements, which will use the global allocator for allocation of the map data.
Sourcepub fn with_capacity(
capacity: usize,
) -> HashMap<K, V, BuildHasherDefault<DefaultHasher>, Global>
pub fn with_capacity( capacity: usize, ) -> HashMap<K, V, BuildHasherDefault<DefaultHasher>, Global>
Creates the hash map with space for capacity elements, which will use
the global allocator for allocation of the map data.
Source§impl<K, V, H> HashMap<K, V, H, Global>
impl<K, V, H> HashMap<K, V, H, Global>
Sourcepub fn with_capacity_and_hasher(
capacity: usize,
builder: H,
) -> HashMap<K, V, H, Global>
pub fn with_capacity_and_hasher( capacity: usize, builder: H, ) -> HashMap<K, V, H, Global>
Creates the hash map with space for capacity elements, using the
builder to create the hasher, which will use the global allocator for
allocation of the map data.
Source§impl<'a, K, V, H, A> HashMap<K, V, H, A>
impl<'a, K, V, H, A> HashMap<K, V, H, A>
Sourcepub fn new_in(allocator: A) -> HashMap<K, V, H, A>
pub fn new_in(allocator: A) -> HashMap<K, V, H, A>
Creates the hash map with space for the default number of elements, using
the provided allocator to allocate data for the map.
Sourcepub fn with_capacity_and_hasher_in(
capacity: usize,
builder: H,
allocator: A,
) -> HashMap<K, V, H, A>
pub fn with_capacity_and_hasher_in( capacity: usize, builder: H, allocator: A, ) -> HashMap<K, V, H, A>
Creates the hash map with space for the capacity number of elements
using the provided builder to build the hasher, and allocator for
allocating the map data.
Sourcepub fn hash_usize<Q>(&self, key: &Q) -> usize
pub fn hash_usize<Q>(&self, key: &Q) -> usize
Returns the hashed value for the key as usize.
Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts a key-value pair into the map.
If the map did not have this key present, None is returned.
If the map did have this key present, the value is updated and the old value is returned.
§Examples
let mut map = leapfrog::HashMap::new();
assert_eq!(map.insert(37, 12), None);
assert_eq!(map.insert(37, 14), Some(12));Sourcepub fn get<Q>(&'a self, key: &Q) -> Option<&'a V>
pub fn get<Q>(&'a self, key: &Q) -> Option<&'a V>
Returns an optional reference type to the value corresponding to the key.
§Examples
let mut map = leapfrog::HashMap::new();
map.insert(0, 12);
if let Some(value) = map.get(&0) {
assert_eq!(*value, 12);
}
assert!(map.get(&2).is_none());Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&'a mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&'a mut V>
Returns a mutable reference type to the value corresponding to the key.
§Examples
let mut map = leapfrog::HashMap::new();
map.insert(1, 12);
if let Some(value) = map.get_mut(&1) {
*value = 14;
}
assert_eq!(*map.get(&1).unwrap(), 14);
assert!(map.get(&2).is_none());Sourcepub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
Returns the key-value pair corresponding to the supplied key.
The supplied key may be any borrower form of the map’s key type, but
Hash and Eq on the borrowed form must match those for the key
type.
§Examples
let mut map = leapfrog::HashMap::new();
map.insert(1, 12);
assert_eq!(map.get_key_value(&1), Some((&1, &12)));
assert!(map.get(&2).is_none());Sourcepub fn entry(&mut self, key: K) -> Entry<'_, K, V, H, A>
pub fn entry(&mut self, key: K) -> Entry<'_, K, V, H, A>
Gets the given key’s corresponding entry in the map for in-place manipulation.
§Examples
let mut map = leapfrog::HashMap::<i32, i32>::new();
map.insert(1, 10);
map.insert(2, 20);
for i in 1..5 {
let value = map.entry(i).or_insert(i * 10);
*value += 1;
}
assert_eq!(map.get(&1), Some(&11));
assert_eq!(map.get(&2), Some(&21));
assert_eq!(map.get(&3), Some(&31));
assert_eq!(map.get(&4), Some(&41));Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Returns true if the map contains the specified key.
§Examples
let mut map = leapfrog::HashMap::new();
map.insert(1, 47u64);
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);Sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<V>
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
Removes the key from the map, returning the value at the key if the key was present in the map.
§Examples
let mut map = leapfrog::HashMap::new();
map.insert(2, 17);
assert_eq!(map.remove(&2), Some(17));
assert_eq!(map.remove(&2), None);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the map.
§Examples
let mut map = leapfrog::HashMap::new();
map.insert(2, 17);
assert_eq!(map.len(), 1);
map.insert(4, 17);
assert_eq!(map.len(), 2);
map.remove(&2);
assert_eq!(map.len(), 1);
map.remove(&4);
assert_eq!(map.len(), 0);
map.remove(&4);
assert_eq!(map.len(), 0);