From 92f21a95cfcff3266945435e6e3132b9d5809775 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Mon, 26 Mar 2018 21:38:38 +0200 Subject: [PATCH] Add hashing pointers --- source/tanya/hash/lookup.d | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/source/tanya/hash/lookup.d b/source/tanya/hash/lookup.d index 2ca5b66..b006239 100644 --- a/source/tanya/hash/lookup.d +++ b/source/tanya/hash/lookup.d @@ -38,7 +38,11 @@ private struct FNV 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]))(); } @@ -50,10 +54,6 @@ private struct FNV { 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) { foreach (e; key) @@ -186,7 +186,6 @@ static if (size_t.sizeof == 4) @nogc nothrow pure @safe unittest assert(hash(HashRange()) == 0x6222e842U); assert(hash(ToHashRange()) == 1268118805U); } - static if (size_t.sizeof == 8) @nogc nothrow pure @safe unittest { assert(hash('a') == 0xaf63dc4c8601ec8cUL); @@ -194,6 +193,15 @@ static if (size_t.sizeof == 8) @nogc nothrow pure @safe unittest 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. */