-
Notifications
You must be signed in to change notification settings - Fork 1
/
murmur.js
101 lines (81 loc) · 3.13 KB
/
murmur.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Gary Court's code, with minimal modifications so that it works on an array of 32-bit positive integers instead of a string
/* eslint-disable */
/**
* JS Implementation of MurmurHash3 (r136) (as of May 20, 2011) (modified at Dec 05, 2018)
*
* @author <a href="mailto:[email protected]">Gary Court</a>
* @see http://github.com/garycourt/murmurhash-js
* @author <a href="mailto:[email protected]">Austin Appleby</a>
* @see http://sites.google.com/site/murmurhash/
*
* @param {number[]} key 32-bit positive integers only
* @param {number} seed Positive integer only
* @return {number} 32-bit positive integer hash
*/
function murmurhash3_32_gc(key, seed) {
var remainder, bytes, h1, h1b, c1, c1b, c2, c2b, k1, i;
// unnecessary, (actual) key length is always divisible by 4
// remainder = key.length & 3; // key.length % 4
remainder = 0
bytes = key.length // - remainder; // not actually bytes btw
h1 = seed;
c1 = 0xcc9e2d51;
c2 = 0x1b873593;
i = 0;
while (i < bytes) {
// because I first thought uint8array would be a good idea, but if we're converting from float32array, it's unnecessary
// k1 =
// (key[i]) |
// (key[++i] << 8) |
// (key[++i] << 16) |
// (key[++i] << 24);
// ++i;
k1 = key[i++]
k1 = ((((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16))) & 0xffffffff;
k1 = (k1 << 15) | (k1 >>> 17);
k1 = ((((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16))) & 0xffffffff;
h1 ^= k1;
h1 = (h1 << 13) | (h1 >>> 19);
h1b = ((((h1 & 0xffff) * 5) + ((((h1 >>> 16) * 5) & 0xffff) << 16))) & 0xffffffff;
h1 = (((h1b & 0xffff) + 0x6b64) + ((((h1b >>> 16) + 0xe654) & 0xffff) << 16));
}
k1 = 0;
// unnecessary, remaninder would be always 0
// switch (remainder) {
// case 3: k1 ^= key[i + 2] << 16;
// case 2: k1 ^= key[i + 1] << 8;
// case 1: k1 ^= key[i];
// k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff;
// k1 = (k1 << 15) | (k1 >>> 17);
// k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff;
// h1 ^= k1;
// }
h1 ^= key.length * 4; // because we were cheating with uint32array all the way through
h1 ^= h1 >>> 16;
h1 = (((h1 & 0xffff) * 0x85ebca6b) + ((((h1 >>> 16) * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff;
h1 ^= h1 >>> 13;
h1 = ((((h1 & 0xffff) * 0xc2b2ae35) + ((((h1 >>> 16) * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff;
h1 ^= h1 >>> 16;
return h1 >>> 0;
}
/* eslint-enable */
// and now the custom wrapper stuff around it
const MAX_INT = Math.pow(2, 32)
const buffer = new ArrayBuffer(4)
const view = new DataView(buffer)
const convert = value => {
view.setFloat32(0, value, true)
return view.getUint32(0, true)
}
/**
* Deterministic Math.random() replacement with Murmur3
*
* @param {number[]} numbers hash input, same values will return the same result
* @param {boolean} raw if true, the result will be the raw 32-bit integer
* @returns {number} between 0 and 1
*/
module.exports = function murmur (numbers, raw = false) {
const converted = numbers.map(convert)
const result = murmurhash3_32_gc(converted)
return raw ? result : result / MAX_INT
}