Crate souphash

Crate souphash 

Source
Expand description

SoupHash: an order-indipendent hash function

SoupHash is a non-cryptographic hash function whose output does not depend on the order of the elements that are fed into it. It is ideal for:

  • hashing of unordered collections (like hash sets and hash maps);
  • hashing large collections in parallel, using multiple threads or processes, without locks or any form of synchronization.

SoupHash is based on the popular SipHash hashing algorithm, specifically the SipHash 2-4 variant. At this moment the number of rounds used by SoupHash is not configurable (but this may change in the future).

Check the documentation for the SoupHasher struct for more details and examples.

§Examples

use souphash::SoupHasher;

let mut hasher = SoupHasher::new();

// Add a few elements in arbitrary order: elements of any type are
// accepted, as long as they implement `std::hash::Hash`
hasher.add(123);
hasher.add("abc");
hasher.add([1, 2, 3]);

// Compute the final hash
let hash = hasher.finish();
assert_eq!(hash, 0xbe6f445accb8829d);

// Now repeat the same procedure as above, but this time with the elements
// in a different order: notice that the final hash does not change
let mut hasher = SoupHasher::new();
hasher.add([1, 2, 3]);
hasher.add(123);
hasher.add("abc");
let hash = hasher.finish();
assert_eq!(hash, 0xbe6f445accb8829d);

Structs§

KeyMismatch
Error returned by SoupHasher::try_combine() when trying to merge two hashers constructed with different keys.
SoupElementHasher
Hasher for a single element to be fed into SoupHasher.
SoupHasher
Implementation for SoupHash.