Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented BuildHasher for WyHash #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/final3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ mod functions;
pub use functions::{make_secret, wyhash, wyrng};

mod traits;
pub use traits::{WyHash, WyRng};
pub use traits::{WyHash, WyRng, WyHasherBuilder};
30 changes: 29 additions & 1 deletion src/final3/traits.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
use crate::final3::functions::{wyhash_core, wyhash_finish, wyrng};
use crate::v1::functions::{read64, P0, P1, P2, P3};
use core::hash::Hasher;
use core::hash::{Hasher, BuildHasher};
use rand_core::{impls, Error, RngCore, SeedableRng};

/// WyHash hasher builder
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Copy, Hash)]
pub struct WyHasherBuilder {
seed: u64,
secret: [u64; 4],
}

impl WyHasherBuilder {
/// Create hasher builder with a seed
pub fn new(seed: u64, secret: [u64; 4]) -> Self {
WyHasherBuilder { seed, secret }
}
}

impl Default for WyHasherBuilder {
fn default() -> Self {
WyHasherBuilder::new(0, [P0, P1, P2, P3])
}
}

impl BuildHasher for WyHasherBuilder {
type Hasher = WyHash;

fn build_hasher(&self) -> Self::Hasher {
WyHash::new(self.seed, self.secret)
}
}

/// WyHash hasher
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Copy, Hash)]
pub struct WyHash {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ pub mod v1;
/// WyHash version final 3
pub mod final3;
pub use crate::final3::{make_secret, wyhash, wyrng};
pub use crate::final3::{WyHash, WyRng};
pub use crate::final3::{WyHash, WyRng, WyHasherBuilder};
2 changes: 1 addition & 1 deletion src/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pub(crate) mod functions;
pub use functions::{wyhash, wyrng};

mod traits;
pub use traits::{WyHash, WyRng};
pub use traits::{WyHash, WyRng, WyHasherBuilder};
29 changes: 28 additions & 1 deletion src/v1/traits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
use crate::v1::functions::{mix_with_p0, read64, wyhash_core, wyhash_finish, wyrng};
use core::hash::Hasher;
use core::hash::{Hasher, BuildHasher};
use rand_core::{impls, Error, RngCore, SeedableRng};

/// WyHash hasher builder
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Copy, Hash)]
pub struct WyHasherBuilder {
seed: u64,
}

impl WyHasherBuilder {
/// Create hasher builder with a seed
pub fn new(seed: u64) -> Self {
WyHasherBuilder { seed }
}
}

impl Default for WyHasherBuilder {
fn default() -> Self {
WyHasherBuilder::new(0)
}
}

impl BuildHasher for WyHasherBuilder {
type Hasher = WyHash;

fn build_hasher(&self) -> Self::Hasher {
WyHash { h: self.seed, size: 0 }
}
}

/// WyHash hasher
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct WyHash {
Expand Down
23 changes: 22 additions & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate core;
extern crate wyhash;
use core::hash::Hasher;
use core::hash::{Hasher, BuildHasher};
use wyhash::{final3, v1};

#[test]
Expand All @@ -21,3 +21,24 @@ fn final3_default_constructed() {
hasher.write(&[0]);
assert_eq!(expected, hasher.finish());
}

#[test]
fn v1_default_constructed_build_hasher() {
let builder = v1::WyHasherBuilder::default();
let mut hasher = builder.build_hasher();
hasher.write(&[0]);
assert_eq!(0x8c73_a8ab_4659_6ae4, hasher.finish());
}

#[test]
fn final3_default_constructed_build_hasher() {
#[cfg(not(feature = "mum32bit"))]
let expected = 0x22a2_d5db_3856_770f;
#[cfg(feature = "mum32bit")]
let expected = 0x6a2c_d506_62d4_cdf1;

let builder = final3::WyHasherBuilder::default();
let mut hasher = builder.build_hasher();
hasher.write(&[0]);
assert_eq!(expected, hasher.finish());
}