#hash-values #hash #probabilistic #bloom-filter #traits #hasher #extend

aabel-multihash-rs

A crate the extends Hasher trait. The added functionality allows users to get sequeces of hash values. It can be used inside of implementations of probabilistic data structures such bloom filter or count-min.

3 releases

0.1.2 Feb 29, 2024
0.1.1 Feb 22, 2024
0.1.0 Feb 22, 2024

#662 in Data structures

38 downloads per month
Used in aabel-bloom-rs

MIT/Apache

17KB
257 lines

Simplee > Aabel > Multi-Hash

Crates.io CI GitHub top language License:MIT GitHub code size in bytes GitHub last commit GitHub watchers

A crate which extends Hasher and BuildHasher traits. These extensions can be used in algorithms, eg streaming ones, which require several hashing functions to be executed for each incoming item. As example, bloom filter and count-min use such hash values to represent the incoming items.

HasherExt Trait

The HasherExt trait extends the Hasher trait by adding capabilities to finalize the hashing operation and getting back an infinite sequest of hash values. The crate provides PairHasher which implements the HasherExt trait. The PairHasher is a combinator of two separate hashers, which are used to obtain the sequece of hash values, each value representing the result of an independent hashing function.

pub trait HasherExt: Hasher {
    fn finish_iter(self) -> impl Iterator<Item = u64>;
}

You can see the full definition of the HasherExt trait in the lib.hs file.

The crate provides PairHasher which implements the HasherExt trait. The implementation uses two hashers, which are used to obtain the sequence of the hash values. You can see the full definition at pairhasher.rs.

BuildHasherExt Trait

The BuildHasherExt trait extends the BuildHasher trait. It adds a cabapility to internally build an instance of the HasherExt trait which is used to generate the sequence of the hash values. The BuildHasherExt trait exposes the hashes_one function, which is the one that takes as input an item and returns the sequence of the hash values. You cann see the definition of the BuildHasher trait in the lib.hs file.

pub trait BuildHasherExt: BuildHasher {
    /// Generates the sequece of hash values for a given item.
    fn hashes_one<T: Hash>(&self, item: T) -> impl Iterator<Item = u64>;
}

The crate provides BuildPairHasher which implements the BuildHasherExt trait. The implementation uses two hashers builders, which are used to build internally a PairHasher instance. You can find the source of the BuildPairHasher in the pairhasher.rs.

The BuildPairHasher is the main entry point for the users who want to get sequences of hash values, see the example below.

Example

use aabel_multihash_rs::{BuildHasherExt, BuildPairHasher};
use std::hash::{BuildHasher, Hash};

// Create the hasher builder
let keys1 = (0, 0);
let keys2 = (1, 1);
let builder = BuildPairHasher::new_with_keys(keys1, keys2);

// The number of hash functions
const HASH_COUNT: usize = 10;

// Compute 10 hash values
let item = "Hello world!";
let hashes = builder
    .hashes_one(item)
    .take(HASHE_COUNT)
    .collect::<Vec<_>>();

assert_eq!(hashes.len(), HASHE_COUNT)

About

Code designed and written on the beautiful island of Saaremaa, Estonia.

Dependencies

~370KB