mirror of
https://gitee.com/openharmony/third_party_rust_rustc-hash
synced 2024-11-23 07:49:54 +00:00
Hash up to 8 bytes at once with FxHasher
This commit is contained in:
parent
abd3d05849
commit
970081f180
12
Cargo.lock
generated
12
Cargo.lock
generated
@ -1,4 +1,14 @@
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hash"
|
||||
version = "0.1.0"
|
||||
version = "1.0.0"
|
||||
dependencies = [
|
||||
"byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[metadata]
|
||||
"checksum byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "74c0b906e9446b0a2e4f760cdb3fa4b2c48cdc6db8766a845c54b6ff063fd2e9"
|
||||
|
@ -9,3 +9,4 @@ keywords = ["hash", "fxhash", "rustc"]
|
||||
repository = "https://github.com/rust-lang-nursery/rustc-hash"
|
||||
|
||||
[dependencies]
|
||||
byteorder = "1.1"
|
||||
|
32
src/lib.rs
32
src/lib.rs
@ -18,10 +18,15 @@
|
||||
//! map.insert(22, 44);
|
||||
//! ```
|
||||
|
||||
extern crate byteorder;
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::default::Default;
|
||||
use std::hash::{Hasher, BuildHasherDefault};
|
||||
use std::ops::BitXor;
|
||||
use std::mem::size_of;
|
||||
|
||||
use byteorder::{ByteOrder, NativeEndian};
|
||||
|
||||
/// Type alias for a hashmap using the `fx` hash algorithm.
|
||||
pub type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
|
||||
@ -65,11 +70,30 @@ impl FxHasher {
|
||||
|
||||
impl Hasher for FxHasher {
|
||||
#[inline]
|
||||
fn write(&mut self, bytes: &[u8]) {
|
||||
for byte in bytes {
|
||||
let i = *byte;
|
||||
self.add_to_hash(i as usize);
|
||||
fn write(&mut self, mut bytes: &[u8]) {
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
let read_usize = |bytes| NativeEndian::read_u32(bytes);
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
let read_usize = |bytes| NativeEndian::read_u64(bytes);
|
||||
|
||||
let mut hash = FxHasher { hash: self.hash };
|
||||
assert!(size_of::<usize>() <= 8);
|
||||
while bytes.len() >= size_of::<usize>() {
|
||||
hash.add_to_hash(read_usize(bytes) as usize);
|
||||
bytes = &bytes[size_of::<usize>()..];
|
||||
}
|
||||
if (size_of::<usize>() > 4) && (bytes.len() >= 4) {
|
||||
hash.add_to_hash(NativeEndian::read_u32(bytes) as usize);
|
||||
bytes = &bytes[4..];
|
||||
}
|
||||
if (size_of::<usize>() > 2) && bytes.len() >= 2 {
|
||||
hash.add_to_hash(NativeEndian::read_u16(bytes) as usize);
|
||||
bytes = &bytes[2..];
|
||||
}
|
||||
if (size_of::<usize>() > 1) && bytes.len() >= 1 {
|
||||
hash.add_to_hash(bytes[0] as usize);
|
||||
}
|
||||
self.hash = hash.hash;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
Loading…
Reference in New Issue
Block a user