Created
December 14, 2014 16:11
-
-
Save drslump/0834cd141375b2141c9d to your computer and use it in GitHub Desktop.
bloom filter hashing
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
| djb2b = function (str, seed) { | |
| var i, l = str.length, hash = seed || 5381; | |
| for (i = 0; i < l; i++) { | |
| hash += (hash << 5) + (str.charCodeAt(i)|0); | |
| hash = hash & hash; // 32bit cast | |
| } | |
| return hash; | |
| } | |
| sdbm = function(str, seed){ | |
| var hash = seed || 0; | |
| for (i = 0; i < str.length; i++) { | |
| char = str.charCodeAt(i); | |
| hash = char + (hash << 6) + (hash << 16) - hash; | |
| hash = hash & hash; // 32bit cast | |
| } | |
| return hash; | |
| } | |
| // http://citeseer.ist.psu.edu/viewdoc/download?doi=10.1.1.152.579&rep=rep1&type=pdf | |
| hashk = function (k, v, modulo) { | |
| var a = djb2(v), b = sdbm(v), hashes = []; | |
| for (var i=1, hash; i<=k; i++) { | |
| hash = a + b * k; | |
| hashes.push( | |
| modulo ? Math.abs(hash) % modulo : hash | |
| ); | |
| } | |
| return hashes; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment