Skip to content

Latest commit

 

History

History
110 lines (78 loc) · 2.87 KB

README.md

File metadata and controls

110 lines (78 loc) · 2.87 KB

rust-fasthash travis build crate docs

A suite of non-cryptographic hash functions for Rust, base on a forked smhasher.

Usage

To use fasthash, first add this to your Cargo.toml:

[dependencies]
fasthash = "0.2"

When use fasthash 128bit supports in Rust 1.15 (beta) or earlier version, we need enable i128 feature.

[dependencies.fasthash]
version = "0.2"
features = ["i128", "sse42"]

Then, add this to your crate root

extern crate fasthash;

use fasthash::*;

And then, use hash function with module or hasher

let h = city::hash64("hello world");

Or work with std::hash::Hash

use std::hash::Hash;

use fasthash::MetroHasher;

fn hash<T: Hash>(t: &T) -> u64 {
    let mut s = MetroHasher::new();
    t.hash(&mut s);
    s.finish()
}

hash(&"hello world");

It also works with HashMap or HashSet, act as the hash function

use std::collections::HashSet;

use fasthash::spooky::SpookyHash128;

let mut set = HashSet::with_hasher(SpookyHash128 {});
set.insert(2);

Or use RandomState with a random seed.

use std::collections::HashMap;

use fasthash::RandomState;
use fasthash::city::CityHash64;

let s = RandomState::<CityHash64>::new();
let mut map = HashMap::with_hasher(s);

assert_eq!(map.insert(37, "a"), None);
assert_eq!(map.is_empty(), false);

map.insert(37, "b");
assert_eq!(map.insert(37, "c"), Some("b"));
assert_eq!(map[&37], "c");

Goal

  • High performance
  • Zero cost binding
  • Compatibility with libstd/libcollection

Features

Performance

To bench the hash function, we need nighly rust

$ rustup run nightly cargo bench

Please check smhasher reports for more details.