Sha256: 8f53afb38891fbdab8553c7a89e61f0ce30947a22fb24ae84523c9a1ab025c3f

Contents?: true

Size: 1.95 KB

Versions: 38

Compression:

Stored size: 1.95 KB

Contents

//! Runtime support for precomputed constant hash tables.
//!
//! The shared module with the same name can generate constant hash tables using open addressing
//! and quadratic probing.
//!
//! The hash tables are arrays that are guaranteed to:
//!
//! - Have a power-of-two size.
//! - Contain at least one empty slot.
//!
//! This module provides runtime support for lookups in these tables.

// Re-export entities from constant_hash for simplicity of use.
pub use cranelift_codegen_shared::constant_hash::*;

/// Trait that must be implemented by the entries in a constant hash table.
pub trait Table<K: Copy + Eq> {
    /// Get the number of entries in this table which must be a power of two.
    fn len(&self) -> usize;

    /// Get the key corresponding to the entry at `idx`, or `None` if the entry is empty.
    /// The `idx` must be in range.
    fn key(&self, idx: usize) -> Option<K>;
}

/// Look for `key` in `table`.
///
/// The provided `hash` value must have been computed from `key` using the same hash function that
/// was used to construct the table.
///
/// Returns `Ok(idx)` with the table index containing the found entry, or `Err(idx)` with the empty
/// sentinel entry if no entry could be found.
pub fn probe<K: Copy + Eq, T: Table<K> + ?Sized>(
    table: &T,
    key: K,
    hash: usize,
) -> Result<usize, usize> {
    debug_assert!(table.len().is_power_of_two());
    let mask = table.len() - 1;

    let mut idx = hash;
    let mut step = 0;

    loop {
        idx &= mask;

        match table.key(idx) {
            None => return Err(idx),
            Some(k) if k == key => return Ok(idx),
            _ => {}
        }

        // Quadratic probing.
        step += 1;

        // When `table.len()` is a power of two, it can be proven that `idx` will visit all
        // entries. This means that this loop will always terminate if the hash table has even
        // one unused entry.
        debug_assert!(step < table.len());
        idx += step;
    }
}

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/cranelift-codegen-0.116.0/src/constant_hash.rs
wasmtime-28.0.0 ./ext/cargo-vendor/cranelift-codegen-0.115.0/src/constant_hash.rs
wasmtime-27.0.0 ./ext/cargo-vendor/cranelift-codegen-0.114.0/src/constant_hash.rs
wasmtime-26.0.0 ./ext/cargo-vendor/cranelift-codegen-0.113.0/src/constant_hash.rs
wasmtime-25.0.2 ./ext/cargo-vendor/cranelift-codegen-0.112.2/src/constant_hash.rs
wasmtime-25.0.1 ./ext/cargo-vendor/cranelift-codegen-0.112.1/src/constant_hash.rs
wasmtime-25.0.0 ./ext/cargo-vendor/cranelift-codegen-0.112.0/src/constant_hash.rs
wasmtime-24.0.0 ./ext/cargo-vendor/cranelift-codegen-0.111.0/src/constant_hash.rs
wasmtime-23.0.2 ./ext/cargo-vendor/cranelift-codegen-0.110.2/src/constant_hash.rs
wasmtime-22.0.0 ./ext/cargo-vendor/cranelift-codegen-0.109.0/src/constant_hash.rs
wasmtime-21.0.1 ./ext/cargo-vendor/cranelift-codegen-0.108.1/src/constant_hash.rs
wasmtime-20.0.2 ./ext/cargo-vendor/cranelift-codegen-0.107.2/src/constant_hash.rs
wasmtime-20.0.0 ./ext/cargo-vendor/cranelift-codegen-0.107.2/src/constant_hash.rs
wasmtime-18.0.3 ./ext/cargo-vendor/cranelift-codegen-0.105.3/src/constant_hash.rs
wasmtime-17.0.1 ./ext/cargo-vendor/cranelift-codegen-0.104.1/src/constant_hash.rs
wasmtime-17.0.0 ./ext/cargo-vendor/cranelift-codegen-0.104.0/src/constant_hash.rs
wasmtime-16.0.0 ./ext/cargo-vendor/cranelift-codegen-0.103.0/src/constant_hash.rs
wasmtime-15.0.1 ./ext/cargo-vendor/cranelift-codegen-0.102.1/src/constant_hash.rs
wasmtime-15.0.0 ./ext/cargo-vendor/cranelift-codegen-0.102.1/src/constant_hash.rs
wasmtime-14.0.4 ./ext/cargo-vendor/cranelift-codegen-0.101.4/src/constant_hash.rs