Created
June 9, 2018 22:51
-
-
Save unleashy/1f3eae87841eb660dca86203e41aa9e2 to your computer and use it in GitHub Desktop.
tabulation hashing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
import std.datetime; | |
@safe: | |
private immutable ulong[256][8] tables_; | |
static this() | |
{ | |
import std.random : unpredictableSeed; | |
foreach (ref table; tables_) { | |
foreach (ref b; table) { | |
b = unpredictableSeed(); | |
} | |
} | |
} | |
ulong hash(in ulong key) @trusted @nogc pure nothrow | |
{ | |
/** | |
* Trusted operation: taking address of parameter | |
*/ | |
const bytes = (cast(const(ubyte)*) &key)[0 .. ulong.sizeof / ubyte.sizeof]; | |
ulong h = 0; | |
static foreach (i; 0 .. tables_.length) { | |
h ^= tables_[i][bytes[i]]; | |
} | |
return h; | |
} | |
void main() | |
{ | |
immutable start = Clock.currTime; | |
foreach (i; 0 .. 100_000) { | |
hash(i); | |
} | |
immutable delta = Clock.currTime - start; | |
writeln(delta); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment