std.digest.murmurhash

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!32 produces 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

Note

  • MurmurHash3!(128, 32) and MurmurHash3!(128, 64) produce different values.
  • The current implementation is optimized for little endian architectures.

    It will exhibit different results on big endian architectures and a slightly less uniform distribution.

This module conforms to the APIs defined in std.digest.

This module publicly imports std.digest and can be used as a stand-alone module.

Source: std/digest/murmurhash.d

License

Boost License 1.0.

Authors

Guillaume Chatelet

References: Reference implementation


Wikipedia

Types 1

structMurmurHash3(uint size /* 32 or 128 */ , uint opt = size_t.sizeof == 8 ? 64 : 32)

Implements the 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 Digest API:

  • void start()
  • void put(scope const(ubyte)[] data...)
  • ubyte[Element.sizeof] finish()

It also provides a faster, low level API working with data of size Element.sizeof:

  • void putElements(scope const(Element[]) elements...)
  • void putRemainder(scope const(ubyte[]) data...)
  • void finalize()
  • Element get()
  • ubyte[Element.sizeof] getBytes()
Fields
size blockSize
size_t element_count
private BufferUnion buffer
private size_t bufferSize
Methods
void putElements(scope const(Element[]) elements...) pure nothrow @nogcPushes 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 m...
void start()
void put(scope const(ubyte)[] data...) pure nothrowAdds data to the digester. This function can be called many times in a row after start but before finish.
ubyte[Element.sizeof] finish() pure nothrowFinalizes 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.
private T rotl(T)(T x, uint y)
private T shuffle(T)(T k, T c1, T c2, ubyte r1)
private T update(T)(ref T h, T k, T mixWith, T c1, T c2, ubyte r1, ubyte r2, T n)
private uint fmix(uint h) pure nothrow @nogc
private ulong fmix(ulong k) pure nothrow @nogc