Skip to main content

ExHashMap

Struct ExHashMap 

Source
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.
  • CHashContext<K> providing hash and equality functions.

§Memory Layout

Two separate heap allocations:

  1. ctrlcap + GROUP_WIDTH control bytes. The extra GROUP_WIDTH bytes at the end are mirror copies of the first GROUP_WIDTH control bytes, enabling SIMD group loads at the table boundary without out-of-bounds access.
  2. datacap [Slot<K, V>] entries.

Both allocations are freed on drop.

Implementations§

Source§

impl<'a, K, V, C: HashContext<K>> ExHashMap<'a, K, V, C>

Source

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?
examples/hash_map.rs (line 8)
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}
Source

pub fn len(&self) -> usize

Returns the number of key-value pairs in the map.

Examples found in repository?
examples/hash_map.rs (line 19)
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}
Source

pub fn capacity(&self) -> usize

Returns the current table capacity.

Examples found in repository?
examples/hash_map.rs (line 19)
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}
Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no entries.

Source

pub fn get(&self, key: &K) -> Option<&V>

Returns a reference to the value associated with key, or None.

Examples found in repository?
examples/hash_map.rs (line 13)
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}
Source

pub fn get_mut(&mut self, key: &K) -> Option<&mut V>

Returns a mutable reference to the value associated with key, or None.

Source

pub fn contains_key(&self, key: &K) -> bool

Returns true if the map contains an entry for key.

Source

pub fn insert(&mut self, key: K, val: V) -> Option<V>

Inserts keyval, returning the previous value for key if it existed.

§Panics

Panics if the allocator fails during table growth.

Examples found in repository?
examples/hash_map.rs (line 10)
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}
Source

pub fn try_insert(&mut self, key: K, val: V) -> Result<Option<V>, (K, V)>

Attempts to insert keyval.

Returns:

  • Ok(None) — new entry inserted.
  • Ok(Some(old)) — key already existed; old value returned.
  • Err((key, val)) — allocation failed; inputs returned to caller.
Source

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?
examples/hash_map.rs (line 17)
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}
Source

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.

Source

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.

Trait Implementations§

Source§

impl<K, V, C: HashContext<K>> Drop for ExHashMap<'_, K, V, C>

Source§

fn drop(&mut self)

Drops all live key-value pairs and releases both backing allocations.

Auto Trait Implementations§

§

impl<'a, K, V, C> Freeze for ExHashMap<'a, K, V, C>
where C: Freeze,

§

impl<'a, K, V, C> !RefUnwindSafe for ExHashMap<'a, K, V, C>

§

impl<'a, K, V, C> !Send for ExHashMap<'a, K, V, C>

§

impl<'a, K, V, C> !Sync for ExHashMap<'a, K, V, C>

§

impl<'a, K, V, C> Unpin for ExHashMap<'a, K, V, C>
where C: Unpin, K: Unpin, V: Unpin,

§

impl<'a, K, V, C> UnsafeUnpin for ExHashMap<'a, K, V, C>
where C: UnsafeUnpin,

§

impl<'a, K, V, C> !UnwindSafe for ExHashMap<'a, K, V, C>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.