PolymurHash

Struct PolymurHash 

Source
pub struct PolymurHash { /* private fields */ }
Expand description

A fast, non-cryptographic hash function based on polynomial evaluation.

PolymurHash is a universal hash function that provides excellent performance and good distribution properties. It’s particularly well-suited for hash tables and other non-cryptographic applications.

§Examples

Basic usage:

use polymur_hash::PolymurHash;

let hasher = PolymurHash::new(0);
let data = b"Hello, world!";
let hash = hasher.hash(data);

Using with a custom seed:

use polymur_hash::PolymurHash;

let seed = 0xDEADBEEFCAFEBABE_u64;
let hasher = PolymurHash::from_u64_seed(seed);
let hash = hasher.hash(b"Some data");

Hash with tweak for additional randomization:

use polymur_hash::PolymurHash;

let hasher = PolymurHash::new(42);
let tweak = 0x123456789ABCDEF0;
let hash = hasher.hash_with_tweak(b"Data", tweak);

Implementations§

Source§

impl PolymurHash

Source

pub fn new(seed: u128) -> Self

Creates a new PolymurHash instance from a 128-bit seed.

§Arguments
  • seed - A 128-bit seed value to initialize the hasher
§Examples
use polymur_hash::PolymurHash;

let hasher = PolymurHash::new(0x123456789ABCDEF0123456789ABCDEF0);
Source

pub fn from_u64_seed(seed: u64) -> Self

Creates a new PolymurHash instance from a 64-bit seed.

This method expands the 64-bit seed into the required internal state.

§Arguments
  • seed - A 64-bit seed value to initialize the hasher
§Examples
use polymur_hash::PolymurHash;

let hasher = PolymurHash::from_u64_seed(0xDEADBEEF);
Source

pub fn from_u64x2_seed(k_seed: u64, s_seed: u64) -> Self

Creates a new PolymurHash instance from two 64-bit seeds.

This provides direct control over the key and state seeds.

§Arguments
  • k_seed - Seed for the polynomial key generation
  • s_seed - Seed for the final mixing state
§Examples
use polymur_hash::PolymurHash;

let hasher = PolymurHash::from_u64x2_seed(0x12345678, 0x9ABCDEF0);
Source

pub fn hash_with_tweak(&self, buf: impl AsRef<[u8]>, tweak: u64) -> u64

Computes the hash of the given data with an additional tweak value.

The tweak allows for additional randomization without changing the key. This is useful for applications that need multiple independent hash values from the same key.

§Arguments
  • buf - The data to hash
  • tweak - An additional value to mix into the hash
§Examples
use polymur_hash::PolymurHash;

let hasher = PolymurHash::new(0);
let data = b"Hello, world!";
let hash1 = hasher.hash_with_tweak(data, 1);
let hash2 = hasher.hash_with_tweak(data, 2);
assert_ne!(hash1, hash2); // Different tweaks produce different hashes
Source

pub fn hash(&self, buf: impl AsRef<[u8]>) -> u64

Computes the hash of the given data.

§Arguments
  • buf - The data to hash
§Returns

A 64-bit hash value

§Examples
use polymur_hash::PolymurHash;

let hasher = PolymurHash::new(0);
let hash = hasher.hash(b"Hello, world!");

Trait Implementations§

Source§

impl Clone for PolymurHash

Source§

fn clone(&self) -> PolymurHash

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PolymurHash

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.