CyclicHash.d
#kód
import core.bitop; import std.random; /* Massively simplified version of https://github.com/lemire/rollinghashcpp/blob/master/cyclichash.h by Daniel Lemire License: Apache 2.0 */ struct CyclicHash(HashT = uint) { HashT hash = 0; private uint n; HashT[1 << 8] table; this(uint _n, uint seed = 1337) { n = _n; auto rng = Random(seed); foreach (ref h; table) { h = uniform!HashT(rng); } } /* add inByte and remove outByte from the hash */ void update(T : ubyte)(T outByte, T inByte) { hash = rol!1(hash) ^ rol(table[outByte], n) ^ table[inByte]; } /* add byte to the hash, this is used at the start to initialize hash */ void add(T : ubyte)(T b) { hash = rol!1(hash) ^ table[b]; } void init(T : ubyte)(T[] bytes) { foreach (i; 0 .. n) { add(bytes[i]); } } void reset() { hash = 0; } }