pub struct ExHashMap<'a, K, V, C: HashContext<K>> { /* private fields */ }Expand description
A high-performance open-addressing hash map with SIMD probing.
§Type Parameters
'a— Lifetime of the allocator reference.K— Key type.V— Value type.C—HashContext<K>providing hash and equality functions.
§Memory Layout
Two separate heap allocations:
ctrl—cap + GROUP_WIDTHcontrol bytes. The extraGROUP_WIDTHbytes at the end are mirror copies of the firstGROUP_WIDTHcontrol bytes, enabling SIMD group loads at the table boundary without out-of-bounds access.data—cap[Slot<K, V>] entries.
Both allocations are freed on drop.
Implementations§
Source§impl<'a, K, V, C: HashContext<K>> ExHashMap<'a, K, V, C>
impl<'a, K, V, C: HashContext<K>> ExHashMap<'a, K, V, C>
Sourcepub fn new(alloc: &'a dyn Allocator, ctx: C) -> Self
pub fn new(alloc: &'a dyn Allocator, ctx: C) -> Self
Creates a new, empty map that will allocate through alloc.
No memory is allocated until the first insertion.
Examples found in repository?
4fn main() {
5 let alloc = SystemAllocator;
6 let ctx = U64HashCtx;
7
8 let mut map = ExHashMap::new(&alloc, ctx);
9
10 map.insert(42, 100);
11 map.insert(1337, 9000);
12
13 if let Some(val) = map.get(&42) {
14 println!("Found: {}", val);
15 }
16
17 map.remove(&1337);
18
19 println!("Map len: {}, capacity: {}", map.len(), map.capacity());
20}Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of key-value pairs in the map.
Examples found in repository?
4fn main() {
5 let alloc = SystemAllocator;
6 let ctx = U64HashCtx;
7
8 let mut map = ExHashMap::new(&alloc, ctx);
9
10 map.insert(42, 100);
11 map.insert(1337, 9000);
12
13 if let Some(val) = map.get(&42) {
14 println!("Found: {}", val);
15 }
16
17 map.remove(&1337);
18
19 println!("Map len: {}, capacity: {}", map.len(), map.capacity());
20}Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the current table capacity.
Examples found in repository?
4fn main() {
5 let alloc = SystemAllocator;
6 let ctx = U64HashCtx;
7
8 let mut map = ExHashMap::new(&alloc, ctx);
9
10 map.insert(42, 100);
11 map.insert(1337, 9000);
12
13 if let Some(val) = map.get(&42) {
14 println!("Found: {}", val);
15 }
16
17 map.remove(&1337);
18
19 println!("Map len: {}, capacity: {}", map.len(), map.capacity());
20}Sourcepub fn get(&self, key: &K) -> Option<&V>
pub fn get(&self, key: &K) -> Option<&V>
Returns a reference to the value associated with key, or None.
Examples found in repository?
4fn main() {
5 let alloc = SystemAllocator;
6 let ctx = U64HashCtx;
7
8 let mut map = ExHashMap::new(&alloc, ctx);
9
10 map.insert(42, 100);
11 map.insert(1337, 9000);
12
13 if let Some(val) = map.get(&42) {
14 println!("Found: {}", val);
15 }
16
17 map.remove(&1337);
18
19 println!("Map len: {}, capacity: {}", map.len(), map.capacity());
20}Sourcepub fn get_mut(&mut self, key: &K) -> Option<&mut V>
pub fn get_mut(&mut self, key: &K) -> Option<&mut V>
Returns a mutable reference to the value associated with key, or None.
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Returns true if the map contains an entry for key.
Sourcepub fn insert(&mut self, key: K, val: V) -> Option<V>
pub fn insert(&mut self, key: K, val: V) -> Option<V>
Inserts key→val, returning the previous value for key if it existed.
§Panics
Panics if the allocator fails during table growth.
Examples found in repository?
4fn main() {
5 let alloc = SystemAllocator;
6 let ctx = U64HashCtx;
7
8 let mut map = ExHashMap::new(&alloc, ctx);
9
10 map.insert(42, 100);
11 map.insert(1337, 9000);
12
13 if let Some(val) = map.get(&42) {
14 println!("Found: {}", val);
15 }
16
17 map.remove(&1337);
18
19 println!("Map len: {}, capacity: {}", map.len(), map.capacity());
20}Sourcepub fn try_insert(&mut self, key: K, val: V) -> Result<Option<V>, (K, V)>
pub fn try_insert(&mut self, key: K, val: V) -> Result<Option<V>, (K, V)>
Attempts to insert key→val.
Returns:
Ok(None)— new entry inserted.Ok(Some(old))— key already existed; old value returned.Err((key, val))— allocation failed; inputs returned to caller.
Sourcepub fn remove(&mut self, key: &K) -> Option<V>
pub fn remove(&mut self, key: &K) -> Option<V>
Removes the entry for key and returns its value, or None.
Uses backward-shift deletion to preserve the probing invariant without a tombstone mechanism.
Examples found in repository?
4fn main() {
5 let alloc = SystemAllocator;
6 let ctx = U64HashCtx;
7
8 let mut map = ExHashMap::new(&alloc, ctx);
9
10 map.insert(42, 100);
11 map.insert(1337, 9000);
12
13 if let Some(val) = map.get(&42) {
14 println!("Found: {}", val);
15 }
16
17 map.remove(&1337);
18
19 println!("Map len: {}, capacity: {}", map.len(), map.capacity());
20}Sourcepub fn for_each<F: FnMut(&K, &V)>(&self, f: F)
pub fn for_each<F: FnMut(&K, &V)>(&self, f: F)
Iterates over all key-value pairs, calling f for each.
The iteration order is unspecified.
Sourcepub fn for_each_mut<F: FnMut(&K, &mut V)>(&mut self, f: F)
pub fn for_each_mut<F: FnMut(&K, &mut V)>(&mut self, f: F)
Iterates over all key-value pairs with mutable access to values.