Don't use the default hasher

This commit is contained in:
Thinkofname 2016-03-22 19:49:11 +00:00
parent 11a4fcb33d
commit 49a749534c
1 changed files with 25 additions and 3 deletions

View File

@ -17,6 +17,7 @@ use self::block::BlockSet;
use std::sync::Arc;
use std::collections::HashMap;
use std::hash::{BuildHasherDefault, Hasher};
use types::bit;
use types::nibble;
use protocol;
@ -197,7 +198,7 @@ impl World {
section.dirty = true;
let bit_size = try!(data.read_u8());
let mut block_map = HashMap::new();
let mut block_map = HashMap::with_hasher(BuildHasherDefault::<FNVHash>::default());
if bit_size <= 8 {
let count = try!(VarInt::read_from(&mut data)).0;
for i in 0 .. count {
@ -359,6 +360,27 @@ impl Chunk {
}
}
struct FNVHash(u64);
impl Hasher for FNVHash {
fn write(&mut self, bytes: &[u8]) {
for b in bytes {
self.0 = self.0.wrapping_mul(0x100000001b3);
self.0 ^= *b as u64
}
}
fn finish(&self) -> u64 {
self.0
}
}
impl Default for FNVHash {
fn default() -> Self {
FNVHash(0xcbf29ce484222325)
}
}
#[derive(PartialEq, Eq, Hash)]
pub struct SectionKey {
pos: (i32, u8, i32),
@ -370,7 +392,7 @@ struct Section {
blocks: bit::Map,
block_map: Vec<(&'static block::Block, u32)>,
rev_block_map: HashMap<usize, usize>,
rev_block_map: HashMap<usize, usize, BuildHasherDefault<FNVHash>>,
block_light: nibble::Array,
sky_light: nibble::Array,
@ -391,7 +413,7 @@ impl Section {
block_map: vec![
(block::AIR.base(), 0xFFFFFFFF)
],
rev_block_map: HashMap::new(),
rev_block_map: HashMap::with_hasher(BuildHasherDefault::default()),
block_light: nibble::Array::new(16 * 16 * 16),
sky_light: nibble::Array::new(16 * 16 * 16),