Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function murmurhash3_32(key, seed = 0) {
- const c1 = 0xcc9e2d51;
- const c2 = 0x1b873593;
- const r1 = 15;
- const r2 = 13;
- const m = 5;
- const n = 0xe6546b64;
- let hash = seed;
- let len = key.length;
- const getUint32 = (str, pos) => {
- return (str.charCodeAt(pos) & 0xff) |
- ((str.charCodeAt(pos + 1) & 0xff) << 8) |
- ((str.charCodeAt(pos + 2) & 0xff) << 16) |
- ((str.charCodeAt(pos + 3) & 0xff) << 24);
- };
- let i = 0;
- while (i + 4 <= len) {
- let k = getUint32(key, i);
- k = Math.imul(k, c1);
- k = (k << r1) | (k >>> (32 - r1));
- k = Math.imul(k, c2);
- hash ^= k;
- hash = (hash << r2) | (hash >>> (32 - r2));
- hash = Math.imul(hash, m) + n;
- i += 4;
- }
- let remaining = len % 4;
- if (remaining > 0) {
- let k = 0;
- for (let j = 0; j < remaining; j++) {
- k |= (key.charCodeAt(i + j) & 0xff) << (j * 8);
- }
- k = Math.imul(k, c1);
- k = (k << r1) | (k >>> (32 - r1));
- k = Math.imul(k, c2);
- hash ^= k;
- }
- hash ^= len;
- hash ^= (hash >>> 16);
- hash = Math.imul(hash, 0x85ebca6b);
- hash ^= (hash >>> 13);
- hash = Math.imul(hash, 0xc2b2ae35);
- hash ^= (hash >>> 16);
- return hash >>> 0; // Ensure unsigned 32-bit
- }
- // Usage
- const text = "Hello World";
- console.log(murmurhash3_32(text)); // Outputs a number like 2951470589
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement