Skip to content

Commit

Permalink
extend hasher mock
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurprs committed Jul 6, 2016
1 parent 4b86220 commit 1a5bfd2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![feature(asm)]
#![feature(test)]
#![feature(test)]#![feature(sip_hash_13)]
#![cfg_attr(test, feature(hashmap_hasher))]
#![allow(unused_imports, dead_code)]

Expand Down Expand Up @@ -31,6 +31,19 @@ fn main() {
do_it().unwrap();
}

#[test]
fn test_diff() {
use std::hash::Hasher;
use std::hash::Hash;
let mut hasher1 = sip_opt::SipHasher::default();
let a = (b"12", b"34");
a.hash(&mut hasher1);
let mut hasher2 = sip_opt::SipHasher::default();
let b = (b"123", b"4");
b.hash(&mut hasher2);
assert!(hasher1.finish() != hasher2.finish());
}

struct DataPoint {
magnitude: u64,
average: u64,
Expand Down Expand Up @@ -121,7 +134,7 @@ fn do_it() -> IoResult<()> {

macro_rules! hash_benches {
($Impl: ty) => {
use std::hash::SipHasher as Sip;
use std::hash::SipHasher13 as Sip;
use twox_hash::XxHash as Xx;
// use murmurhash64 as murmur2;
// use murmurhash3::Murmur3State as Murmur3State;
Expand Down Expand Up @@ -304,10 +317,10 @@ macro_rules! tree_benches {
}

#[cfg(test)] mod sip { hash_benches!{Sip} }
#[cfg(test)] mod xx { hash_benches!{Xx} }
#[cfg(test)] mod farm { hash_benches!{Farm} }
#[cfg(test)] mod fnv { hash_benches!{Fnv} }
#[cfg(test)] mod horner { hash_benches!{HornerHasher} }
// #[cfg(test)] mod xx { hash_benches!{Xx} }
// #[cfg(test)] mod farm { hash_benches!{Farm} }
// #[cfg(test)] mod fnv { hash_benches!{Fnv} }
// #[cfg(test)] mod horner { hash_benches!{HornerHasher} }
#[cfg(test)] mod sip_opt_b { hash_benches!{SipOpt} }

// one day?
Expand Down
16 changes: 16 additions & 0 deletions src/sip_opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub struct SipHasher {
v3: u64,
tail: u64, // unprocessed bytes le
ntail: usize, // how many bytes in tail are valid
delimiters: usize,
}

// sadly, these macro definitions can't appear later,
Expand Down Expand Up @@ -121,6 +122,7 @@ impl SipHasher {
v3: 0,
tail: 0,
ntail: 0,
delimiters: 0,
};
state.reset();
state
Expand All @@ -138,8 +140,21 @@ impl SipHasher {
}

impl Hasher for SipHasher {
#[inline]
fn write_usize(&mut self, i: usize) {
self.delimiters += 1;
if self.delimiters > 1 {
use std;
let bytes = unsafe {
std::slice::from_raw_parts(&i as *const usize as *const u8, std::mem::size_of::<usize>())
};
self.write(bytes);
}
}

#[inline]
fn write(&mut self, msg: &[u8]) {
// println!("write {:?}", msg);
let length = msg.len();
self.length += length;

Expand Down Expand Up @@ -220,6 +235,7 @@ impl Clone for SipHasher {
v3: self.v3,
tail: self.tail,
ntail: self.ntail,
delimiters: 0,
}
}
}
Expand Down

0 comments on commit 1a5bfd2

Please sign in to comment.