From c8759c9dff0dfc8262150529bd200a37dcc219ce Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 15 Sep 2016 23:27:32 -0700 Subject: [PATCH 1/2] Add FnvHashMap and FnvHashSet type aliases This is a much easier way to use FNV in the standard collections. --- README.md | 35 ++++++++++++++++++++--------------- lib.rs | 51 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 53 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 074d2c8..96001e2 100644 --- a/README.md +++ b/README.md @@ -39,34 +39,39 @@ fnv = "1.0.3" ## Using FNV in a HashMap -To configure a `HashMap` in the standard library to use the FNV hasher, you -must create a default instance of a `FnvHasher` state, then create a new -map using this state with `HashMap::with_hash_state`. A full example: +The `FnvHashMap` type alias is the easiest way to use the standard library’s +`HashMap` with FNV. ```rust -use std::collections::HashMap; -use std::hash::BuildHasherDefault; -use fnv::FnvHasher; +use fnv::FnvHashMap; -let fnv = BuildHasherDefault::::default(); -let mut map = HashMap::with_hasher(fnv); +let mut map = FnvHashMap::default(); +map.insert(1, "one"); +map.insert(2, "two"); + +map = FnvHashMap::with_capacity_and_hasher(10, Default::default()); map.insert(1, "one"); map.insert(2, "two"); ``` +Note, the standard library’s `HashMap::new` and `HashMap::with_capacity` +are only implemented for the `RandomState` hasher, so using `Default` to +get the hasher is the next best option. + ## Using FNV in a HashSet -The standard library’s `HashSet` can be configured to use the FNV hasher -with the same mechanism. +Similarly, `FnvHashSet` is a type alias for the standard library’s `HashSet` +with FNV. ```rust -use std::collections::HashSet; -use std::hash::BuildHasherDefault; -use fnv::FnvHasher; +use fnv::FnvHashSet; + +let mut set = FnvHashSet::default(); +set.insert(1); +set.insert(2); -let fnv = BuildHasherDefault::::default(); -let mut set = HashSet::with_hasher(fnv); +set = FnvHashSet::with_capacity_and_hasher(10, Default::default()); set.insert(1); set.insert(2); ``` diff --git a/lib.rs b/lib.rs index b191f23..1fc5d28 100644 --- a/lib.rs +++ b/lib.rs @@ -22,36 +22,40 @@ //! small—a perfect use case for FNV. //! //! -//! ## Using FNV in a HashMap +//! ## Using FNV in a `HashMap` //! -//! To configure a `HashMap` in the standard library to use the FNV hasher, you -//! must create a default instance of a `FnvHasher` state, then create a new -//! map using this state with `HashMap::with_hash_state`. A full example: +//! The `FnvHashMap` type alias is the easiest way to use the standard library’s +//! `HashMap` with FNV. //! //! ```rust -//! use std::collections::HashMap; -//! use std::hash::BuildHasherDefault; -//! use fnv::FnvHasher; +//! use fnv::FnvHashMap; //! -//! let fnv = BuildHasherDefault::::default(); -//! let mut map = HashMap::with_hasher(fnv); +//! let mut map = FnvHashMap::default(); +//! map.insert(1, "one"); +//! map.insert(2, "two"); +//! +//! map = FnvHashMap::with_capacity_and_hasher(10, Default::default()); //! map.insert(1, "one"); //! map.insert(2, "two"); //! ``` //! +//! Note, the standard library’s `HashMap::new` and `HashMap::with_capacity` +//! are only implemented for the `RandomState` hasher, so using `Default` to +//! get the hasher is the next best option. //! -//! ## Using FNV in a HashSet +//! ## Using FNV in a `HashSet` //! -//! The standard library’s `HashSet` can be configured to use the FNV hasher -//! with the same mechanism. +//! Similarly, `FnvHashSet` is a type alias for the standard library’s `HashSet` +//! with FNV. //! //! ```rust -//! use std::collections::HashSet; -//! use std::hash::BuildHasherDefault; -//! use fnv::FnvHasher; +//! use fnv::FnvHashSet; +//! +//! let mut set = FnvHashSet::default(); +//! set.insert(1); +//! set.insert(2); //! -//! let fnv = BuildHasherDefault::::default(); -//! let mut set = HashSet::with_hasher(fnv); +//! set = FnvHashSet::with_capacity_and_hasher(10, Default::default()); //! set.insert(1); //! set.insert(2); //! ``` @@ -62,7 +66,8 @@ use std::default::Default; -use std::hash::Hasher; +use std::hash::{Hasher, BuildHasherDefault}; +use std::collections::{HashMap, HashSet}; /// An implementation of the Fowler–Noll–Vo hash function. /// @@ -106,6 +111,16 @@ impl Hasher for FnvHasher { } } +/// A builder for default FNV hashers. +pub type FnvBuildHasher = BuildHasherDefault; + +/// A `HashMap` using a default FNV hasher. +pub type FnvHashMap = HashMap; + +/// A `HashSet` using a default FNV hasher. +pub type FnvHashSet = HashSet; + + #[cfg(test)] mod test { use super::*; From fd28754914c505a59ce9ab1fc5374e2245ac342b Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 16 Sep 2016 23:09:39 -0700 Subject: [PATCH 2/2] Bump to 1.0.5 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index db37da5..c401150 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fnv" -version = "1.0.4" +version = "1.0.5" authors = ["Alex Crichton "] description = "Fowler–Noll–Vo hash function" license = "Apache-2.0 / MIT"