1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- /**
- * Copyright (c) 2017-2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
- *
- * @author David Sehnal <david.sehnal@gmail.com>
- * @author Alexander Rose <alexander.rose@weirdbyte.de>
- */
- // from http://burtleburtle.net/bob/hash/integer.html
- export function hash1(i: number) {
- let a = i ^ (i >> 4);
- a = (a ^ 0xdeadbeef) + (a << 5);
- a = a ^ (a >> 11);
- return a;
- }
- export function hash2(i: number, j: number) {
- let a = 23;
- a = (31 * a + i) | 0;
- a = (31 * a + j) | 0;
- a = a ^ (a >> 4);
- a = (a ^ 0xdeadbeef) + (a << 5);
- a = a ^ (a >> 11);
- return a;
- }
- export function hash3(i: number, j: number, k: number) {
- let a = 23;
- a = (31 * a + i) | 0;
- a = (31 * a + j) | 0;
- a = (31 * a + k) | 0;
- a = a ^ (a >> 4);
- a = (a ^ 0xdeadbeef) + (a << 5);
- a = a ^ (a >> 11);
- return a;
- }
- export function hash4(i: number, j: number, k: number, l: number) {
- let a = 23;
- a = (31 * a + i) | 0;
- a = (31 * a + j) | 0;
- a = (31 * a + k) | 0;
- a = (31 * a + l) | 0;
- a = a ^ (a >> 4);
- a = (a ^ 0xdeadbeef) + (a << 5);
- a = a ^ (a >> 11);
- return a;
- }
- export function hashString(s: string) {
- let h = 0;
- for (let i = 0, l = s.length; i < l; i++) {
- h = (h << 5) - h + s.charCodeAt(i++) | 0;
- }
- return h;
- }
- /**
- * A unique number for each pair of integers
- * Biggest representable pair is (67108863, 67108863) (limit imposed by Number.MAX_SAFE_INTEGER)
- */
- export function cantorPairing(a: number, b: number) {
- return (a + b) * (a + b + 1) / 2 + b;
- }
- /**
- * A unique number for each sorted pair of integers
- * Biggest representable pair is (67108863, 67108863) (limit imposed by Number.MAX_SAFE_INTEGER)
- */
- export function sortedCantorPairing(a: number, b: number) {
- return a < b ? cantorPairing(a, b) : cantorPairing(b, a);
- }
- /**
- * 32 bit FNV-1a hash, see http://isthe.com/chongo/tech/comp/fnv/
- */
- export function hashFnv32a(array: ArrayLike<number>) {
- let hval = 0x811c9dc5;
- for (let i = 0, il = array.length; i < il; ++i) {
- hval ^= array[i];
- hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
- }
- return hval >>> 0;
- }
|