Computes MurmurHash hashes of arbitrary data. MurmurHash is a non-cryptographic hash function suitable for general hash-based lookup. It is optimized for x86 but can be used on all architectures.
The current version is MurmurHash3, which yields a 32-bit or 128-bit hash value. The older MurmurHash 1 and 2 are currently not supported.
MurmurHash3 comes in three flavors, listed in increasing order of throughput:
MurmurHash3!32produces a 32-bit value and is optimized for 32-bit architectures
MurmurHash3!(128, 32)produces a 128-bit value and is optimized for 32-bit architectures
MurmurHash3!(128, 64)produces a 128-bit value and is optimized for 64-bit architectures
MurmurHash3!(128, 64)produce different values.
std.digest. This module publicly imports
std.digestand can be used as a stand-alone module.
// MurmurHash3!32, MurmurHash3!(128, 32) and MurmurHash3!(128, 64) implement // the std.digest Template API. static assert(isDigest!(MurmurHash3!32)); // The convenient digest template allows for quick hashing of any data. ubyte hashed = digest!(MurmurHash3!32)([1, 2, 3, 4]); writeln(hashed); // [0, 173, 69, 68]
// One can also hash ubyte data piecewise by instanciating a hasher and call // the 'put' method. const(ubyte) data1 = [1, 2, 3]; const(ubyte) data2 = [4, 5, 6, 7]; // The incoming data will be buffered and hashed element by element. MurmurHash3!32 hasher; hasher.put(data1); hasher.put(data2); // The call to 'finish' ensures: // - the remaining bits are processed // - the hash gets finalized auto hashed = hasher.finish(); writeln(hashed); // [181, 151, 88, 252]
// Using `putElements`, `putRemainder` and `finalize` you gain full // control over which part of the algorithm to run. // This allows for maximum throughput but needs extra care. // Data type must be the same as the hasher's element type: // - uint for MurmurHash3!32 // - uint for MurmurHash3!(128, 32) // - ulong for MurmurHash3!(128, 64) const(uint) data = [1, 2, 3, 4]; // Note the hasher starts with 'Fast'. MurmurHash3!32 hasher; // Push as many array of elements as you need. The less calls the better. hasher.putElements(data); // Put remainder bytes if needed. This method can be called only once. hasher.putRemainder(ubyte(1), ubyte(1), ubyte(1)); // Call finalize to incorporate data length in the hash. hasher.finalize(); // Finally get the hashed value. auto hashed = hasher.getBytes(); writeln(hashed); // [188, 165, 108, 2]
MurmurHash3 functions. You can specify the
size of the hash in bit. For 128 bit hashes you can specify whether to optimize for 32 or 64 bit architectures. If you don't specify the
opt value it will select the fastest version of the host platform.
This hasher is compatible with the
void put(scope const(ubyte) data...)
void putElements(scope const(Element) elements...)
void putRemainder(scope const(ubyte) data...)
The element type for 32-bit implementation.
Adds a single Element of data without increasing
element_count. Make sure to increase
Element.sizeof for each call to
Put remainder bytes. This must be called only once after
putElement and before
element_count and finalizes the hash.
Returns the hash as an uint value.
Returns the current hashed value as an ubyte array.
Pushes an array of
elements at once. It is more efficient to push as much data as possible in a single call. On platforms that do not support unaligned reads (MIPS or old ARM chips), the compiler may produce slower code to ensure correctness.
data to the digester. This function can be called many times in a row after start but before finish.
Finalizes the computation of the hash and returns the computed value. Note that
finish can be called only once and that no subsequent calls to
put is allowed.
© 1999–2017 The D Language Foundation
Licensed under the Boost License 1.0.