antlr-ng
Version:
Next generation ANTLR Tool
98 lines (97 loc) • 2.95 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const c1 = 3432918353;
const c2 = 461845907;
const r1 = 15;
const r2 = 13;
const m = 5;
const n = 3864292196;
class MurmurHash {
static {
__name(this, "MurmurHash");
}
static defaultSeed = 701;
constructor() {
}
/**
* Initialize the hash using the specified {@code seed}.
*
* @param seed the seed
*
* @returns the intermediate hash value
*/
static initialize(seed = MurmurHash.defaultSeed) {
return seed;
}
/**
* Update the intermediate hash value for the next input {@code value}.
*
* @param hash The intermediate hash value.
* @param value the value to add to the current hash.
*
* @returns the updated intermediate hash value
*/
static update(hash, value) {
let actualValue = this.hashString(hash, value);
actualValue = Math.imul(actualValue, c1);
actualValue = actualValue << r1 | actualValue >>> 32 - r1;
actualValue = Math.imul(actualValue, c2);
hash = hash ^ actualValue;
hash = hash << r2 | hash >>> 32 - r2;
hash = Math.imul(hash, m) + n;
return hash;
}
/**
* Apply the final computation steps to the intermediate value `hash` to form the final result of the
* MurmurHash 3 hash function.
*
* @param hash The intermediate hash value.
* @param entryCount The number of (32 bit) values added to the hash.
*
* @returns the final hash result
*/
static finish = /* @__PURE__ */ __name((hash, entryCount) => {
hash ^= entryCount * 4;
hash ^= hash >>> 16;
hash = Math.imul(hash, 2246822507);
hash ^= hash >>> 13;
hash = Math.imul(hash, 3266489909);
hash ^= hash >>> 16;
return hash;
}, "finish");
/**
* An all-in-one convenience method to compute a hash for a single value.
*
* @param value The value to hash.
* @param seed The seed for the hash value.
*
* @returns The computed hash.
*/
static hashCode = /* @__PURE__ */ __name((value, seed) => {
return MurmurHash.finish(MurmurHash.update(seed ?? MurmurHash.defaultSeed, value), 1);
}, "hashCode");
/**
* Function to hash a string. Based on the implementation found here:
* https://stackoverflow.com/a/52171480/1137174
*
* @param hash The intermediate hash value.
* @param str The string to hash.
*
* @returns The computed hash.
*/
static hashString(hash = 0, str) {
let h1 = 3735928559 ^ hash;
let h2 = 1103547991 ^ hash;
for (const c of str) {
const ch = c.charCodeAt(0);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 = Math.imul(h1 ^ h1 >>> 16, 2246822507) ^ Math.imul(h2 ^ h2 >>> 13, 3266489909);
h2 = Math.imul(h2 ^ h2 >>> 16, 2246822507) ^ Math.imul(h1 ^ h1 >>> 13, 3266489909);
return Math.imul(4294967296, 2097151 & h2) + (h1 >>> 0);
}
}
export {
MurmurHash
};