-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove batched release, replace hash with cyrb53
- Loading branch information
1 parent
0189b06
commit 1ae3e0a
Showing
4 changed files
with
50 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Extremely simple function to convert a string to a number for bucketing | ||
// Will return the same value for the same letters, regardless of order (collisions very likely) | ||
export function hashString(input: string) { | ||
var hash = 0; | ||
for (var i = 0; i < input.length; i++) { | ||
var charCode = input.charCodeAt(i); | ||
hash += charCode; | ||
} | ||
return hash; | ||
} | ||
|
||
// An improved alternative: | ||
// cyrb53 (c) 2018 bryc (github.com/bryc). License: Public domain. Attribution appreciated. | ||
// A fast and simple 64-bit (or 53-bit) string hash function with decent collision resistance. | ||
// Largely inspired by MurmurHash2/3, but with a focus on speed/simplicity. | ||
// See https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript/52171480#52171480 | ||
// https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js | ||
export const cyrb53 = (str: string, seed = 0) => { | ||
let h1 = 0xdeadbeef ^ seed, | ||
h2 = 0x41c6ce57 ^ seed; | ||
for (let i = 0, ch; i < str.length; i++) { | ||
ch = str.charCodeAt(i); | ||
h1 = Math.imul(h1 ^ ch, 2654435761); | ||
h2 = Math.imul(h2 ^ ch, 1597334677); | ||
} | ||
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507); | ||
h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909); | ||
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507); | ||
h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909); | ||
|
||
return 4294967296 * (2097151 & h2) + (h1 >>> 0); | ||
}; | ||
// If we want all 64 bits of the output: | ||
/* | ||
return [h2>>>0, h1>>>0]; | ||
// or | ||
return (h2>>>0).toString(16).padStart(8,0)+(h1>>>0).toString(16).padStart(8,0); | ||
// or | ||
return 4294967296n * BigInt(h2) + BigInt(h1); | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters