#hash #hashing

fasthash

A suite of non-cryptographic hash functions for Rust

14 unstable releases (3 breaking)

Uses old Rust 2015

0.4.0 Mar 13, 2019
0.3.2 Aug 6, 2018
0.3.1 Jul 28, 2018
0.2.8 Jan 22, 2018
0.1.1 Dec 25, 2016

#2202 in Algorithms

Download history 1909/week @ 2023-10-26 1966/week @ 2023-11-02 1869/week @ 2023-11-09 2352/week @ 2023-11-16 1828/week @ 2023-11-23 2031/week @ 2023-11-30 1744/week @ 2023-12-07 2142/week @ 2023-12-14 1466/week @ 2023-12-21 824/week @ 2023-12-28 1399/week @ 2024-01-04 1708/week @ 2024-01-11 1958/week @ 2024-01-18 2061/week @ 2024-01-25 2016/week @ 2024-02-01 1745/week @ 2024-02-08

8,158 downloads per month
Used in 24 crates (22 directly)

Apache-2.0

1MB
30K SLoC

C++ 17K SLoC // 0.1% comments C 8K SLoC // 0.2% comments Rust 4K SLoC Visual Studio Project 1K SLoC Assembly 306 SLoC // 0.3% comments Visual Studio Solution 62 SLoC Shell 31 SLoC // 0.2% comments Batch 17 SLoC Perl 10 SLoC

A suite of non-cryptographic hash functions for Rust.

Example

use std::hash::{Hash, Hasher};

use fasthash::{metro, MetroHasher};

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

let h = metro::hash64(b"hello world\xff");

assert_eq!(h, hash(&"hello world"));

By default, HashMap uses a hashing algorithm selected to provide resistance against HashDoS attacks. The hashing algorithm can be replaced on a per-HashMap basis using the HashMap::with_hasher or HashMap::with_capacity_and_hasher methods.

It also cowork with HashMap or HashSet, act as a hash function

use std::collections::HashSet;

use fasthash::spooky::Hash128;

let mut set = HashSet::with_hasher(Hash128);
set.insert(2);

Or use RandomState<CityHash64> with a random seed.

use std::collections::HashMap;

use fasthash::{city, RandomState};

let s = RandomState::<city::Hash64>::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");

Dependencies