algorighm/hash/city-hash.js

27 lines
524 B
JavaScript

/**
* cityHash实现
* @param {string} s 需要计算hash的字符串
*/
export default function cityHash32(s) {
const seed = 0;
const len = s.length;
let h = len;
let g = seed;
let f = 0;
for (let i = 0; i < len; i++) {
f += s.charCodeAt(i);
g += f;
h ^= g;
g = ((g << 10) | (g >>> (32 - 10))) >>> 0;
g = ((g + f) >>> 0) * 9 >>> 0;
}
h ^= g;
h = ((h ^ (h >>> 16)) >>> 0) * 0x85ebca6b >>> 0;
h = ((h ^ (h >>> 13)) >>> 0) * 0xc2b2ae35 >>> 0;
h ^= h >>> 16;
return h >>> 0;
}