[−][src]Crate prehash
The prehash crate provides the type Prehashed, which stores a value of
any type along with a precomputed hash. This makes it possible to avoid
computing large expensive hashes many times, for example when searching for
a particular value in a variety of hash tables.
The crate also defines an extremely simple Hasher, Passthru, which
is tailor built for use with Prehashed. If the std feature is
selected (which is done by default), type definitions and constructors are
provided for the standard HashMap and HashSet collections which
combine Prehashed keys/elements with the Passthru hasher for optimum
performance.
Example
use prehash::{new_prehashed_set, DefaultPrehasher, Prehasher}; let prehasher = DefaultPrehasher::new(); let mut zeros = new_prehashed_set(); let mut evens = new_prehashed_set(); let mut odds = new_prehashed_set(); // 'evens' and 'odds' are going to re-hash a couple times, but they'll only // need to recompute the hashes of their keys once. (The rehashing process // does provide some resilience against denial-of-service attacks in some // theoretical cases, but the primary form of resilience comes from using an // unpredictable hash function in the first place.) zeros.insert(prehasher.prehash(0)); for num in 0..100000 { if num % 2 == 0 { evens.insert(prehasher.prehash(num)); } else { odds.insert(prehasher.prehash(num)); } } for num in 0..100000 { // We only have to compute the hash of 'num' once, despite checking it // against three different sets. This would be a big deal if computing // the hash of 'num' is expensive. let prehashed = prehasher.prehash(num); if zeros.contains(&prehashed) { assert_eq!(num, 0) } if evens.contains(&prehashed) { assert_eq!(num % 2, 0); } if odds.contains(&prehashed) { assert_eq!(num % 2, 1); } }
License
prehash is licensed under the terms of the
Mozilla Public License, v. 2.0. All Source Code Forms are
"Incompatible With Secondary Licenses", as described in §3.3 of the
license.
Development
prehash is developed at GitLab.
Structs
| Passthru | |
| Prehashed | A value (of type |
Traits
| Prehasher | A convenience trait for producing |
Functions
| new_prehashed_map | stdConstructs a new standard hash map with |
| new_prehashed_map_with_capacity | stdConstructs a new standard hash map with |
| new_prehashed_set | stdConstructs a new standard hash set with |
| new_prehashed_set_with_capacity | stdConstructs a new standard hash set with |
Type Definitions
| DefaultPrehasher | stdA secure choice of hasher for pre-hashing values. |
| PrehashedMap | stdA standard hash map with |
| PrehashedSet | stdA standard hash set with |