Skip to content

Instantly share code, notes, and snippets.

@unleashy
Created June 9, 2018 22:51
Show Gist options
  • Save unleashy/1f3eae87841eb660dca86203e41aa9e2 to your computer and use it in GitHub Desktop.
Save unleashy/1f3eae87841eb660dca86203e41aa9e2 to your computer and use it in GitHub Desktop.
tabulation hashing
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