Skip to content

A rust crate which extends the Hasher trait. The added functionality allows the users to get multiple hash values for any given hashable item.

Notifications You must be signed in to change notification settings

veminovici/aabel-multihash-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

A rust crate which extends the Hasher trait. The added functionality allows the users to get multiple hash values for any given hashable item.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages