Using SipHasher from crates.io

This commit is contained in:
Tomasz Drwięga
2016-10-21 11:57:30 +02:00
parent 5aadda95c3
commit d3ae463a75
3 changed files with 27 additions and 8 deletions

View File

@@ -7,3 +7,6 @@ license = "GPL3"
[lib]
path = "src/lib.rs"
[dependencies]
siphasher = "0.1.1"

View File

@@ -14,12 +14,18 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
extern crate siphasher;
use std::cmp;
use std::mem;
use std::f64;
use std::hash::{Hash, Hasher, BuildHasher};
use std::collections::hash_map::RandomState;
use std::hash::{Hash, Hasher};
use std::collections::HashSet;
use siphasher::sip::SipHasher;
// TODO [ToDr] Both hashers are exactly the same - no point to keep two.
const NUMBER_OF_HASHERS: usize = 2;
/// BitVec structure with journalling
/// Every time any of the blocks is getting set it's index is tracked
@@ -74,7 +80,8 @@ pub struct Bloom {
bitmap: BitVecJournal,
bitmap_bits: u64,
k_num: u32,
sips: [RandomState; 2],
// TODO [ToDr] Both hashers are exactly the same - no point to keep two.
sips: [SipHasher; NUMBER_OF_HASHERS],
}
impl Bloom {
@@ -86,7 +93,7 @@ impl Bloom {
let bitmap_bits = (bitmap_size as u64) * 8u64;
let k_num = Bloom::optimal_k_num(bitmap_bits, items_count);
let bitmap = BitVecJournal::new(bitmap_bits as usize);
let sips = [RandomState::default(), RandomState::default()];
let sips = [SipHasher::new(), SipHasher::new()];
Bloom {
bitmap: bitmap,
bitmap_bits: bitmap_bits,
@@ -100,7 +107,7 @@ impl Bloom {
let bitmap_size = parts.len() * 8;
let bitmap_bits = (bitmap_size as u64) * 8u64;
let bitmap = BitVecJournal::from_parts(parts);
let sips = [RandomState::default(), RandomState::default()];
let sips = [SipHasher::new(), SipHasher::new()];
Bloom {
bitmap: bitmap,
bitmap_bits: bitmap_bits,
@@ -171,11 +178,11 @@ impl Bloom {
cmp::max(k_num, 1)
}
fn bloom_hash<T>(&self, hashes: &mut [u64; 2], item: &T, k_i: u32) -> u64
fn bloom_hash<T>(&self, hashes: &mut [u64; NUMBER_OF_HASHERS], item: &T, k_i: u32) -> u64
where T: Hash
{
if k_i < 2 {
let mut sip = self.sips[k_i as usize].build_hasher();
if k_i < NUMBER_OF_HASHERS as u32 {
let mut sip = self.sips[k_i as usize].clone();
item.hash(&mut sip);
let hash = sip.finish();
hashes[k_i as usize] = hash;