Crates.io | aabel-multihash-rs |
lib.rs | aabel-multihash-rs |
version | 0.1.2 |
source | src |
created_at | 2024-02-22 14:07:07.025961 |
updated_at | 2024-02-29 09:49:01.658862 |
description | 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. |
homepage | https://github.com/veminovici/aabel-multihash-rs |
repository | https://github.com/veminovici/aabel-multihash-rs |
max_upload_size | |
id | 1149352 |
size | 17,737 |
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.
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.
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.
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)
Code designed and written on the beautiful island of Saaremaa, Estonia.