Add hashing pointers

This commit is contained in:
Eugen Wissner 2018-03-26 21:38:38 +02:00
parent 72140a8583
commit 92f21a95cf

View File

@ -38,7 +38,11 @@ private struct FNV
void opCall(T)(auto ref T key) void opCall(T)(auto ref T key)
{ {
static if (isScalarType!T) static if (is(typeof(key.toHash()) == size_t))
{
opCall(key.toHash()); // Combine user-defined hashes
}
else static if (isScalarType!T || isPointer!T)
{ {
(() @trusted => add((cast(const ubyte*) &key)[0 .. T.sizeof]))(); (() @trusted => add((cast(const ubyte*) &key)[0 .. T.sizeof]))();
} }
@ -50,10 +54,6 @@ private struct FNV
{ {
add(key); add(key);
} }
else static if (is(typeof(key.toHash()) == size_t))
{
opCall(key.toHash()); // Combine user-defined hashes
}
else static if (isInputRange!T && !isInfinite!T) else static if (isInputRange!T && !isInfinite!T)
{ {
foreach (e; key) foreach (e; key)
@ -186,7 +186,6 @@ static if (size_t.sizeof == 4) @nogc nothrow pure @safe unittest
assert(hash(HashRange()) == 0x6222e842U); assert(hash(HashRange()) == 0x6222e842U);
assert(hash(ToHashRange()) == 1268118805U); assert(hash(ToHashRange()) == 1268118805U);
} }
static if (size_t.sizeof == 8) @nogc nothrow pure @safe unittest static if (size_t.sizeof == 8) @nogc nothrow pure @safe unittest
{ {
assert(hash('a') == 0xaf63dc4c8601ec8cUL); assert(hash('a') == 0xaf63dc4c8601ec8cUL);
@ -194,6 +193,15 @@ static if (size_t.sizeof == 8) @nogc nothrow pure @safe unittest
assert(hash(ToHashRange()) == 12161962213042174405UL); assert(hash(ToHashRange()) == 12161962213042174405UL);
} }
static if (size_t.sizeof == 4) @nogc nothrow pure @system unittest
{
assert(hash(cast(void*) 0x6e6f6863) == 0xac297727U);
}
static if (size_t.sizeof == 8) @nogc nothrow pure @system unittest
{
assert(hash(cast(void*) 0x77206f676e6f6863) == 0xd1edd10b507344d0UL);
}
/* /*
* These are official FNV-1a test vectors and they are in the public domain. * These are official FNV-1a test vectors and they are in the public domain.
*/ */