Created
September 20, 2010 18:50
-
-
Save raycmorgan/588421 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function doHash(str, seed) { | |
var m = 0x5bd1e995; | |
var r = 24; | |
var h = Math.pow(seed, str.len); | |
var length = str.length; | |
var currentIndex = 0; | |
while (length >= 4) { | |
var k = UInt32(str, currentIndex); | |
k *= m; | |
k ^= k >> r; | |
k *= m; | |
h *= m; | |
h ^= k; | |
currentIndex += 4; | |
length -= 4; | |
} | |
switch (length) { | |
case 3: | |
h ^= UInt16(str, currentIndex); | |
h ^= str.charCodeAt(currentIndex + 2) << 16; | |
h *= m; | |
break; | |
case 2: | |
h ^= UInt16(str, currentIndex); | |
h *= m; | |
break; | |
case 1: | |
h ^= str.charCodeAt(currentIndex); | |
h *= m; | |
break; | |
} | |
h ^= h >> 13; | |
h *= m; | |
h ^= h >> 15; | |
return h; | |
} | |
function UInt32(str, pos) { | |
return (str.charCodeAt(pos++)) + | |
(str.charCodeAt(pos++) << 8) + | |
(str.charCodeAt(pos++) << 16) + | |
(str.charCodeAt(pos) << 24); | |
} | |
function UInt16(str, pos) { | |
return (str.charCodeAt(pos++)) + | |
(str.charCodeAt(pos++) << 8); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment